forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalo_env.py
688 lines (605 loc) · 21.4 KB
/
palo_env.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#!/bin/env python
# -*- coding: utf-8 -*-
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""
This module describe environment info about Palo.
Date: 2015/10/07 17:23:06
"""
import os
import pexpect
import threading
import time
import env_config
from lib import palo_client
class PaloEnv(object):
"""
Palo部署及运行环境信息
"""
def __init__(self):
self.__master = env_config.master
self.__follower_list = env_config.follower_list
self.__observer_list = env_config.observer_list
self.__be_list = env_config.be_list
self.__fe_path = env_config.fe_path
self.__be_path = env_config.be_path
self.__host_username = env_config.host_username
self.__host_password = env_config.host_password
self.__fe_query_port = env_config.fe_query_port
self.__be_heartbeat_port = self.__fe_query_port + 20
self.__be_data_path_list = env_config.be_data_path_list
self.__master_lock = threading.Lock()
self.__follower_lock = threading.Lock()
self.__observer_lock = threading.Lock()
self.__be_lock = threading.Lock()
self.__dynamic_add_fe_list = env_config.dynamic_add_fe_list
self.__dynamic_add_be_list = env_config.dynamic_add_be_list
self.__dynamic_add_fe_lock = threading.Lock()
self.__dynamic_add_be_lock = threading.Lock()
def init(self):
"""init
"""
if not self.reset_master():
return False
return True
def get_master(self):
"""返回mster
"""
with self.__master_lock:
return self.__master
def set_master(self, host_name):
"""设置mster
"""
assert host_name
with self.__master_lock:
self.__master = host_name
def remove_master(self):
"""remove mster
"""
with self.__master_lock:
self.__master = None
def __get_client(self, host_name, port):
"""
"""
assert host_name
client = palo_client.PaloClient(host_name, port)
if client.init():
time.sleep(10)
return client
else:
return None
def reset_master(self):
"""reset master
"""
# 如果没有get到master, 重试,等待重新选了一个新的master
ix = 0
while ix < 120:
if self.reset_master_once():
return True
time.sleep(1)
ix += 1
else:
return False
def reset_master_once(self):
"""reset master once
"""
cur_master = self.get_master()
electable_tuple = (self.get_master(),) + self.get_follower_tuple() if \
self.get_master() else self.get_follower_tuple()
for host_name in electable_tuple:
client = self.__get_client(host_name, self.get_fe_query_port())
if not client:
return False
if client.is_master():
if host_name != cur_master:
if cur_master:
self.add_follower(cur_master)
self.remove_follower(host_name)
self.set_master(host_name)
return True
return False
def get_follower_tuple(self):
"""返回follower
"""
with self.__follower_lock:
return tuple(self.__follower_list)
def add_follower(self, host_name):
"""增加follower
"""
assert host_name
if host_name not in self.__follower_list:
with self.__follower_lock:
self.__follower_list.append(host_name)
def remove_follower(self, host_name):
"""删除follower
"""
assert host_name
if host_name in self.__follower_list:
with self.__follower_lock:
self.__follower_list.remove(host_name)
def get_observer_tuple(self):
"""返回observer
"""
with self.__follower_lock:
return tuple(self.__observer_list)
def add_observer(self, host_name):
"""增加observer
"""
assert host_name
if host_name not in self.__observer_list:
with self.__observer_lock:
self.__observer_list.append(host_name)
def remove_observer(self, host_name):
"""删除observer
"""
assert host_name
if host_name in self.__observer_list:
with self.__observer_lock:
self.__observer_list.remove(host_name)
def get_fe_tuple(self):
"""返回FE
"""
return (self.get_master(),) + self.get_follower_tuple() + self.get_observer_tuple()
def get_dynamic_add_fe_tuple(self):
"""返回dynamic added fe
"""
with self.__dynamic_add_fe_lock:
return tuple(self.__dynamic_add_fe_list)
def add_dynamic_add_fe(self, host_name):
"""增加dynamic added fe
"""
assert host_name
if host_name not in self.__dynamic_add_fe_list:
with self.__dynamic_add_fe_lock:
self.__dynamic_add_fe_list.append(host_name)
def remove_dynamic_add_fe(self, host_name):
"""删除dynamic added fe
"""
assert host_name
if host_name in self.__dynamic_add_fe_list:
with self.__dynamic_add_fe_lock:
self.__dynamic_add_fe_list.remove(host_name)
def get_be_tuple(self):
"""返回BE
"""
with self.__be_lock:
return tuple(self.__be_list)
def add_be(self, host_name):
"""增加BE
"""
assert host_name
if host_name not in self.__be_list:
with self.__be_lock:
self.__be_list.append(host_name)
def remove_be(self, host_name):
"""删除BE
"""
assert host_name
if host_name in self.__be_list:
with self.__be_lock:
self.__be_list.remove(host_name)
def get_dynamic_add_be_tuple(self):
"""返回dynamic added be
"""
with self.__dynamic_add_be_lock:
return tuple(self.__dynamic_add_be_list)
def add_dynamic_add_be(self, host_name):
"""增加dynamic added be
"""
assert host_name
if host_name not in self.__dynamic_add_be_list:
with self.__dynamic_add_be_lock:
self.__dynamic_add_be_list.append(host_name)
def remove_dynamic_add_be(self, host_name):
"""删除dynamic added be
"""
assert host_name
if host_name in self.__dynamic_add_be_list:
with self.__dynamic_add_be_lock:
self.__dynamic_add_be_list.remove(host_name)
def get_fe_query_port(self):
"""fe query port
"""
return self.__fe_query_port
def get_be_heartbeat_port(self):
"""be port
"""
return self.__be_heartbeat_port
def __exec_cmd(self, cmd, host_name, timeout=120):
"""exec cmd"""
exe_cmd = 'ssh %s@%s "%s"' % (self.__host_username, host_name, cmd)
output, status = pexpect.run(exe_cmd, timeout=timeout, withexitstatus=True, \
events = {"continue connecting":"yes\n", "password:":"%s\n" % self.__host_password})
if 'PALO_CLIENT_LOG_SQL' in os.environ.keys():
palo_client.LOG.info(palo_client.L("execute CMD", exe_cmd=exe_cmd, \
status=status, output=output))
return status, output
def stop_master(self):
"""Stop master
"""
# 结束当前master进程
cmd = 'cd %s/fe;sh bin/stop_fe.sh' % (self.__fe_path)
ix = 0
while ix < 120:
self.__exec_cmd(cmd, host_name=self.__master)
if not self.is_fe_alive(self.get_master()):
self.remove_master()
return True
time.sleep(1)
ix += 1
else:
return False
def start_electable(self, host_name):
"""Start electable
"""
return self.start_follower(host_name)
def start_follower(self, host_name):
"""Start follower
"""
if host_name in self.get_follower_tuple():
return False
cmd = 'cd %s/fe;sh bin/start_fe.sh' % self.__fe_path
self.__exec_cmd(cmd, host_name)
time.sleep(3)
if self.__get_client(host_name, self.get_fe_query_port()):
self.add_follower(host_name)
time.sleep(10)
return True
else:
return False
def stop_follower(self, host_name):
"""stop follower
"""
if host_name not in self.get_follower_tuple():
return False
cmd = 'cd %s/fe;sh bin/stop_fe.sh' % (self.__fe_path)
ix = 0
while ix < 120:
self.__exec_cmd(cmd, host_name)
if not self.is_fe_alive(host_name):
self.remove_follower(host_name)
return True
time.sleep(1)
ix += 1
else:
return False
def start_observer(self, host_name):
"""Start master
"""
if host_name in self.get_observer_tuple():
return False
cmd = 'cd %s/fe;sh bin/start_fe.sh' % (self.__fe_path)
self.__exec_cmd(cmd, host_name)
time.sleep(3)
if self.__get_client(host_name, self.get_fe_query_port()):
self.add_observer(host_name)
time.sleep(10)
return True
else:
return False
def stop_observer(self, host_name):
"""stop observer
"""
if host_name not in self.get_observer_tuple():
return False
cmd = 'cd %s/fe;sh bin/stop_fe.sh' % (self.__fe_path)
ix = 0
while ix < 120:
self.__exec_cmd(cmd, host_name)
if not self.is_fe_alive(host_name):
self.remove_observer(host_name)
return True
time.sleep(1)
ix += 1
else:
return False
def is_fe_alive(self, host_name):
"""check is fe alive
"""
cmd = "cd %s/fe;if [ -f bin/fe.pid ]; then if kill -0 `cat bin/fe.pid` " \
">/dev/null 2>&1; then echo 'running'; fi; fi" % (self.__fe_path)
if self.__exec_cmd(cmd, host_name)[1].rstrip('\r\n') == 'running':
return True
else:
return False
def is_be_alive(self, host_name):
"""check is be alive
"""
cmd = "cd %s/be;if [ -f bin/be.pid ]; then if kill -0 `cat bin/be.pid` " \
">/dev/null 2>&1; then echo 'running'; fi; fi" % (self.__be_path)
if self.__exec_cmd(cmd, host_name)[1].rstrip('\r\n') == 'running':
return True
else:
return False
def restart_master(self):
"""restart master
"""
# follower_tuple = self.get_follower_tuple()
# for follower in follower_tuple:
# if not self.stop_follower(follower):
# return False
old_master = self.get_master()
if not self.stop_master():
return False
if not self.start_electable(old_master):
return False
# for follower in follow_tuple:
# if not self.start_follower(follower):
# return False
# return True
if not self.reset_master():
return False
return True
def switch_master(self):
"""主备切换
"""
cur_master = self.get_master()
if not self.stop_master():
return False
if not self.reset_master():
return False
if not self.start_electable(cur_master):
return False
return True
def restart_follower(self, host_name=None):
"""restart follower
"""
if not self.stop_follower(host_name):
return False
if not self.start_follower(host_name):
return False
return True
def restart_observer(self, host_name=None):
"""restart observer
"""
if not self.stop_observer(host_name):
return False
if not self.start_observer(host_name):
return False
return True
def stop_be(self, host_name):
"""Stop BE
"""
cmd_a = 'cd %s/be;sh bin/stop_be.sh' % (self.__be_path)
cmd_b = 'pkill -f %s/be/lib/palo_be' % (self.__be_path)
ix = 0
while ix < 120:
self.__exec_cmd(cmd_a, host_name)
self.__exec_cmd(cmd_b, host_name)
if not self.is_be_alive(host_name):
self.remove_be(host_name)
return True
time.sleep(1)
ix += 1
return False
def start_be(self, host_name):
"""Start BE
"""
if host_name in self.get_be_tuple():
return False
cmd= 'cd %s/be;sh bin/start_be.sh' % (self.__be_path)
ix = 0
while ix < 120:
self.__exec_cmd(cmd, host_name)
if self.is_be_alive(host_name):
self.add_be(host_name)
time.sleep(10)
return True
time.sleep(1)
ix += 1
return False
def restart_be(self, host_name):
"""Restart be
"""
if not self.stop_be(host_name):
return False
if not self.start_be(host_name):
return False
return True
def stop_all_fe(self):
"""stop all fe
"""
for host_name in self.get_follower_tuple():
if not self.stop_follower(host_name):
return False
for host_name in self.get_observer_tuple():
if not self.stop_observer(host_name):
return False
if not self.stop_master():
return False
return True
def stop_all_be(self):
"""stop all be
"""
for host_name in self.get_be_tuple():
if not self.stop_be(host_name):
return False
return True
def update_fe_config(self, option_value_dict):
"""Update FE config
"""
master = self.get_master()
follower_tuple = self.get_follower_tuple()
observer_tuple = self.get_observer_tuple()
fe_tuple = self.get_fe_tuple()
if not self.stop_all_fe():
return False
configfile_path = '%s/fe/conf/fe.conf' % (self.__fe_path)
for host_name in fe_tuple:
for option, value in option_value_dict.iteritems():
if not self.modify_config(host_name, configfile_path, option, value):
return False
self.start_electable(master)
follower_threads = []
observer_threads = []
for host_name in follower_tuple:
t = threading.Thread(target=self.start_follower, args=(host_name,))
t.start()
follower_threads.append(t)
for host_name in observer_tuple:
t = threading.Thread(target=self.start_observer, args=(host_name,))
t.start()
observer_threads.append(t)
for t in follower_threads:
t.join()
for t in observer_threads:
t.join()
for host_name in follower_tuple + observer_tuple:
if not self.is_fe_alive(host_name):
return False
if not self.reset_master():
return False
return True
def update_be_config(self, option_value_dict):
"""Update BE config
"""
be_tuple = self.get_be_tuple()
if not self.stop_all_be():
return False
configfile_path = '%s/be/conf/be.conf' % (self.__be_path)
for host_name in be_tuple:
for option, value in option_value_dict.iteritems():
if not self.modify_config(host_name, configfile_path, option, value):
return False
be_threads = []
for host_name in self.be_tuple:
t = threading.Thread(target=self.start_be, args=(host_name,))
t.start()
be_threads.append(t)
for t in be_threads:
t.join()
for host_name in be_tuple:
if not self.is_be_alive(host_name):
return False
return True
def modify_config(self, host_name, filepath, option, value):
"""修改配置
"""
cmd = "grep -q '^{option}' {filepath} && "\
"sed -i 's/^{option}.*/{option} = {value}/g' {filepath} || "\
"echo '{option} = {value}' >> {filepath}".format( \
option=option, value=value, filepath=filepath)
status, output = self.__exec_cmd(cmd, host_name)
if status != 0 or output:
return False
cmd = "grep -q '^{} = {}' {}".format(option, value, filepath)
status, output = self.__exec_cmd(cmd, host_name)
if status != 0:
return False
return True
def remove_config(self, host_name, filepath, option):
"""删除配置
"""
cmd = 'sed -i "s/^%s.*/ /" %s' % (option, filepath)
self.__exec_cmd(cmd, host_name)
cmd = "grep -q '^{} {}'".format(option, filepath)
status, output = self.__exec_cmd(cmd, host_name)
if status == 0:
return False
return True
def clean_fe(self, host_name):
"""clean fe
"""
cmd= 'cd %s/fe;rm -rf log palo-meta/image palo-meta/bdb temp' % (self.__fe_path)
return self.__exec_cmd(cmd, host_name)
def clean_be(self, host_name):
"""clean be
"""
data_path = '/* '.join(self.__be_data_path_list) + '/*'
cmd= 'cd %s/be;rm -rf log unused bin/be.pid %s' % (self.__be_path, data_path)
return self.__exec_cmd(cmd, host_name)
def clean_start(self):
"""clean start
"""
self.stop_all_fe()
self.stop_all_be()
master = env_config.master
follower_tuple = tuple(env_config.follower_list)
observer_tuple = tuple(env_config.observer_list)
be_tuple = tuple(env_config.be_list)
for host_name in (master,) + follower_tuple + observer_tuple:
self.clean_fe(host_name)
for host_name in be_tuple:
self.clean_be(host_name)
self.start_electable(master,)
follower_threads = []
observer_threads = []
be_threads = []
for host_name in follower_tuple:
t = threading.Thread(target=self.start_follower, args=(host_name,))
t.start()
follower_threads.append(t)
for host_name in observer_tuple:
t = threading.Thread(target=self.start_observer, args=(host_name,))
t.start()
observer_threads.append(t)
for t in follower_threads:
t.join()
for t in observer_threads:
t.join()
for host_name in be_tuple:
t = threading.Thread(target=self.start_be, args=(host_name,))
t.start()
be_threads.append(t)
for t in be_threads:
t.join()
for host_name in follower_tuple:
if not self.is_fe_alive(host_name):
return False
for host_name in observer_tuple:
if not self.is_fe_alive(host_name):
return False
for host_name in be_tuple:
if not self.is_be_alive(host_name):
return False
if not self.reset_master():
return False
client = self.__get_client(self.get_master(), self.get_fe_query_port())
if not client:
return False
be_heartbeat_port = self.get_be_heartbeat_port()
backend_list = ['%s:%s' % (be, be_heartbeat_port) for be in self.get_be_tuple()]
return client.add_backend_list(backend_list)
def check_be(self, host_name, tablet_id):
"""check whether base expansion done
"""
assert host_name
data_path = '/data '.join(self.__be_data_path_list) + '/data'
cmd = 'find %s -name %s_0_*.dat' % (data_path, tablet_id)
status, output = self.__exec_cmd(cmd, host_name)
assert not status
file_name = output.split('/')[-1]
versions = file_name.split('_')
if versions[2] > 1:
return True
else:
return False
def check_ce(self, host_name, tablet_id):
"""check whether cumulative done
"""
assert host_name
data_path = '/data '.join(self.__be_data_path_list) + '/data'
cmd = 'find %s -name %s_*.dat' % (data_path, tablet_id)
status, output = self.__exec_cmd(cmd, host_name)
assert not status
file_path_list = output.split('\r\n')
for file_path in file_path_list:
file_name = file_path.split('/')[-1]
versions = file_name.split('_')
if versions[1] > 0 and versions[1] != versions[2]:
return True
return False