This repository has been archived by the owner on Feb 10, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
core.py
358 lines (291 loc) · 11.6 KB
/
core.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
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
349
350
351
352
353
354
355
356
357
358
from collections import namedtuple
import logging
import os
import shutil
import socket
import sys
import tempfile
import drmaa
from toolz import merge
from tornado import gen
from distributed import LocalCluster
from distributed.deploy import Cluster
from distributed.utils import log_errors, ignoring
from distributed.utils import PeriodicCallback
logger = logging.getLogger(__name__)
_global_session = [None]
def get_session():
if not _global_session[0]:
_global_session[0] = drmaa.Session()
_global_session[0].initialize()
return _global_session[0]
WorkerSpec = namedtuple('WorkerSpec',
('job_id', 'kwargs', 'stdout', 'stderr'))
worker_bin_path = (
'%(python)s -m distributed.cli.dask_worker'
% dict(python=sys.executable)
)
# All JOB_ID and TASK_ID environment variables
_drm_info = get_session().drmsInfo
_drmaa_implementation = get_session().drmaaImplementation
if "SLURM" in _drm_info:
JOB_PARAM = "%j"
JOB_ID = "$SLURM_JOB_ID"
TASK_ID = "$SLURM_ARRAY_TASK_ID"
elif "LSF" in _drm_info:
JOB_PARAM = "%J"
JOB_ID = "$LSB_JOBID"
TASK_ID = "$LSB_JOBINDEX"
elif "GE" in _drm_info:
JOB_PARAM = "$JOB_ID"
JOB_ID = "$JOB_ID"
TASK_ID = "$SGE_TASK_ID"
elif "Torque" == _drm_info or "PBS" in _drmaa_implementation:
JOB_PARAM = "$PBS_JOBID"
JOB_ID = "$PBS_JOBID"
TASK_ID = "$PBS_TASKNUM"
else:
JOB_PARAM = ""
JOB_ID = ""
TASK_ID = ""
worker_out_path_template = os.path.join(
os.getcwd(),
'worker.%(jid)s.%(ext)s'
)
default_template = {
'jobName': 'dask-worker',
'outputPath': ':' + worker_out_path_template % dict(
jid=".".join([JOB_PARAM, '$drmaa_incr_ph$']), ext='out'
),
'errorPath': ':' + worker_out_path_template % dict(
jid=".".join([JOB_PARAM, '$drmaa_incr_ph$']), ext='err'
),
'workingDirectory': os.getcwd(),
'nativeSpecification': '',
# stdout/stderr are redirected to files, make sure their contents don't lag
'jobEnvironment': {'PYTHONUNBUFFERED': '1'},
'args': []
}
def make_job_script(executable, name, preexec=()):
shebang = '#!/bin/bash'
execute = (
'%(executable)s $1 --name %(name)s "${@:2}"'
% dict(executable=executable, name=name)
)
preparation = list(preexec)
script_template = '\n'.join([shebang] + preparation + [execute, ''])
return script_template
class DRMAACluster(Cluster):
def __init__(self, template=None, cleanup_interval=1000, hostname=None,
script=None, preexec_commands=(), copy_script=True,
ip='',
**kwargs):
"""
Dask workers launched by a DRMAA-compatible cluster
Parameters
----------
template: dict
Dictionary specifying options to pass to the DRMAA cluster
and the worker. Relevant items are:
jobName: string
Name of the job as known by the DRMAA cluster.
args: list
Extra string arguments to pass to dask-worker
outputPath: string
Path to the dask-worker stdout. Must start with ':'.
Defaults to worker.JOBID.TASKID.out in current directory.
errorPath: string
Path to the dask-worker stderr. Must start with ':'
Defaults to worker.JOBID.TASKID.err in current directory.
workingDirectory: string
Where dask-worker runs, defaults to current directory
nativeSpecification: string
Options native to the job scheduler
cleanup_interval: int
Time interval in seconds at which closed workers are cleaned.
Defaults to 1000
hostname: string
Host on which to start the local scheduler, defaults to localhost
script: string (optional)
Path to the dask-worker executable script.
A temporary file will be made if none is provided (recommended)
preexec_commands: tuple (optional)
Commands to be executed first by temporary script. Cannot be
specified at the same time as script.
copy_script: bool
Whether should copy the passed script to the current working
directory. This is primarily to work around an issue with SGE.
ip: string
IP of the scheduler, default is the empty string
which will listen on the primary ip address of the host
**kwargs:
Additional keyword arguments to be passed to the local scheduler
Examples
--------
>>> from dask_drmaa import DRMAACluster # doctest: +SKIP
>>> cluster = DRMAACluster() # doctest: +SKIP
>>> cluster.start_workers(10) # doctest: +SKIP
>>> from distributed import Client # doctest: +SKIP
>>> client = Client(cluster) # doctest: +SKIP
>>> future = client.submit(lambda x: x + 1, 10) # doctest: +SKIP
>>> future.result() # doctest: +SKIP
11
"""
self.hostname = hostname or socket.gethostname()
logger.info("Start local scheduler at %s", self.hostname)
self.local_cluster = LocalCluster(n_workers=0, ip=ip, **kwargs)
if script is None:
fn = os.path.abspath(tempfile.mktemp(
suffix='.sh',
prefix='dask-worker-script-',
dir=os.path.curdir,
))
self.script = fn
self._should_cleanup_script = True
script_contents = make_job_script(executable=worker_bin_path,
name='%s.%s' % (JOB_ID, TASK_ID),
preexec=preexec_commands)
with open(fn, 'wt') as f:
f.write(script_contents)
@atexit.register
def remove_script():
if os.path.exists(fn):
os.remove(fn)
os.chmod(self.script, 0o777)
else:
self._should_cleanup_script = False
if copy_script:
with ignoring(EnvironmentError): # may be in the same path
shutil.copy(script, os.path.curdir) # python 2.x returns None
script = os.path.join(os.path.curdir, os.path.basename(script))
self._should_cleanup_script = True
self.script = os.path.abspath(script)
assert not preexec_commands, "Cannot specify both script and preexec_commands"
# TODO: check that user-provided script is executable
self.template = merge(default_template,
{'remoteCommand': self.script},
template or {})
self._cleanup_callback = PeriodicCallback(callback=self.cleanup_closed_workers,
callback_time=cleanup_interval,
io_loop=self.scheduler.loop)
self._cleanup_callback.start()
self.workers = {} # {job-id: WorkerSpec}
def adapt(self, **kwargs):
""" Turn on adaptivity
For keyword arguments see dask_drmaa.adaptive.Adaptive
Examples
--------
>>> cluster.adapt(minimum=0, maximum=10, interval='500ms')
See Also
--------
Cluster: an interface for other clusters to inherit from
"""
from .adaptive import Adaptive
with ignoring(AttributeError):
self._adaptive.stop()
if not hasattr(self, '_adaptive_options'):
self._adaptive_options = {}
self._adaptive_options.update(kwargs)
self._adaptive = Adaptive(
self, self.scheduler, **self._adaptive_options
)
return self._adaptive
@gen.coroutine
def _start(self):
pass
@property
def scheduler(self):
return self.local_cluster.scheduler
def create_job_template(self, **kwargs):
template = self.template.copy()
if kwargs:
template.update(kwargs)
template['args'] = [self.scheduler_address] + template['args']
jt = get_session().createJobTemplate()
valid_attributes = dir(jt)
for key, value in template.items():
if key not in valid_attributes:
raise ValueError("Invalid job template attribute %s" % key)
setattr(jt, key, value)
return jt
def start_workers(self, n=1, **kwargs):
if n == 0:
return
with log_errors():
with self.create_job_template(**kwargs) as jt:
ids = get_session().runBulkJobs(jt, 1, n, 1)
logger.info("Start %d workers. Job ID: %s", len(ids), ids[0].split('.')[0])
self.workers.update(
{jid: WorkerSpec(job_id=jid, kwargs=kwargs,
stdout=worker_out_path_template % dict(jid=jid, ext='out'),
stderr=worker_out_path_template % dict(jid=jid, ext='err'),
)
for jid in ids})
@gen.coroutine
def stop_workers(self, worker_ids, sync=False):
if isinstance(worker_ids, str):
worker_ids = [worker_ids]
elif worker_ids:
worker_ids = list(worker_ids)
else:
return
# Let the scheduler gracefully retire workers first
ids_to_ips = {
v['name']: k for k, v in self.scheduler.worker_info.items()
}
worker_ips = [ids_to_ips[wid]
for wid in worker_ids
if wid in ids_to_ips]
retired = yield self.scheduler.retire_workers(workers=worker_ips,
close_workers=True)
logger.info("Retired workers %s", retired)
for wid in list(worker_ids):
try:
get_session().control(wid, drmaa.JobControlAction.TERMINATE)
except drmaa.errors.InvalidJobException:
pass
try:
self.workers.pop(wid)
except KeyError:
# If we have multiple callers at once, it may have already
# been popped off
pass
logger.info("Stop workers %s", worker_ids)
if sync:
get_session().synchronize(worker_ids, dispose=True)
@gen.coroutine
def scale_up(self, n, **kwargs):
yield [self.start_workers(**kwargs)
for _ in range(n - len(self.workers))]
@gen.coroutine
def scale_down(self, workers):
workers = set(workers)
yield self.scheduler.retire_workers(workers=workers)
def close(self):
logger.info("Closing DRMAA cluster")
self.stop_workers(self.workers, sync=True)
self.local_cluster.close()
if self._should_cleanup_script and os.path.exists(self.script):
os.remove(self.script)
def __enter__(self):
return self
def __exit__(self, *args):
self.close()
def cleanup_closed_workers(self):
for jid in list(self.workers):
if get_session().jobStatus(jid) in ('closed', 'done'):
logger.info("Removing closed worker %s", jid)
del self.workers[jid]
def __del__(self):
try:
self.close()
except:
pass
def __str__(self):
return "<%s: %d workers>" % (self.__class__.__name__, len(self.workers))
__repr__ = __str__
def remove_workers():
get_session().control(drmaa.Session.JOB_IDS_SESSION_ALL,
drmaa.JobControlAction.TERMINATE)
import atexit
atexit.register(remove_workers)