-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampler.py
executable file
·459 lines (339 loc) · 15.6 KB
/
sampler.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#!/usr/bin/env python
import argparse
import os
import re
import json
from collections import defaultdict
from datetime import datetime, timezone
from kubernetes import client, config
import subprocess
import psycopg2
import logging
from settings import *
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
RUNAI_GROUP = 'run.ai'
RUNAI_VERSION = 'v2alpha1'
def k8s_setup():
config.load_kube_config()
k8s = client.CoreV1Api()
return k8s
def get_pods(k8s, namespace):
try:
pods = k8s.list_namespaced_pod(namespace).items
except Exception as e:
logging.error(f"list_namespaced_pod failed: {e}")
return pods
def get_events_for_namespace(namespace):
api_instance = client.CoreV1Api()
events = api_instance.list_namespaced_event(namespace)
return events.items
def get_resources_by_type(resource_type, namespace, crd_group='', version='v1'):
resources = {}
try:
custom_api = client.CustomObjectsApi()
resources = custom_api.list_namespaced_custom_object(group=crd_group, version=version, namespace=namespace, plural=resource_type)
except Exception as e:
logging.error(f"list_namespaced_custom_object failed: {e}")
return resources['items']
def get_runai_resources_by_type(resource_type, namespace):
return get_resources_by_type(resource_type, namespace, RUNAI_GROUP, RUNAI_VERSION)
def get_workload_key_from_workload(resource):
# resource['metadata']['name'] is not the name we want, as in old runai-cli
# date is added to the workload name. we need the short name
workload_name = resource['spec']['name']['value']
workload_namespace = resource['metadata']['namespace']
return workload_name, workload_namespace
def get_workload_key_from_job(resource):
workload_name = resource['metadata']['name']
workload_namespace = resource['metadata']['namespace']
return workload_name, workload_namespace
def get_workload_key_from_pod(pod):
keys = ['release', 'job-name', 'training.kubeflow.org/job-name']
for key in keys:
if key in pod.metadata.labels:
return pod.metadata.labels[key], pod.metadata.namespace
raise KeyError
def get_workload_key_from_podgroup(podgroup):
return podgroup['metadata']['labels']['release'], podgroup['metadata']['namespace']
def get_required_resources_from_cluster(k8s, workload_type, namespace):
# get all the required resources from the cluster
workload_crd_plural = f"{workload_type}workloads"
logging.info(f'getting {workload_crd_plural}')
workloads = get_runai_resources_by_type(workload_crd_plural, namespace)
if workload_type == "distributed":
logging.info('getting pytorchjobs')
jobs = get_resources_by_type('pytorchjobs', namespace, 'kubeflow.org')
else:
logging.info('getting runaijobs')
jobs = get_resources_by_type('runaijobs', namespace, 'run.ai')
logging.info('getting pods')
pods = get_pods(k8s, namespace)
logging.info('getting podgroups')
podgroups = get_resources_by_type('podgroups', namespace, 'scheduling.run.ai')
if TEST_SCHEDULER_EVENT_TIMES:
logging.info('getting events')
events = get_events_for_namespace(namespace)
else:
events = {}
return workloads, jobs, pods, podgroups, events
def connect_to_database():
if IS_SELF_HOSTED_DB:
db_params = {
'dbname': SELF_HOSTED_DB_NAME,
'user': SELF_HOSTED_DB_USER,
'password': SELF_HOSTED_DB_PASSWORD,
'host': 'localhost',
'port': 5432
}
else:
cmd = f"aws rds generate-db-auth-token \
--hostname {STAGING_DB_HOST} \
--port 5432 \
--region {STAGING_DB_REGION} \
--username {STAGING_DB_USER}"
result = subprocess.run(cmd, shell=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
db_password = result.stdout.strip()
db_params = {
'dbname': STAGING_DB_NAME,
'user': STAGING_DB_USER,
'password': db_password,
'host': STAGING_DB_HOST,
'port': 5432
}
return psycopg2.connect(**db_params)
def get_required_data_from_backend(workload_type, namespace):
# read job info from the jobs table in the control-plane
project = namespace.replace('runai-', '')
kind_operator = '='
if workload_type == 'distributed':
kind_operator = '!='
query = f"""
SELECT name, time_created
FROM jobs
WHERE project = '{project}'
AND type = 'Train'
AND kind {kind_operator} 'RunaiJob'
AND exists_in_cluster = 'true'
"""
if not IS_SELF_HOSTED_DB:
query += '\n' + f"AND cluster_uuid = '{STAGING_RUNAI_CLUSTER_UUID}'"
try:
logging.info('getting backend jobs')
connection = connect_to_database()
cursor = connection.cursor()
cursor.execute(query)
rows = cursor.fetchall()
cursor.close()
connection.close()
except psycopg2.Error as e:
logging.error(f"Database error: {e}")
return []
backend_jobs = []
for row in rows:
job_name = row[0]
timestamp = row[1]/1000
job = {"jobName": job_name,
"projectName": project,
"jobNamespace": namespace,
"backendJobCreatedTimestamp": datetime.fromtimestamp(timestamp, timezone.utc)
}
backend_jobs.append(job)
return backend_jobs
def join_data_by_workload(workloads, jobs, pods, podgroups, events, backend_jobs):
# join all the data into a single dictionary
# key = (workload_name, namespace)
# value = dict with objects (e.g. workload, pod)
data = defaultdict(dict)
pgEvictions, pgPvcBindRequests, pgPvcBinds = sort_events_by_podgroup(events, podgroups)
for workload in workloads:
k = get_workload_key_from_workload(workload)
data[k]['workload'] = workload
for job in jobs:
k = get_workload_key_from_job(job)
data[k]['job'] = job
for pod in pods:
k = get_workload_key_from_pod(pod)
if 'pods' not in data[k]:
data[k]['pods'] = []
data[k]['pods'].append(pod)
for podgroup in podgroups:
k = get_workload_key_from_podgroup(podgroup)
data[k]['podgroup'] = podgroup
pgName = podgroup['metadata']['name']
if pgName in pgEvictions:
data[k]['evictionTimes'] = pgEvictions[pgName]
else:
data[k]['evictionTimes'] = []
if pgName in pgPvcBindRequests:
data[k]['pvcBindRequestTimes'] = pgPvcBindRequests[pgName]
else:
data[k]['pvcBindRequestTimes'] = []
if pgName in pgPvcBinds:
data[k]['pvcBindTimes'] = pgPvcBinds[pgName]
else:
data[k]['pvcBindTimes'] = []
for backend_job in backend_jobs:
k = (backend_job['jobName'], backend_job['jobNamespace'])
data[k]['backend_job'] = backend_job
return data
def sort_events_by_podgroup(events, pod_groups):
evictions_by_pg = {}
pvc_bind_requests_by_pg = {}
pvc_binds_by_pg = {}
for event in events:
if event.reason == 'Evict':
if "preempted" in event.message:
pod, pg, time = extract_preemption_data(event)
if pg not in evictions_by_pg:
evictions_by_pg[pg] = []
evictions_by_pg[pg].append(event.first_timestamp)
elif "reclaim" in event.message:
pod, pg, time = extract_reclaim_data(event)
if pg not in evictions_by_pg:
evictions_by_pg[pg] = []
evictions_by_pg[pg].append(event.first_timestamp)
elif event.reason == 'ExternalProvisioning':
pg, time = extract_pvc_bind_request_data(event, pod_groups)
if pg not in evictions_by_pg:
pvc_bind_requests_by_pg[pg] = []
pvc_bind_requests_by_pg[pg].append(event.first_timestamp)
elif event.reason == 'ProvisioningSucceeded':
pg, time = extract_pvc_bind_data(event, pod_groups)
if pg not in evictions_by_pg:
pvc_binds_by_pg[pg] = []
pvc_binds_by_pg[pg].append(event.first_timestamp)
return evictions_by_pg, pvc_bind_requests_by_pg, pvc_binds_by_pg
def extract_preemption_data(event):
preempteePattern = "runai-.*?/.*? "
preempteePod = re.findall(preempteePattern, event.message)[0].replace(" ", "").split('/')[1]
preemptorPattern = "preempted by higher priority job runai-.*?/pg.*?$"
preemptorPG = re.findall(preemptorPattern, event.message)[0].replace("preempted by higher priority job ", "").split('/')[1]
time = event.first_timestamp
return preempteePod, preemptorPG, time
def extract_reclaim_data(event):
reclaimeePattern = "runai-.*?/.*? "
reclaimeePod = re.findall(reclaimeePattern, event.message)[0].replace(" ", "").split('/')[1]
reclaimerPattern = "reclaimed by job runai-.*?/pg.*?\."
reclaimerPG = re.findall(reclaimerPattern, event.message)[0].replace("reclaimed by job .+ ", "").replace(".", "").split('/')[1]
time = event.first_timestamp
return reclaimeePod, reclaimerPG, time
def extract_pvc_bind_request_data(event, podgroups):
pgPattern = "j-.{6}"
workload = re.findall(pgPattern, event.involved_object.name)[0]
time = event.first_timestamp
pg = ""
for podGroup in podgroups:
if podGroup['metadata']['labels']['workloadName'] == workload:
pg = podGroup['metadata']['name']
return pg, time
def extract_pvc_bind_data(event, podgroups):
pgPattern = "j-.{6}"
workload = re.findall(pgPattern, event.involved_object.name)[0]
time = event.first_timestamp
pg = ""
for podGroup in podgroups:
if podGroup['metadata']['labels']['workloadName'] == workload:
pg = podGroup['metadata']['name']
return pg, time
def get_min_max_pod_times(pods):
pod_times = [pod.metadata.creation_timestamp for pod in pods]
return min(pod_times), max(pod_times)
def get_pod_scheduling_decision_time(pod):
# returns the last transition time of the PodScheduled condition of the pod,
# if the condition is either true (i.e. scheduled) or false with reason.
# otherwise returns None
try:
if "status" in pod.to_dict() and "conditions" in pod.status.to_dict() and pod.status.conditions:
for condition in pod.status.conditions:
if condition.type == "PodScheduled" and (condition.status == "True" or condition.reason):
return condition.last_transition_time
except AttributeError:
pass
raise KeyError
def extract_times_from_resources(data):
times = []
for workload_key, workload_resources in data.items():
workload_name, namespace = workload_key
workload_times = {}
workload_times['jobName'] = workload_name
workload_times['jobNamespace'] = namespace
workload_times['projectName'] = namespace.replace('runai-', '')
if 'workload' not in workload_resources:
# that means we found some resource (e.g. pod) but its parent workload
# no longer exists. it should not be part of our result
continue
try:
first_pod_timestamp, last_pod_timestamp = get_min_max_pod_times(workload_resources['pods'])
workload_times['workloadCreatedTimestamp'] = workload_resources['workload']['metadata']['creationTimestamp']
workload_times['jobCreatedTimestamp'] = workload_resources['job']['metadata']['creationTimestamp']
workload_times['firstPodCreatedTimestamp'] = first_pod_timestamp.isoformat()
workload_times['lastPodCreatedTimestamp'] = last_pod_timestamp.isoformat()
workload_times['podGroupCreatedTimestamp'] = workload_resources['podgroup']['metadata']['creationTimestamp']
workload_times['podSchedulingDecisionTimestamp'] = get_pod_scheduling_decision_time(workload_resources['pods'][0]).isoformat()
workload_times['firstEvictionTimestamp'] = workload_times['podSchedulingDecisionTimestamp']
if workload_resources['evictionTimes']:
workload_times['firstEvictionTimestamp'] = min(workload_resources['evictionTimes']).isoformat()
workload_times['firstPVCBindRequestTimestamp'] = workload_times['podSchedulingDecisionTimestamp']
if workload_resources['pvcBindRequestTimes']:
workload_times['firstPVCBindRequestTimestamp'] = min(workload_resources['pvcBindRequestTimes']).isoformat()
workload_times['firstPVCBindTimestamp'] = workload_times['podSchedulingDecisionTimestamp']
if workload_resources['pvcBindTimes']:
workload_times['firstPVCBindTimestamp'] = min(workload_resources['pvcBindTimes']).isoformat()
if TEST_BACKEND_TIMES:
workload_times['backendJobCreatedTimestamp'] = workload_resources['backend_job']['backendJobCreatedTimestamp'].isoformat()
else:
workload_times['backendJobCreatedTimestamp'] = workload_times['workloadCreatedTimestamp']
except KeyError as e:
logging.warning(f"some resources are not ready yet, workloads are still being handled. Workload {workload_name}/{namespace}, error: {e} (skipping)")
continue
times.append(workload_times)
return times
def sample_workloads(k8s, workload_type, namespace):
workloads, jobs, pods, podgroups, events = get_required_resources_from_cluster(k8s, workload_type, namespace)
if TEST_BACKEND_TIMES:
backend_jobs = get_required_data_from_backend(workload_type, namespace)
else:
backend_jobs = []
print()
logging.info("resources:")
logging.info(f" num workloads: {len(workloads)}")
logging.info(f" num jobs: {len(jobs)}")
logging.info(f" num pods: {len(pods)}")
logging.info(f" num podgroups: {len(podgroups)}")
logging.info(f" num events: {len(events)}")
logging.info(f" num backend jobs: {len(backend_jobs)}")
print()
data = join_data_by_workload(workloads, jobs, pods, podgroups, events, backend_jobs)
times = extract_times_from_resources(data)
return times
def write_json(output_dir, times):
file_path = f"{output_dir}/sampled.json"
try:
with open(file_path, "w") as file:
json.dump(times, file)
except Exception as e:
logging.error(f"Failed writing to {file_path}: {str(e)}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--output-dir', '-o', type=str, default=DEFAULT_OUTPUT_DIR, help='Output dir')
parser.add_argument('--workload-type', '-t', choices=['training', 'distributed', 'interactive'], default='training', help='Workload type (default: training)')
parser.add_argument('--project', '-p', type=str, default=DEFAULT_PROJECT, help='Project to sample')
args = parser.parse_args()
if not os.path.isdir(args.output_dir):
logging.error(f"output dir '{args.output_dir}' doesn't exist")
exit(1)
if TEST_BACKEND_TIMES:
try:
connection = connect_to_database()
connection.close()
except Exception as e:
logging.error(f"failed to connect to database, check your vpn connection and credentials: {e}")
exit(1)
namespace = f'runai-{args.project}'
k8s = k8s_setup()
times = sample_workloads(k8s, args.workload_type, namespace)
write_json(args.output_dir, times)
logging.info(f"generated time info for {len(times)} workloads")
if False:
for t in times:
print(t)