Skip to content

Commit

Permalink
fix(backend): mongo缩容shard节点数协议变更 #8939
Browse files Browse the repository at this point in the history
# Reviewed, transaction id: 28694
  • Loading branch information
ygcyao authored and iSecloud committed Jan 20, 2025
1 parent 4200058 commit 7a9fa33
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 31 deletions.
9 changes: 9 additions & 0 deletions dbm-ui/backend/flow/utils/clear_machine_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = {
Expand All @@ -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,
}
44 changes: 24 additions & 20 deletions dbm-ui/backend/ticket/builders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
# 转移到故障池,记录机器事件(如果是资源池则资源导入后会记录)
Expand All @@ -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,此时要更新到资源池参数里面
Expand Down
32 changes: 21 additions & 11 deletions dbm-ui/backend/ticket/builders/mongodb/mongo_reduce_shard_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 7a9fa33

Please sign in to comment.