Project

General

Profile

MAUSCelery » History » Version 9

Jackson, Mike, 25 January 2012 15:09

1 1 Jackson, Mike
h1. Celery configuration and monitoring
2
3
MAUS can be used with the Celery asynchronous distributed task queue to allow transform steps to be executed on multiple processors.
4
5
For full information on using Celery, see http://celeryproject.org/ and:
6
7
* "Introduction":http://docs.celeryproject.org/en/latest/getting-started/introduction.html
8
* "Help resources":http://ask.github.com/celery/getting-started/resources.html
9
* "User doc":http://ask.github.com/celery/index.html
10
* "Source code":https://github.com/ask/celery/tree/master/celery
11
12
h2. Installation
13
14
Celery is automatically downloaded and installed when you build MAUS.
15
16
h2. Configure a host to run a Celery worker
17
18
To configure a host to run a Celery worker:
19
20
* Ensure MAUS is available on the host you want to use as a Celery worker.
21
* Ensure you have run:
22
<pre>
23
$ source env.sh
24
</pre>
25
* If you have deployed RabbitMQ on a remote host,
26
** Edit @src/common_py/mauscelery/celeryconfig.py@
27
** Change
28
<pre>
29
BROKER_HOST = "localhost"
30
</pre>
31
to specify the full hostname of the host on which RabbitMQ was deployed e.g.
32
<pre>
33
BROKER_HOST = "maus.epcc.ed.ac.uk"
34
</pre>
35
36
h2. Start up a Celery worker
37
38
To start up a Celery worker, run:
39
<pre>
40
$ celeryd -l INFO -n WORKER_ID
41
</pre>
42
where @WORKER_ID@ is a unique ID for the worker. You should provide one of these as it helps when monitoring if you are using many workers.
43
44
h3. Specify the number of sub-processes
45
46
The worker will set up a number of sub-processes, depending on the number of CPUs available to your host. You can explicitly set the number of sub-processes via the @-c@ flag e.g.
47
<pre>
48
$ celeryd -l INFO -n WORKER_ID -c 2
49
</pre>
50
51 2 Jackson, Mike
You can specify as many sub-processes as you like but exceeding the number of CPUs available may cause performance to suffer.
52
53 1 Jackson, Mike
h3. Purge queued tasks
54
55
If any tasks are held by RabbitMQ awaiting despatch to workers then the worker, when started, will immediately start to process these. If you want to purge this queue prior to your worker starting, which can be useful if one or more workers have ran into problems and you don't want the backlog of pending tasks to be processed after you restart them, then use the @--purge@ flag e.g.
56
<pre>
57
$ celeryd -l INFO -n WORKER_ID --purge
58
</pre>
59
60
See also "Purge queued tasks" below.
61
62
h3. Specify the logging level
63
64
The @-l@ flag specifies the logging level of the worker. The other options, asides from @INFO@, are @DEBUG@, @WARNING@, @ERROR@, @CRITICAL@ e.g.
65
<pre>
66
$ celeryd -l DEBUG -n WORKER_ID 
67
</pre>
68
69 7 Jackson, Mike
The default logging format is:
70
<pre>
71
[%(asctime)s: %(levelname)s/%(processName)s] %(message)s
72
</pre>
73
You can specify another format in @src/common_py/mauscelery/celeryconfig.py@ using a @CELERYD_LOG_FORMAT@ variable. See the Python @logging@ module, http://ask.github.com/celery/configuration.html#logging, for more on logging.
74
75 4 Jackson, Mike
h2. Initialise the worker to execute MAUS tasks
76
77
By default, Celery workers use the default MAUS configuration and apply the MapPyDoNothing transform. They need to be explicity configured to apply other transformations.
78
79
If you are running analyses using a client that uses Go.py then this configuration will be done by Go.py on your behalf. 
80
81
If you are not using Go.py then you can do this manually (or in your own Python code) using the MAUS-specific commands described below in "Update workers with a new MAUS configuration and/or transforms" and "Restart the workers"
82
83 1 Jackson, Mike
h2. Celery monitoring
84
85
Celery provides a number of commands to allow inspection of Celery workers. Many of these can be invoked from the command-line, via the @celeryctl@ command, or from within Python. For full information, see http://ask.github.com/celery/userguide/monitoring.html
86
87
In the following, the names shown e.g. @worker1@, @worker2@ etc. are the worker names specified with the @-n@ flag when starting the workers using @celeryd@.
88
89 9 Jackson, Mike
In the invocations of the Celery @inspect@ command in Python, a specific Celery worker can be specified e.g.
90
<pre>
91
i = inspect("worker1")
92
</pre>
93
where the worker name is that given to the @-n@ flag when @celeryd@ is invoked to start the worker.
94
95 1 Jackson, Mike
h3. Check live workers
96
97
Check for live Celery workers that have registered with RabbitMQ and are available for use:
98
<pre>
99
$ celeryctl status
100
worker1: OK
101
worker2: OK
102
103
2 nodes online.
104
</pre>
105
106
<pre>
107
$ celeryctl inspect ping
108
<- ping
109
-> worker1: OK
110
    pong
111
-> worker2: OK
112
    pong
113
</pre>
114
115
To specify a specific worker, use @-d@ e.g.:
116
<pre>
117
$ celeryctl inspect -d worker1 ping
118
<- ping
119
-> worker1: OK
120
</pre>
121
122
From within Python, use:
123
<pre>
124
$ from celery.task.control import inspect
125
$ i = inspect()
126
$ i.ping()
127
{u'worker1': u'pong', u'worker2': u'pong'}
128
</pre>
129
130
h3. Check worker configuration
131
132
<pre>
133
$ celeryctl inspect stats
134
<- stats
135
-> worker1: OK
136
    {u'autoscaler': {},
137
     u'consumer': {u'broker': {u'connect_timeout': 4,
138
                               u'hostname': u'127.0.0.1',
139
                               u'insist': False,
140
                               u'login_method': u'AMQPLAIN',
141
                               u'port': 5672,
142
                               u'ssl': False,
143
                               u'transport': u'amqp',
144
                               u'transport_options': {},
145
                               u'userid': u'maus',
146
                               u'virtual_host': u'maushost'},
147
                   u'prefetch_count': 8},
148
     u'pool': {u'max-concurrency': 2,
149
               u'max-tasks-per-child': None,
150
               u'processes': [11991, 11992],
151
               u'put-guarded-by-semaphore': True,
152
               u'timeouts': [None, None]},
153
     u'total': {}}
154
-> worker2: OK
155
    {u'autoscaler': {},
156
     u'consumer': {u'broker': {u'connect_timeout': 4,
157
                               u'hostname': u'maus.epcc.ed.ac.uk',
158
                               u'insist': False,
159
                               u'login_method': u'AMQPLAIN',
160
                               u'port': 5672,
161
                               u'ssl': False,
162
                               u'transport': u'amqp',
163
                               u'transport_options': {},
164
                               u'userid': u'maus',
165
                               u'virtual_host': u'maushost'},
166
                   u'prefetch_count': 8},
167
     u'pool': {u'max-concurrency': 2,
168
               u'max-tasks-per-child': None,
169
               u'processes': [21964, 21965],
170
               u'put-guarded-by-semaphore': True,
171
               u'timeouts': [None, None]},
172
     u'total': {}}
173
</pre>
174
175
From within Python, use:
176
<pre>
177
$ from celery.task.control import inspect
178
$ i.stats()
179
{...}
180
</pre>
181
182
h3. Check registered tasks
183
184
Check the tasks that each worker can execute.
185
186
<pre>
187
$ celeryctl inspect registered
188
<- registered
189
-> worker1: OK
190
    * celery.backend_cleanup
191
    * celery.chord
192
    * celery.chord_unlock
193
    * celery.ping
194
    * mauscelery.maustasks.MausGenericTransformTask
195
-> worker2: OK
196
    * celery.backend_cleanup
197
    * celery.chord
198
    * celery.chord_unlock
199
    * celery.ping
200
    * mauscelery.maustasks.MausGenericTransformTask
201
</pre>
202
203
From within Python, use:
204
<pre>
205
$ from celery.task.control import inspect
206
$ i = inspect()
207
$ i.registered()
208
{u'worker1': [u'celery.backend_cleanup',
209
  u'celery.chord',
210
  u'celery.chord_unlock',
211
  u'celery.ping',
212
  u'mauscelery.maustasks.MausGenericTransformTask'],
213
 u'worker2': [u'celery.backend_cleanup',
214
  u'celery.chord',
215
  u'celery.chord_unlock',
216
  u'celery.ping',
217
  u'mauscelery.maustasks.MausGenericTransformTask']}
218
</pre>
219
220
h3. Check task states
221
222 8 Jackson, Mike
Check the tasks submitted for execution to a Celery worker by a client.
223 1 Jackson, Mike
224 8 Jackson, Mike
Check the tasks currently being executed by the worker:
225 1 Jackson, Mike
<pre>
226
$ celeryctl inspect active
227
<- active
228
-> worker1: OK
229 8 Jackson, Mike
    * {u'args': u'(\'{MAUS_SPILL_DOCUMENT}\', \'maus.epcc.ed.ac.uk (13067)\', 13)', 
230
u'time_start':1327503329.679438, u'name':u'mauscelery.maustasks.MausGenericTransformTask', 
231
u'delivery_info':{u'consumer_tag': u'2', u'routing_key': u'celery', u'exchange':u'celery'}, 
232
u'hostname': u'worker1', u'acknowledged': True, u'kwargs':u'{}', 
233
u'id': u'7222138d-bb2d-4e1b-ba70-5c0f9e90aa08', u'worker_pid':13059}
234
    * {...}
235
...
236 1 Jackson, Mike
</pre>
237 8 Jackson, Mike
Note the @worker_pid@ which specifies the process ID of the Celery sub-process executing the task.
238 1 Jackson, Mike
239 8 Jackson, Mike
Check the tasks received by the worker but awaiting execution:
240 1 Jackson, Mike
<pre>
241
$ celeryctl inspect reserved
242
<- reserved
243
-> worker1: OK
244 8 Jackson, Mike
    * {u'args': u'(\'{MAUS_SPILL_DOCUMENT}\', \'maus.epcc.ed.ac.uk (13067)\', 95)', 
245
u'time_start': None, u'name': u'mauscelery.maustasks.MausGenericTransformTask',
246
u'delivery_info': {u'consumer_tag': u'2', u'routing_key': u'celery', u'exchange': u'celery'}, 
247
u'hostname': u'worker1', u'acknowledged': False, u'kwargs': u'{}', 
248
u'id': u'ee1b3a88-58cc-4e26-b77d-4424ec9161d1', u'worker_pid': None}
249
    * {...}
250
...
251 1 Jackson, Mike
</pre>
252 8 Jackson, Mike
Note the @worker_pid@ which specifies the process ID of the Celery sub-process executing the task is currently @None@.
253 1 Jackson, Mike
254
From within Python. use:
255
<pre>
256
$ from celery.task.control import inspect
257
$ i = inspect()
258
$ i.active()
259 8 Jackson, Mike
{u'worker1': [...], ...}
260 1 Jackson, Mike
261
$ i.reserved()
262 8 Jackson, Mike
{u'worker1': [...], ...}
263 1 Jackson, Mike
</pre>
264
265
h3. Purge queued tasks
266
267
To purge tasks currently awaiting dispatch from RabbitMQ. This can be useful if one or more workers have ran into problems and you don't want the backlog of pending tasks to be processed after you restart them.
268
269
<pre>
270
$ celeryctl purge
271
Purged 4 messages from 1 known task queue.
272
</pre>
273
274
From within Python, use:
275
<pre>
276
$ from celery.task.control import discard_all
277
$ discard_all()
278
4
279
</pre>
280
281
h3. Shut down workers
282
283
All workers can be shut down from within Python via:
284
285
<pre>
286
$ from celery.task.control import broadcast
287
$ broadcast("shutdown")
288
</pre>
289
290 3 Jackson, Mike
Each worker will complete the tasks they are currently processing before shutting down.
291 1 Jackson, Mike
292 3 Jackson, Mike
Alternatively, you can use the Linux @kill@ command e.g.:
293
<pre>
294
$ ps -a
295
12614 pts/6    00:00:02 celeryd
296
12627 pts/6    00:00:00 celeryd
297 1 Jackson, Mike
12628 pts/6    00:00:00 celeryd
298 3 Jackson, Mike
$ kill -s TERM 12614
299 2 Jackson, Mike
</pre>
300 3 Jackson, Mike
The process ID should be that of the main worker process. This will usually have the lowest process ID.
301 2 Jackson, Mike
302 3 Jackson, Mike
To kill the worker immediately, without waiting for currently processing tasks to complete, use:
303 2 Jackson, Mike
<pre>
304
$ kill -s KILL 12614
305 3 Jackson, Mike
</pre>
306 2 Jackson, Mike
307
To kill all @celeryd@ processes, use:
308
<pre>
309
$ ps auxww | grep celeryd | awk '{print $2}' | xargs kill -9
310 1 Jackson, Mike
</pre>
311
312
h2. MAUS-specific actions
313
314
These actions are supported by Celery worker's running on top of MAUS.
315
316
h3. Get information on worker processes
317
318
Get a list of the worker node processes and their child processes (child processes are responsible for executing jobs):
319
320
<pre>
321
$ from celery.task.control import broadcast
322 2 Jackson, Mike
$ broadcast("get_process_pool", reply=True)
323
[{u'worker1': {u'master_name': u'MainProcess',
324 1 Jackson, Mike
   u'master_pid': 12614,
325
   u'pool_pids': [12627, 12628]}}]
326 3 Jackson, Mike
</pre>
327 2 Jackson, Mike
328
The sub-process IDs correspond to those visible in the @processes@ field of the document returned by the @celeryctl inspect stats@ command, described in "Check worker configuration" above and to those visible if running the Linux @ps -a@ command e.g.
329
<pre>
330
$ ps -a
331
12614 pts/6    00:00:02 celeryd
332
12627 pts/6    00:00:00 celeryd
333
12628 pts/6    00:00:00 celeryd
334 1 Jackson, Mike
</pre>
335
336
h3. Get worker MAUS configuration
337
338
Get the current MAUS configuration known to the workers, and the transforms that the workers will execute: 
339
340
<pre>
341
$ from celery.task.control import broadcast
342
$ broadcast("get_maus_configuration", reply=True)
343
[{u'worker1': {u'configuration': u'{}', u'transform': u'MapPyDoNothing'}}]
344
</pre>
345
346
Note that, for "transform", a list represents a MapPyGroup and nested lists represent nested MapPyGroups.
347
348
h3. Update workers with a new MAUS configuration and/or transforms
349
350
The worker must be restarted, see below, before the new configuration and/or transforms is actually available for use in the worker.
351
352
Update a worker with a new MAUS configuration. Existing transforms have death then birth invoked with the new configuration.
353
<pre>
354
$ from celery.task.control import broadcast
355
$ broadcast("set_maus_configuration", arguments= \
356
{"configuration":"""{"TOFconversionFactor":0.01}"""}, reply=True)
357
[{u'worker1': {u'status': u'ok'}}]
358
</pre>
359
360
Update a worker with a new transform. Existing transforms have death invoked. Instances of the new transforms are created and the current MAUS configuration passed into their birth method.
361
<pre>
362
$ from celery.task.control import broadcast
363
$ broadcast("set_maus_configuration", arguments= \
364
{"transform":["MapPyPrint", "MapPyDoNothing"]}, reply=True)
365
[{u'worker1': {u'status': u'ok'}}]
366
</pre>
367
368
Update a worker with a new MAUS configuration and a new transform.
369
<pre>
370
$ from celery.task.control import broadcast
371
$ broadcast("set_maus_configuration", arguments= \
372
{"transform":["MapPyPrint", "MapPyDoNothing"], \
373
 "configuration":"""{"TOFconversionFactor":0.01}"""}, reply=True)
374
[{u'worker1': {u'status': u'ok'}}]
375
</pre>
376
377 5 Jackson, Mike
h3. Restart the workers
378
379 1 Jackson, Mike
To restart Celery workers, run:
380
<pre>
381
$ from celery.task.control import broadcast
382
$ broadcast("restart_pool", reply=True)
383 5 Jackson, Mike
[{u'worker1': {u'status': u'ok'}}]
384 6 Jackson, Mike
</pre>
385
This instructs the Celery worker to terminate its sub-processes and spawn new ones. These new ones will inherit any updated configuration or transforms sent using @set_maus_configuration@ above.
386
387
In the @celeryd@ window you might see this:
388
<pre>
389
Consumer: Connection to broker lost. Trying to re-establish the connection...
390 7 Jackson, Mike
Traceback (most recent call last):
391
...
392
...
393 6 Jackson, Mike
...
394
error: [Errno 4] Interrupted system call
395 1 Jackson, Mike
</pre>
396
This can be ignored. If in doubt you can use @get_maus_configuration@ above to ensure that the workers have the new configuration.