ITPub博客

首页 > 云计算 > 虚拟化 > Ceph 自动reweight脚本

Ceph 自动reweight脚本

原创 虚拟化 作者:jesselyu 时间:2017-06-06 23:46:28 0 删除 编辑

Ceph的数据分布是由CRUSH Map解决定的,而CRUSH算法是伪HASH的,所以在一定时间内,数据会存在倾斜,这就需要我们用脚本来进行定期的reweight:

#author

#reweight for ceph osds

 

 

import hashlib

import commands

import threading

import sys

from time import ctime,sleep

import urllib2

import json

def doReweight(osdMaxUtilId, osdTargetReweight):

    print 'ceph osd reweight ' + str(osdMaxUtilId) + ' ' + str(osdTargetReweight)

    (status,output) = commands.getstatusoutput('ceph osd reweight '+ str(osdMaxUtilId) +' ' + str(osdTargetReweight))

    print status,output

def  canOSDReweight():

    #

    canDoReweight = False

    pgStateIsOk = False

    osdUtilDiffEnough = False

    #determine pg status

    (status,output) = commands.getstatusoutput('ceph -s --format=json-pretty')

    pgMapJson = json.loads(output)

    #print pgMapJson['pgmap']['pgs_by_state']

    numPGS = pgMapJson['pgmap']['num_pgs']

    for pgState in pgMapJson['pgmap']['pgs_by_state']:

        if pgState['state_name'] == 'active+clean' and pgState['count'] == numPGS:

            pgStateIsOk = True

    if pgStateIsOk == False:

        print "pg is reweighting or can't do reweight right now...."

        exit

    #determine OSD util diff

    (status,output) = commands.getstatusoutput('ceph osd df --format=json-pretty')

    #print status,output

    osdMaxUtil=0

    osdMaxUtilId=-1

    osdMaxReweight=1

    osdMinUtil=100

    osdMinUtilId=-1

    osdReweightStep=0.01

    if status == 0:

        osdDictJson = json.loads(output)

        for node in osdDictJson['nodes']:

                if node['utilization'] > osdMaxUtil:

                        osdMaxUtilId = node['id']

                        osdMaxUtil = node['utilization']

                        osdMaxReweight = node['reweight']

                if node['utilization'] < osdMinUtil:

                        osdMinUtilId = node['id']

                        osdMinUtil = node['utilization']

        osdTargetReweight = osdMaxReweight - osdReweightStep

        osdUtilDiff = (osdMaxUtil - osdMinUtil)/osdMinUtil*100

        if osdUtilDiff > 10:

            osdUtilDiffEnough = True

            print "Max and Min OSD's utilization diff is " + str(osdUtilDiff)

        else:

            print "Max and Min OSD's utilization diff is " + str(osdUtilDiff) + ",less then 10%, give up..."

            exit

    if  pgStateIsOk == True and osdUtilDiffEnough == True and osdTargetReweight > 0: 

        print "================ doing reweight ==============="

        print osdMaxUtilId,osdMaxUtil,osdMaxReweight

        print osdMinUtilId,osdMinUtil

        doReweight(osdMaxUtilId, osdTargetReweight)

def invokeOSDReweight():

    while True:

        sleep(30)

        canOSDReweight()

if __name__ == '__main__':

    invokeOSDReweight()

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30088583/viewspace-2140370/,如需转载,请注明出处,否则将追究法律责任。

上一篇: Big and Little Endian
下一篇: 没有了~
请登录后发表评论 登录
全部评论
资深Oracle RAC及Exadata部署实施运维专家,曾从事PostgreSQL内核开发与性能优化。丰富的大规模数据库管理架构设计经验。目前在阿里巴巴从事数据库架构工作以及新技术引进,对分布式存储,云计算及并行编程有一定研究。技术讨论请加微信:ljs521688

注册时间:2015-01-01

  • 博文量
    41
  • 访问量
    557964