[eventlet-removal] remove eventlet references from dhcp_agent

Initialize oslo.service backend to Threading, and change
all the references to threading in DHCP agent code.

Depends-On: https://review.opendev.org/c/openstack/oslo.service/+/945720
Related-Bug: #2087944
Change-Id: I50aafbfb0572b0285aef9ee04c570dce3bbd2998
This commit is contained in:
elajkat authored and Lajos Katona committed 2025-06-25 12:31:29 +00:00
1 parent 421016017b
commit 6b1744eb8a
5 files changed
+53 -30

No files matched your search

+13 -8
View File
@@ -18,8 +18,8 @@ from concurrent import futures
import functools
import os
import threading
import time
import eventlet
from neutron_lib.agent import constants as agent_consts
from neutron_lib.agent import topics
from neutron_lib import constants
@@ -181,9 +181,12 @@ class DhcpAgent(manager.Manager):
"""Activate the DHCP agent."""
self.periodic_resync()
self.start_ready_ports_loop()
eventlet.spawn_n(self._process_loop)
pr_loop_thread = threading.Thread(target=self._process_loop)
pr_loop_thread.start()
if self.conf.bulk_reload_interval:
eventlet.spawn_n(self._reload_bulk_allocations)
bulk_thread = threading.Thread(
target=self._reload_bulk_allocations)
bulk_thread.start()
def _reload_bulk_allocations(self):
while True:
@@ -196,7 +199,7 @@ class DhcpAgent(manager.Manager):
network = self.cache.get_network_by_id(network_id)
if network is not None:
self.call_driver('bulk_reload_allocations', network)
eventlet.greenthread.sleep(self.conf.bulk_reload_interval)
time.sleep(self.conf.bulk_reload_interval)
def call_driver(self, action, network, **action_kwargs):
sid_segment = {}
@@ -293,7 +296,7 @@ class DhcpAgent(manager.Manager):
# This helps prevent one thread from acquiring the same lock over and
# over again, in which case no other threads waiting on the
# "dhcp-agent" lock would make any progress.
eventlet.greenthread.sleep(0)
time.sleep(0)
@_sync_lock
def sync_state(self, networks=None):
@@ -375,12 +378,13 @@ class DhcpAgent(manager.Manager):
self.dhcp_ready_ports |= ports_to_send
while True:
eventlet.sleep(0.2)
time.sleep(0.2)
dhcp_ready_ports_loop()
def start_ready_ports_loop(self):
"""Spawn a thread to push changed ports to server."""
eventlet.spawn(self._dhcp_ready_ports_loop)
# TODO(lajoskatona): check the usage of ThreadPoolExecutor
threading.Thread(target=self._dhcp_ready_ports_loop).start()
@utils.exception_logger()
def _periodic_resync_helper(self):
@@ -412,7 +416,8 @@ class DhcpAgent(manager.Manager):
def periodic_resync(self):
"""Spawn a thread to periodically resync the dhcp state."""
eventlet.spawn(self._periodic_resync_helper)
resync_thread = threading.Thread(target=self._periodic_resync_event)
resync_thread.start()
def safe_get_network_info(self, network_id):
try:
@@ -10,10 +10,14 @@
# License for the specific language governing permissions and limitations
# under the License.
import setproctitle
from oslo_service import backend
backend.init_backend(backend.BackendType.THREADING)
from neutron.agent import dhcp_agent
from neutron_lib import constants
# pylint: disable=wrong-import-position
import setproctitle # noqa: E402
from neutron.agent import dhcp_agent # noqa: E402
from neutron_lib import constants # noqa: E402
def main():
+1 -1
View File
@@ -21,7 +21,7 @@ from oslo_config import cfg
from oslo_utils import uuidutils
from neutron.agent.linux import dhcp as linux_dhcp
from neutron.cmd.eventlet.agents import dhcp as dhcp_agent
from neutron.cmd.agents import dhcp as dhcp_agent
from neutron.common import config
+31 -17
View File
@@ -383,12 +383,18 @@ class TestDhcpAgent(base.BaseTestCase):
['periodic_resync', 'start_ready_ports_loop',
'_process_loop']}
with mock.patch.multiple(dhcp, **attrs_to_mock) as mocks:
with mock.patch.object(dhcp_agent.eventlet,
'spawn_n') as spawn_n:
dhcp.run()
mocks['periodic_resync'].assert_called_once_with()
mocks['start_ready_ports_loop'].assert_called_once_with()
spawn_n.assert_called_once_with(mocks['_process_loop'])
with mock.patch.object(
dhcp_agent.threading.Thread, '__init__') as mock_t:
with mock.patch.object(
dhcp_agent.threading.Thread, 'start'):
mock_t.return_value = None
dhcp.run()
per_resyn_mck = mocks['periodic_resync']
srt_ready_ports_mck = mocks['start_ready_ports_loop']
per_resyn_mck.assert_called_once_with()
srt_ready_ports_mck.assert_called_once_with()
mock_t.assert_called_once_with(
target=mocks['_process_loop'])
def test_call_driver(self):
network = mock.MagicMock()
@@ -580,16 +586,24 @@ class TestDhcpAgent(base.BaseTestCase):
def test_periodic_resync(self):
dhcp = dhcp_agent.DhcpAgent(HOSTNAME)
dhcp.init_host()
with mock.patch.object(dhcp_agent.eventlet, 'spawn') as spawn:
dhcp.periodic_resync()
spawn.assert_called_once_with(dhcp._periodic_resync_helper)
with mock.patch.object(
dhcp_agent.threading.Thread, '__init__') as mock_t:
with mock.patch.object(dhcp_agent.threading.Thread, 'start'):
mock_t.return_value = None
dhcp.periodic_resync()
mock_t.assert_called_once_with(
target=dhcp._periodic_resync_event)
def test_start_ready_ports_loop(self):
dhcp = dhcp_agent.DhcpAgent(HOSTNAME)
dhcp.init_host()
with mock.patch.object(dhcp_agent.eventlet, 'spawn') as spawn:
dhcp.start_ready_ports_loop()
spawn.assert_called_once_with(dhcp._dhcp_ready_ports_loop)
with mock.patch.object(
dhcp_agent.threading.Thread, '__init__') as mock_t:
with mock.patch.object(dhcp_agent.threading.Thread, 'start'):
mock_t.return_value = None
dhcp.start_ready_ports_loop()
mock_t.assert_called_once_with(
target=dhcp._dhcp_ready_ports_loop)
def test__dhcp_ready_ports_doesnt_log_exception_on_timeout(self):
dhcp = dhcp_agent.DhcpAgent(HOSTNAME)
@@ -599,7 +613,7 @@ class TestDhcpAgent(base.BaseTestCase):
with mock.patch.object(dhcp.plugin_rpc, 'dhcp_ready_on_ports',
side_effect=oslo_messaging.MessagingTimeout):
# exit after 2 iterations
with mock.patch.object(dhcp_agent.eventlet, 'sleep',
with mock.patch.object(dhcp_agent.time, 'sleep',
side_effect=[0, 0, RuntimeError]):
with mock.patch.object(dhcp_agent.LOG, 'exception') as lex:
with testtools.ExpectedException(RuntimeError):
@@ -614,7 +628,7 @@ class TestDhcpAgent(base.BaseTestCase):
with mock.patch.object(dhcp.plugin_rpc, 'dhcp_ready_on_ports',
side_effect=[RuntimeError, 0]) as ready:
# exit after 2 iterations
with mock.patch.object(dhcp_agent.eventlet, 'sleep',
with mock.patch.object(dhcp_agent.time, 'sleep',
side_effect=[0, 0, RuntimeError]):
with testtools.ExpectedException(RuntimeError):
dhcp._dhcp_ready_ports_loop()
@@ -631,7 +645,7 @@ class TestDhcpAgent(base.BaseTestCase):
with mock.patch.object(dhcp.plugin_rpc,
'dhcp_ready_on_ports') as ready:
# exit after 2 iterations
with mock.patch.object(dhcp_agent.eventlet, 'sleep',
with mock.patch.object(dhcp_agent.time, 'sleep',
side_effect=[0, 0, RuntimeError]):
with testtools.ExpectedException(RuntimeError):
dhcp._dhcp_ready_ports_loop()
@@ -660,7 +674,7 @@ class TestDhcpAgent(base.BaseTestCase):
with mock.patch.object(dhcp.plugin_rpc,
'dhcp_ready_on_ports') as ready:
# exit after 1 iteration
with mock.patch.object(dhcp_agent.eventlet, 'sleep',
with mock.patch.object(dhcp_agent.time, 'sleep',
side_effect=[0, RuntimeError]):
with testtools.ExpectedException(RuntimeError):
dhcp._dhcp_ready_ports_loop()
@@ -681,7 +695,7 @@ class TestDhcpAgent(base.BaseTestCase):
with mock.patch.object(dhcp.plugin_rpc,
'dhcp_ready_on_ports') as ready:
# exit after 1 iteration
with mock.patch.object(dhcp_agent.eventlet, 'sleep',
with mock.patch.object(dhcp_agent.time, 'sleep',
side_effect=[0, RuntimeError]):
with testtools.ExpectedException(RuntimeError):
dhcp._dhcp_ready_ports_loop()
+1 -1
View File
@@ -33,7 +33,7 @@ wsgi_scripts =
neutron-api = neutron.cmd.server:main_api_uwsgi
console_scripts =
neutron-db-manage = neutron.db.migration.cli:main
neutron-dhcp-agent = neutron.cmd.eventlet.agents.dhcp:main
neutron-dhcp-agent = neutron.cmd.agents.dhcp:main
neutron-keepalived-state-change = neutron.cmd.keepalived_state_change:main
neutron-ipset-cleanup = neutron.cmd.ipset_cleanup:main
neutron-l3-agent = neutron.cmd.agents.l3:main