Skip to content

Commit

Permalink
fix(mongodb): 副本集单实例部署,分片集群分片单实例部署 TencentBlueKing#8734
Browse files Browse the repository at this point in the history
  • Loading branch information
yyhenryyy committed Dec 19, 2024
1 parent ee2bb07 commit 1f70b2c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion dbm-ui/backend/flow/utils/mongodb/calculate_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 35 additions & 13 deletions dbm-ui/backend/flow/utils/mongodb/mongodb_dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -595,18 +596,31 @@ 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"],
"port": self.payload["config"]["port"],
"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
Expand All @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 = {}
Expand All @@ -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
Expand Down

0 comments on commit 1f70b2c

Please sign in to comment.