/
auto_client.py
242 lines (208 loc) · 6.23 KB
/
auto_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
import configparser
import os
import time
import traceback
from concurrent.futures.thread import ThreadPoolExecutor
from configparser import ConfigParser
from json import loads, dumps
from threading import Thread
import docker
import grpc
from auto import get_container_stat, calculate_time, get_container_config, get_all_container, calculate_cpu_percent, \
get_container_stats_stream, update_container
from util import logger
import auto_pb2
import auto_pb2_grpc
from util.util import get_docker0_IP
LOG = "/var/log/auto.log"
logger = logger.set_logger(os.path.basename(__file__), LOG)
DEFAULT_PORT = '8090'
def submit(cmd):
logger.debug(cmd)
try:
host = get_docker0_IP()
channel = grpc.insecure_channel("{0}:{1}".format(host, DEFAULT_PORT))
client = auto_pb2_grpc.AutoControlStub(channel)
# ideally, you should have try catch block here too
response = client.Submit(auto_pb2.Request(cmd=cmd))
result = loads(str(response.json))
return result
except grpc.RpcError as e:
logger.debug(traceback.format_exc())
# ouch!
# lets print the gRPC error message
# which is "Length of `Name` cannot be more than 10 characters"
logger.debug(e.details())
# lets access the error code, which is `INVALID_ARGUMENT`
# `type` of `status_code` is `grpc.StatusCode`
status_code = e.code()
# should print `INVALID_ARGUMENT`
logger.debug(status_code.name)
# should print `(3, 'invalid argument')`
logger.debug(status_code.value)
# want to do some specific action based on the error?
if grpc.StatusCode.INVALID_ARGUMENT == status_code:
# do your stuff here
pass
except Exception:
logger.debug(traceback.format_exc())
# set = {
# 'kind': 'set',
# 'data': {
# 'group': 222,
# 'cid': 1,
# 'cpu': [2]
# }
# }
#
# group = {
# 'kind': 'group',
# 'data': {
# 'gid': 222,
# 'storage': [
# {
# 'cid': 1,
# 'cpu': 2
# },
# {
# 'cid': 2,
# 'cpu': 2
# }
# ],
# 'compute': [
# {
# 'cid': 2,
# 'cpu': 2
# }
# ],
# }
# }
def submit_group():
groups = get_all_group()
for gid in groups.keys():
submit(dumps(groups[gid]))
return groups
def get_all_group():
CWD = os.getcwd()
cfg = "%s/auto.conf" % CWD
config_raw = configparser.ConfigParser()
config_raw.read(cfg)
groups = {}
# ch = config_raw.sections()
cons = get_all_container()
for gid in config_raw['groups'].keys():
# print()
g_config = loads(config_raw.get('groups', gid))
config_storage = g_config['storage']
config_compute = g_config['compute']
print(g_config)
# set origin cpu limit
group = {
'kind': 'group',
'gid': gid
}
for cid in config_storage:
if cid in cons:
config = get_container_config(cid)
limit = config['NanoCpus'] / 1000000000
group[cid] = {
'kind': 'storage',
'limit': limit
}
for cid in config_compute:
if cid in cons:
config = get_container_config(cid)
limit = config['NanoCpus'] / 1000000000
group[cid] = {
'kind': 'compute',
'limit': limit
}
groups[gid] = group
return groups
def parallel_submit(group):
import multiprocessing as mp
lock = mp.Lock()
gid = group['gid']
def stats(group, cid, lock):
MAX_CPU = group[cid]['limit']
stream = get_container_stats_stream(cid)
count = 0
usage = []
predict = []
for stat in stream:
# print(stat)
# print(type(stat))
u, p = submit_container(gid, cid, stat, MAX_CPU)
usage.append(u)
predict.append(p)
count = count + 1
if count > 2000:
print('container: %s' % cid)
with open('usage.txt', 'w') as f:
f.write(dumps(usage))
with open('predict.txt', 'w') as f:
f.write(dumps(predict))
return
processes = []
for cid in group.keys():
if cid in ['gid', 'kind']:
pass
processes.append(mp.Process(target=stats, args=(group, cid, lock)))
# Run processes
for p in processes:
p.start()
# Exit the completed processes
for p in processes:
p.join()
def submit_container(gid, cid, stat, MAX_CPU):
set = {
'kind': 'set',
'group': gid,
'cid': cid
}
predict = None
cpu = []
set['cpu'] = cpu
usage = None
try:
usage = calculate_cpu_percent(stat)
# print(usage)
cpu.append(usage)
except Exception:
traceback.print_exc()
if usage is None:
print('error: %s' % dumps(stat))
else:
try:
res = submit(dumps(set))
# print(res)
if 'predict' in res['data'].keys():
predict = res['data']['predict']
print('predict: %s' % predict)
print('usage: %s' % usage)
update_container(cid, predict, MAX_CPU)
except Exception:
traceback.print_exc()
return usage, predict
def run_service():
groups = submit_group()
for gid in groups.keys():
print('start subbmit group %s' % gid)
parallel_submit(groups[gid])
while True:
time.sleep(5)
run_service()
# print(get_all_group())
# with ThreadPoolExecutor(max_workers=10) as executor:
# groups = {}
# threads = {}
# while True:
# current_groups = get_all_group()
# for gid in current_groups.keys():
# if gid not in groups.keys():
# group = {}
# # thread = Thread(target=submit(group))
# # thread.daemon = True
# # thread.name = 'group'
# # thread.start()
# time.sleep(5)