This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
forked from kubeflow/training-operator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tf_job_client.py
434 lines (380 loc) · 17 KB
/
tf_job_client.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# Copyright 2019 The Kubeflow Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import multiprocessing
import time
import logging
import threading
import queue
from kubernetes import client, config
from kubernetes import watch as k8s_watch
from kubeflow.training.constants import constants
from kubeflow.training.utils import utils
from .tf_job_watch import watch as tfjob_watch
logging.basicConfig(format='%(message)s')
logging.getLogger().setLevel(logging.INFO)
def wrap_log_stream(q, stream):
while True:
try:
logline = next(stream)
q.put(logline)
except StopIteration:
q.put(None)
return
except Exception as e:
raise RuntimeError(
"Exception when calling CoreV1Api->read_namespaced_pod_log: %s\n" % e)
def get_log_queue_pool(streams):
pool = []
for stream in streams:
q = queue.Queue(maxsize=100)
pool.append(q)
threading.Thread(target=wrap_log_stream, args=(q, stream)).start()
return pool
class TFJobClient(object):
def __init__(self, config_file=None, context=None, # pylint: disable=too-many-arguments
client_configuration=None, persist_config=True):
"""
TFJob client constructor
:param config_file: kubeconfig file, defaults to ~/.kube/config
:param context: kubernetes context
:param client_configuration: kubernetes configuration object
:param persist_config:
"""
if config_file or not utils.is_running_in_k8s():
config.load_kube_config(
config_file=config_file,
context=context,
client_configuration=client_configuration,
persist_config=persist_config)
else:
config.load_incluster_config()
self.custom_api = client.CustomObjectsApi()
self.core_api = client.CoreV1Api()
def create(self, tfjob, namespace=None):
"""
Create the TFJob
:param tfjob: tfjob object
:param namespace: defaults to current or default namespace
:return: created tfjob
"""
if namespace is None:
namespace = utils.set_tfjob_namespace(tfjob)
try:
outputs = self.custom_api.create_namespaced_custom_object(
constants.TFJOB_GROUP,
constants.TFJOB_VERSION,
namespace,
constants.TFJOB_PLURAL,
tfjob)
except client.rest.ApiException as e:
raise RuntimeError(
"Exception when calling CustomObjectsApi->create_namespaced_custom_object:\
%s\n" % e)
return outputs
def get(self, name=None, namespace=None, watch=False,
timeout_seconds=600): # pylint: disable=inconsistent-return-statements
"""
Get the tfjob
:param name: existing tfjob name, if not defined, the get all tfjobs in the namespace.
:param namespace: defaults to current or default namespace
:param watch: Watch the TFJob if `True`.
:param timeout_seconds: How long to watch the job..
:return: tfjob
"""
if namespace is None:
namespace = utils.get_default_target_namespace()
if name:
if watch:
tfjob_watch(
name=name,
namespace=namespace,
timeout_seconds=timeout_seconds)
else:
thread = self.custom_api.get_namespaced_custom_object(
constants.TFJOB_GROUP,
constants.TFJOB_VERSION,
namespace,
constants.TFJOB_PLURAL,
name,
async_req=True)
tfjob = None
try:
tfjob = thread.get(constants.APISERVER_TIMEOUT)
except multiprocessing.TimeoutError:
raise RuntimeError("Timeout trying to get TFJob.")
except client.rest.ApiException as e:
raise RuntimeError(
"Exception when calling CustomObjectsApi->get_namespaced_custom_object:\
%s\n" % e)
except Exception as e:
raise RuntimeError(
"There was a problem to get TFJob {0} in namespace {1}. Exception: \
{2} ".format(name, namespace, e))
return tfjob
else:
if watch:
tfjob_watch(
namespace=namespace,
timeout_seconds=timeout_seconds)
else:
thread = self.custom_api.list_namespaced_custom_object(
constants.TFJOB_GROUP,
constants.TFJOB_VERSION,
namespace,
constants.TFJOB_PLURAL,
async_req=True)
tfjobs = None
try:
tfjobs = thread.get(constants.APISERVER_TIMEOUT)
except multiprocessing.TimeoutError:
raise RuntimeError("Timeout trying to get TFJob.")
except client.rest.ApiException as e:
raise RuntimeError(
"Exception when calling CustomObjectsApi->list_namespaced_custom_object:\
%s\n" % e)
except Exception as e:
raise RuntimeError(
"There was a problem to list TFJobs in namespace {0}. \
Exception: {1} ".format(namespace, e))
return tfjobs
def patch(self, name, tfjob, namespace=None):
"""
Patch existing tfjob
:param name: existing tfjob name
:param tfjob: patched tfjob
:param namespace: defaults to current or default namespace
:return: patched tfjob
"""
if namespace is None:
namespace = utils.set_tfjob_namespace(tfjob)
try:
outputs = self.custom_api.patch_namespaced_custom_object(
constants.TFJOB_GROUP,
constants.TFJOB_VERSION,
namespace,
constants.TFJOB_PLURAL,
name,
tfjob)
except client.rest.ApiException as e:
raise RuntimeError(
"Exception when calling CustomObjectsApi->patch_namespaced_custom_object:\
%s\n" % e)
return outputs
def delete(self, name, namespace=None):
"""
Delete the tfjob
:param name: tfjob name
:param namespace: defaults to current or default namespace
:return:
"""
if namespace is None:
namespace = utils.get_default_target_namespace()
try:
return self.custom_api.delete_namespaced_custom_object(
group=constants.TFJOB_GROUP,
version=constants.TFJOB_VERSION,
namespace=namespace,
plural=constants.TFJOB_PLURAL,
name=name,
body=client.V1DeleteOptions())
except client.rest.ApiException as e:
raise RuntimeError(
"Exception when calling CustomObjectsApi->delete_namespaced_custom_object:\
%s\n" % e)
def wait_for_job(self, name, # pylint: disable=inconsistent-return-statements
namespace=None,
timeout_seconds=600,
polling_interval=30,
watch=False,
status_callback=None):
"""Wait for the specified job to finish.
:param name: Name of the TfJob.
:param namespace: defaults to current or default namespace.
:param timeout_seconds: How long to wait for the job.
:param polling_interval: How often to poll for the status of the job.
:param watch: Watch the TFJob if `True`.
:param status_callback: (Optional): Callable. If supplied this callable is
invoked after we poll the job. Callable takes a single argument which
is the job.
:return:
"""
if namespace is None:
namespace = utils.get_default_target_namespace()
if watch:
tfjob_watch(
name=name,
namespace=namespace,
timeout_seconds=timeout_seconds)
else:
return self.wait_for_condition(
name,
["Succeeded", "Failed"],
namespace=namespace,
timeout_seconds=timeout_seconds,
polling_interval=polling_interval,
status_callback=status_callback)
def wait_for_condition(self, name,
expected_condition,
namespace=None,
timeout_seconds=600,
polling_interval=30,
status_callback=None):
"""Waits until any of the specified conditions occur.
:param name: Name of the job.
:param expected_condition: A list of conditions. Function waits until any of the
supplied conditions is reached.
:param namespace: defaults to current or default namespace.
:param timeout_seconds: How long to wait for the job.
:param polling_interval: How often to poll for the status of the job.
:param status_callback: (Optional): Callable. If supplied this callable is
invoked after we poll the job. Callable takes a single argument which
is the job.
:return: Object TFJob status
"""
if namespace is None:
namespace = utils.get_default_target_namespace()
for _ in range(round(timeout_seconds / polling_interval)):
tfjob = None
tfjob = self.get(name, namespace=namespace)
if tfjob:
if status_callback:
status_callback(tfjob)
# If we poll the CRD quick enough status won't have been set yet.
conditions = tfjob.get("status", {}).get("conditions", [])
# Conditions might have a value of None in status.
conditions = conditions or []
for c in conditions:
if c.get("type", "") in expected_condition:
return tfjob
time.sleep(polling_interval)
raise RuntimeError(
"Timeout waiting for TFJob {0} in namespace {1} to enter one of the "
"conditions {2}.".format(name, namespace, expected_condition), tfjob)
def get_job_status(self, name, namespace=None):
"""Returns TFJob status, such as Running, Failed or Succeeded.
:param name: The TFJob name.
:param namespace: defaults to current or default namespace.
:return: Object TFJob status
"""
if namespace is None:
namespace = utils.get_default_target_namespace()
tfjob = self.get(name, namespace=namespace)
last_condition = tfjob.get("status", {}).get("conditions", [{}])[-1]
return last_condition.get("type", "")
def is_job_running(self, name, namespace=None):
"""Returns true if the TFJob running; false otherwise.
:param name: The TFJob name.
:param namespace: defaults to current or default namespace.
:return: True or False
"""
tfjob_status = self.get_job_status(name, namespace=namespace)
return tfjob_status.lower() == "running"
def is_job_succeeded(self, name, namespace=None):
"""Returns true if the TFJob succeeded; false otherwise.
:param name: The TFJob name.
:param namespace: defaults to current or default namespace.
:return: True or False
"""
tfjob_status = self.get_job_status(name, namespace=namespace)
return tfjob_status.lower() == "succeeded"
def get_pod_names(self, name, namespace=None, master=False, # pylint: disable=inconsistent-return-statements
replica_type=None, replica_index=None):
"""
Get pod names of TFJob.
:param name: tfjob name
:param namespace: defaults to current or default namespace.
:param master: Only get pod with label 'job-role: master' pod if True.
:param replica_type: User can specify one of 'worker, ps, chief' to only get one type pods.
By default get all type pods.
:param replica_index: User can specfy replica index to get one pod of TFJob.
:return: set: pods name
"""
if namespace is None:
namespace = utils.get_default_target_namespace()
labels = utils.get_tfjob_labels(name, master=master,
replica_type=replica_type,
replica_index=replica_index)
try:
resp = self.core_api.list_namespaced_pod(
namespace, label_selector=utils.to_selector(labels))
except client.rest.ApiException as e:
raise RuntimeError(
"Exception when calling CoreV1Api->read_namespaced_pod_log: %s\n" % e)
pod_names = []
for pod in resp.items:
if pod.metadata and pod.metadata.name:
pod_names.append(pod.metadata.name)
if not pod_names:
logging.warning("Not found Pods of the TFJob %s with the labels %s.", name, labels)
else:
return set(pod_names)
def get_logs(self, name, namespace=None, master=True,
replica_type=None, replica_index=None,
follow=False):
"""
Get training logs of the TFJob.
By default only get the logs of Pod that has labels 'job-role: master'.
:param name: tfjob name
:param namespace: defaults to current or default namespace.
:param master: By default get pod with label 'job-role: master' pod if True.
If need to get more Pod Logs, set False.
:param replica_type: User can specify one of 'worker, ps, chief' to only get one type pods.
By default get all type pods.
:param replica_index: User can specfy replica index to get one pod of TFJob.
:param follow: Follow the log stream of the pod. Defaults to false.
:return: str: pods logs
"""
if namespace is None:
namespace = utils.get_default_target_namespace()
pod_names = list(self.get_pod_names(name, namespace=namespace,
master=master,
replica_type=replica_type,
replica_index=replica_index))
if pod_names:
if follow:
log_streams = []
for pod in pod_names:
log_streams.append(k8s_watch.Watch().stream(self.core_api.read_namespaced_pod_log,
name=pod, namespace=namespace))
finished = [False for _ in log_streams]
# create thread and queue per stream, for non-blocking iteration
log_queue_pool = get_log_queue_pool(log_streams)
# iterate over every watching pods' log queue
while True:
for index, log_queue in enumerate(log_queue_pool):
if all(finished):
return
if finished[index]:
continue
# grouping the every 50 log lines of the same pod
for _ in range(50):
try:
logline = log_queue.get(timeout=1)
if logline is None:
finished[index] = True
break
logging.info("[Pod %s]: %s", pod_names[index], logline)
except queue.Empty:
break
else:
for pod in pod_names:
try:
pod_logs = self.core_api.read_namespaced_pod_log(pod, namespace)
logging.info("The logs of Pod %s:\n %s", pod, pod_logs)
except client.rest.ApiException as e:
raise RuntimeError(
"Exception when calling CoreV1Api->read_namespaced_pod_log: %s\n" % e)
else:
raise RuntimeError("Not found Pods of the TFJob {} "
"in namespace {}".format(name, namespace))