From 7a9fa33fc770af7d43d3d282ccf47cfb00693706 Mon Sep 17 00:00:00 2001 From: ycggyao Date: Tue, 7 Jan 2025 17:51:20 +0800 Subject: [PATCH] =?UTF-8?q?fix(backend):=20mongo=E7=BC=A9=E5=AE=B9shard?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E6=95=B0=E5=8D=8F=E8=AE=AE=E5=8F=98=E6=9B=B4?= =?UTF-8?q?=20#8939=20#=20Reviewed,=20transaction=20id:=2028694?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flow/utils/clear_machine_script.py | 9 ++++ dbm-ui/backend/ticket/builders/__init__.py | 44 ++++++++++--------- .../mongodb/mongo_reduce_shard_nodes.py | 32 +++++++++----- 3 files changed, 54 insertions(+), 31 deletions(-) diff --git a/dbm-ui/backend/flow/utils/clear_machine_script.py b/dbm-ui/backend/flow/utils/clear_machine_script.py index fa433c2198..5e209fbced 100644 --- a/dbm-ui/backend/flow/utils/clear_machine_script.py +++ b/dbm-ui/backend/flow/utils/clear_machine_script.py @@ -232,6 +232,13 @@ echo "Step 6 completed" """ +mongo_clear_machine_script = """ +rm -rf /data1/mongodata /data/mongodata /data/mongolog /data/dbbak/* /data/install +rm -rf /usr/local/mongodb /usr/local/mongodb-linux* +rm -rf /home/mysql/dbmon /home/mysql/filebeat-deploy /home/mysql/install /home/mysql/prometheus +rm -rf /home/mysql/log /home/mysql/bk-dbmon /home/mysql/dbtools /home/mysql/dbareport +""" + db_type_script_map = { DBType.MySQL.value: mysql_clear_machine_script, DBType.TenDBCluster.value: mysql_clear_machine_script, @@ -242,6 +249,7 @@ DBType.Pulsar.value: pulsar_clear_machine_script, DBType.Doris.value: doris_clear_machine_script, DBType.Vm.value: vm_clear_machine_script, + DBType.MongoDB.value: mongo_clear_machine_script, } db_type_account_user_map = { @@ -254,4 +262,5 @@ DBType.Pulsar.value: DBA_ROOT_USER, DBType.Doris.value: DBA_ROOT_USER, DBType.Vm.value: DBA_ROOT_USER, + DBType.MongoDB.value: DBA_ROOT_USER, } diff --git a/dbm-ui/backend/ticket/builders/__init__.py b/dbm-ui/backend/ticket/builders/__init__.py index e59132eb72..126f8b1ef5 100644 --- a/dbm-ui/backend/ticket/builders/__init__.py +++ b/dbm-ui/backend/ticket/builders/__init__.py @@ -291,14 +291,16 @@ def build_controller_info(self) -> dict: return super().build_controller_info() def format_ticket_data(self): - self.ticket_data = { - "clear_hosts": self.ticket_data["recycle_hosts"], - "ip_dest": self.ip_dest, - # 一批机器的操作系统类型一致,任取一个即可 - "os_name": self.ticket_data["recycle_hosts"][0]["os_name"], - "os_type": self.ticket_data["recycle_hosts"][0]["os_type"], - "db_type": self.ticket.group, - } + self.ticket_data.update( + { + "clear_hosts": self.ticket_data["recycle_hosts"], + "ip_dest": self.ip_dest, + # 一批机器的操作系统类型一致,任取一个即可 + "os_name": self.ticket_data["recycle_hosts"][0]["os_name"], + "os_type": self.ticket_data["recycle_hosts"][0]["os_type"], + "db_type": self.ticket.group, + } + ) def post_callback(self): # 转移到故障池,记录机器事件(如果是资源池则资源导入后会记录) @@ -325,18 +327,20 @@ def __init__(self, ticket: Ticket): def format_ticket_data(self): recycle_hosts = self.ticket_data["recycle_hosts"] # 我们认为,在资源申请的情况下,不会混用多个集群类型 - self.ticket_data = { - "ticket_id": self.ticket.id, - "for_biz": self.ticket_data["ip_recycle"]["for_biz"], - "resource_type": self.ticket.group, - "os_type": recycle_hosts[0]["bk_os_type"], - "hosts": recycle_hosts, - "operator": self.ticket.creator, - # 标记为退回 - "return_resource": True, - # 要查询主机实际的业务管控 - "bk_biz_id": recycle_hosts[0]["bk_host_id"], - } + self.ticket_data.update( + { + "ticket_id": self.ticket.id, + "for_biz": self.ticket_data["ip_recycle"]["for_biz"], + "resource_type": self.ticket.group, + "os_type": recycle_hosts[0]["bk_os_type"], + "hosts": recycle_hosts, + "operator": self.ticket.creator, + # 标记为退回 + "return_resource": True, + # 要查询主机实际的业务管控 + "bk_biz_id": recycle_hosts[0]["bk_biz_id"], + } + ) def pre_callback(self): # 在run的时候才会生成task id,此时要更新到资源池参数里面 diff --git a/dbm-ui/backend/ticket/builders/mongodb/mongo_reduce_shard_nodes.py b/dbm-ui/backend/ticket/builders/mongodb/mongo_reduce_shard_nodes.py index bb8e20809f..3f3506ff52 100644 --- a/dbm-ui/backend/ticket/builders/mongodb/mongo_reduce_shard_nodes.py +++ b/dbm-ui/backend/ticket/builders/mongodb/mongo_reduce_shard_nodes.py @@ -18,6 +18,7 @@ from backend.db_meta.enums import ClusterType from backend.db_meta.models import AppCache, Cluster from backend.flow.engine.controller.mongodb import MongoDBController +from backend.flow.utils.mongodb.shard_reduce_node_get_host import get_hosts_reduce_node from backend.ticket import builders from backend.ticket.builders.common.base import HostRecycleSerializer from backend.ticket.builders.mongodb.base import BaseMongoDBOperateDetailSerializer, BaseMongoDBTicketFlowBuilder @@ -51,22 +52,31 @@ class MongoDBReduceShardNodesFlowParamBuilder(builders.FlowParamBuilder): def format_ticket_data(self): bk_biz_id = self.ticket_data["bk_biz_id"] self.ticket_data["bk_app_abbr"] = AppCache.objects.get(bk_biz_id=bk_biz_id).db_app_abbr - cluster_ids = [cluster_id for info in self.ticket_data["infos"] for cluster_id in info["cluster_ids"]] - id__cluster = {cluster.id: cluster for cluster in Cluster.objects.filter(id__in=cluster_ids)} + + +@builders.BuilderFactory.register(TicketType.MONGODB_REDUCE_SHARD_NODES, is_recycle=True) +class MongoDBAddMongosApplyFlowBuilder(BaseMongoDBTicketFlowBuilder): + serializer = MongoDBReduceShardNodesDetailSerializer + inner_flow_builder = MongoDBReduceShardNodesFlowParamBuilder + inner_flow_name = _("MongoDB 缩容Shard节点数执行") + need_patch_recycle_host_details = True + + def patch_old_shard_nodes(self): mongo_type__apply_infos: Dict[str, List] = defaultdict(list) - for info in self.ticket_data["infos"]: + cluster_ids = [cluster_id for info in self.ticket.details["infos"] for cluster_id in info["cluster_ids"]] + id__cluster = {cluster.id: cluster for cluster in Cluster.objects.filter(id__in=cluster_ids)} + for info in self.ticket.details["infos"]: cluster = id__cluster[info["cluster_ids"][0]] info["db_version"] = cluster.major_version cluster_type = cluster.cluster_type if cluster_type == ClusterType.MongoShardedCluster.value: info["cluster_id"] = info.pop("cluster_ids")[0] mongo_type__apply_infos[cluster_type].append(info) + for cluster_type in [ClusterType.MongoReplicaSet.value, ClusterType.MongoShardedCluster.value]: + mongo_type__apply_infos.setdefault(cluster_type, []) + self.ticket.details["infos"] = mongo_type__apply_infos + self.ticket.details["infos"]["old_nodes"] = {"reduced_shard_hosts": get_hosts_reduce_node(self.ticket.details)} - self.ticket_data["infos"] = mongo_type__apply_infos - - -@builders.BuilderFactory.register(TicketType.MONGODB_REDUCE_SHARD_NODES) -class MongoDBAddMongosApplyFlowBuilder(BaseMongoDBTicketFlowBuilder): - serializer = MongoDBReduceShardNodesDetailSerializer - inner_flow_builder = MongoDBReduceShardNodesFlowParamBuilder - inner_flow_name = _("MongoDB 缩容Shard节点数执行") + def patch_ticket_detail(self): + self.patch_old_shard_nodes() + super().patch_ticket_detail()