From e68a20b246f9dfcea49830458787b3bb85bc024e Mon Sep 17 00:00:00 2001 From: "Zhaowangx.Liang" Date: Sat, 1 Apr 2017 11:28:53 +0800 Subject: [PATCH] do reweight for balance the PGS of osds Signed-off-by: Zhaowangx.Liang --- conf/handler.py | 2 ++ tuner/reweight.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++ tuner/tuner.py | 10 ++++++++ 3 files changed, 74 insertions(+) create mode 100644 tuner/reweight.py diff --git a/conf/handler.py b/conf/handler.py index facb3db..85f114e 100644 --- a/conf/handler.py +++ b/conf/handler.py @@ -222,6 +222,8 @@ def list_required_config(self): required_list["system"]["disk|read_ahead_kb"] = 2048 required_list["ceph_tuning"] = OrderedDict() required_list["ceph_tuning"]["pool|rbd|size"] = 2 + required_list["ceph_tuning"]["pool|rbd|reweight_enable"] = "true" + required_list["ceph_tuning"]["pool|rbd|reweight_target"] = 0.05 required_list["ceph_tuning"]["global|mon_pg_warn_max_per_osd"] = 1000 required_list["analyzer"] = OrderedDict() required_list["analyzer"]["analyzer"] = "all" diff --git a/tuner/reweight.py b/tuner/reweight.py new file mode 100644 index 0000000..8c582be --- /dev/null +++ b/tuner/reweight.py @@ -0,0 +1,62 @@ +import os,sys +lib_path = os.path.abspath(os.path.join('../conf/')) +sys.path.append(lib_path) +from conf import * +import time + +class Reweight: + def __init__(self, target): + self.oload = 101 + self.max_change = 0.01 + self.max_change_osds = 2 + self.target = float(target) + self.max_time_re = 300 + self.max_count_re = 1000 + self.t_start = 0 + self.t_end = 0 + self.count_re = 0 + self.result = [] + self.isFitTarget = False + + def Doreweight(self, oload, max_change, max_change_osds): + common.bash("ceph osd reweight-by-pg %s %s %s" % (oload, max_change, max_change_osds)) + + def GetPgs(self): + out = common.bash("ceph osd df | awk '{print $9}'") + pgsList = [int(x) for x in out.split('\n') if x != '' and x != 'PGS'] + return pgsList + + def GetValueDiff(self, pgs): + if pgs: + diff = round((max(pgs)-min(pgs))*1.0/max(pgs),3) + else: + diff = -1 + self.result.append([self.count_re, diff]) + print self.count_re, diff, pgs + return diff + + def main(self): + for i in range(3): + self.t_start = time.time() + while True: + diff = self.GetValueDiff(self.GetPgs()) + self.t_end = time.time() + self.count_re += 1 + if diff < self.target or self.max_count_re < self.count_re or self.max_time_re < self.t_end - self.t_start: + if diff < self.target: + self.isFitTarget = True + common.printout("LOG", "reweight complete.") + break + else: + self.Doreweight(self.oload, self.max_change, self.max_change_osds) + time.sleep(0.5) + if self.isFitTarget: + break + else: + self.target += 0.01 + if not self.isFitTarget: + common.printout("WARNING", "after reweight, the diff of PGS also too large!") + +def do(target): + re = Reweight(target) + re.main() diff --git a/tuner/tuner.py b/tuner/tuner.py index 3e102cd..0c38361 100644 --- a/tuner/tuner.py +++ b/tuner/tuner.py @@ -10,6 +10,7 @@ import json import argparse import threading +import reweight pp = pprint.PrettyPrinter(indent=4) class Tuner: @@ -324,6 +325,9 @@ def apply_tuning(self, jobname, no_check = False): if self.worksheet[jobname]['pool'][new_poolname][param] != latest_pool_config[new_poolname][param]: self.handle_pool(option = 'set', param = {'name':new_poolname, param:self.worksheet[jobname]['pool'][new_poolname][param]}) + if not pool_exist and self.worksheet[jobname]['pool'][new_poolname]['reweight_enable'] == 'true': + self.handle_pool(option = 'reweight', param = {'reweight_target':self.worksheet[jobname]['pool'][new_poolname]['reweight_target']}) + if no_check: return @@ -359,6 +363,12 @@ def handle_pool(self, option="set", param = {}): common.printout("LOG","delete ceph pool %s" % pool) common.pdsh(user, [controller], "ceph osd pool delete %s %s --yes-i-really-really-mean-it" % (pool, pool), option="check_return") + if option == "reweight": + common.printout("LOG", "start reweight, target: %s" % param['reweight_target']) + reweight.do(param['reweight_target']) + common.printout("LOG", "reweight complete.") + + def main(args): print args tuner_parser = argparse.ArgumentParser(description='Tuner.')