-
Notifications
You must be signed in to change notification settings - Fork 9
/
work_manager.py
executable file
·229 lines (207 loc) · 12.3 KB
/
work_manager.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
#!/usr/bin/env python
import rospy
import argparse
import json
import yaml
from threading import RLock
from os.path import join
from rospkg import RosPack
from copy import deepcopy
from apex_playground.srv import UpdateWorkStatus, GetWork, AddWork, GetWorkResponse, UpdateWorkStatusResponse, AddWorkResponse
class WorkManager(object):
def __init__(self, outside_ros=False):
self.rospack = RosPack()
self.num_workers = 0
self.outside_ros = rospy.get_param('/use_sim_time', outside_ros) # True if work manager <-> controller comm must use ZMQ
self.file_experiment = join(self.rospack.get_path('apex_playground'), 'config', 'experiment.json')
with open(self.file_experiment) as f:
experiment = self.check(json.load(f))
rospy.set_param('/work', experiment)
self.lock_experiment = RLock()
self.failing_workers = {} # association {num_worker: remaining attempts before blacklist}
@staticmethod
def is_completed(task, trial, experiment):
if experiment[task]['progress'][trial]['status'] == 'complete':
return True
return experiment[task]['progress'][trial]['iteration'] >= experiment[task]['num_iterations'] - 1
def _cb_get_work(self, worker):
with self.lock_experiment:
experiment = rospy.get_param('/work')
disabled_workers = rospy.get_param("/experiment/disabled_workers", [])
if worker not in disabled_workers:
for task in range(len(experiment)):
for trial in range(experiment[task]['num_trials']):
status = experiment[task]['progress'][trial]['status']
resuming = status =='taken' and experiment[task]['progress'][trial]['worker'] == worker
# If this work is open or taken and the same worker is requesting some work, distribute it!
if status == 'open' or resuming:
if self.is_completed(task, trial, experiment):
experiment[task]['progress'][trial]['status'] = 'complete'
self.num_workers -= 1
rospy.set_param('/work', experiment)
else:
# This task needs work, distribute it to the worker
experiment[task]['progress'][trial]['status'] = 'taken'
experiment[task]['progress'][trial]['worker'] = worker
rospy.set_param('/work', experiment)
self.num_workers += 1
if resuming:
rospy.logwarn("Resuming worker {} {} from iteration {}/{}".format(worker, experiment[task]['method'],
experiment[task]['progress'][trial]['iteration'],
experiment[task]['num_iterations']))
else:
rospy.logwarn("Distributing {} iterations {} trial {} to worker {}".format(experiment[task]['num_iterations'], experiment[task]['method'], trial, worker))
return dict(method=experiment[task]['method'],
iteration=experiment[task]['progress'][trial]['iteration'],
num_iterations=experiment[task]['num_iterations'],
task=task, trial=trial, work_available=True)
else:
pass
#rospy.logwarn("Worker {} requested work but it is blacklisted".format(worker))
return dict(work_available=False)
def _cb_update_work(self, task, trial, worker, iteration, success):
inconsistent = True
with self.lock_experiment:
experiment = rospy.get_param('/work')
if task > len(experiment) - 1:
rospy.logerr("No such task: {}".format(task))
else:
known_status = experiment[task]['progress'][trial]['status']
known_worker = experiment[task]['progress'][trial]['worker']
known_iteration = experiment[task]['progress'][trial]['iteration']
if self.num_workers > 0:
if known_worker == worker or known_worker < 0:
if known_status == 'aborted':
rospy.logerr("Received aborting requested, stopping task of worker {}".format(worker))
return dict(abort=True)
elif known_status == 'taken':
inconsistent = False
if self.is_completed(task, trial, experiment):
experiment[task]['progress'][trial]['status'] = 'complete'
rospy.logwarn("{} trial {} completed by worker {}".format(experiment[task]['method'], trial, worker))
else:
# This is a regular update
if success:
experiment[task]['progress'][trial]['iteration'] = iteration + 1
if iteration % 100 == 0 and iteration > 0:
rospy.loginfo("Regular update: iteration {}/{} from worker {}".format(iteration, experiment[task]['num_iterations'], worker))
else:
# Iteration failed
rospy.logerr("Iteration {}/{} failed on worker {}".format(iteration, experiment[task]['num_iterations'], worker))
if iteration > 0:
# Try to recover if iteration is not 0
new_iteration = iteration - 1
experiment[task]['progress'][trial]['iteration'] = new_iteration
rospy.logerr("rewinding worker {} to iteration {}".format(worker, new_iteration))
else:
# If we rewind up to iteration 0, we might have a hardware issue, abandon
rospy.logerr("Autoblacklisting worker {}".format(worker))
return dict(abort=True)
blacklisted = rospy.get_param("/experiment/disabled_workers", [])
blacklisted.append(worker)
rospy.set_param("/experiment/disabled_workers", blacklisted)
if iteration != known_iteration:
rospy.logwarn("Got iteration {} while expecting {} from worker {}".format(iteration, known_iteration, known_worker))
if known_worker in self.failing_workers:
self.failing_workers[known_worker] -= 1
if self.failing_workers[known_worker] == 0:
rospy.logerr("Autoblacklisting worker {}".format(known_worker))
return dict(abort=True)
blacklisted = rospy.get_param("/experiment/disabled_workers", [])
blacklisted.append(known_worker)
rospy.set_param("/experiment/disabled_workers", blacklisted)
else:
self.failing_workers[known_worker] = 5
if inconsistent:
rospy.logerr("Inconsistent data: Worker ID {} has returned task {} trial {} "
"which is known {} by worker {}".format(worker, task, trial, known_status, known_worker))
else:
rospy.logerr("There is no known worker, we didn't expect that work update")
rospy.set_param('/work', experiment)
self.save_experiment()
return dict(abort=False)
def _cb_add_work(self, request):
with self.lock_experiment:
experiment = rospy.get_param('/work')
task = {
"num_iterations": request.num_iterations,
"num_trials": request.num_trials,
"method": request.method
}
experiment.append(task)
rospy.set_param('/work', self.check(experiment))
return AddWorkResponse(task=len(experiment)-1)
def run(self):
try:
if self.outside_ros:
self.run_outside_ros()
else:
self.run_within_ros()
finally:
self.save_experiment()
def save_experiment(self):
with self.lock_experiment:
experiment = rospy.get_param('/work')
with open(self.file_experiment, 'w') as f:
json.dump(self.cleanup(deepcopy(experiment)), f, indent=4)
def run_outside_ros(self):
# Use ZMQ for work manager <-> controller comm
from zmq.eventloop import zmqstream, ioloop
from zmq import Context, REP
rospy.logwarn('Work Manager is using ZMQ to distribute work')
def callback_call(request):
try:
type = request['type']
worker = request['worker']
if type == 'get':
self.socket.send_json(self._cb_get_work(worker))
elif type == 'update':
task, trial, iteration, success = request['task'], request['trial'], request['iteration'], request['success']
self.socket.send_json(self._cb_update_work(task, trial, worker, iteration, success))
else:
rospy.logerr("The Work Manager can't handle this request type: '{}'".format(type))
except (ValueError, KeyError):
rospy.logerr("The Work Manager can't handle this request: {}".format(request))
self.socket.send_json({})
self.context = Context()
self.loop = ioloop.IOLoop.instance()
self.socket = self.context.socket(REP)
self.socket.bind('tcp://127.0.0.1:33589')
#self.stream = zmqstream.ZMQStream(self.socket, self.loop)
#self.stream.on_recv(callback_call)
while not rospy.is_shutdown():
callback_call(self.socket.recv_json())
self.loop.stop()
def run_within_ros(self):
rospy.logwarn('Work Manager is using ROS to distribute work')
# Use ROS for work manager <-> controller comm
rospy.Service('work/get', GetWork, lambda req: GetWorkResponse(**self._cb_get_work(req.worker)))
rospy.Service('work/update', UpdateWorkStatus, lambda req: UpdateWorkStatusResponse(**self._cb_update_work(req.task, req.trial, req.worker, req.iteration, req.success)))
rospy.Service('work/add', AddWork, self._cb_add_work)
rospy.spin()
@staticmethod
def check(experiment):
# Check and prepare the experiment file
for task in range(len(experiment)):
if 'progress' not in experiment[task]:
experiment[task]['progress'] = []
for trial in range(experiment[task]['num_trials']):
experiment[task]['progress'].append({'iteration': 0, 'status': 'open', 'worker': -1})
return experiment
@staticmethod
def cleanup(experiment):
# Mark all taken jobs as open
#for task in range(len(experiment)):
# if 'progress' in experiment[task]:
# for trial in range(experiment[task]['num_trials']):
# if experiment[task]['progress'][trial]['status'] == 'taken':
# experiment[task]['progress'][trial]['status'] = 'open'
return experiment
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--comm-outside-ros",
help="Work distribution must occur outside ROS master on localhost:33589",
action="store_true")
args, _ = parser.parse_known_args()
rospy.init_node("work_manager")
WorkManager(args.comm_outside_ros).run()