Merge "Cache default security group IDs in memory"

This commit is contained in:
Zuul 2021-04-07 13:11:22 +00:00 committed by Gerrit Code Review
commit 4e4625413f
2 changed files with 13 additions and 0 deletions

View File

@ -59,6 +59,8 @@ class SecurityGroupDbMixin(ext_sg.SecurityGroupPluginBase,
__native_bulk_support = True
default_sg_cache = dict()
def create_security_group_bulk(self, context, security_groups):
return self._create_bulk('security_group', context,
security_groups)
@ -275,6 +277,9 @@ class SecurityGroupDbMixin(ext_sg.SecurityGroupPluginBase,
**kwargs)
sg.delete()
project_id = self.default_sg_cache.pop(id, None)
self.default_sg_cache.pop(project_id, None)
kwargs.pop('security_group')
kwargs['name'] = sg['name']
registry.notify(resources.SECURITY_GROUP, events.AFTER_DELETE,
@ -881,11 +886,16 @@ class SecurityGroupDbMixin(ext_sg.SecurityGroupPluginBase,
security_groups else [])
def _get_default_sg_id(self, context, tenant_id):
if tenant_id in self.default_sg_cache:
return self.default_sg_cache[tenant_id]
default_group = sg_obj.DefaultSecurityGroup.get_object(
context,
project_id=tenant_id,
)
if default_group:
self.default_sg_cache[tenant_id] = default_group.security_group_id
self.default_sg_cache[default_group.security_group_id] = tenant_id
return default_group.security_group_id
@registry.receives(resources.PORT, [events.BEFORE_CREATE,

View File

@ -55,6 +55,7 @@ from neutron.api.rpc.callbacks.producer import registry as rpc_producer_reg
from neutron.common import config
from neutron.conf.agent import common as agent_config
from neutron.db import agentschedulers_db
from neutron.db import securitygroups_db
from neutron import manager
from neutron import policy
from neutron.quota import resource_registry
@ -570,6 +571,8 @@ class PluginFixture(fixtures.Fixture):
# TODO(marun) Fix plugins that do not properly initialize notifiers
agentschedulers_db.AgentSchedulerDbMixin.agent_notifiers = {}
securitygroups_db.SecurityGroupDbMixin.default_sg_cache = {}
nm.clear_instance()