-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpw_poller.py
More file actions
executable file
·336 lines (279 loc) · 11.8 KB
/
pw_poller.py
File metadata and controls
executable file
·336 lines (279 loc) · 11.8 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (C) 2019 Netronome Systems, Inc.
# Copyright (c) 2020 Facebook
import configparser
import datetime
import json
import os
import shutil
import socket
import time
import queue
from typing import Dict
from importlib import import_module
from core import NIPA_DIR
from core import NipaLifetime
from core import log, log_open_sec, log_end_sec, log_init
from core import Tester
from core import Tree
from pw import Patchwork
from pw import PwSeries
import core
class IncompleteSeries(Exception):
pass
class PwPoller:
def __init__(self, config) -> None:
self._worker_id = 0
self._async_workers = []
self.result_dir = config.get('dirs', 'results', fallback=os.path.join(NIPA_DIR, "results"))
self.worker_dir = config.get('dirs', 'workers', fallback=os.path.join(NIPA_DIR, "workers"))
tree_dir = config.get('dirs', 'trees', fallback=os.path.join(NIPA_DIR, "../"))
self._trees = { }
for tree in config['trees']:
opts = [x.strip() for x in config['trees'][tree].split(',')]
prefix = opts[0]
fspath = opts[1]
remote = opts[2]
branch = None
if len(opts) > 3:
branch = opts[3]
src = os.path.join(tree_dir, fspath)
# name, pfx, fspath, remote=None, branch=None
self._trees[tree] = Tree(tree, prefix, src, remote=remote, branch=branch)
if os.path.exists(self.worker_dir):
shutil.rmtree(self.worker_dir)
os.makedirs(self.worker_dir)
self._done_queue = queue.Queue()
self._workers = []
self._work_queues = {}
for k, tree in self._trees.items():
self._work_queues[k] = queue.Queue()
worker_cnt = config.getint('workers', tree.name, fallback=1)
for worker_id in range(worker_cnt):
worker = Tester(self.result_dir, tree.work_tree(worker_id),
self._work_queues[k], self._done_queue)
worker.start()
log(f"Started worker {worker.name} for {k}")
self._workers.append(worker)
self._pw = Patchwork(config)
self._state = {
'last_event_ts': (datetime.datetime.now() -
datetime.timedelta(hours=2)).strftime('%Y-%m-%dT%H:%M:%S'),
}
self.init_state_from_disk()
self._recheck_period = config.getint('poller', 'recheck_period', fallback=3)
self._recheck_lookback = config.getint('poller', 'recheck_lookback', fallback=9)
listmodname = config.get('list', 'module', fallback='netdev')
self.list_module = import_module(listmodname)
self._local_sock = None
self._start_lock_sock(config)
def init_state_from_disk(self) -> None:
try:
with open('poller.state', 'r') as f:
loaded = json.load(f)
for k in loaded.keys():
self._state[k] = loaded[k]
except FileNotFoundError:
pass
def _series_determine_tree(self, s: PwSeries) -> str:
s.tree_name = self.list_module.series_tree_name_direct(self._trees.keys(), s)
s.tree_mark_expected = True
s.tree_marked = bool(s.tree_name)
if s.is_pure_pull():
if s.title.find('-next') >= 0:
s.tree_name = self.list_module.next_tree
else:
s.tree_name = self.list_module.current_tree
s.tree_mark_expected = None
return f"Pull request for {s.tree_name}"
if s.tree_name:
log(f'Series is clearly designated for: {s.tree_name}', "")
return f"Clearly marked for {s.tree_name}"
s.tree_mark_expected, should_test = self.list_module.series_tree_name_should_be_local(s)
if not should_test:
log("No tree designation found or guessed", "")
return "Not a local patch"
if self.list_module.series_ignore_missing_tree_name(s):
s.tree_mark_expected = None
log('Okay to ignore lack of tree in subject, ignoring series', "")
return "Series ignored based on subject"
if s.tree_mark_expected:
log_open_sec('Series should have had a tree designation')
else:
log_open_sec('Series okay without a tree designation')
if self.list_module.current_tree in self._trees and \
self.list_module.series_is_a_fix_for(s, self._trees[self.list_module.current_tree]):
s.tree_name = self.list_module.current_tree
elif self.list_module.next_tree in self._trees and \
self._trees[self.list_module.next_tree].check_applies(s):
s.tree_name = self.list_module.next_tree
if s.tree_name:
log(f"Target tree - {s.tree_name}", "")
res = f"Guessed tree name to be {s.tree_name}"
else:
log("Target tree not found", "")
res = "Guessing tree name failed - patch did not apply"
log_end_sec()
return res
def series_determine_tree(self, s: PwSeries) -> str:
log_open_sec('Determining the tree')
try:
ret = self._series_determine_tree(s)
finally:
log_end_sec()
return ret
def _process_series(self, pw_series, force_tree=None) -> None:
s = PwSeries(self._pw, pw_series)
log("Series info",
f"Series ID {s['id']}\n" + f"Series title {s['name']}\n" +
f"Author {s['submitter']['name']}\n" + f"Date {s['date']}")
log_open_sec('Patches')
for p in s['patches']:
log(p['name'], "")
log_end_sec()
if force_tree:
comment = f"Force tree {force_tree}"
s.tree_name = force_tree
s.tree_mark_expected = None
s.tree_marked = True
else:
comment = self.series_determine_tree(s)
if not s['received_all']:
raise IncompleteSeries
s.need_async = self.list_module.series_needs_async(s)
if s.need_async:
comment += ', async'
if hasattr(s, 'tree_name') and s.tree_name:
s.tree_selection_comment = comment
if not s.tree_name in self._work_queues:
log(f"skip {pw_series['id']} for unknown tree {s.tree_name}", "")
return
self._work_queues[s.tree_name].put(s)
else:
core.write_tree_selection_result(self.result_dir, s, comment)
core.mark_done(self.result_dir, s)
def process_series(self, pw_series, force_tree=None) -> None:
log_open_sec(f"Checking series {pw_series['id']} with {pw_series['total']} patches")
try:
self._process_series(pw_series, force_tree)
finally:
log_end_sec()
def _start_lock_sock(self, config) -> None:
socket_path = config.get('poller', 'local_sock_path', fallback=None)
if not socket_path:
return
if os.path.exists(socket_path):
os.unlink(socket_path)
self._local_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self._local_sock.setblocking(False)
self._local_sock.bind(socket_path)
self._local_sock.listen(5)
log(f"Socket listener started on {socket_path}", "")
def _check_local_sock(self) -> None:
if not self._local_sock:
return
try:
conn, _ = self._local_sock.accept()
except BlockingIOError:
return
log_open_sec("Processing local socket connection")
try:
data = b""
while True:
chunk = conn.recv(4096)
data += chunk
if len(chunk) < 4096:
break
if data:
data = data.decode("utf-8")
series_ids = []
items = data.split(";")
for item in items:
item = item.strip()
if not item:
continue
# We accept "series [tree]; series [tree]; ..."
parts = item.rsplit(" ", 1)
if len(parts) == 2:
tree = parts[1].strip()
else:
tree = None
try:
s_id = int(parts[0].strip())
series_ids.append((tree, s_id))
log("Processing", series_ids[-1])
except ValueError:
log("Invalid number in tuple", item)
continue
for tree, series_id in series_ids:
try:
pw_series = self._pw.get("series", series_id)
self.process_series(pw_series, force_tree=tree)
conn.sendall(f"OK: {series_id}\n".encode("utf-8"))
except Exception as e:
log("Error processing series", str(e))
conn.sendall(f"ERROR: {series_id}: {e}\n".encode("utf-8"))
else:
conn.sendall(b"DONE\n")
except Exception as e:
log("Error processing socket request", str(e))
finally:
conn.close()
log_end_sec()
def run(self, life) -> None:
since = self._state['last_event_ts']
try:
# We poll every 2 minutes after this
secs = 0
while life.next_poll(secs):
req_time = datetime.datetime.now()
log_open_sec(f"Querying patchwork at {req_time} since {since}")
json_resp, since = self._pw.get_new_series(since=since)
log(f"Loaded {len(json_resp)} series", "")
# Advance the time by 1 usec, pw does >= for time comparison
since = datetime.datetime.fromisoformat(since)
since += datetime.timedelta(microseconds=1)
since = since.isoformat()
for pw_series in json_resp:
try:
self.process_series(pw_series)
except IncompleteSeries:
# didn't make it to the list fully, patchwork
# shouldn't have had this event at all though
pass
self._check_local_sock()
while not self._done_queue.empty():
s = self._done_queue.get()
log(f"Testing complete for series {s['id']}", "")
secs = 120 - (datetime.datetime.now() - req_time).total_seconds()
if secs > 0:
log(f"Sleep {secs} seconds")
log_end_sec()
except KeyboardInterrupt:
pass # finally will still run, but don't splat
finally:
# Dump state before trying to stop workers, in case they hang
self._state['last_event_ts'] = since
with open('poller.state', 'w') as f:
json.dump(self._state, f)
log_open_sec(f"Stopping threads")
for worker in self._workers:
worker.should_die = True
worker.queue.put(None)
for worker in self._workers:
log(f"Waiting for worker {worker.tree.name} / {worker.name}")
worker.join()
log_end_sec()
if __name__ == "__main__":
os.umask(0o002)
config = configparser.ConfigParser()
config.read(['nipa.config', 'pw.config', 'poller.config'])
log_dir = config.get('log', 'dir', fallback=NIPA_DIR)
log_init(config.get('log', 'type', fallback='org'),
config.get('log', 'file', fallback=os.path.join(log_dir, "poller.org")))
life = NipaLifetime(config)
poller = PwPoller(config)
poller.run(life)
life.exit()