-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroker.py
More file actions
348 lines (290 loc) · 10.7 KB
/
broker.py
File metadata and controls
348 lines (290 loc) · 10.7 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
337
338
339
340
341
342
343
344
345
346
347
348
import argparse
import json
import logging
import sqlite3
# import signal
import typing
import fastapi
import uvicorn
from fastapi.middleware.cors import CORSMiddleware
# -----------------------------------------------------------------------
OOM_AUTO_REDUCE_BATCH_SIZE = True
def db_connect(isolation_level: str = None) -> sqlite3.Connection:
db = sqlite3.connect(
f'broker.sqlite3',
check_same_thread=True,
isolation_level=isolation_level) # autocommit
db.execute("PRAGMA journal_mode=WAL") # concurrency
return db
# -----------------------------------------------------------------------
app = fastapi.FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get(
path="/api/job",
description="Get the job list",
response_class=fastapi.responses.JSONResponse,
)
def getJobList(pageStart: int, count: int, status: str):
with db_connect() as db:
if status == '':
result = db.execute("""
SELECT id, status, whenQueued, whenStarted, whenEnded, workerHostname, workerGPUID
FROM jobs
ORDER BY id DESC
LIMIT ?, ?
""", (pageStart, count))
else:
result = db.execute("""
SELECT id, whenQueued, whenStarted, whenEnded, workerHostname, workerGPUID
FROM jobs
WHERE status = ?
ORDER BY id DESC
LIMIT ?, ?
""", (status, pageStart, count))
result: sqlite3.Cursor
return {
"header": list(map(lambda x: x[0], result.description)),
"data": result.fetchall(),
}
@app.get(
path="/api/job/detail",
description="Get information of a job",
response_class=fastapi.responses.JSONResponse,
)
def getJobDetail(jobID: str):
with db_connect() as db:
result = db.execute("SELECT * FROM jobs WHERE id == ?", (jobID,))
result: sqlite3.Cursor
return dict(zip(
list(map(lambda x: x[0], result.description)),
result.fetchall()[0]
))
@app.post(
path="/api/job/control",
description="CRUD for a job",
response_class=fastapi.responses.JSONResponse,
)
def setJob(request: str = fastapi.Query(''), payload: dict = fastapi.Body({})):
with db_connect() as db:
if request == "insert":
cur = db.cursor()
try:
cur.execute(
"""
INSERT INTO jobs (argument, stdin)
VALUES (?, ?)
""",
(payload['argument'], payload['stdin']))
db.commit()
jobID = cur.lastrowid
return {"jobID": jobID}
except db.Error:
db.rollback()
raise fastapi.HTTPException(
status_code=fastapi.status.HTTP_404_NOT_FOUND, detail="wrong insert")
if request == "delete":
jobID = payload['jobID']
cur = db.cursor()
try:
# incomplete -> NULL
cur.execute("DELETE FROM jobs WHERE id = ?", (jobID,))
db.commit()
except db.Error:
db.rollback()
raise fastapi.HTTPException(
status_code=fastapi.status.HTTP_404_NOT_FOUND, detail="wrong delete")
return
def _jobFunctionWorkerSide_requestGet(db: sqlite3.Connection, payload: dict) -> dict:
# incomplete -> started
result = db.execute(
"""
UPDATE jobs
SET
status = "started",
whenStarted = datetime('now', 'localtime'),
workerHostname = ?,
workerGPUID = ?
WHERE jobs.id = (
SELECT id
FROM jobs
WHERE status = "incomplete"
ORDER BY id ASC
LIMIT 1)
RETURNING jobs.id, jobs.argument, jobs.stdin
""",
(payload["workerHostname"], payload["workerGPUID"]),
).fetchall()
if len(result) > 0:
jobID, argument, stdin = result[0]
return {"jobID": jobID, "argument": argument, "stdin": stdin}
else:
return {"jobID": ""}
def _jobFunctionWorkerSide_requestSet_incomplete(db: sqlite3.Connection, payload: dict):
jobID, workerHostname, workerGPUID =\
payload['jobID'], payload['workerHostname'], payload['workerGPUID']
stdout, stderr = payload['stdout'], payload['stderr']
# started -> incomplete
db.execute(
"""
UPDATE jobs
SET
status = "incomplete",
whenStarted = NULL,
whenEnded = NULL,
workerHostname = NULL,
workerGPUID = NULL,
stdout = ?,
stderr = ?
WHERE
id = ? AND
status = "started" AND
workerHostname = ? AND
workerGPUID = ?
""",
(stdout, stderr,
jobID, workerHostname, workerGPUID))
return None
def _jobFunctionWorkerSide_requestSet_complete(db: sqlite3.Connection, payload: dict):
status, jobID, workerHostname, workerGPUID =\
payload['status'], payload['jobID'], payload['workerHostname'], payload['workerGPUID']
stdout, stderr = payload['stdout'], payload['stderr']
# similar to do-while block
while OOM_AUTO_REDUCE_BATCH_SIZE:
if status != 'failure':
break
if stderr is None:
break
if "cuda out of memory" not in stderr.lower():
break
stdin = db.execute(
'''
SELECT stdin
FROM jobs
WHERE id = ? AND status = "started" AND workerHostname = ? AND workerGPUID = ?
''',
(jobID, workerHostname, workerGPUID)).fetchall()[0][0]
if len(stdin) == 0:
break
stdin = json.loads(stdin)
if 'batchSize' not in stdin:
break
if stdin['batchSize'] <= 1:
# if batchSize is already 1, there is no way to reduce it further
break
# stdin['batchSize'] = int(stdin['batchSize'] / 2)
stdin['batchSize'] = int(float(stdin['batchSize']) * 0.75)
stdin = json.dumps(stdin)
# started -> incomplete
db.execute(
"""
UPDATE jobs
SET
status = "incomplete",
whenStarted = NULL,
whenEnded = NULL,
workerHostname = NULL,
workerGPUID = NULL,
stdin = ?,
stdout = NULL,
stderr = NULL
WHERE
id = ? AND
status = "started" AND
workerHostname = ? AND
workerGPUID = ?
""",
(stdin,
jobID, workerHostname, workerGPUID))
return None
if stdout is not None and len(stdout) == 0:
stdout = None
if stderr is not None and len(stderr) == 0:
stderr = None
# started -> success, failure
db.execute(
"""
UPDATE jobs
SET
status = ?,
whenEnded = datetime('now', 'localtime'),
stdout = ?,
stderr = ?
WHERE
id = ? AND
status = "started" AND
workerHostname = ? AND
workerGPUID = ?
""",
(status, stdout, stderr,
jobID, workerHostname, workerGPUID))
return None
@app.post(
path="/api/job/process",
description="Process jobs (for Worker)",
response_class=fastapi.responses.JSONResponse,
)
def jobFunctionWorkerSide(request: str = fastapi.Query(''), payload: dict = fastapi.Body({})):
with db_connect() as db:
try:
# get a incomplete job
if request == "get":
return _jobFunctionWorkerSide_requestGet(db, payload)
# report the job status
if request == "set":
status = payload['status']
if status == 'incomplete':
return _jobFunctionWorkerSide_requestSet_incomplete(db, payload)
elif status == 'success' or status == 'failure':
return _jobFunctionWorkerSide_requestSet_complete(db, payload)
except db.Error:
db.rollback()
raise fastapi.HTTPException(status_code=fastapi.status.HTTP_404_NOT_FOUND,
detail="nothing to process (no incomplete job in system)")
# Suppress the "200 OK /api/job/process?request=get" log. Too verbose.
class EndpointFilter(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
return record.getMessage().find("POST /api/job/process?request=get") == -1
def main():
'''
# due to the db_connect() function, the following code is not necessary
def _handler(signum: int, _):
db.close()
# print(f"DB closed")
# print(f"Detect {signal.Signals(signum).name}; Goodbye")
exit(0)
signal.signal(signal.SIGINT, _handler)
signal.signal(signal.SIGTERM, _handler)
'''
parser = argparse.ArgumentParser()
parser.add_argument('--port', dest='broker_port', default='9999')
args = vars(parser.parse_args())
BROKER_PORT = int(args['broker_port'])
with db_connect() as db:
db.execute(
"""
CREATE TABLE IF NOT EXISTS jobs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
status TEXT NOT NULL DEFAULT "incomplete",
whenQueued TIMESTAMP NOT NULL DEFAULT (datetime('now', 'localtime')),
whenStarted TIMESTAMP DEFAULT NULL,
whenEnded TIMESTAMP DEFAULT NULL,
workerHostname TEXT DEFAULT NULL,
workerGPUID UNSIGNED SMALLINT DEFAULT NULL,
argument TEXT DEFAULT NULL,
stdin TEXT DEFAULT NULL,
stdout TEXT DEFAULT NULL,
stderr TEXT DEFAULT NULL
)
"""
)
logging.getLogger("uvicorn.access").addFilter(EndpointFilter())
uvicorn.run("broker:app", host="0.0.0.0", port=BROKER_PORT)
# reload=True, reload_dirs=['.'], reload_includes=['broker.py']) # auto reloading is not working as expected; it restarts even when other files are changed
if __name__ == '__main__':
main()