-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixture_3.py
434 lines (373 loc) · 14.4 KB
/
fixture_3.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/env python
# Version 2
import hashlib
import http.server
import http.server
import json
import optparse
import os
import queue
import random
import signal
import socketserver
import string
import sys
import threading
import time
import urllib.error
import urllib.parse
import urllib.request
assert sys.version_info >= (3, 6)
block_size = 100
countdown_window = block_size * 4
# final_wait's value is reference in README.txt, so if we change
# this value then it must be updated there also.
final_wait = 60 # seconds
class COUNTER:
ORPHANS_CREATED = 'orphans_created'
DEFECTS_CREATED = 'defects_created'
JOINED_CREATED = 'joined_created'
ORPHANS_RECEIVED = 'orphans_received'
JOINED_RECEIVED = 'joined_received'
RECORDS_RECEIVED_SUCCESS = 'received_success'
RECORDS_RECEIVED_FAILURE = 'received_failure'
def main():
parser = optparse.OptionParser()
parser.add_option('-f', '--file', dest='results_file', default='expected.txt',
help='write master data to FILE', metavar='FILE')
parser.add_option('-o', '--outfile', dest='output_file', default='submitted.txt',
help='write submitted results to FILE', metavar='FILE')
parser.add_option('-n', '--num', dest='num', default=1000,
help='send roughly NUM messages (minimum %d)' % (block_size * 2), metavar='NUM', )
parser.add_option('--host', dest='host', default='',
help='host to connect to HOST', metavar='HOST')
parser.add_option('-p', '--port', dest='port', default=7299,
help='listen on PORT', metavar='PORT')
(opts, args) = parser.parse_args()
ChallengeFixture.state_model.set_max(int(opts.num))
threading.Thread(target=write_out, args=(ChallengeFixture.outs, opts.output_file)).start()
threading.Thread(target=results_dumper, args=(ChallengeFixture.state_model, opts.results_file)).start()
threading.Thread(target=shepherd, args=(ChallengeFixture.state_model, )).start()
server_address = ('', int(opts.port))
httpd = ThreadingSimpleServer(server_address, ChallengeFixture)
try:
httpd.serve_forever()
except KeyboardInterrupt:
sys.stderr.flush()
sys.stdout.flush()
print("Escape via keyboard; terminating with extreme prejudice.", file=sys.stderr)
sys.stderr.flush()
dump_state(ChallengeFixture.state_model)
os.kill(os.getpid(), signal.SIGTERM)
time.sleep(5)
os.kill(os.getpid(), signal.SIGKILL)
def shepherd(model):
while True:
if model.is_complete():
time.sleep(3) # wait for other threads to flush output
sys.stdout.flush()
sys.stderr.flush()
print("Looks like we're done. Waiting %d seconds for incoming messages." % final_wait, file=sys.stderr)
sys.stderr.flush()
time.sleep(final_wait) # yeah, ok, this is really hacky, but it's late at night
dump_state(model)
os.kill(os.getpid(), signal.SIGTERM)
if model.kill is not None:
print(model.kill, file=sys.stderr)
dump_state(model)
os.kill(os.getpid(), signal.SIGTERM)
time.sleep(0.1)
class AccessGuard:
def __init__(self):
self.countdown = random.randint(countdown_window, countdown_window + 10)
self.lock = threading.RLock()
def is_blocked(self):
with self.lock:
if self.countdown == 0:
return True
self.countdown -= 1
return False
def reset(self):
with self.lock:
self.countdown = random.randint(countdown_window, countdown_window + 10)
class StateModel():
def __init__(self):
self.lock = threading.RLock()
self.kill = None # If set then the shepherd kills immediately.
self.max = block_size
self.count = 0
self.counters = {
COUNTER.ORPHANS_CREATED: 0,
COUNTER.DEFECTS_CREATED: 0,
COUNTER.JOINED_CREATED: 0,
COUNTER.ORPHANS_RECEIVED: 0,
COUNTER.JOINED_RECEIVED: 0,
COUNTER.RECORDS_RECEIVED_SUCCESS: 0,
COUNTER.RECORDS_RECEIVED_FAILURE: 0,
}
self.done = False
self.drained_a = False
self.drained_b = False
self.results_blocks = []
self.source_a_queue = queue.Queue()
self.source_b_queue = queue.Queue()
def set_max(self, x):
self.max = x
def get_source_a_next(self):
with self.lock:
if self.source_a_queue.empty():
self.feed_queues()
if self.source_a_queue.empty() and self.done:
self.drained_a = True
return (None, None)
return self.source_a_queue.get()
def get_source_b_next(self):
with self.lock:
if self.source_b_queue.empty():
self.feed_queues()
if self.source_b_queue.empty() and self.done:
self.drained_b = True
return (None, None)
return self.source_b_queue.get()
def generate_block(self, low, high):
data = []
for i in range(low, high):
kind = self.generate_kind()
if kind in KIND.ORPHANS:
self.counters[COUNTER.ORPHANS_CREATED] += 1
elif kind in KIND.DEFECTS:
self.counters[COUNTER.DEFECTS_CREATED] += 1
elif kind == KIND.JOINED:
self.counters[COUNTER.JOINED_CREATED] += 1
else:
raise Exception('Unreachable state reached. Please let HR at Raisin know.')
data.append((i, kind))
random.shuffle(data)
return data
def feed_queues(self):
with self.lock:
if self.done:
return
low = self.count
high = max(low + random.randint(block_size, block_size * 2), self.max)
if high >= self.max:
self.done = True
self.count = high
block = self.generate_block(low, high)
for (i, kind) in block:
self.queue_datum(i, kind)
self.results_blocks.append(block)
def get_results_blocks(self):
with self.lock:
if self.results_blocks:
rb = self.results_blocks
self.results_blocks = []
return rb
else:
return []
def is_done(self):
with self.lock:
return self.done
def generate_kind(self):
n = random.randint(0, 100) # a percentage
if n < 5:
return KIND.ORPHAN_A
elif n < 10:
return KIND.ORPHAN_B
elif n < 13:
return KIND.DEFECT_A
elif n < 16:
return KIND.DEFECT_B
else:
return KIND.JOINED
def queue_datum(self, i, kind):
if kind == KIND.ORPHAN_A:
with self.lock:
self.source_a_queue.put((i, True))
elif kind == KIND.ORPHAN_B:
with self.lock:
self.source_b_queue.put((i, True))
elif kind == KIND.DEFECT_A:
with self.lock:
self.source_a_queue.put((i, False))
elif kind == KIND.DEFECT_B:
with self.lock:
self.source_b_queue.put((i, False))
else:
with self.lock:
self.source_a_queue.put((i, True))
self.source_b_queue.put((i, True))
def is_complete(self):
with self.lock:
return self.done and self.drained_a and self.drained_b and not self.results_blocks
class ChallengeFixture(http.server.CGIHTTPRequestHandler):
guard_source_a = AccessGuard()
guard_source_b = AccessGuard()
guard_sink_a = AccessGuard()
state_model = StateModel()
outs = queue.Queue()
def do_GET(self):
collapsed_path = http.server._url_collapse_path(urllib.parse.unquote(self.path))
if collapsed_path.endswith("/") and collapsed_path != "/":
collapsed_path = collapsed_path[:-1]
if collapsed_path == "/source/a":
return self.do_source_a()
elif collapsed_path == "/source/b":
return self.do_source_b()
else:
self.send_error(404, "No such endpoint (%s)" % collapsed_path)
return
def do_POST(self):
collapsed_path = http.server._url_collapse_path(urllib.parse.unquote(self.path))
if collapsed_path.endswith("/") and collapsed_path != "/":
collapsed_path = collapsed_path[:-1]
if collapsed_path == "/sink/a":
return self.do_sink_a()
else:
self.send_error(404, "No such endpoint (%s)" % collapsed_path)
return
def do_source_a(self):
if self.guard_source_a.is_blocked():
self.send_error(406, "you gotta read or write somewhere else first")
return
self.guard_source_b.reset()
self.guard_sink_a.reset()
self.send_response(200, "success")
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
datum = self.state_model.get_source_a_next()
if datum is None:
self.send_error(406, "nothing else at the moment")
return
self.wfile.write(bytearray(generate_json(datum), 'utf-8'))
def do_source_b(self):
if self.guard_source_b.is_blocked():
self.send_error(406, "you gotta read or write somewhere else first")
return
self.guard_source_a.reset()
self.guard_sink_a.reset()
self.send_response(200, "success")
self.send_header("Content-Type", "application/xml")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
datum = self.state_model.get_source_b_next()
if datum is None:
self.send_error(406, "nothing else at the moment")
return
self.wfile.write(bytearray(generate_xml(datum), 'utf-8'))
def do_sink_a(self):
# Give a helpful message if they ignored the instructions.
if (self.state_model.drained_a and
self.state_model.drained_b and
block_size >= 50 and
self.state_model.counters[COUNTER.RECORDS_RECEIVED_SUCCESS] == 0):
self.state_model.kill = "You must start sending results before reading is complete."
self.guard_source_a.reset()
self.guard_source_b.reset()
try:
bytes = int(self.headers['Content-Length'])
data = json.loads(self.rfile.read(bytes))
if set(["id", "kind"]) != set(data.keys()):
raise ValueError
self.outs.put((data['id'], data['kind']))
self.state_model.counters[COUNTER.RECORDS_RECEIVED_SUCCESS] += 1
if data['kind'] == 'joined':
self.state_model.counters[COUNTER.JOINED_RECEIVED] += 1
elif data['kind'] == 'orphaned':
self.state_model.counters[COUNTER.ORPHANS_RECEIVED] += 1
except Exception as e:
print("Exception while ingesting results: " + str(e))
self.state_model.counters[COUNTER.RECORDS_RECEIVED_FAILURE] += 1
resp = '{"status": "fail"}'
self.send_response(200, "bad result")
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", len(resp))
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
self.wfile.write(bytearray(resp, 'utf-8'))
return
try:
self.send_response(200, "success")
resp = '{"status": "ok"}'
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", len(resp))
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
self.wfile.write(bytearray(resp, 'utf-8'))
except Exception as e:
print("Exception while sending results: " + str(e))
def results_dumper(model, results_file):
def kind_op(k):
if k in KIND.ORPHANS:
return "orphaned"
elif k in KIND.DEFECTS:
return "defect"
else:
return "joined"
with open(results_file, "w") as f:
while True:
blocks = model.get_results_blocks()
if not blocks and model.is_done():
return
for block in blocks:
for (i, kind) in block:
op = kind_op(kind)
if op != "defect":
print("%s %s" % (op, hash_key(i)), file=f)
time.sleep(0.3)
def write_out(outs, out_file):
with open(out_file, "w") as f:
while True:
if not outs.empty():
(i, kind) = outs.get()
print("%s %s" % (kind, i), file=f)
print("%s %s" % (kind, i))
f.flush()
else:
time.sleep(1)
def is_sentinel(x):
"""x can never be None"""
(id, kind) = x
return id is None and kind is None
def hash_key(i):
return hashlib.md5(str(i).encode('utf-8')*13).hexdigest()
def random_string(n):
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(n))
def generate_json(datum):
if is_sentinel(datum):
return '{"status": "done"}'
(i, valid) = datum
if valid:
return '{"status": "ok", "id": "%s"}' % hash_key(i)
else:
return '{"status": "ok", "id": [%s [}' % random_string(random.randint(10, 100))
def generate_xml(datum):
if is_sentinel(datum):
return '<?xml version="1.0" encoding="UTF-8"?><msg><done/></msg>'
(i, valid) = datum
if valid:
return '<?xml version="1.0" encoding="UTF-8"?><msg><id value="%s"/></msg>' % hash_key(i)
else:
return '<?xml version="1.0" encoding="UTF-8"?><msg><%s</foo></msg>' % random_string(random.randint(10, 100))
class KIND:
ORPHAN_A = "ORPHAN_A"
ORPHAN_B = "ORPHAN_B"
DEFECT_A = "DEFECT_A"
DEFECT_B = "DEFECT_B"
JOINED = "JOINED"
ORPHANS = [ORPHAN_A, ORPHAN_B]
DEFECTS = [DEFECT_A, DEFECT_B]
class ThreadingSimpleServer(socketserver.ThreadingMixIn,
http.server.HTTPServer):
pass
def dump_state(model):
json.dump(
model.counters,
sys.stdout,
indent=2,
sort_keys=True)
# necessary to flush the closing paren before termination
print()
if __name__ == '__main__':
main()