Skip to content

Commit

Permalink
fix(backend): redis集群列表展示优化 TencentBlueKing#8416
Browse files Browse the repository at this point in the history
# Reviewed, transaction id: 25876
  • Loading branch information
ygcyao committed Dec 4, 2024
1 parent a2d201f commit 59b12c5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
6 changes: 5 additions & 1 deletion dbm-ui/backend/db_services/dbbase/resources/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,11 @@ def _filter_cluster_hook(
# 预取proxy_queryset,storage_queryset,clusterentry_set,加块查询效率
cluster_list = cluster_queryset[offset : limit + offset].prefetch_related(
Prefetch("proxyinstance_set", queryset=proxy_queryset.select_related("machine"), to_attr="proxies"),
Prefetch("storageinstance_set", queryset=storage_queryset.select_related("machine"), to_attr="storages"),
Prefetch(
"storageinstance_set",
queryset=storage_queryset.select_related("machine").prefetch_related("as_ejector", "as_receiver"),
to_attr="storages",
),
Prefetch("clusterentry_set", to_attr="entries"),
"tag_set",
)
Expand Down
47 changes: 41 additions & 6 deletions dbm-ui/backend/db_services/redis/resources/redis_cluster/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,44 @@ def _to_cluster_representation(
**kwargs,
) -> Dict[str, Any]:
"""集群序列化"""
redis_master = [m.simple_desc for m in cluster.storages if m.instance_role == InstanceRole.REDIS_MASTER]
redis_slave = [m.simple_desc for m in cluster.storages if m.instance_role == InstanceRole.REDIS_SLAVE]
machine_list = list(set([inst["bk_host_id"] for inst in [*redis_master, *redis_slave]]))
# 创建一个字典来存储 ejector_id 到cluster.storages下标的映射
ejector_id_to_index = {}

# 遍历 cluster.storages,获取每个 StorageInstance 的下标和 id
for index, storage_instance in enumerate(cluster.storages):
ejector_id_to_index[storage_instance.id] = index

remote_infos = {InstanceRole.REDIS_MASTER.value: [], InstanceRole.REDIS_SLAVE.value: []}
for inst in cluster.storages:
try:
seg_range = inst.nosqlstoragesetdtl_set.all()[0].seg_range
except IndexError:
# 异常处理 因nosqlstoragesetdtl的分片数据只有redis为master才有seg_range值 以下处理是slave找出对应master seg_range并赋予值 供主从对应排序处理
ejector_id = inst.as_receiver.first().ejector_id
index = ejector_id_to_index.get(ejector_id)
if index is not None:
seg_range = cluster.storages[index].nosqlstoragesetdtl_set.all()[0].seg_range
else:
seg_range = "-1:-1"
except Exception:
# 如果无法找到seg_range,则默认为-1:-1。有可能实例处于restoring状态(比如集群容量变更时)
seg_range = "-1:-1"

remote_infos[inst.instance_role].append({**inst.simple_desc, "seg_range": seg_range})

remote_infos[InstanceRole.REDIS_MASTER.value].sort(key=lambda x: int(x.get("seg_range", -1).split("-")[0]))
remote_infos[InstanceRole.REDIS_SLAVE.value].sort(key=lambda x: int(x.get("seg_range", -1).split("-")[0]))
machine_list = list(
set(
[
inst["bk_host_id"]
for inst in [
*remote_infos[InstanceRole.REDIS_MASTER.value],
*remote_infos[InstanceRole.REDIS_SLAVE.value],
]
]
)
)
machine_pair_cnt = len(machine_list) / 2

# 补充集群的规格和容量信息
Expand Down Expand Up @@ -163,9 +198,9 @@ def _to_cluster_representation(
"cluster_capacity": cluster_capacity,
"dns_to_clb": dns_to_clb,
"proxy": [m.simple_desc for m in cluster.proxies],
"redis_master": redis_master,
"redis_slave": redis_slave,
"cluster_shard_num": len(redis_master),
"redis_master": remote_infos[InstanceRole.REDIS_MASTER.value],
"redis_slave": remote_infos[InstanceRole.REDIS_SLAVE.value],
"cluster_shard_num": len(remote_infos[InstanceRole.REDIS_MASTER.value]),
"machine_pair_cnt": machine_pair_cnt,
"module_names": module_names,
}
Expand Down

0 comments on commit 59b12c5

Please sign in to comment.