-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathlog_queue.py
157 lines (124 loc) · 5.52 KB
/
log_queue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import collections
import glob
import json
import logging
import os
import threading
import time
import traceback
logger = logging.getLogger(__name__)
class LogQueue:
"""A queue of records that still need to be written out.
For efficiency's sake, records are grouped into time windows of
'batch_window_s' seconds (say, 5 minutes). x'es below indicate
log record events:
300 600 900
| x x x | x |
--+-----------------+-----------------+---------
^ ^ ^
wake wake wake
Upon 'wake' events (every batch window seconds), a background thread
wakes up and collects all events from previous time windows.
We need to use a mutex since the dict we keep the queue in is not
thread-safe. We do as little work as possible every time we hold the mutex
to allow for maximum parallelism.
"""
def __init__(self, name, batch_window_s, do_print=False):
self.name = name
self.records_queue = collections.defaultdict(list)
self.batch_window_s = batch_window_s
self.transmitter = None
self.do_print = do_print
self.mutex = threading.Lock()
self.thread = threading.Thread(target=self._write_thread, name=f"{name}Writer", daemon=True)
self.thread.start()
def add(self, data):
bucket = div_clip(time.time(), self.batch_window_s)
if self.do_print:
logger.debug(repr(data))
with self.mutex:
self.records_queue[bucket].append(data)
def set_transmitter(self, transmitter):
"""Configure a function that will be called for every set of records.
The function looks like:
def transmitter(timestamp, records) -> Boolean:
...
"""
self.transmitter = transmitter
def emergency_save_to_disk(self):
"""Save all untransmitted records to disk.
They will be picked up and attempted to be transmitted by a future
(restarted) process.
"""
all_records = []
with self.mutex:
for records in self.records_queue.values():
all_records.extend(records)
self.records_queue.clear()
if not all_records:
return
filename = f"{self.name}_dump.{os.getpid()}.{time.time()}.jsonl"
with open(filename, "w", encoding="utf-8") as f:
json.dump(all_records, f)
def try_load_emergency_saves(self):
"""Try to load emergency saves from disk, if found.
There may be multiple LogQueues trying to load the same files at the
same time, so we need to be careful that only one of them actually
loads a single file (in order to avoid record duplication).
We use the atomicity of renaming the file as a way of claiming ownership of it.
"""
candidates = glob.glob(f"{self.name}_dump.*.jsonl")
for candidate in candidates:
try:
claim_name = candidate + ".claimed"
os.rename(candidate, claim_name)
# If this succeeded, we're guaranteed to be able to read this file (and because
# we renamed it to something not matching the glob pattern, no one else is going to
# try to pick it up later)
with open(claim_name, "r", encoding="utf-8") as f:
all_records = json.load(f)
bucket = div_clip(time.time(), self.batch_window_s)
with self.mutex:
self.records_queue[bucket].extend(all_records)
os.unlink(claim_name)
except OSError:
pass
def transmit_now(self, max_time=None):
"""(Try to) transmit all pending records with recording timestamps
smaller than the given time now."""
with self.mutex:
keys = list(self.records_queue.keys())
keys.sort()
max_time = max_time or time.time()
buckets_to_send = [k for k in keys if k < max_time]
for bucket_ts in buckets_to_send:
# Get the records out of the queue
with self.mutex:
bucket_records = self.records_queue[bucket_ts]
# Try to send the records (to signal failure, this can either
# throw or return False, depending on how loud it wants to be).
success = self._save_records(bucket_ts, bucket_records)
# Only remove them from the queue if sending didn't fail
if success is not False:
with self.mutex:
del self.records_queue[bucket_ts]
def _save_records(self, timestamp, records):
if self.transmitter:
return self.transmitter(timestamp, records)
def _write_thread(self):
"""Background thread which will wake up every batch_window_s seconds
to emit records from the queue."""
next_wake = div_clip(time.time(), self.batch_window_s) + self.batch_window_s
while True:
try:
# Wait for next wake time
time.sleep(max(0, next_wake - time.time()))
# Once woken, see what buckets we have left to push (all buckets
# with numbers lower than next_wake)
self.transmit_now(next_wake)
except Exception:
traceback.print_exc()
next_wake += self.batch_window_s
def div_clip(x, y):
"""Return the highest value < x that's a multiple of y."""
return int(x // y) * y