Exemplo n.º 1
0
def set_weight(ring_name,
               disk_device,
               weight,
               ip,
               port,
               policy_num,
               cluster_name,
               taskid=None):
    rm = RingManager(cluster_name)
    try:
        if not policy_num:
            ring_name = ring_name.split('.')[0]
            content = rm.set_weight(ring_name=ring_name,
                                    weight=weight,
                                    disk_device=disk_device,
                                    ip=ip,
                                    port=port)
        else:
            content = rm.set_weight(policy_num=policy_num,
                                    weight=weight,
                                    disk_device=disk_device,
                                    ip=ip,
                                    port=port,
                                    policy=True)
        message = content
    except Exception, error:
        message = str(error)
        assert False
Exemplo n.º 2
0
def rebalance(cluster_name, datajson):
    """
    平衡环
    :param datajson:
    :return:
    """
    status = ''
    message = ''
    resp = {"status": status, "message": message}
    policy_num = ''
    ring_name = datajson.get('ring_name')
    ring_name = ring_name if ring_name.endswith(
        '.ring.gz') else ring_name + '.ring.gz'
    if 'object-' in ring_name:
        obj_ring, policy_num = ring_name.rstrip('.ring.gz').split('-')
    sfo_clu_nodes = SfoClusterNodesMethod.query_host_list_by_cluster_name(
        cluster_name)
    if not sfo_clu_nodes:
        raise ValueError('Not Master Node in %s' % cluster_name)
    # 建立task 任务
    rm = RingManager(cluster_name)
    try:
        if not policy_num:
            ring_name = ring_name.split('.')[0]
            content = rm.rebalance(ring_name=ring_name)
        else:
            content = rm.rebalance(policy_num=policy_num, policy=True)
    except Exception, error:
        status = 501
        message = str(error)
Exemplo n.º 3
0
def remove_disk_immediately(ring_name,
                            disk_device,
                            ip,
                            port,
                            policy_num,
                            cluster_name,
                            taskid=None):
    rm = RingManager(cluster_name)
    try:
        if not policy_num:
            ring_name = ring_name.split('.')[0]
            content = rm.remove_disk_immediately(ring_name=ring_name,
                                                 disk_device=disk_device,
                                                 ip=ip,
                                                 port=port)
        else:
            content = rm.remove_disk_immediately(disk_device=disk_device,
                                                 ip=ip,
                                                 port=port,
                                                 policy=True,
                                                 policy_num=policy_num)
        message = content
    except Exception, error:
        message = str(error)
        assert False
Exemplo n.º 4
0
def create_ring(cluster_name,
                ring_name,
                part_power,
                replicas,
                min_part_hours,
                policy_num,
                taskid=None):
    rm = RingManager(cluster_name)
    try:
        if policy_num:
            content = rm.create(part_power=part_power,
                                replicas=replicas,
                                min_part_hours=min_part_hours,
                                policy=True,
                                policy_num=policy_num)

        else:
            ring_name = ring_name.split('.')[0]
            content = rm.create(ring_name=ring_name,
                                part_power=part_power,
                                replicas=replicas,
                                min_part_hours=min_part_hours)

        message = content
    except Exception, error:
        message = str(error)
        assert False
Exemplo n.º 5
0
def give_away_ring(ring_name, cluster_name, taskid=None):
    rm = RingManager(cluster_name)
    try:
        content = rm.give_away_ring(cluster_name=cluster_name,
                                    ring_file=ring_name)
        message = content
    except Exception, error:
        message = str(error)
        assert False
Exemplo n.º 6
0
def add_disk_cluster(cluster_name, datajson):
    """
        向环中添加磁盘
        :param ringjson:
        :return:
        """
    status = ''
    message = ''
    resp = {"status": status, "message": message}
    policy_num = ''
    ip = datajson.get('ip')
    port = datajson.get('port')
    zone = datajson.get('zone')
    device = datajson.get('device')
    weight = datajson.get('weight')
    region = datajson.get('region')
    ring_name = datajson.get('ring_name')
    ring_name = ring_name if ring_name.endswith(
        '.ring.gz') else ring_name + '.ring.gz'
    if 'object-' in ring_name:
        obj_ring, policy_num = ring_name.rstrip('.ring.gz').split('-')
    replication_ip = datajson.get('replication_ip')
    replication_port = datajson.get('replication_port')
    sfo_clu_nodes = SfoClusterNodesMethod.query_host_list_by_cluster_name(
        cluster_name)
    if not sfo_clu_nodes:
        raise ValueError('Not Master Node in %s' % cluster_name)
    # 建立task 任务
    rm = RingManager(cluster_name)
    try:
        if not policy_num:
            ring_name = ring_name.split('.')[0]
            content = rm.add_disk_2_ring(ring_name=ring_name,
                                         region=region,
                                         zone=zone,
                                         ip=ip,
                                         port=port,
                                         disk_device=device,
                                         weight=weight,
                                         replication_ip=replication_ip,
                                         replication_port=replication_port)
        else:
            content = rm.add_disk_2_ring(region=region,
                                         zone=zone,
                                         ip=ip,
                                         port=port,
                                         disk_device=device,
                                         weight=weight,
                                         replication_ip=replication_ip,
                                         replication_port=replication_port,
                                         policy=True,
                                         policy_num=policy_num)

    except Exception, error:
        status = 501
        message = str(error)
Exemplo n.º 7
0
def rebalance(ring_name, policy_num, cluster_name, taskid=None):
    rm = RingManager(cluster_name)
    try:
        if not policy_num:
            ring_name = ring_name.split('.')[0]
            content = rm.rebalance(ring_name=ring_name)
        else:
            content = rm.rebalance(policy_num=policy_num, policy=True)
        message = content
    except Exception, error:
        message = str(error)
        assert False
Exemplo n.º 8
0
def get_ring_info(cluster_name, ring_name):
    status = ''
    message = ''
    data = ''
    resp = {"status": status, "data": data, "message": message}
    rm = RingManager(cluster_name)
    try:
        content = rm.ring_info(ring_name)
        status = 200
        message = 'OK'
        data = content
    except IOError, error:
        status = 500
        message = str(error)
Exemplo n.º 9
0
def add_disk_2_ring(ring_name,
                    region,
                    zone,
                    ip,
                    port,
                    disk_device,
                    weight,
                    replication_ip='',
                    replication_port='',
                    policy_num='',
                    cluster_name='',
                    taskid=None):

    rm = RingManager(cluster_name)
    try:
        if not policy_num:
            ring_name = ring_name.split('.')[0]
            content = rm.add_disk_2_ring(ring_name=ring_name,
                                         region=region,
                                         zone=zone,
                                         ip=ip,
                                         port=port,
                                         disk_device=disk_device,
                                         weight=weight,
                                         replication_ip=replication_ip,
                                         replication_port=replication_port)
        else:
            content = rm.add_disk_2_ring(region=region,
                                         zone=zone,
                                         ip=ip,
                                         port=port,
                                         disk_device=disk_device,
                                         weight=weight,
                                         replication_ip=replication_ip,
                                         replication_port=replication_port,
                                         policy=True,
                                         policy_num=policy_num)
        message = content
    except Exception, error:
        message = str(error)
        assert False
Exemplo n.º 10
0
def get_disk_list(cluster_name):
    """
    获取节点磁盘列表
    :param cluster_name:
    :return:
    """
    sfo_disks = []
    apply_result_list = []
    status = ''
    message = {}
    resp = {"status": status, "data": sfo_disks, "message": message}
    sfo_node = SfoClusterNodesMethod.query_host_list_by_cluster_name(cluster_name=cluster_name)
    rm = RingManager(cluster_name)
    try:
        ring_host_label_map = rm.used_map(cluster_name)
    except IOError:
        ring_host_label_map = {}
    pool = Pool(25)
    for node in sfo_node:
        apply_result = pool.apply_async(func=async_disk_operation, args=(node,))
        apply_result_list.append(apply_result)
    pool.close()
    pool.join()
    for apply_result in apply_result_list:
        apply_result_data = apply_result.get(timeout=1)
        if apply_result_data:
            for ring_name, ring_info in ring_host_label_map.items():
                for disk_infomation in apply_result_data:
                    for host_labels_dict in ring_info:
                        if (disk_infomation.host_ip == host_labels_dict['host_ip'] or disk_infomation.host_re_ip == host_labels_dict['host_ip']) and disk_infomation.label in host_labels_dict['labels']:
                            disk_infomation.is_used.append(ring_name)
            sfo_disks.extend(apply_result_data)
    if sfo_disks:
        status = 200
        message = 'OK'
        sfo_disks = sorted(sfo_disks, key=lambda x: x.host_name)
    else:
        status = 404
        message = 'Not Found Record'
    resp.update({"status": status, "data": sfo_disks, "message": message})
    return resp, status
Exemplo n.º 11
0
def add_ring_2cluster(cluster_name, ringjson):
    """
    添加环数据到数据库
    :param ringjson:
    :return:
    """
    status = ''
    message = ''
    resp = {"status": status, "message": message}
    policy_num = ''
    replicas = ringjson.get('replicas')
    part_power = ringjson.get('part_power')
    ring_name = ringjson.get('ring_name')
    ring_name = ring_name if ring_name.endswith(
        '.ring.gz') else ring_name + '.ring.gz'
    if 'object-' in ring_name:
        obj_ring, policy_num = ring_name.rstrip('.ring.gz').split('-')
    min_part_hours = ringjson.get('min_part_hours')
    sfo_clu_nodes = SfoClusterNodesMethod.query_host_list_by_cluster_name(
        cluster_name)
    if not sfo_clu_nodes:
        raise ValueError('Not Master Node in %s' % cluster_name)
    rm = RingManager(cluster_name)
    try:
        if policy_num:
            content = rm.create(part_power=part_power,
                                replicas=replicas,
                                min_part_hours=min_part_hours,
                                policy=True,
                                policy_num=policy_num)

        else:
            ring_name = ring_name.split('.')[0]
            content = rm.create(ring_name=ring_name,
                                part_power=part_power,
                                replicas=replicas,
                                min_part_hours=min_part_hours)
    except Exception, error:
        status = 501
        message = str(error)
Exemplo n.º 12
0
def get_ring_list(cluster_name):
    """
    获取环列表
    :param cluster_name:
    :return:
    """
    data = []
    sfo_rings = []
    status = ''
    message = ''
    resp = {"status": status, "data": data, "message": message}
    rm = RingManager(cluster_name=cluster_name)
    try:
        ring_map = rm.rings()
    except Exception:
        ring_map = {}
    for ring_name in ring_map:
        sfo_rings.append({
            "cluster_name":
            cluster_name,
            "ring_name":
            ring_name,
            "replicas":
            ring_map[ring_name].get('replicas'),
            "part_power":
            ring_map[ring_name].get('part_power'),
            "min_part_hours":
            ring_map[ring_name].get('min_part_hours'),
        })
    if sfo_rings:
        status = 200
        message = 'OK'
        data = sfo_rings
    else:
        status = 404
        message = 'Not Found Record'
    resp.update({"status": status, "data": data, "message": message})
    return resp, status
Exemplo n.º 13
0
def give_away_ring(cluster_name, datajson):
    """
        平衡环
        :param datajson:
        :return:
        """
    status = ''
    message = ''
    resp = {"status": status, "message": message}
    ring_name = datajson.get('ring_name')
    ring_name = ring_name if ring_name.endswith(
        '.ring.gz') else ring_name + '.ring.gz'
    sfo_clu_nodes = SfoClusterNodesMethod.query_host_list_by_cluster_name(
        cluster_name)
    if not sfo_clu_nodes:
        raise ValueError('Not Master Node in %s' % cluster_name)
    rm = RingManager(cluster_name)
    try:
        content = rm.give_away_ring(cluster_name=cluster_name,
                                    ring_file=ring_name)
    except Exception, error:
        message = str(error)
        status = 501
Exemplo n.º 14
0
    min_part_hours = policyjson.get('min_part_hours', '')
    pm = PolicyManager(cluster_name)
    try:
        content = pm.add(num=policy_num,
                         name=policy_name,
                         deprecated=deprecated,
                         policy_type=policy_type)
    except Exception, error:
        status = 501
        message = str(error)
    else:
        status = 201
        message = content
        if sync_policy_ring:
            try:
                rm = RingManager(cluster_name)
                content = rm.create(part_power=part_power,
                                    replicas=replicas,
                                    min_part_hours=min_part_hours,
                                    policy=True,
                                    policy_num=policy_num)
            except Exception, error:
                status = 501
                message = str(error)
            else:
                status = 201
                message = content
    resp.update({"status": status, "message": message})
    return resp, status