Browse Source

Cache default security group IDs in memory

Getting default SG for a project is quite a frequent operation
that happens for example on each port create/update events.
Since default SG id does not change often for a project/tenant
(if at all) it makes sense to cache it in memory to avoid DB access
each time.

Change-Id: I2493eb65ddd548435cf846e1ae7ac65ce8bc14a4
changes/05/777605/3
Oleg Bondarev 3 months ago
committed by Oleg Bondarev
parent
commit
433deed0b7
2 changed files with 13 additions and 0 deletions
  1. +10
    -0
      neutron/db/securitygroups_db.py
  2. +3
    -0
      neutron/tests/base.py

+ 10
- 0
neutron/db/securitygroups_db.py 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,


+ 3
- 0
neutron/tests/base.py 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()


Loading…
Cancel
Save