Skip to content

Commit

Permalink
[fix] Fix updating SubnetDivisionRule label openwisp#547
Browse files Browse the repository at this point in the history
Use iterator() instead of all() on queryset, since the number
of database row will be large for SubnetDivisionIndex.

Closes openwisp#547
  • Loading branch information
pandafy committed Jan 19, 2022
1 parent 58881a8 commit cca0b7c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
17 changes: 9 additions & 8 deletions openwisp_controller/subnet_division/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ def update_subnet_division_index(rule_id):
)
return

index_queryset = division_rule.subnetdivisionindex_set.all()
for index in index_queryset:
for index in division_rule.subnetdivisionindex_set.only('keyword').iterator():
identifiers = index.keyword.split('_')
identifiers[0] = division_rule.label
index.keyword = '_'.join(identifiers)

SubnetDivisionIndex.objects.bulk_update(
index_queryset, fields=['keyword'], batch_size=20
)
if index.ip_id is not None:
required_identifiers = 2
else:
required_identifiers = 1
index.keyword = '_'.join(
[division_rule.label] + identifiers[-required_identifiers:]
)
index.save()


@shared_task
Expand Down
12 changes: 10 additions & 2 deletions openwisp_controller/subnet_division/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def test_provisioned_subnets(self):

def test_rule_label_updated(self):
new_rule_label = 'TSDR'
rule = self._get_vpn_subdivision_rule()
rule = self._get_vpn_subdivision_rule(label='VPN_OW')
self.config.templates.add(self.template)
index_queryset = rule.subnetdivisionindex_set.filter(config_id=self.config.id)
subnet_queryset = self.subnet_query.filter(
Expand All @@ -218,7 +218,7 @@ def test_rule_label_updated(self):
)
index_count = index_queryset.count()
subnet_count = subnet_queryset.count()
rule.label = 'TSDR'
rule.label = new_rule_label
rule.save()
rule.refresh_from_db()

Expand All @@ -230,6 +230,14 @@ def test_rule_label_updated(self):
).count()
self.assertEqual(index_count, new_index_count)

# Verify context of config
context = self.config.get_subnet_division_context()
self.assertIn(f'{rule.label}_prefixlen', context)
for subnet_id in range(1, rule.number_of_subnets + 1):
self.assertIn(f'{rule.label}_subnet{subnet_id}', context)
for ip_id in range(1, rule.number_of_ips + 1):
self.assertIn(f'{rule.label}_subnet{subnet_id}_ip{ip_id}', context)

# Assert name and description of Subnet are updated
new_subnet_count = subnet_queryset.filter(
name__startswith=new_rule_label, description__contains=new_rule_label,
Expand Down

0 comments on commit cca0b7c

Please sign in to comment.