Merge "Add lock when calling idl.run()" into stable/train

This commit is contained in:
Zuul 2020-10-30 11:45:29 +00:00 committed by Gerrit Code Review
commit e588273127
4 changed files with 15 additions and 13 deletions

View File

@ -104,7 +104,8 @@ class Backend(object):
def lookup(self, table, record, default=_NO_DEFAULT):
try:
return self._lookup(table, record)
with self.ovsdb_connection.lock:
return self._lookup(table, record)
except idlutils.RowNotFound:
if default is not _NO_DEFAULT:
return default

View File

@ -35,8 +35,9 @@ class BaseCommand(api.Command):
def execute(self, check_error=False, log_errors=True, **kwargs):
try:
if self.READ_ONLY:
self.run_idl(None)
return self.result
with self.api.ovsdb_connection.lock:
self.run_idl(None)
return self.result
with self.api.transaction(check_error, log_errors, **kwargs) as t:
t.add(self)
return self.result

View File

@ -65,7 +65,7 @@ class Connection(object):
"""
self.timeout = timeout
self.txns = TransactionQueue(1)
self.lock = threading.Lock()
self.lock = threading.RLock()
self.idl = idl
self.thread = None
self._is_running = None
@ -99,19 +99,18 @@ class Connection(object):
try:
self.idl.wait(self.poller)
self.poller.fd_wait(self.txns.alert_fileno, poller.POLLIN)
# TODO(jlibosva): Remove next line once losing connection to
# ovsdb is solved.
self.poller.timer_wait(self.timeout * 1000)
self.poller.block()
self.idl.run()
with self.lock:
self.idl.run()
except Exception as e:
# This shouldn't happen, but is possible if there is a bug
# in python-ovs
errors += 1
LOG.exception(e)
if errors <= 3:
self.idl.force_reconnect()
idlutils.wait_for_change(self.idl, self.timeout)
with self.lock:
self.idl.force_reconnect()
idlutils.wait_for_change(self.idl, self.timeout)
continue
self._is_running = False
break
@ -119,7 +118,8 @@ class Connection(object):
txn = self.txns.get_nowait()
if txn is not None:
try:
txn.results.put(txn.do_commit())
with self.lock:
txn.results.put(txn.do_commit())
except Exception as ex:
er = idlutils.ExceptionResult(ex=ex,
tb=traceback.format_exc())

View File

@ -33,13 +33,13 @@ class FakeBackend(ovs_idl.Backend):
lookup_table = {'Faketable': idlutils.RowLookup('Faketable', 'name', None)}
def start_connection(self, connection):
pass
self.ovsdb_connection = connection
class TestBackendOvsIdl(base.TestCase):
def setUp(self):
super(TestBackendOvsIdl, self).setUp()
self.backend = FakeBackend(mock.Mock())
self.backend = FakeBackend(mock.MagicMock())
def test_lookup_found(self):
row = self.backend.lookup('Faketable', 'Fake1')