Make NeutronOvsdbIdl singleton

"NeutronOvsdbIdl" should be a singleton class; only one instance
should be shared everywhere is called. Currently the connection
and the "OvsIdlMonitor" instances are global; those instances are
used to declare the "NeutronOvsdbIdl" instance.

As commented in the related bug, this singleton will ensure that
the OvsIdl indexes are created just once.

Change-Id: I639cf673a983b7b1be810495d8a8c2d89919a9b6
Related-Bug: #1881424
This commit is contained in:
Rodolfo Alonso Hernandez 2020-06-02 11:10:21 +00:00
parent 31280695a2
commit 178227c8b6
3 changed files with 33 additions and 1 deletions

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)
super().__init__(connection)
def ovs_cleanup(self, bridges, all_ports=False):
return OvsCleanup(self, bridges, all_ports)

View File

@ -987,3 +987,15 @@ def timecost(f):
ret = f(*args, **kwargs)
return ret
return wrapper
class SingletonDecorator:
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

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)