This repository has been 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
/
schedule.py
executable file
·287 lines (241 loc) · 7.55 KB
/
schedule.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
#!/usr/bin/python
# -*- coding: :new utf-8 -*-
import logging
import os
from urlparse import urlparse
from datetime import datetime
from tornado import gen
from tornado.ioloop import IOLoop
from nonblocking import Yarn
from nonblocking import MRClient
from nonblocking import Namenode
from nonblocking import Namenodes
from gen import hdfs_pb2
logging.basicConfig(level=logging.INFO,format=u'%(asctime)s [%(levelname)s] (%(name)s) {%(pathname)s:%(lineno)d@%(funcName)s} - %(message)s')
@gen.coroutine
def move_spark_task(yarn):
logging.info('trigger move_spark_task...')
for application in filter(
lambda x: x.queue == 'root.default' and x.applicationType == 'SPARK',
(yield yarn.running_applications()).applications
):
logging.info('moving spark application:%s from root.default to root.spark' % application)
ok = yield yarn.move_to_queue(
{
'id': application.applicationId.id,
'cluster_timestamp' : application.applicationId.cluster_timestamp,
},
'root.spark'
)
if not ok:
logging.info('fail to move spark application_%s_%s to queue:root.spark,killing it' % (
application.applicationId.id,
application.applicationId.cluster_timestamp,
))
yield yarn.kill({
'id': application.applicationId.id,
'cluster_timestamp' : application.applicationId.cluster_timestamp,
})
# requeue
IOLoop.current().call_later(5,move_spark_task,yarn)
@gen.coroutine
def evict_large_spark_task(yarn):
logging.info('triger evict_large_spark_task...')
for application in filter(
lambda x: x.applicationType == 'SPARK',
(yield yarn.running_applications()).applications,
):
"""
app_resource_Usage {
num_used_containers: 17
num_reserved_containers: 0
used_resources {
memory: 69632
virtual_cores: 33
}
reserved_resources {
memory: 0
virtual_cores: 0
}
needed_resources {
memory: 69632
virtual_cores: 33
}
memory_seconds: 17216444230
vcore_seconds: 8159203
}
"""
requested = application.app_resource_Usage.needed_resources
if requested.memory > 250*10240:
logging.info('spark application request too much memory(2,500G), killing :%s' % application)
yield yarn.kill({
'id': application.applicationId.id,
'cluster_timestamp' : application.applicationId.cluster_timestamp,
})
# requeue
IOLoop.current().call_later(5,evict_large_spark_task,yarn)
@gen.coroutine
def audit_mr_jobs(mr_history,yarn,namenodes):
for application in filter(
lambda x:x.applicationType == 'MAPREDUCE',
(yield yarn.running_applications()).applications
):
# prepare callback
@gen.coroutine
def attempt_callback(future):
attempt = future.result()
# no attempt
if attempt is None:
raise gen.Return()
mr = MRClient([
(attempt.application_attempt_report.host,attempt.application_attempt_report.rpc_port)
])
# fetch job report
job = yield mr.jobreport({
'id' : attempt.application_attempt_report.application_attempt_id.application_id.id,
'cluster_timestamp' : attempt.application_attempt_report.application_attempt_id.application_id.cluster_timestamp,
},
attempt.application_attempt_report.application_attempt_id.application_id.id,
)
yield mr.close()
job_config = job.job_report.jobFile
namenode = namenodes.resolve(job.job_report.jobFile)
if namenode is None:
logging.warn('no namenode for job_file:%s' % job)
raise gen.Return()
#TODO
info = yield namenode.file_info(urlparse(job_config).path)
if info is None:
logging.warn('no job file for application:%s' % attempt)
yield gen.Return()
blocks = yield namenode.blocks(job_config.path,0,info.fs.length)
logging.info(blocks)
exit(0)
raise gen.Return()
# fetch attempt
yarn.application_attempt({
'id': application.applicationId.id,
'cluster_timestamp' : application.applicationId.cluster_timestamp,
},
application.currentApplicationAttemptId.attemptId
).add_done_callback(attempt_callback)
# end loop
raise gen.Return()
@gen.coroutine
def clean_hive_staging_dir(namenodes,dirs):
now = int((datetime.now() - datetime(1970,1,1)).total_seconds() * 1000)
logging.info('trigger clean_hive_staging_dir... %s' % datetime.fromtimestamp(now/1000))
for candidate in dirs:
namenode = namenodes.resolve(candidate)
if namenode is None:
logging.warn('no namenode for file:%s' % candidate)
raise gen.Return()
url = urlparse(candidate)
@gen.coroutine
def each(entry):
if not os.path.split(entry.path)[-1].startswith('.hive-staging_hive'):
raise gen.Return()
full_path = '%s/%s' % (
url.path,
entry.path,
)
# process older than a day
if now - entry.modification_time > (7 * 3600 * 24 * 1000):
trash(namenode,full_path)
# do work
yield namenode.list_dirs_all(urlparse(candidate).path,each)
IOLoop.current().call_later(5,clean_hive_staging_dir,namenodes,dirs)
@gen.coroutine
def trash(namenode,candidate):
parent,file_name = os.path.split(candidate)
trash_root = '/user/hdfs/.Trash/%s%s' % (
'%s0000' % datetime.now().strftime('%y%m%d%H'),
parent,
)
yield namenode.mkdirs(trash_root)
# move last modify 24 hours ago
file_info = yield namenode.file_info(candidate)
target = '%s/%s' % (
trash_root,
file_name,
)
response = yield namenode.move(candidate,target,True)
logging.info('move %s to %s, done:%s modify:%s' % (
candidate,
target,
response,
datetime.fromtimestamp(file_info.fs.modification_time/1000),
))
@gen.coroutine
def clean_hive_scratch_dir(self,dirs):
now = int((datetime.now() - datetime(1970,1,1)).total_seconds() * 1000)
logging.info('trigger clean_hive_scratch_dir... %s' % datetime.fromtimestamp(now/1000))
for candidate in dirs:
namenode = namenodes.resolve(candidate)
if namenode is None:
logging.warn('no namenode for file:%s' % candidate)
raise gen.Return()
url = urlparse(candidate)
@gen.coroutine
def each(entry):
if entry.path.startswith('.hive-staging'):
raise gen.Return()
full_path = '%s/%s' % (
url.path,
entry.path,
)
# remove file
if entry.fileType != 1:
trash(namenode,full_path)
raise gen.Return()
@gen.coroutine
def uuid(child):
sample = '0034a069-f4f7-4cdf-855a-c7513a2e8e3f'
path = child.path
if len(path) != len(sample) and \
len(path.split('-')) != len(sample.split('-')):
raise gen.Return()
full = '%s/%s' % (
full_path,
child.path,
)
if now - child.modification_time > (7 * 3600 * 24 * 1000):
# remove
trash(namenode,full)
# do work
yield namenode.list_dirs_all(full_path,uuid)
# do work
yield namenode.list_dirs_all(urlparse(candidate).path,each)
# end for
IOLoop.current().call_later(5,clean_hive_scratch_dir,namenodes,dirs)
pass
if __name__ == '__main__':
yarn = Yarn([
('10.116.100.10',23140),
('10.116.100.11',23140),
])
mr_history = MRClient([
('10.116.100.12',10020),
],True)
namenodes = Namenodes({
'sfbd': Namenode([
('10.116.100.2',8020),
('10.116.100.1',8020),
]),
'sfbdp1': Namenode([
('10.116.100.3',8020),
('10.116.100.4',8020),
])
})
hive_stagings = [
'hdfs://sfbdp1/tmp/hive',
]
hive_scratch = [
'hdfs://sfbdp1/tmp/hive',
]
#IOLoop.current().add_callback(lambda :move_spark_task(yarn))
#IOLoop.current().add_callback(lambda :evict_large_spark_task(yarn))
IOLoop.current().add_callback(lambda :clean_hive_staging_dir(namenodes,hive_stagings))
IOLoop.current().add_callback(lambda :clean_hive_scratch_dir(namenodes,hive_scratch))
#IOLoop.current().add_callback(lambda :audit_mr_jobs(mr_history,yarn,namenodes))
IOLoop.current().start()