From 4b543c67529d8efe6db06678462d92ca76c0703a Mon Sep 17 00:00:00 2001 From: iSecloud <869820505@qq.com> Date: Tue, 19 Dec 2023 16:14:24 +0800 Subject: [PATCH] =?UTF-8?q?fix(backend):=20=E4=BF=AE=E6=94=B9ES=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E5=8F=82=E6=95=B0=20#2685?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configuration/handlers/password.py | 1 + .../mysql/resources/tendbcluster/query.py | 25 ++++++++++++------- .../mysql/resources/tendbha/query.py | 11 ++++++-- .../backend/ticket/builders/es/es_shrink.py | 3 ++- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/dbm-ui/backend/configuration/handlers/password.py b/dbm-ui/backend/configuration/handlers/password.py index 320e45ea7a..101c07acaf 100644 --- a/dbm-ui/backend/configuration/handlers/password.py +++ b/dbm-ui/backend/configuration/handlers/password.py @@ -79,6 +79,7 @@ def query_mysql_admin_password( if end_time: filters.update(end_time=end_time) + # 获取密码生效实例结果 mysql_admin_password_data = MySQLPrivManagerApi.get_mysql_admin_password(params=filters) mysql_admin_password_data["results"] = mysql_admin_password_data.pop("items") cloud_info = ResourceQueryHelper.search_cc_cloud(get_cache=True) diff --git a/dbm-ui/backend/db_services/mysql/resources/tendbcluster/query.py b/dbm-ui/backend/db_services/mysql/resources/tendbcluster/query.py index 25ad21d7c3..9627f76864 100644 --- a/dbm-ui/backend/db_services/mysql/resources/tendbcluster/query.py +++ b/dbm-ui/backend/db_services/mysql/resources/tendbcluster/query.py @@ -188,24 +188,31 @@ def _filter_instance_qs(query_conditions, query_params): ] # 获取remote实例的查询集 - remote_insts = StorageInstance.objects.annotate(role=F("instance_role"), inst_port=F("port")).filter( - query_conditions + remote_insts = ( + StorageInstance.objects.select_related("machine") + .prefetch_related("cluster") + .annotate(role=F("instance_role"), inst_port=F("port")) + .filter(query_conditions) ) # 获取spider实例的查询集 - spider_insts = ProxyInstance.objects.annotate( - role=F("tendbclusterspiderext__spider_role"), inst_port=F("port") - ).filter(query_conditions) + spider_insts = ( + ProxyInstance.objects.select_related("machine") + .prefetch_related("cluster") + .annotate(role=F("tendbclusterspiderext__spider_role"), inst_port=F("port")) + .filter(query_conditions) + ) # 涉及spider_controller的查询 if query_params.get("spider_ctl"): # 额外获取spider master混部的中控节点的查询集 这里有一点: 通过value和annotate来将admin_port覆写port,达到过滤效果 + filter_spider_master = Q(tendbclusterspiderext__spider_role=TenDBClusterSpiderRole.SPIDER_MASTER.value) controller_insts = ( - ProxyInstance.objects.values("admin_port") + ProxyInstance.objects.select_related("machine") + .prefetch_related("cluster") + .values("admin_port") .annotate(port=F("admin_port")) .values(role=Value("spider_ctl"), inst_port=F("admin_port")) - .filter( - query_conditions & Q(tendbclusterspiderext__spider_role=TenDBClusterSpiderRole.SPIDER_MASTER.value) - ) + .filter(query_conditions & filter_spider_master) ) # ⚠️这里的union不要用链式union(eg: s1.union(s2).union(s3)),否则django解析sql语句会生成额外的扩号导致mysql语法错误。 instances = remote_insts.union(spider_insts, controller_insts).values(*fields).order_by("-create_at") diff --git a/dbm-ui/backend/db_services/mysql/resources/tendbha/query.py b/dbm-ui/backend/db_services/mysql/resources/tendbha/query.py index 7354aa1d1f..0b30807ff6 100644 --- a/dbm-ui/backend/db_services/mysql/resources/tendbha/query.py +++ b/dbm-ui/backend/db_services/mysql/resources/tendbha/query.py @@ -115,9 +115,16 @@ def _filter_instance_qs(query_conditions, query_params): """获取过滤的queryset""" instances_qs = ( # 此处的实例视图需要同时得到 storage instance 和 proxy instance - StorageInstance.objects.annotate(role=F("instance_inner_role")) + StorageInstance.objects.select_related("machine") + .prefetch_related("cluster") + .annotate(role=F("instance_inner_role")) .filter(query_conditions) - .union(ProxyInstance.objects.annotate(role=F("access_layer")).filter(query_conditions)) + .union( + ProxyInstance.objects.annotate(role=F("access_layer")) + .select_related("machine") + .prefetch_related("cluster") + .filter(query_conditions) + ) .values( "id", "role", diff --git a/dbm-ui/backend/ticket/builders/es/es_shrink.py b/dbm-ui/backend/ticket/builders/es/es_shrink.py index 2c9d2b76b2..8299d6c5d0 100644 --- a/dbm-ui/backend/ticket/builders/es/es_shrink.py +++ b/dbm-ui/backend/ticket/builders/es/es_shrink.py @@ -46,7 +46,8 @@ def validate(self, attrs): all_shrink_hosts = [] all_exist_hosts = [] - for role in [InstanceRole.ES_DATANODE_HOT, InstanceRole.ES_DATANODE_COLD, InstanceRole.ES_CLIENT]: + # 暂时只需要校验hot节点至少保留一台,cold和client不校验 + for role in [InstanceRole.ES_DATANODE_HOT]: shrink_hosts = {host["bk_host_id"] for host in role_hash[role]} exist_hosts = set( cluster.storageinstance_set.filter(instance_role=role).values_list("machine__bk_host_id", flat=True)