Skip to content

Commit feee384

Browse files
committed
Slow down execution of bot aggregation queries to lessen the load on the system
1 parent 6e7a5aa commit feee384

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

netflowbot.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@
3636
TOP_N_MAX = 10
3737

3838

39+
# Decorator: to avoid overwhelming the system, we sleep some amount after performing
40+
# demanding tasks. For how long? Some factor of the timed interval.
41+
def slow_down(func):
42+
def wrapper(*args, **kwargs):
43+
start = time.time()
44+
ret = func(*args, **kwargs)
45+
end = time.time()
46+
time.sleep((end - start) * 0.5)
47+
return ret
48+
return wrapper
49+
50+
3951
def path_part_encode(s):
4052
return s.replace(".", '%2e')
4153

@@ -107,7 +119,7 @@ def perform_account_aggr_job(*args, **job_params):
107119
interval_label = job_params["interval_label"]
108120
account_id = job_params["account_id"]
109121
entities = [(entity_info["entity_id"], entity_info["details"]["ipv4"],) for entity_info in job_params["entities_infos"]]
110-
122+
log.info(f"Starting {interval_label} aggregation job for account {account_id}...")
111123

112124
last_used_seq, last_used_ts = _get_last_used_seq(job_id)
113125
max_seq, max_ts = _get_current_max_seq()
@@ -179,6 +191,7 @@ def construct_output_path_prefix(interval_label, direction, entity_id, interface
179191

180192

181193
@staticmethod
194+
@slow_down
182195
def get_traffic_for_entity(interval_label, last_seq, max_seq, time_between, direction, entity_id, entity_ip):
183196
# returns cumulative traffic for the whole entity, and traffic per interface for this entity
184197
with get_db_cursor() as c:
@@ -219,6 +232,7 @@ def get_traffic_for_entity(interval_label, last_seq, max_seq, time_between, dire
219232

220233

221234
@staticmethod
235+
@slow_down
222236
def get_top_N_IPs_for_entity_interfaces(interval_label, last_seq, max_seq, time_between, direction, entity_id, entity_ip):
223237
with get_db_cursor() as c, get_db_cursor() as c2:
224238

@@ -270,6 +284,7 @@ def get_top_N_IPs_for_entity_interfaces(interval_label, last_seq, max_seq, time_
270284
return values
271285

272286
@staticmethod
287+
@slow_down
273288
def get_top_N_IPs_for_entity(interval_label, last_seq, max_seq, time_between, direction, entity_id, entity_ip):
274289
with get_db_cursor() as c:
275290
values = []
@@ -305,6 +320,7 @@ def get_top_N_IPs_for_entity(interval_label, last_seq, max_seq, time_between, di
305320

306321

307322
@staticmethod
323+
@slow_down
308324
def get_top_N_protocols_for_entity_interfaces(interval_label, last_seq, max_seq, time_between, direction, entity_id, entity_ip):
309325
with get_db_cursor() as c, get_db_cursor() as c2:
310326

@@ -356,6 +372,7 @@ def get_top_N_protocols_for_entity_interfaces(interval_label, last_seq, max_seq,
356372
return values
357373

358374
@staticmethod
375+
@slow_down
359376
def get_top_N_protocols_for_entity(interval_label, last_seq, max_seq, time_between, direction, entity_id, entity_ip):
360377
with get_db_cursor() as c:
361378
values = []
@@ -390,6 +407,7 @@ def get_top_N_protocols_for_entity(interval_label, last_seq, max_seq, time_betwe
390407
return values
391408

392409
# @staticmethod
410+
# @slow_down
393411
# def get_top_N_protocols(output_path_prefix, from_time, to_time, interface_index, is_direction_in=True):
394412
# with get_db_cursor() as c:
395413
# # TODO: missing check for IP: r.client_ip = %s AND

0 commit comments

Comments
 (0)