-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster_testing.py
More file actions
188 lines (139 loc) · 6.8 KB
/
cluster_testing.py
File metadata and controls
188 lines (139 loc) · 6.8 KB
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
import constants
import requests
import json
import re
from pprint import pprint
class cluster_testing:
def __init__(self):
self.config = {
"host":constants.HOST,
"token":constants.TOKEN,
"check-exists":True, # True = avoid dupes
"enforce-create-limit":False, # True = only create `create-limit` quantity
"create-limit":10, # integer = create x amount of clusters if `enforce-create-limit` == True
"skip-gpu":True, # True = No clusters with GPU will be tested
"skip-pattern":r"A10|A100|DC\d{1,2}as_v5|EC\d{1,2}ads_v5|EC\d{1,2}as_v5|E\d{1,2}ids_v4|E\d{1,2}is_v4|H\d{1,2}", # RegEx pattern, add new patterns with | delimiter, if search == True will be skipped
"test-mode":False, # True = Will not create clusters
}
self.instance_types = None;
self.cluster_test_list = None;
self.cluster_current_info = None;
def collect(self):
## Get all current instance types from workspace
self.get_instance_types()
## Build our list fo smallest instance types
self.smallest_instances()
## Add our API cluster JSON defs
self.build_cluster_defs()
## Get workspace current workspace cluster info
self.get_clusters_current()
def build(self):
print('\n\n[Info] Build our clusters')
## Build our clusters
skip_pattern = self.config["skip-pattern"]
pattern = re.compile(skip_pattern)
created = 0
for c in self.cluster_test_list.keys():
if self.config['enforce-create-limit'] and created >= self.config['create-limit']:
print(f"[Info] Stopping: The create limit has been reached {self.config['create-limit']}")
break
if self.config['check-exists'] and c in self.cluster_current_info.keys() != None:
print(f"[Info] Skipping: '{c}' cluster already exists with id='{self.cluster_current_info[c]}'")
created += 1 #increment for limit
continue
if self.config['skip-gpu'] == True and self.cluster_test_list[c]['num_gpus'] > 0:
print(f"[Info] Skipping: '{c}' for skip-gpu == True")
continue
if pattern.search(self.cluster_test_list[c]['node_type_id']):
print(f"[Info] Skipping: '{c}' for pattern match on '{self.cluster_test_list[c]['node_type_id']}' = {pattern.search(self.cluster_test_list[c]['node_type_id']) is not None}")
continue
if self.config['test-mode'] == False:
print(f"[Info] Creating cluster '{c}'")
result = self.cluster_create(cluster_def=self.cluster_test_list[c]['cluster_def_json'])
print(f"[Info] Result {result.json()}")
else:
print(f"[Info] Test mode: Would have created cluster '{c}'")
created += 1
## Collect related methods
def get_instance_types(self):
print(f"[Info] Getting instance types from Clusters API")
try:
response = requests.get(
f"https://{self.config['host']}/api/2.0/clusters/list-node-types",
headers={"Authorization": f"Bearer {self.config['token']}"},
json={}
)
if 'node_types' in response.json().keys():
self.instance_types = response.json()['node_types']
else:
raise TypeError
print(f"[Info] There are {len(self.instance_types)} instance types")
except:
print("[Error] No instance types were retrieved or there was an error")
quit()
def smallest_instances(self):
print(f"[Info] Finding smallest size for each instance type")
recap = r"(Standard_)([A-z]+)(\d{1,3})(.*)$"
x = re.compile(recap)
self.cluster_test_list = {}
for i in self.instance_types:
m = x.match(i['node_type_id'])
instance_class = ''.join(m.group(1,2,4))
instance_size = int(m.group(3))
is_new_or_lower = False
if instance_class in self.cluster_test_list.keys():
if self.cluster_test_list[instance_class]["instance_size"] > instance_size:
is_new_or_lower = True
else:
is_new_or_lower = True
if is_new_or_lower:
self.cluster_test_list[instance_class] = { "node_type_id" : i['node_type_id'],
"instance_size" : instance_size,
"category" : i['category'],
"memory_mb": i['memory_mb'],
"num_cores": i['num_cores'],
"num_gpus": i['num_gpus']
}
print(f"[Info] There are {len(self.cluster_test_list)} unique small instance types")
def build_cluster_defs(self):
print(f"[Info] Adding json cluster definition for API based creation")
for c in self.cluster_test_list.keys():
instance_type = c
node_type_id = self.cluster_test_list[instance_type]['node_type_id']
self.cluster_test_list[instance_type]['cluster_def_json'] = {
"cluster_name": instance_type,
"spark_version": "13.3.x-scala2.12",
"node_type_id": node_type_id,
"num_workers": 0,
"autotermination_minutes":20
}
def get_clusters_current(self):
try:
response = requests.get(
f"https://{self.config['host']}/api/2.0/clusters/list",
headers={"Authorization": f"Bearer {self.config['token']}"},
json={}
)
## Get list of current clusters
clusters_current = response.json()['clusters']
self.cluster_current_info = {}
for c in clusters_current:
self.cluster_current_info[c['cluster_name']] = c['cluster_id']
except:
print("[Error] Unable to get current workspace clusters")
quit()
def cluster_create(self,cluster_def):
try:
return requests.post(
f"https://{self.config['host']}/api/2.0/clusters/create",
headers={"Authorization": f"Bearer {self.config['token']}"},
json=cluster_def
)
except:
return None
def main():
testing = cluster_testing()
testing.collect()
testing.build()
if __name__ == "__main__":
main()