This repository was archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.py
More file actions
executable file
·354 lines (297 loc) · 9.45 KB
/
task.py
File metadata and controls
executable file
·354 lines (297 loc) · 9.45 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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
import struct
import socket
import time
import json
import collections
from google.protobuf.internal.encoder import _EncodeVarint
from google.protobuf.internal.decoder import _DecodeVarint32
from gen import ClientNamenodeProtocol_pb2
from gen import RpcHeader_pb2
from gen import IpcConnectionContext_pb2
from gen import ProtobufRpcEngine_pb2
from gen import yarn_service_protos_pb2
from gen import yarn_protos_pb2
logging.basicConfig(level=logging.INFO,format=u'%(asctime)s [%(levelname)s] (%(name)s) {%(pathname)s:%(lineno)d@%(funcName)s} - %(message)s')
class HadoopRPC(object):
def __init__(self,host,port):
super(HadoopRPC,self).__init__()
self._sock = socket.create_connection((host,port))
self._rpc_request_header =None
self._connect()
return
def _connect(self):
self._sock.send('hrpc')
self._sock.send(struct.pack('!B',9)) # version
self._sock.send(struct.pack('!B',0)) # service class
self._sock.send(struct.pack('!B',0)) # simple auth
self._rpc_request_header = self._init_rpc_request_header()
context = IpcConnectionContext_pb2.IpcConnectionContextProto()
self._write(self._sock,[self._rpc_request_header,context])
return
def _next_callid(self):
if self._rpc_request_header.callId == -3:
self._rpc_request_header.callId = 0
self._rpc_request_header.callId += 1
def _write(self,socket,payloads):
out = ''.join(
map(lambda x:self._wrap(x),payloads)
)
socket.send(struct.pack('!I',len(out)))
socket.send(out)
return
def _read_varint32(self,raw,start):
return _DecodeVarint32(raw,start)
def _varint(self,value):
encoded = []
_EncodeVarint(encoded.append,value)
return ''.join(encoded)
def _init_rpc_request_header(self):
header = RpcHeader_pb2.RpcRequestHeaderProto()
header.callId = -3
header.rpcKind = RpcHeader_pb2.RPC_PROTOCOL_BUFFER
header.rpcOp = RpcHeader_pb2.RpcRequestHeaderProto.RPC_FINAL_PACKET
header.clientId = 'sonm-python-client'
return header
def _wrap(self,message):
message = message.SerializeToString()
message = '%s%s' % (
self._varint(len(message)),
message,
)
return message
def call(self,clazz,method,request,response,callback=None):
request_header = ProtobufRpcEngine_pb2.RequestHeaderProto()
request_header.methodName = method
request_header.declaringClassProtocolName = clazz
request_header.clientProtocolVersion = 1
self._next_callid()
self._write(
self._sock,
[
self._rpc_request_header,
request_header,
request,
]
)
# wait for response
length = struct.unpack('!I',self._sock.recv(4))[0]
more = length
raws = []
while more > 0:
raw = self._sock.recv(length)
more -= len(raw)
raws.append(raw)
raw = ''.join(raws)
# response header
response_header = RpcHeader_pb2.RpcResponseHeaderProto()
response_header_length,offest = self._read_varint32(raw,0)
response_header.ParseFromString(raw[offest:offest+response_header_length])
if response_header.status == RpcHeader_pb2.RpcResponseHeaderProto.SUCCESS:
# response
message_header_length,offest = self._read_varint32(raw,offest+response_header_length)
response.ParseFromString(raw[offest:offest+message_header_length])
if callback is not None:
callback(response_header,response)
else:
return response_header,response
class MultiRPC(object):
def __init__(self,rpcs):
super(MultiRPC,self).__init__()
self._rpcs = rpcs
self._client = None
def applications(self):
request = yarn_service_protos_pb2.GetApplicationsRequestProto()
request.application_states.extend([
yarn_protos_pb2.ACCEPTED,
yarn_protos_pb2.RUNNING,
])
response = yarn_service_protos_pb2.GetApplicationsResponseProto()
_,response = self.rpc(request,response)
def _find_client(self):
if self._client is None:
for rpc in self._rpcs:
try:
self._client = HadoopRPC(rpc[0],rpc[1])
except:
pass
if self._client is None:
return self._find_client()
else:
return self._client
def rpc(self,protocol,method,request,response):
while True:
try:
return self._find_client().call(
protocol,
method,
request,
response,
)
except:
self._client = None
class Scheduler(object):
def __init__(self,hosts):
self._yarn = MultiRPC(hosts)
def _is_ok(self,response_header):
return response_header is not None and response_header.status == RpcHeader_pb2.RpcResponseHeaderProto.SUCCESS
def applications(self):
request = yarn_service_protos_pb2.GetApplicationsRequestProto()
request.application_states.extend([
yarn_protos_pb2.RUNNING,
])
#request.application_types.append('MAPREDUCE')
request.application_types.append('SPARK')
response = yarn_service_protos_pb2.GetApplicationsResponseProto()
header,response = self._yarn.rpc(
'org.apache.hadoop.yarn.api.ApplicationClientProtocolPB',
'getApplications',
request,
response,
)
if self._is_ok(header):
return map(
lambda x:{
'id': {
'cluster_timestamp' : x.applicationId.cluster_timestamp,
'id' : x.applicationId.id,
},
'name' : x.name,
'queue' : x.queue,
'resouces' : {
'cpu' : x.app_resource_Usage.used_resources.virtual_cores,
'memory' : x.app_resource_Usage.used_resources.memory,
},
'times' : {
'cpu' : x.app_resource_Usage.vcore_seconds,
'memory' : x.app_resource_Usage.memory_seconds,
},
'greedy' : {
'cpu' : x.app_resource_Usage.vcore_seconds / x.app_resource_Usage.used_resources.virtual_cores,
'memory' : x.app_resource_Usage.memory_seconds / x.app_resource_Usage.used_resources.memory
}
},
response.applications,
)
else:
return []
def _queue_info(self,name):
request = yarn_service_protos_pb2.GetQueueInfoRequestProto()
request.queueName = name
request.recursive = True
response = yarn_service_protos_pb2.GetQueueInfoResponseProto()
header,response = self._yarn.rpc(
'org.apache.hadoop.yarn.api.ApplicationClientProtocolPB',
'getQueueInfo',
request,
response,
)
if not self._is_ok(header):
return None
return {
'name' : response.queueInfo.queueName ,
'busy' : response.queueInfo.currentCapacity,
'capacity' : response.queueInfo.capacity,
'usage' : response.queueInfo.currentCapacity,
}
def queues(self):
request = yarn_service_protos_pb2.GetQueueUserAclsInfoRequestProto()
response = yarn_service_protos_pb2.GetQueueUserAclsInfoResponseProto()
header,response = self._yarn.rpc(
'org.apache.hadoop.yarn.api.ApplicationClientProtocolPB',
'getQueueUserAcls',
request,
response,
)
if not self._is_ok(header):
return []
# collect leaf queues
queues = {}
for name in map(lambda x:x.queueName,response.queueUserAcls):
part = name.split('.')
container = queues
for part in name.split('.'):
child = container.get(part,None)
if child is None:
container[part] = child = {}
container = child
# flatten back
flatten = set()
def drill(container,context=[]):
for key,value in container.iteritems():
context.append(key)
if len(value) > 0:
drill(value,context)
else:
# leaf
flatten.add('.'.join(context))
context = context[:-1]
drill(queues)
# find queue info
queue_infos = []
for queue in flatten:
info = self._queue_info(queue)
if info is None:
continue
logging.info('fetch queue:%s' % info)
queue_infos.append(info)
return queue_infos
def _move(self,task,queue):
request = yarn_service_protos_pb2.MoveApplicationAcrossQueuesRequestProto()
request.application_id.id = task['id']['id']
request.application_id.cluster_timestamp = task['id']['cluster_timestamp']
request.target_queue = queue['name']
response = yarn_service_protos_pb2.MoveApplicationAcrossQueuesResponseProto()
header,response = self._yarn.rpc(
'org.apache.hadoop.yarn.api.ApplicationClientProtocolPB',
'moveApplicationAcrossQueues',
request,
response,
)
if not self._is_ok(header):
logging.warn('fail to move task:%s to queue:%s' % (task,queue))
return False
logging.info('move ok')
return True
def _kill(self,task):
request = yarn_service_protos_pb2.KillApplicationRequestProto()
request.application_id.id = task['id']['id']
request.application_id.cluster_timestamp = task['id']['cluster_timestamp']
response = yarn_service_protos_pb2.KillApplicationResponseProto()
header,response = self._yarn.rpc(
'org.apache.hadoop.yarn.api.ApplicationClientProtocolPB',
'forceKillApplication',
request,
response,
)
if not self._is_ok(header):
logging.warn('fail to kill task:%s' % task)
return False
return True
def do(self):
applications = self.applications()
if len(applications) <= 0:
return
for application in filter(lambda x:x['queue'] == 'root.spark',applications):
if application['resouces']['cpu'] >= 500:
logging.info('killing for used too much cpu, application:%s' % application)
self._kill(application)
applications = filter(lambda x:x['queue'] == 'root.default',applications)
logging.info(json.dumps(applications))
queue = {'name':'root.spark'}
for application in filter(lambda x:x['queue'] != queue['name'] ,applications):
logging.info('moving application:%s to root.spark' % application)
if not self._move(application,queue):
self._kill(application)
return
if __name__ == '__main__':
scheduler = Scheduler([
('10.116.100.10',23140),
('10.116.100.11',23140),
])
while True:
scheduler.do()
logging.info('sleep...')
time.sleep(5)