-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve rpki validation #62
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import subprocess | ||
import sys | ||
|
||
from concurrent.futures import ThreadPoolExecutor | ||
from concurrent.futures import ThreadPoolExecutor, as_completed | ||
import json | ||
from threading import Lock | ||
from pathlib import Path | ||
import requests | ||
|
@@ -97,7 +98,7 @@ def validate_rpki_db(context): | |
with open(context.debug_log, 'a') as logs: | ||
logs.write("\n\n=== RPKI Validation ===\n") | ||
|
||
def process_file(file): | ||
def process_file(batch): | ||
result = subprocess.run(["rpki-client", | ||
"-j", | ||
"-n", | ||
|
@@ -106,26 +107,37 @@ def process_file(file): | |
"-P", | ||
context.epoch, | ||
] + tal_options + | ||
["-f", file], # -f has to be last | ||
["-f"] + batch, # -f has to be last | ||
capture_output=True, | ||
check=False) | ||
|
||
if result.stderr and context.debug_log: | ||
stderr_output = result.stderr.decode() | ||
with debug_file_lock: | ||
with open(context.debug_log, 'a') as logs: | ||
logs.write(f'\nfile: {file}\n') | ||
logs.write(stderr_output) | ||
return result.stdout | ||
|
||
with ThreadPoolExecutor() as executor: | ||
results = list(tqdm(executor.map(process_file, files), total=len(files))) | ||
|
||
json_results = [result.decode() for result in results if result] | ||
total = len(files) | ||
batch_size = 250 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should test more batch size values. 250 worked out better than 100 and 500, so I stuck with it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is already a great win, it can be optimized further later. Let's first make sure it's robust and get it in asap :) |
||
batches = [] | ||
for i in range(0, total, batch_size): | ||
batch = [str(f) for f in files[i:i + batch_size]] | ||
batches.append(batch) | ||
|
||
with open(result_path, "w") as res_file: | ||
res_file.write("[") | ||
res_file.write(",".join(json_results)) | ||
res_file.write("]") | ||
|
||
print(f"{len(json_results)} RKPI ROAs validated and saved to {result_path}, file hash: {calculate_sha256(result_path)}") | ||
total_batches = len(batches) | ||
results = [] | ||
with ThreadPoolExecutor() as executor: | ||
futures = [executor.submit(process_file, batch) for batch in batches] | ||
for future in tqdm(as_completed(futures), total=total_batches): | ||
result = future.result() | ||
if result: | ||
normalized = result.replace(b"\n}\n{\n\t", b"\n},\n{\n").decode('utf-8').strip() | ||
results.append(normalized) | ||
results_json = json.loads("[" + ",".join(results) + "]") | ||
s = sorted(results_json, key=lambda result: result["hash_id"]) | ||
|
||
with open(result_path, 'w') as f: | ||
json.dump(s, f) | ||
|
||
print(f"RKPI ROAs validated and saved to {result_path}, file hash: {calculate_sha256(result_path)}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing in one commit and then re-adding in the next is a bit annoying to review. You could squash both commits and that would be a bit easier.