Fix handling of duplicate prefixes

This commit is contained in:
jeremystretch 2021-05-27 10:03:00 -04:00
parent 34e673f7d6
commit 84017776ec

View File

@ -105,7 +105,7 @@ def rebuild_prefixes(vrf):
for n in stack: for n in stack:
n['children'] += 1 n['children'] += 1
stack.append({ stack.append({
'pk': prefix['pk'], 'pk': [prefix['pk']],
'prefix': prefix['prefix'], 'prefix': prefix['prefix'],
'children': 0, 'children': 0,
}) })
@ -123,18 +123,17 @@ def rebuild_prefixes(vrf):
# Handle duplicate prefixes # Handle duplicate prefixes
elif stack[-1]['prefix'] == p['prefix']: elif stack[-1]['prefix'] == p['prefix']:
update_queue.append( stack[-1]['pk'].append(p['pk'])
Prefix(pk=p['pk'], _depth=len(stack) - 1, _children=stack[-1]['children'])
)
# If this is a sibling or parent of the most recent prefix, pop nodes from the # If this is a sibling or parent of the most recent prefix, pop nodes from the
# stack until we reach a parent prefix (or the root) # stack until we reach a parent prefix (or the root)
else: else:
while stack and not contains(stack[-1]['prefix'], p['prefix']): while stack and not contains(stack[-1]['prefix'], p['prefix']):
node = stack.pop() node = stack.pop()
update_queue.append( for pk in node['pk']:
Prefix(pk=node['pk'], _depth=len(stack), _children=node['children']) update_queue.append(
) Prefix(pk=pk, _depth=len(stack), _children=node['children'])
)
push_to_stack(p) push_to_stack(p)
# Flush the update queue once it reaches 100 Prefixes # Flush the update queue once it reaches 100 Prefixes
@ -145,9 +144,10 @@ def rebuild_prefixes(vrf):
# Clear out any prefixes remaining in the stack # Clear out any prefixes remaining in the stack
while stack: while stack:
node = stack.pop() node = stack.pop()
update_queue.append( for pk in node['pk']:
Prefix(pk=node['pk'], _depth=len(stack), _children=node['children']) update_queue.append(
) Prefix(pk=pk, _depth=len(stack), _children=node['children'])
)
# Final flush of any remaining Prefixes # Final flush of any remaining Prefixes
Prefix.objects.bulk_update(update_queue, ['_depth', '_children']) Prefix.objects.bulk_update(update_queue, ['_depth', '_children'])