Skip to content

Commit

Permalink
fix(backend): 修复mysql proxy的聚合逻辑 #8223
Browse files Browse the repository at this point in the history
  • Loading branch information
iSecloud committed Nov 27, 2024
1 parent ec0f506 commit 4b6fb1d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
20 changes: 17 additions & 3 deletions dbm-ui/backend/ticket/builders/mysql/mysql_proxy_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,25 @@ def validate(self, attrs):
class MysqlProxyAddParamBuilder(builders.FlowParamBuilder):
controller = MySQLController.mysql_proxy_add_scene

@classmethod
def merge_same_proxy_clusters(cls, infos):
"""聚合增加相同的proxy的集群"""
add_proxy_cluster_map = {}
for info in infos:
proxy = info["proxy_ip"]
if proxy["bk_host_id"] not in add_proxy_cluster_map:
add_proxy_cluster_map[proxy["bk_host_id"]] = {**info, "cluster_ids": []}
add_proxy_cluster_map[proxy["bk_host_id"]]["cluster_ids"].extend(info["cluster_ids"])
return list(add_proxy_cluster_map.values())

def format_ticket_data(self):
if self.ticket_data["ip_source"] == IpSource.RESOURCE_POOL:
return

for info in self.ticket_data["infos"]:
info["proxy_ip"] = info["new_proxy"]
# 聚合集群
infos = self.merge_same_proxy_clusters(self.ticket_data["infos"])
self.ticket_data["infos"] = infos


class MysqlProxyAddResourceParamBuilder(BaseOperateResourceParamBuilder):
Expand All @@ -67,13 +80,14 @@ def post_callback(self):
for info in ticket_data["infos"]:
info["new_proxy"] = info.pop("new_proxy")[0]
info["proxy_ip"] = info["new_proxy"]

# 聚合集群
infos = MysqlProxyAddParamBuilder.merge_same_proxy_clusters(ticket_data["infos"])
next_flow.details["ticket_data"]["infos"] = infos
next_flow.save(update_fields=["details"])


@builders.BuilderFactory.register(TicketType.MYSQL_PROXY_ADD, is_apply=True)
class MysqlProxyAddFlowBuilder(BaseMySQLHATicketFlowBuilder):
serializer = MysqlProxyAddDetailSerializer
inner_flow_builder = MysqlProxyAddParamBuilder
inner_flow_name = _("添加PROXY执行")
resource_batch_apply_builder = MysqlProxyAddResourceParamBuilder
20 changes: 18 additions & 2 deletions dbm-ui/backend/ticket/builders/mysql/mysql_proxy_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,26 @@ def validate(self, attrs):
class MysqlProxySwitchParamBuilder(builders.FlowParamBuilder):
controller = MySQLController.mysql_proxy_switch_scene

@classmethod
def merge_same_proxy_clusters(cls, infos):
"""聚合替换相同的proxy的集群"""
switch_proxy_cluster_map = {}
for info in infos:
switch_key = f"{info['origin_proxy_ip']['bk_host_id']}--{info['target_proxy_ip']['bk_host_id']}"
if switch_key not in switch_proxy_cluster_map:
switch_proxy_cluster_map[switch_key] = {**info, "cluster_ids": []}
switch_proxy_cluster_map[switch_key]["cluster_ids"].extend(info["cluster_ids"])
return list(switch_proxy_cluster_map.values())

def format_ticket_data(self):
for info in self.ticket_data["infos"]:
info["origin_proxy_ip"] = info["origin_proxy"]
if self.ticket_data["ip_source"] == IpSource.MANUAL_INPUT:
info["target_proxy_ip"] = info["target_proxy"]
# 聚合集群
if self.ticket_data["ip_source"] == IpSource.MANUAL_INPUT:
infos = self.merge_same_proxy_clusters(self.ticket_data["infos"])
self.ticket_data["infos"] = infos


class MysqlProxySwitchResourceParamBuilder(BaseOperateResourceParamBuilder):
Expand All @@ -86,15 +101,16 @@ def post_callback(self):
for info in ticket_data["infos"]:
info["target_proxy"] = info.pop("target_proxy")[0]
info["target_proxy_ip"] = info["target_proxy"]

# 聚合集群
infos = MysqlProxySwitchParamBuilder.merge_same_proxy_clusters(ticket_data["infos"])
next_flow.details["ticket_data"]["infos"] = infos
next_flow.save(update_fields=["details"])


@builders.BuilderFactory.register(TicketType.MYSQL_PROXY_SWITCH, is_apply=True)
class MysqlProxySwitchFlowBuilder(BaseMySQLHATicketFlowBuilder):
serializer = MysqlProxySwitchDetailSerializer
inner_flow_builder = MysqlProxySwitchParamBuilder
inner_flow_name = _("替换PROXY执行")
resource_batch_apply_builder = MysqlProxySwitchResourceParamBuilder
retry_type = FlowRetryType.MANUAL_RETRY
pause_node_builder = MySQLBasePauseParamBuilder

0 comments on commit 4b6fb1d

Please sign in to comment.