Initialize modifiable list of resources in CacheBackedPluginApi.

Currently, if one wanted to add any other resources (including custom
objects), there is no simple way to achieve that, since list of defined
resource types is hardcoded in create_cache_for_l2_agent function,
which is called in __init__ of the CacheBackedPluginApi. Even if we
derive from it, we must call super() on descendant, otherwise we end up
with uninitialized PluginApi part. But if we do the super() on it, we
end up on having hardcoded resources only, and creating a new remote
resource cache object will make a new set of listeners, while the
listeners for the old object still exist, and may cause memory leaks.
RemoteResourceWatcher class have only initializers for those listeners,
and there is no obvious way to stop/clean them.

In this patch we propose to move create_cache_for_l2_agent function to
CacheBackedPluginApi class, and make resource list to be class
attribute, so that it can be easily modified.

Change-Id: Ia65ecaf7b48926b74505226a5922b85e2cb593a6
Closes-Bug: 1837529
changes/61/672261/3
Roman Dobosz 3 years ago
parent c456d9f658
commit 2db02023ee
  1. 32
      neutron/agent/rpc.py
  2. 26
      neutron/tests/unit/agent/test_rpc.py

@ -200,27 +200,18 @@ class PluginApi(object):
vnic_type=vnic_type, host=host)
def create_cache_for_l2_agent():
"""Create a push-notifications cache for L2 agent related resources."""
objects.register_objects()
resource_types = [
resources.PORT,
resources.SECURITYGROUP,
resources.SECURITYGROUPRULE,
resources.NETWORK,
resources.SUBNET
]
rcache = resource_cache.RemoteResourceCache(resource_types)
rcache.start_watcher()
return rcache
class CacheBackedPluginApi(PluginApi):
RESOURCE_TYPES = [resources.PORT,
resources.SECURITYGROUP,
resources.SECURITYGROUPRULE,
resources.NETWORK,
resources.SUBNET]
def __init__(self, *args, **kwargs):
super(CacheBackedPluginApi, self).__init__(*args, **kwargs)
self.remote_resource_cache = create_cache_for_l2_agent()
self.remote_resource_cache = None
self._create_cache_for_l2_agent()
def register_legacy_notification_callbacks(self, legacy_interface):
"""Emulates the server-side notifications from ml2 AgentNotifierApi.
@ -380,3 +371,10 @@ class CacheBackedPluginApi(PluginApi):
def get_devices_details_list(self, context, devices, agent_id, host=None):
return [self.get_device_details(context, device, agent_id, host)
for device in devices]
def _create_cache_for_l2_agent(self):
"""Create a push-notifications cache for L2 agent related resources."""
objects.register_objects()
rcache = resource_cache.RemoteResourceCache(self.RESOURCE_TYPES)
rcache.start_watcher()
self.remote_resource_cache = rcache

@ -315,3 +315,29 @@ class TestCacheBackedPluginApi(base.BaseTestCase):
self.assertNotIn('port_id', entry)
self.assertNotIn('network_id', entry)
self.assertIn(constants.NO_ACTIVE_BINDING, entry)
@mock.patch('neutron.agent.resource_cache.RemoteResourceCache')
def test_initialization_with_default_resources(self, rcache_class):
rcache_obj = mock.MagicMock()
rcache_class.return_value = rcache_obj
rpc.CacheBackedPluginApi(lib_topics.PLUGIN)
rcache_class.assert_called_once_with(
rpc.CacheBackedPluginApi.RESOURCE_TYPES)
rcache_obj.start_watcher.assert_called_once_with()
@mock.patch('neutron.agent.resource_cache.RemoteResourceCache')
def test_initialization_with_custom_resource(self, rcache_class):
CUSTOM = 'test'
rcache_obj = mock.MagicMock()
rcache_class.return_value = rcache_obj
class CustomCacheBackedPluginApi(rpc.CacheBackedPluginApi):
RESOURCE_TYPES = [resources.PORT, CUSTOM]
CustomCacheBackedPluginApi(lib_topics.PLUGIN)
rcache_class.assert_called_once_with(
CustomCacheBackedPluginApi.RESOURCE_TYPES)
rcache_obj.start_watcher.assert_called_once_with()

Loading…
Cancel
Save