forked from owolabioromidayo/lightbox_simulations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
233 lines (156 loc) · 6.84 KB
/
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
import os, sys, json, random, base64, time, copy, requests, datetime, threading
from flask import Flask, request
# from flask_limiter import Limiter
# from flask_limiter.util import get_remote_address
import heapq
app = Flask(__name__)
TRUST_SCORE_UPDATE = 40
TRUST_SCORE_RESET_PVE_HRS = 24
TRUST_SCORE_RESET_NVE_HRS = 48
GPU_TASK_SLEEP_TIME = 5
FEDERATED_TRUST_SCORE = 400
GPU_WORKERS = None
LOGFILE= None
def log_to_file(str):
str += '\n'
# print(LOGFILE)
fname = LOGFILE
if os.path.isfile(fname):
with open(fname, 'a' ) as f:
f.write(str)
return
else:
with open(fname, 'w' ) as f:
f.write(str)
class PriorityQueue:
def __init__(self):
self._queue = []
self._index = 0
def push(self, item, priority):
heapq.heappush(self._queue, (-priority, self._index, item))
self._index += 1
def pop(self):
return heapq.heappop(self._queue)[-1]
def __len__(self):
return len(self._queue)
workers = dict()
work_queue = dict()
work_returns = dict()
# limiter = Limiter(
# get_remote_address,
# app=app,
# default_limits=["100 per minute"],
# storage_uri="memory://",
# strategy="fixed-window", # or "moving-window"
# )
#we need the thread that acts on the queue information. normally we have
#individual gpu workers checking for tasks. we can do that
def gpu_worker_thread(id):
#initialization
# workers[id] = {"in_use": False}
work_queue[id] = PriorityQueue()
while True:
#check if task
if len(work_queue[id]) > 0:
new_task = work_queue[id].pop() #pop and execute highest priority task
# print(f" Sending new task", new_task)
time.sleep(GPU_TASK_SLEEP_TIME)
work_returns[new_task["timestamp"]] = 1 #populate work returns
else:
time.sleep(1) #sleep some more if no task
time.sleep(2) #sleep
# print(workers)
# print(work_queue[1])
@app.route("/federated/execute_task/<_id>", methods=["GET","POST"])
def exec_federated_task(_id):
_id = int(_id)
timestamp = time.time()
work_queue[_id].push({"timestamp": timestamp}, FEDERATED_TRUST_SCORE)
work_returns[timestamp] = None
while work_returns[timestamp] == None:
time.sleep(5)
continue
# if work_returns[timestamp] == "Failed":
# return "Task was aborted by GPU client", 500
# _response = work_returns[timestamp]
del work_returns[timestamp]
return "Done!", 200
@app.route("/make_federated_request/<secondary>/<_id>", methods=["GET","POST"])
def query_federated_server(secondary, _id):
_json = request.json
_id = int(_id)
_start = datetime.datetime.now()
trust_score = _json["trust_score"]
last_update_time = _json["last_update_time"]
user_id = _json['user_id']
user_count = _json['user_count']
task_count = _json['task_count']
trust_score = _json["trust_score"]
# last_update_time = _json["last_update_time"]
# user_id = _json['user_id']
last_update_time = datetime.datetime.fromtimestamp(last_update_time)
time_delta = datetime.datetime.utcnow() - last_update_time
hours_delta = time_delta / datetime.timedelta(hours=1)
if trust_score > 0 and hours_delta >= TRUST_SCORE_RESET_PVE_HRS:
trust_score = 1000
if trust_score <= 0 and hours_delta >= TRUST_SCORE_RESET_NVE_HRS:
trust_score = 1000
if trust_score <= 0:
_response = {"trust_score" : trust_score, "last_update_time": datetime.datetime.utcnow().timestamp()}
log_to_file(f"{str(datetime.datetime.now())} /make_federated_request {user_count} {(datetime.datetime.now() - _start).seconds} {user_id} {_id} {trust_score} {task_count}")
return {"data" : _response}
_url = f"http://localhost:{secondary}"
#make federated request to server
response = requests.get(f"{_url}/federated/execute_task/{_id}")
_response = {"trust_score" : trust_score, "last_update_time": datetime.datetime.utcnow().timestamp()}
log_to_file(f"{str(datetime.datetime.now())} /make_federated_request {user_count} {(datetime.datetime.now() - _start).seconds} {user_id} {_id} {trust_score} {task_count}")
if response.status_code == 200:
return {"data" : _response}
else:
return {"data" : _response}
# return "Request failed", response.status_code
@app.route("/execute_task/<_id>", methods=["GET","POST"])
def exec_task(_id):
_start = datetime.datetime.now()
_json = request.json
_id = int(_id)
trust_score = _json["trust_score"]
last_update_time = _json["last_update_time"]
user_id = _json['user_id']
user_count = _json['user_count']
task_count = _json['task_count']
last_update_time = datetime.datetime.fromtimestamp(last_update_time)
#check trust score and last update time
time_delta = datetime.datetime.utcnow() - last_update_time
hours_delta = time_delta / datetime.timedelta(hours=1)
if trust_score > 0 and hours_delta >= TRUST_SCORE_RESET_PVE_HRS:
trust_score = 1000
if trust_score <= 0 and hours_delta >= TRUST_SCORE_RESET_NVE_HRS:
trust_score = 1000
if trust_score <= 0:
_response = {"trust_score" : trust_score, "last_update_time": datetime.datetime.utcnow().timestamp()}
log_to_file(f"{str(datetime.datetime.now())} /exec_task {user_count} {(datetime.datetime.now() - _start).seconds} {user_id} {_id} {trust_score} {task_count}")
return {"data" : _response}
timestamp = time.time()
work_queue[_id].push({"timestamp": timestamp}, trust_score)
# print([len(work_queue[i]) for i in range(10) ])
work_returns[timestamp] = None
while work_returns[timestamp] == None:
time.sleep(5)
continue
# if work_returns[timestamp] == "Failed":
# return "Task was aborted by GPU client", 500
trust_score -= TRUST_SCORE_UPDATE #trust score update
_response = {"trust_score" : trust_score, "last_update_time": datetime.datetime.utcnow().timestamp()}
del work_returns[timestamp]
log_to_file(f"{str(datetime.datetime.now())} /exec_task {user_count} {(datetime.datetime.now() - _start).seconds} {user_id} {_id} {trust_score} {task_count}")
return {"data" : _response}
if __name__ == "__main__":
port = sys.argv[1]
GPU_WORKERS = int(sys.argv[2])
USER_COUNT= int(sys.argv[3])
LOGFILE = f'log_baseline_{USER_COUNT}_{GPU_WORKERS}_multiple_{random.randrange(112)}'
for i in range(GPU_WORKERS):
thread = threading.Thread(target=gpu_worker_thread, args=(i,))
thread.start()
app.run(debug=True, port=port)