Skip to content

Commit

Permalink
feat(mongodb): mongodb分片集群列表接口添加主机聚合数据结构查询 #7481
Browse files Browse the repository at this point in the history
# Reviewed, transaction id: 21450
  • Loading branch information
WytheLi authored and zhangzhw8 committed Oct 22, 2024
1 parent 7eadc9b commit 7bf9a9f
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions dbm-ui/backend/db_services/dbbase/resources/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@
from backend.constants import IP_PORT_DIVIDER
from backend.db_meta.enums import ClusterEntryType, ClusterType, InstanceRole
from backend.db_meta.enums.comm import SystemTagEnum
from backend.db_meta.models import AppCache, Cluster, ClusterEntry, DBModule, Machine, ProxyInstance, StorageInstance
from backend.db_meta.models import (
AppCache,
Cluster,
ClusterEntry,
DBModule,
Machine,
ProxyInstance,
StorageInstance,
StorageInstanceTuple,
)
from backend.db_services.dbbase.instances.handlers import InstanceHandler
from backend.db_services.dbbase.resources.query_base import (
build_q_for_domain_by_cluster,
Expand Down Expand Up @@ -457,10 +466,20 @@ def _filter_cluster_hook(
if count == 0:
return ResourceList(count=0, data=[])

storage_instance_queryset = StorageInstance.objects.prefetch_related(
Prefetch(
"as_ejector",
queryset=StorageInstanceTuple.objects.filter(
ejector__in=storage_queryset.values_list("id", flat=True)
),
to_attr="instance_tuples",
)
)
# 预取proxy_queryset,storage_queryset,clusterentry_set,加块查询效率
cluster_queryset = cluster_queryset[offset : limit + offset].prefetch_related(
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_instance_queryset, to_attr="storage_instances"),
Prefetch("nosqlstoragesetdtl_set", to_attr="storage_set_dtl"),
Prefetch("clusterentry_set", to_attr="entries"),
"tag_set",
Expand All @@ -487,7 +506,7 @@ def _filter_cluster_hook(

# 将集群的查询结果序列化为集群字典信息
clusters: List[Dict[str, Any]] = []
for cluster in cluster_queryset:
for cluster in cluster_list:
cluster_info = cls._to_cluster_representation(
cluster=cluster,
cluster_entry=[
Expand Down Expand Up @@ -534,13 +553,29 @@ def _to_cluster_representation(
(storage_set_dtl.seg_range, f"{storage_set_dtl.instance.machine.ip}:{storage_set_dtl.instance.port}")
for storage_set_dtl in cluster.storage_set_dtl
]

machine_map = {}
for group_name, machine_ip_port in machine_list:
if not machine_map.get(group_name):
machine_map[group_name] = [machine_ip_port]
else:
machine_map[group_name].append(machine_ip_port)

master_slave_map = {}
for instance in cluster.storage_instances:
for instance_tuple in instance.instance_tuples:
key = f"{instance.machine.ip}:{instance.port}"
item = f"{instance_tuple.receiver.machine.ip}:{instance_tuple.receiver.port}"
if not master_slave_map.get(key):
master_slave_map[key] = [item]
else:
master_slave_map[key].append(item)

for k, v in master_slave_map.items():
for group_name, ip_port_list in machine_map.items():
if k in ip_port_list:
machine_map[group_name].extend(v)

return {
"id": cluster.id,
"phase": cluster.phase,
Expand Down

0 comments on commit 7bf9a9f

Please sign in to comment.