Skip to content

Commit

Permalink
Fix #384 - Run scopefilter update when exclusions change
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsasha committed Sep 3, 2020
1 parent efd609c commit 97e6c0b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
35 changes: 29 additions & 6 deletions irrd/mirroring/scheduler.py
Expand Up @@ -65,19 +65,15 @@ def __init__(self, *args, **kwargs):
self.last_started_time = defaultdict(lambda: 0)
self.previous_scopefilter_prefixes = None
self.previous_scopefilter_asns = None
self.previous_scopefilter_excluded = None

def run(self) -> None:
if get_setting('rpki.roa_source'):
import_timer = int(get_setting('rpki.roa_import_timer'))
self.run_if_relevant(RPKI_IRR_PSEUDO_SOURCE, ROAImportRunner, import_timer)

if get_setting('scopefilter') and any([
self.previous_scopefilter_prefixes != list(get_setting('scopefilter.prefixes', [])),
self.previous_scopefilter_asns != list(get_setting('scopefilter.asns', [])),
]):
if self._check_scopefilter_change():
self.run_if_relevant('scopefilter', ScopeFilterUpdateRunner, 0)
self.previous_scopefilter_prefixes = list(get_setting('scopefilter.prefixes', []))
self.previous_scopefilter_asns = list(get_setting('scopefilter.asns', []))

sources_started = 0
for source in get_setting('sources', {}).keys():
Expand All @@ -101,6 +97,33 @@ def run(self) -> None:
if started_import or started_export:
sources_started += 1

def _check_scopefilter_change(self) -> bool:
"""
Check whether the scope filter has changed since last call.
Always returns True on the first call.
"""
if not get_setting('scopefilter'):
return False

current_prefixes = list(get_setting('scopefilter.prefixes', []))
current_asns = list(get_setting('scopefilter.asns', []))
current_exclusions = {
name
for name, settings in get_setting('sources', {}).items()
if settings['scopefilter_excluded']
}

if any([
self.previous_scopefilter_prefixes != current_prefixes,
self.previous_scopefilter_asns != current_asns,
self.previous_scopefilter_excluded != current_exclusions,
]):
self.previous_scopefilter_prefixes = current_prefixes
self.previous_scopefilter_asns = current_asns
self.previous_scopefilter_excluded = current_exclusions
return True
return False

def run_if_relevant(self, source: str, runner_class, timer: int) -> bool:
process_name = f'{runner_class.__name__}-{source}'

Expand Down
15 changes: 15 additions & 0 deletions irrd/mirroring/tests/test_scheduler.py
Expand Up @@ -140,6 +140,21 @@ def test_scheduler_runs_scopefilter(self, monkeypatch, config_override):
scheduler.run()
assert thread_run_count == 2

config_override({
'rpki': {'roa_source': None},
'scopefilter': {
'asns': [23456],
},
'sources': {
'TEST': {'scopefilter_excluded': True}
},
})

# Should run again, because exclusions have changed
scheduler.update_process_state()
scheduler.run()
assert thread_run_count == 3

def test_scheduler_import_ignores_timer_not_expired(self, monkeypatch, config_override):
monkeypatch.setattr('irrd.mirroring.scheduler.ScheduledTaskProcess', MockScheduledTaskProcess)
global thread_run_count
Expand Down

0 comments on commit 97e6c0b

Please sign in to comment.