Fast exit when initially creating tunnel allocations

If the database tunnel allocations match the current configuration, no
other operation is done. During the initiallization, if several workers
try to execute this method, only the first one will update the
allocations. The next workers will check that the database is correctly
updated and will exit this method.

Closes-Bug: #2089940
Related-Bug: #2083570
Change-Id: I208ba38bff9191cabcc1325fec516d0b0179c97c
This commit is contained in:
Rodolfo Alonso Hernandez 2024-12-02 09:23:00 +00:00 committed by Rodolfo Alonso
parent 865097c689
commit dce01d7550
2 changed files with 14 additions and 1 deletions
neutron
plugins/ml2/drivers
tests/unit/plugins/ml2/drivers

@ -199,6 +199,17 @@ class _TunnelTypeDriverBase(helpers.SegmentTypeDriver, metaclass=abc.ABCMeta):
tunnel_col = getattr(self.model, self.segmentation_key)
ctx = context.get_admin_context()
with db_api.CONTEXT_WRITER.using(ctx):
# Check if the allocations are updated: if the total number of
# allocations for this tunnel type matches the allocations of the
# specific IDs, fast exit in that case.
# If another worker handled that before or the table was updated
# in a previous Neutron API restart, this section will end here.
num_allocs = ctx.session.query(self.model).filter(
tunnel_col.in_(tunnel_ids)).count()
num_allocs_total = ctx.session.query(self.model).count()
if len(tunnel_ids) == num_allocs == num_allocs_total:
return
# remove from table unallocated tunnels not currently allocatable
# fetch results as list via all() because we'll be iterating
# through them twice

@ -127,7 +127,9 @@ class TunnelTypeTestMixin:
with mock.patch.object(
type_tunnel, 'chunks', side_effect=verify_no_chunk) as chunks:
self.driver.sync_allocations()
self.assertEqual(2, len(chunks.mock_calls))
# No writing operation is done, fast exit: current allocations
# already present.
self.assertEqual(0, len(chunks.mock_calls))
def test_partial_segment_is_partial_segment(self):
segment = {api.NETWORK_TYPE: self.TYPE,