forked from xiph/rd_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrd_server.py
executable file
·296 lines (278 loc) · 9.8 KB
/
rd_server.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
#!/usr/bin/env python3
import tornado.ioloop
import tornado.web
import os
import codecs
import json
import argparse
import sshslot
import threading
import time
import awsremote
from work import *
from utility import *
video_sets_f = codecs.open('sets.json','r',encoding='utf-8')
video_sets = json.load(video_sets_f)
machines = []
slots = []
free_slots = []
work_list = []
run_list = []
work_done = []
args = {}
config = {
'runs': '../runs/',
'codecs': '../'
}
def lookup_run_by_id(run_id):
for run in run_list:
if run.runid == run_id:
return run
return None
class CancelHandler(tornado.web.RequestHandler):
def get(self):
global work_list
global work_done
run_id = self.get_query_argument('run_id')
rd_print(None,'Cancelling '+run_id)
run = lookup_run_by_id(run_id)
if not run:
self.write('run_id not found')
return
run.cancel()
for work in work_list[:]:
if work.runid == run_id:
work_list.remove(work)
work.cancel()
work_done.append(work)
else:
print(work.runid)
print(len(work_list))
self.write('ok')
class RunSubmitHandler(tornado.web.RequestHandler):
def get(self):
global work_list
global run_list
run_id = self.get_query_argument('run_id')
rundir = config['runs'] + '/' + run_id
info_file_path = rundir + '/info.json'
log_file_path = rundir + '/output.txt'
info_file = open(info_file_path, 'r')
log_file = open(log_file_path, 'a')
info = json.load(info_file)
run = RDRun(info['codec'])
run.info = info
run.runid = run_id
run.rundir = config['runs'] + '/' + run_id
run.log = log_file
run.set = info['task']
run.bindir = run.rundir + '/x86_64/'
run.prefix = run.rundir + '/' + run.set
try:
os.mkdir(run.prefix)
except FileExistsError:
pass
if 'qualities' in info:
if info['qualities'] != '':
run.quality = info['qualities'].split()
if 'extra_options' in info:
run.extra_options = info['extra_options']
if 'save_encode' in info:
if info['save_encode']:
run.save_encode = True
run.status = 'running'
run.write_status()
run_list.append(run)
video_filenames = video_sets[run.set]['sources']
run.work_items = create_rdwork(run, video_filenames)
work_list.extend(run.work_items)
if False:
if 'ab_compare' in info:
if info['ab_compare']:
abrun = ABRun(info['codec'])
abrun.runid = run_id
abrun.rundir = config['runs'] + '/' + run_id
abrun.log = log_file
abrun.set = info['task']
abrun.bindir = config['codecs'] + '/' + info['codec']
abrun.prefix = run.rundir + '/' + run.set
run_list.append(abrun)
abrun.work_items.extend(create_abwork(abrun, video_filenames))
work_list.extend(abrun.work_items)
pass
self.write(run_id)
class WorkListHandler(tornado.web.RequestHandler):
def get(self):
self.set_header("Content-Type", "application/json")
self.write(json.dumps([w.get_name() for w in work_list]))
class RunStatusHandler(tornado.web.RequestHandler):
def get(self):
self.set_header("Content-Type", "application/json")
runs = []
for run in run_list:
run_json = {}
run_json['run_id'] = run.runid
run_json['completed'] = 0
run_json['total'] = 0
run_json['info'] = run.info
for work in run.work_items:
run_json['total'] += 1
if work.done:
run_json['completed'] += 1
runs.append(run_json)
self.write(json.dumps(runs))
class MachineUsageHandler(tornado.web.RequestHandler):
def get(self):
global machines
machine_usage = []
for machine in machines:
machine_json = {}
slot_in_use = []
for slot in machine.slots:
if slot.work:
slot_in_use.append(slot.work.get_name())
elif slot.busy:
slot_in_use.append('busy with no work')
else:
slot_in_use.append('None')
machine_json['name'] = machine.get_name()
machine_json['slots'] = slot_in_use
machine_usage.append(machine_json)
self.write(json.dumps(machine_usage))
class FreeSlotsHandler(tornado.web.RequestHandler):
def get(self):
global free_slots
slot_text = []
for slot in free_slots:
if slot.work:
slot_text.append(slot.work.get_name())
elif slot.busy:
slot_text.append('busy with no work')
else:
slot_text.append('None')
self.write(json.dumps(slot_text))
class ExecuteTick(tornado.web.RequestHandler):
def get(self):
scheduler_tick()
self.write('ok')
def main():
global free_slots
global machines
global slots
global args
parser = argparse.ArgumentParser(description='Run AWCY scheduler daemon.')
parser.add_argument('-machineconf')
parser.add_argument('-port',default=4000)
parser.add_argument('-awsgroup', default='AOM Test')
parser.add_argument('-max-machines', default=3, type=int)
args = parser.parse_args()
if args.machineconf:
machineconf = json.load(open(args.machineconf, 'r'))
for m in machineconf:
machines.append(sshslot.Machine(m['host'],m['user'],m['cores'],m['work_root'],str(m['port']),m['media_path']))
for machine in machines:
slots.extend(machine.get_slots())
free_slots.extend(slots)
app = tornado.web.Application(
[
(r"/work_list.json", WorkListHandler),
(r"/run_status.json", RunStatusHandler),
(r"/machine_usage.json", MachineUsageHandler),
(r"/free_slots.json", FreeSlotsHandler),
(r"/submit", RunSubmitHandler),
(r"/cancel", CancelHandler),
(r"/execute_tick",ExecuteTick)
],
static_path=os.path.join(os.path.dirname(__file__), "static"),
xsrf_cookies=True,
debug=False,
)
app.listen(args.port)
ioloop = tornado.ioloop.IOLoop.current()
if not args.machineconf:
machine_thread = threading.Thread(target=machine_allocator,daemon=True)
machine_thread.start()
scheduler_tick()
ioloop.start()
def machine_allocator():
global slots
global free_slots
global machines
global work_list
global run_list
while 1:
# start all machines if we don't have any but have work queued
if len(work_list) and not len(machines):
rd_print(None, "Starting machines.")
machines = []
while not machines:
machines = awsremote.get_machines(args.max_machines, args.awsgroup)
for machine in machines:
slots.extend(machine.get_slots())
free_slots.extend(slots)
time.sleep(60*10) # don't shut down for a tleast 10 minutes
# stop all machines if nothing is running
slots_busy = False
for slot in slots:
if slot.busy:
slots_busy = True
if not slots_busy and not len(work_list) and not len(run_list):
rd_print(None, "Stopping all machines.")
machines = []
slots = []
free_slots = []
awsremote.stop_machines(args.awsgroup)
time.sleep(60)
def scheduler_tick():
global free_slots
global work_list
global run_list
global work_done
max_retries = 5
# look for completed work
for slot in slots:
if slot.busy == False and slot.work != None:
if slot.work.failed == False:
slot.work.done = True
work_done.append(slot.work)
rd_print(slot.work.log,slot.work.get_name(),'finished.')
elif slot.work.retries < max_retries:
slot.work.retries += 1
rd_print(slot.work.log,'Retrying work ',slot.work.get_name(),'...',slot.work.retries,'of',max_retries,'retries.')
slot.work.failed = False
work_list.insert(0, slot.work)
else:
slot.work.done = True
work_done.append(slot.work)
rd_print(slot.work.log,slot.work.get_name(),'given up on.')
slot.work = None
free_slots.append(slot)
# fill empty slots with new work
if len(work_list) != 0:
if len(free_slots) != 0:
slot = free_slots.pop()
work = work_list.pop(0)
slot.work = work
rd_print(slot.work.log,'Encoding',work.get_name(),'on',slot.machine.host)
work_thread = threading.Thread(target=slot.execute, args=(work,))
work_thread.daemon = True
slot.busy = True
work_thread.start()
# find runs where all work has been completed
for run in run_list:
done = True
for work in run.work_items:
if work.done == False:
done = False
if done:
run_list.remove(run)
try:
run.reduce()
except Exception as e:
rd_print(run.log,e)
rd_print(run.log,'Failed to run reduce step on '+run.runid)
rd_print(run.log,'Finished '+run.runid)
run.finish()
tornado.ioloop.IOLoop.current().call_later(1,scheduler_tick)
if __name__ == "__main__":
main()