Merge "Improve vxlan type driver initialization performance"
This commit is contained in:
commit
1678742659
@ -153,23 +153,33 @@ class VxlanTypeDriver(type_tunnel.TunnelTypeDriver):
|
|||||||
session = db_api.get_session()
|
session = db_api.get_session()
|
||||||
with session.begin(subtransactions=True):
|
with session.begin(subtransactions=True):
|
||||||
# remove from table unallocated tunnels not currently allocatable
|
# remove from table unallocated tunnels not currently allocatable
|
||||||
allocs = session.query(VxlanAllocation).with_lockmode("update")
|
# fetch results as list via all() because we'll be iterating
|
||||||
for alloc in allocs:
|
# through them twice
|
||||||
try:
|
allocs = (session.query(VxlanAllocation).
|
||||||
# see if tunnel is allocatable
|
with_lockmode("update").all())
|
||||||
vxlan_vnis.remove(alloc.vxlan_vni)
|
# collect all vnis present in db
|
||||||
except KeyError:
|
existing_vnis = set(alloc.vxlan_vni for alloc in allocs)
|
||||||
# it's not allocatable, so check if its allocated
|
# collect those vnis that needs to be deleted from db
|
||||||
if not alloc.allocated:
|
vnis_to_remove = [alloc.vxlan_vni for alloc in allocs
|
||||||
# it's not, so remove it from table
|
if (alloc.vxlan_vni not in vxlan_vnis and
|
||||||
LOG.debug(_("Removing tunnel %s from pool"),
|
not alloc.allocated)]
|
||||||
alloc.vxlan_vni)
|
# Immediately delete vnis in chunks. This leaves no work for
|
||||||
session.delete(alloc)
|
# flush at the end of transaction
|
||||||
|
bulk_size = 100
|
||||||
# add missing allocatable tunnels to table
|
chunked_vnis = (vnis_to_remove[i:i + bulk_size] for i in
|
||||||
for vxlan_vni in sorted(vxlan_vnis):
|
range(0, len(vnis_to_remove), bulk_size))
|
||||||
alloc = VxlanAllocation(vxlan_vni=vxlan_vni)
|
for vni_list in chunked_vnis:
|
||||||
session.add(alloc)
|
session.query(VxlanAllocation).filter(
|
||||||
|
VxlanAllocation.vxlan_vni.in_(vni_list)).delete(
|
||||||
|
synchronize_session=False)
|
||||||
|
# collect vnis that need to be added
|
||||||
|
vnis = list(vxlan_vnis - existing_vnis)
|
||||||
|
chunked_vnis = (vnis[i:i + bulk_size] for i in
|
||||||
|
range(0, len(vnis), bulk_size))
|
||||||
|
for vni_list in chunked_vnis:
|
||||||
|
bulk = [{'vxlan_vni': vni, 'allocated': False}
|
||||||
|
for vni in vni_list]
|
||||||
|
session.execute(VxlanAllocation.__table__.insert(), bulk)
|
||||||
|
|
||||||
def get_vxlan_allocation(self, session, vxlan_vni):
|
def get_vxlan_allocation(self, session, vxlan_vni):
|
||||||
with session.begin(subtransactions=True):
|
with session.begin(subtransactions=True):
|
||||||
|
Loading…
Reference in New Issue
Block a user