Browse Source

Merge "Make NeutronOvsdbIdl singleton" into stable/train

changes/84/761484/1
Zuul 2 months ago
committed by Gerrit Code Review
parent
commit
dc2ed5adc9
3 changed files with 33 additions and 1 deletions
  1. +3
    -1
      neutron/agent/ovsdb/impl_idl.py
  2. +12
    -0
      neutron/common/utils.py
  3. +18
    -0
      neutron/tests/unit/common/test_utils.py

+ 3
- 1
neutron/agent/ovsdb/impl_idl.py View File

@ -22,6 +22,7 @@ from ovsdbapp.backend.ovs_idl import vlog
from ovsdbapp.schema.open_vswitch import impl_idl
from neutron.agent.ovsdb.native import connection as n_connection
from neutron.common import utils
from neutron.conf.agent import ovs_conf
from neutron.plugins.ml2.drivers.openvswitch.agent.common import constants
@ -83,12 +84,13 @@ class OvsCleanup(command.BaseCommand):
return True
@utils.SingletonDecorator
class NeutronOvsdbIdl(impl_idl.OvsdbIdl):
def __init__(self, connection, idl_monitor):
max_level = None if cfg.CONF.OVS.ovsdb_debug else vlog.INFO
vlog.use_python_logger(max_level=max_level)
self.idl_monitor = idl_monitor
super(NeutronOvsdbIdl, self).__init__(connection)
impl_idl.OvsdbIdl.__init__(self, connection)
def ovs_cleanup(self, bridges, all_ports=False):
return OvsCleanup(self, bridges, all_ports)

+ 12
- 0
neutron/common/utils.py View File

@ -982,3 +982,15 @@ def timecost(f):
ret = f(*args, **kwargs)
return ret
return wrapper
class SingletonDecorator(object):
def __init__(self, klass):
self._klass = klass
self._instance = None
def __call__(self, *args, **kwargs):
if self._instance is None:
self._instance = self._klass(*args, **kwargs)
return self._instance

+ 18
- 0
neutron/tests/unit/common/test_utils.py View File

@ -624,3 +624,21 @@ class SpawnWithOrWithoutProfilerTestCase(
def test_spawn_without_profiler(self):
self._compare_profilers_in_parent_and_in_child(init_profiler=False)
@utils.SingletonDecorator
class _TestSingletonClass(object):
def __init__(self):
self.variable = None
class SingletonDecoratorTestCase(base.BaseTestCase):
def test_singleton_instance_class(self):
instance_1 = _TestSingletonClass()
instance_1.variable = 'value1'
instance_2 = _TestSingletonClass()
self.assertEqual(instance_1.__hash__(), instance_2.__hash__())
self.assertEqual('value1', instance_2.variable)

Loading…
Cancel
Save