Prevent duplicating instance links

In receptor address post-save method:
- Fixed detecting if address was missing
a link from control nodes
- Use InstanceLink create_or_update to prevent
adding duplicate InstanceLink source and target
peers

In instance serializer create_or_update,
delete receptor addresses first before doing
instance create or update. This ensures that we don't
trigger unnecessary post-save methods that might
attempt to manipulate receptor addresses that
will just be removed later.

Signed-off-by: Seth Foster <fosterbseth@gmail.com>
This commit is contained in:
Seth Foster
2024-01-31 00:15:48 -05:00
committed by Seth Foster
parent b558397b67
commit 083c05f12a
2 changed files with 21 additions and 15 deletions

View File

@@ -522,13 +522,14 @@ def receptor_address_saved(sender, instance, **kwargs):
control_instances = set(Instance.objects.filter(node_type__in=[Instance.Types.CONTROL, Instance.Types.HYBRID]))
if address.peers_from_control_nodes:
# FIXME: you ought to be able to have more connections than just the control instances
if set(address.peers_from.all()) != control_instances:
# if control_instances is not a subset of current peers of address, then
# that means we need to add some InstanceLinks
if not control_instances <= set(address.peers_from.all()):
with disable_activity_stream():
address.peers_from.add(*control_instances)
for control_instance in control_instances:
InstanceLink.objects.update_or_create(source=control_instance, target=address)
schedule_write_receptor_config()
else:
# FIXME: you shouldn't unconditionally remove every peer when disabling peers_from_control_nodes
if address.peers_from.exists():
with disable_activity_stream():
address.peers_from.remove(*control_instances)