Exemple #1
0
def delete_snapshot_nodes(cluster_name, node_path, recursive='0'):
    """删除快照节点所有信息,包括树结构信息(zd_snapshot_tree)和快照数据(zd_znode_snapshot)
    """
    if recursive == '0' and not _is_tree_leaf_node(cluster_name, node_path):
        return "无法删除非叶子节点!"

    node = ZdSnapshotTree.one(cluster_name=cluster_name, node_path=node_path)
    if not node:
        return "节点不存在!"

    # 删除快照节点,同时维护树结构
    slot_left, slot_right = node.left, node.right
    slot_width = slot_right - slot_left
    del_query = ZdSnapshotTree.delete().where(
        (ZdSnapshotTree.left.between(slot_left, slot_right)) &
        (ZdSnapshotTree.cluster_name == cluster_name)
    )
    update_left_query = ZdSnapshotTree.update(left=ZdSnapshotTree.left - slot_width).where(
        (ZdSnapshotTree.left > slot_right) &
        (ZdSnapshotTree.cluster_name == cluster_name)
    )
    update_right_query = ZdSnapshotTree.update(right=ZdSnapshotTree.right - slot_width).where(
        (ZdSnapshotTree.right > slot_right) &
        (ZdSnapshotTree.cluster_name == cluster_name)
    )
    del_query.execute()
    update_left_query.execute()
    update_right_query.execute()
    # 删除快照信息
    delete_snapshots(cluster_name, node_path, recursive)
Exemple #2
0
def make_snapshot(cluster_name, path, data=None):
    """生成快照,包括快照树结构信息(zd_snapshot_tree)和快照数据(zd_snapshot)
    """
    if data is None:
        data = ZookeeperService.get(cluster_name, path)

    # 验证节点路径是否已经存在,不存在才创建相应树结构节点
    if not _is_tree_node_exists(cluster_name, path):
        if path.strip() == "/":
            # 增加根节点
            _add_tree_root(cluster_name=cluster_name, node_path=path)
        else:
            # 增加子节点
            parent_path = _extract_parent_path(path)
            parent_node = ZdSnapshotTree.one(cluster_name=cluster_name, node_path=parent_path)
            if not parent_node:
                raise MakeSnapshotError("Parent node does not exists, could not build tree!")
            _add_tree_node(cluster_name=cluster_name,
                           parent_node=parent_node,
                           node_path=path)

    # 检验快照是否重复生成
    commit_md5 = hashlib.md5(data).hexdigest()
    if is_snapshot_redundant(cluster_name, path, commit_md5):
        log.warn("Snapshot already exists for znode in cluster: %s, path: %s", cluster_name, path)
        return
    # 保存快照信息
    snapshot = ZdSnapshot(cluster_name=cluster_name,
                          path=path,
                          data=data,
                          create_time=datetime.now(),
                          commit=commit_md5)
    snapshot.save()
Exemple #3
0
def delete_snapshot_nodes(cluster_name, node_path, recursive="0"):
    """删除快照节点所有信息,包括树结构信息(zd_snapshot_tree)和快照数据(zd_znode_snapshot)
    """
    if recursive == "0" and not _is_tree_leaf_node(cluster_name, node_path):
        return "无法删除非叶子节点!"

    node = ZdSnapshotTree.one(cluster_name=cluster_name, node_path=node_path)
    if not node:
        return "节点不存在!"

    # 删除快照节点,同时维护树结构
    slot_left, slot_right = node.left, node.right
    slot_width = slot_right - slot_left
    del_query = ZdSnapshotTree.delete().where(
        (ZdSnapshotTree.left.between(slot_left, slot_right)) & (ZdSnapshotTree.cluster_name == cluster_name)
    )
    update_left_query = ZdSnapshotTree.update(left=ZdSnapshotTree.left - slot_width).where(
        (ZdSnapshotTree.left > slot_right) & (ZdSnapshotTree.cluster_name == cluster_name)
    )
    update_right_query = ZdSnapshotTree.update(right=ZdSnapshotTree.right - slot_width).where(
        (ZdSnapshotTree.right > slot_right) & (ZdSnapshotTree.cluster_name == cluster_name)
    )
    del_query.execute()
    update_left_query.execute()
    update_right_query.execute()
    # 删除快照信息
    delete_snapshots(cluster_name, node_path, recursive)
Exemple #4
0
def make_snapshot(cluster_name, path, data=None):
    """生成快照,包括快照树结构信息(zd_snapshot_tree)和快照数据(zd_snapshot)
    """
    if data is None:
        data = ZookeeperService.get(cluster_name, path)

    # 验证节点路径是否已经存在,不存在才创建相应树结构节点
    if not _is_tree_node_exists(cluster_name, path):
        if path.strip() == "/":
            # 增加根节点
            _add_tree_root(cluster_name=cluster_name, node_path=path)
        else:
            # 增加子节点
            parent_path = _extract_parent_path(path)
            parent_node = ZdSnapshotTree.one(cluster_name=cluster_name, node_path=parent_path)
            if not parent_node:
                raise MakeSnapshotError("Parent node does not exists, could not build tree!")
            _add_tree_node(cluster_name=cluster_name, parent_node=parent_node, node_path=path)

    # 检验快照是否重复生成
    commit_md5 = hashlib.md5(data).hexdigest()
    if is_snapshot_redundant(cluster_name, path, commit_md5):
        log.warn("Snapshot already exists for znode in cluster: %s, path: %s", cluster_name, path)
        return
    # 保存快照信息
    snapshot = ZdSnapshot(
        cluster_name=cluster_name, path=path, data=data, create_time=datetime.now(), commit=commit_md5
    )
    snapshot.save()
Exemple #5
0
def _is_tree_leaf_node(cluster_name, node_path):
    """check if is leaf node
    """
    node = ZdSnapshotTree.one(cluster_name=cluster_name, node_path=node_path)
    if node and node.right == node.left + 1:
        return True
    else:
        return False
Exemple #6
0
def _is_tree_leaf_node(cluster_name, node_path):
    """check if is leaf node
    """
    node = ZdSnapshotTree.one(cluster_name=cluster_name, node_path=node_path)
    if node and node.right == node.left + 1:
        return True
    else:
        return False
Exemple #7
0
def _is_tree_node_exists(cluster_name, node_path):
    """check if node exists
    """
    return ZdSnapshotTree.one(cluster_name=cluster_name, node_path=node_path)
Exemple #8
0
def _is_tree_node_exists(cluster_name, node_path):
    """check if node exists
    """
    return ZdSnapshotTree.one(cluster_name=cluster_name, node_path=node_path)