From 1f70b2c1f4ab3b4c018f537db19937ce62214280 Mon Sep 17 00:00:00 2001 From: yyhenryyy Date: Thu, 19 Dec 2024 19:02:44 +0800 Subject: [PATCH] =?UTF-8?q?fix(mongodb):=20=E5=89=AF=E6=9C=AC=E9=9B=86?= =?UTF-8?q?=E5=8D=95=E5=AE=9E=E4=BE=8B=E9=83=A8=E7=BD=B2=EF=BC=8C=E5=88=86?= =?UTF-8?q?=E7=89=87=E9=9B=86=E7=BE=A4=E5=88=86=E7=89=87=E5=8D=95=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B=E9=83=A8=E7=BD=B2=20#8734?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/cluster/nosqlcomm/create_instances.py | 7 ++- .../flow/utils/mongodb/calculate_cluster.py | 6 ++- .../flow/utils/mongodb/mongodb_dataclass.py | 48 ++++++++++++++----- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/dbm-ui/backend/db_meta/api/cluster/nosqlcomm/create_instances.py b/dbm-ui/backend/db_meta/api/cluster/nosqlcomm/create_instances.py index 8ac5a781b6..fd7f139c5f 100644 --- a/dbm-ui/backend/db_meta/api/cluster/nosqlcomm/create_instances.py +++ b/dbm-ui/backend/db_meta/api/cluster/nosqlcomm/create_instances.py @@ -37,6 +37,7 @@ def create_mongo_mutil_instances(bk_biz_id, bk_cloud_id, machine_type, inst_pair # 找到主节点 if storage["role"] == InstanceRole.MONGO_M1: primary = storage + for storage in inst_pair["nodes"]: if storage["role"] != InstanceRole.MONGO_M1: tuples.append( @@ -47,7 +48,8 @@ def create_mongo_mutil_instances(bk_biz_id, bk_cloud_id, machine_type, inst_pair ) machine.create(machines=list(machines.values()), bk_cloud_id=bk_cloud_id) storage_instance.create(instances=instances) - storage_instance_tuple.create(tuples) + if tuples: + storage_instance_tuple.create(tuples) except Exception as e: logger.error(traceback.format_exc()) raise e @@ -89,7 +91,8 @@ def create_mongo_instances( if not skip_machine: machine.create(machines=list(machines.values()), bk_cloud_id=bk_cloud_id) storage_instance.create(instances=instances) - storage_instance_tuple.create(tuple) + if tuple: + storage_instance_tuple.create(tuple) except Exception as e: logger.error(traceback.format_exc()) raise e diff --git a/dbm-ui/backend/flow/utils/mongodb/calculate_cluster.py b/dbm-ui/backend/flow/utils/mongodb/calculate_cluster.py index 674c739ca8..8b8cc8d5d6 100644 --- a/dbm-ui/backend/flow/utils/mongodb/calculate_cluster.py +++ b/dbm-ui/backend/flow/utils/mongodb/calculate_cluster.py @@ -227,8 +227,12 @@ def cluster_calc(payload: dict, payload_clusters: dict, app: str) -> dict: nodes = [{"ip": machine["ip"], "bk_cloud_id": machine["bk_cloud_id"]} for machine in machines] replica_set["nodes"] = nodes shards.append(replica_set) + if node_count > 1: + add_shard_nodes = nodes[0:-1] + else: + add_shard_nodes = nodes add_shards[replica_set["set_id"]] = ",".join( - ["{}:{}".format(node["ip"], str(replica_set["port"])) for node in nodes[0:-1]] + ["{}:{}".format(node["ip"], str(replica_set["port"])) for node in add_shard_nodes] ) payload_clusters["shards"] = shards diff --git a/dbm-ui/backend/flow/utils/mongodb/mongodb_dataclass.py b/dbm-ui/backend/flow/utils/mongodb/mongodb_dataclass.py index 856067a58d..a3a94c8174 100644 --- a/dbm-ui/backend/flow/utils/mongodb/mongodb_dataclass.py +++ b/dbm-ui/backend/flow/utils/mongodb/mongodb_dataclass.py @@ -583,6 +583,7 @@ def get_add_relationship_to_meta_kwargs(self, replicaset_info: dict) -> dict: info["bk_cloud_id"] = self.payload["config"]["nodes"][0]["bk_cloud_id"] info["machine_specs"] = self.payload["machine_specs"] info["immute_domain"] = self.payload["mongos"]["domain"] + node_count = self.payload["node_count"] # mongos info["proxies"] = [ {"ip": node["ip"], "port": self.payload["mongos"]["port"]} for node in self.payload["mongos"]["nodes"] @@ -595,11 +596,7 @@ def get_add_relationship_to_meta_kwargs(self, replicaset_info: dict) -> dict: } if len(self.payload["config"]["nodes"]) <= 11: for index, node in enumerate(self.payload["config"]["nodes"]): - if index == len(self.payload["config"]["nodes"]) - 1: - config["nodes"].append( - {"ip": node["ip"], "port": self.payload["config"]["port"], "role": self.instance_role[-1]} - ) - else: + if node_count == 1: config["nodes"].append( { "ip": node["ip"], @@ -607,6 +604,23 @@ def get_add_relationship_to_meta_kwargs(self, replicaset_info: dict) -> dict: "role": self.instance_role[index], } ) + elif node_count > 1: + if index == len(self.payload["config"]["nodes"]) - 1: + config["nodes"].append( + { + "ip": node["ip"], + "port": self.payload["config"]["port"], + "role": self.instance_role[-1], + } + ) + else: + config["nodes"].append( + { + "ip": node["ip"], + "port": self.payload["config"]["port"], + "role": self.instance_role[index], + } + ) info["configs"].append(config) # shard @@ -618,14 +632,19 @@ def get_add_relationship_to_meta_kwargs(self, replicaset_info: dict) -> dict: } if len(shard["nodes"]) <= 11: for index, node in enumerate(shard["nodes"]): - if index == len(shard["nodes"]) - 1: - storage["nodes"].append( - {"role": self.instance_role[-1], "ip": node["ip"], "port": shard["port"]} - ) - else: + if node_count == 1: storage["nodes"].append( {"role": self.instance_role[index], "ip": node["ip"], "port": shard["port"]} ) + elif node_count > 1: + if index == len(shard["nodes"]) - 1: + storage["nodes"].append( + {"role": self.instance_role[-1], "ip": node["ip"], "port": shard["port"]} + ) + else: + storage["nodes"].append( + {"role": self.instance_role[index], "ip": node["ip"], "port": shard["port"]} + ) info["storages"].append(storage) return info @@ -985,7 +1004,8 @@ def get_cluster_info_deinstall(self, cluster_id: int): "instance_role": member.role, } ) - nodes.append(backup_node) + if len(cluster_info.get_shards()[0].members) > 1: + nodes.append(backup_node) self.payload["nodes"] = nodes elif cluster_info.cluster_type == ClusterType.MongoShardedCluster.value: mongos = cluster_info.get_mongos() @@ -1019,7 +1039,8 @@ def get_cluster_info_deinstall(self, cluster_id: int): "instance_role": member.role, } ) - nodes.append(backup_node) + if len(shard.members) > 1: + nodes.append(backup_node) shard_info["nodes"] = nodes shards_nodes.append(shard_info) backup_node = {} @@ -1040,7 +1061,8 @@ def get_cluster_info_deinstall(self, cluster_id: int): "instance_role": member.role, } ) - config_nodes.append(backup_node) + if len(config.members) > 1: + config_nodes.append(backup_node) self.payload["mongos_nodes"] = mongos_nodes self.payload["shards_nodes"] = shards_nodes self.payload["config_nodes"] = config_nodes