Skip to content

Commit

Permalink
feat(mysql): mysql、tendbcluster集群列表展示优化 TencentBlueKing#6686
Browse files Browse the repository at this point in the history
# Reviewed, transaction id: 17816
  • Loading branch information
peterxucai committed Sep 9, 2024
1 parent 6c13511 commit 4b337f8
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
56 changes: 55 additions & 1 deletion dbm-ui/backend/db_meta/models/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""

import itertools
import json
import logging
from datetime import datetime, timezone
Expand Down Expand Up @@ -419,6 +419,60 @@ def enable_dbha(self):
ClusterDBHAExt.objects.filter(cluster=self).delete()
self.refresh_from_db()

@classmethod
def get_cluster_id__primary_address_map(cls, cluster_ids: List[int]) -> Dict[int, str]:
"""
通过集群id列表批量
查询并返回 tendbcluster 的中控 primary
集群类型不是 TenDBCluster 时会抛出异常
返回值是 {cluster_id:"ip:port"} 形式的字典
"""
clusters = cls.objects.filter(id__in=cluster_ids).order_by("bk_cloud_id")

cluster_id__primary_address_map = {}
ctl_address__cluster_id_map = {}

grouped_clusters = itertools.groupby(clusters, key=lambda x: x.bk_cloud_id)
for bk_cloud_id, group in grouped_clusters:
addresses = []
for cluster in group:
if cluster.cluster_type != ClusterType.TenDBCluster.value:
raise DBMetaException(message=_("{} 类型集群没有中控节点".format(cluster.cluster_type)))

spider_instance = cluster.proxyinstance_set.filter(
tendbclusterspiderext__spider_role=TenDBClusterSpiderRole.SPIDER_MASTER
).first() # 随便拿一个spider-master接入层

ctl_address = "{}{}{}".format(spider_instance.machine.ip, IP_PORT_DIVIDER, spider_instance.port + 1000)
addresses.append(ctl_address)
ctl_address__cluster_id_map[ctl_address] = cluster.id

logger.info("addresses: {}".format(addresses))

res = DRSApi.rpc(
{
"addresses": addresses,
"cmds": ["tdbctl get primary"],
"force": False,
"bk_cloud_id": bk_cloud_id,
}
)
logger.info("tdbctl get primary res: {}".format(res))

for item in res:

if item["error_msg"]:
raise DBMetaException(message=_("get primary failed: {}".format(res[0]["error_msg"])))

primary_info_table_data = item["cmd_results"][0]["table_data"]
if primary_info_table_data:
cluster_id__primary_address_map[ctl_address__cluster_id_map[item["address"]]] = "{}{}{}".format(
primary_info_table_data[0]["HOST"], IP_PORT_DIVIDER, primary_info_table_data[0]["PORT"]
)
else:
cluster_id__primary_address_map[ctl_address__cluster_id_map[item["address"]]] = item["address"]
return cluster_id__primary_address_map


class ClusterDBHAExt(AuditedModel):
"""
Expand Down
6 changes: 6 additions & 0 deletions dbm-ui/backend/db_meta/models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ def find_storage_instance_by_ip(cls, addresses: List[Union[str, Dict]]):
address_filters = reduce(operator.or_, [Q(machine__ip=address) for address in addresses])
return cls.objects.select_related("machine").filter(address_filters)

@property
def simple_desc(self):
desc = super().simple_desc
desc["is_stand_by"] = self.is_stand_by
return desc


class ProxyInstance(InstanceMixin, AuditedModel):
version = models.CharField(max_length=64, default="", help_text=_("版本号"), blank=True, null=True)
Expand Down
24 changes: 23 additions & 1 deletion dbm-ui/backend/db_services/mysql/resources/tendbcluster/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _filter_cluster_hook(
remote_spec_map = {
spec.spec_id: spec for spec in Spec.objects.filter(spec_cluster_type=ClusterType.TenDBCluster)
}
return super()._filter_cluster_hook(
cluster_infos = super()._filter_cluster_hook(
bk_biz_id,
cluster_queryset,
proxy_queryset,
Expand All @@ -97,6 +97,27 @@ def _filter_cluster_hook(
**kwargs,
)

# spider_master增加primary字段
try:
cluster_id__primary_address_map = Cluster.get_cluster_id__primary_address_map(
[cluster_info["id"] for cluster_info in cluster_infos.data]
)
except Exception:
cluster_id__primary_address_map = {}

for cluster_info in cluster_infos.data:
primary_address = cluster_id__primary_address_map.get(cluster_info["id"])
for spider_master in cluster_info["spider_master"]:
if not primary_address:
spider_master["is_primary"] = False
continue
if spider_master["ip"] in primary_address:
spider_master["is_primary"] = True
else:
spider_master["is_primary"] = False

return cluster_infos

@classmethod
def _to_cluster_representation(
cls,
Expand Down Expand Up @@ -134,6 +155,7 @@ def get_remote_infos(insts: List[StorageInstance]):
role: [inst.simple_desc for inst in cluster.proxies if inst.tendbclusterspiderext.spider_role == role]
for role in TenDBClusterSpiderRole.get_values()
}

remote_db, remote_dr = get_remote_infos(cluster.storages)
# 计算machine分组
machine_list = list(set([inst["bk_host_id"] for inst in [*remote_db, *remote_dr]]))
Expand Down

0 comments on commit 4b337f8

Please sign in to comment.