Skip to content

Commit be742d1

Browse files
committed
feat(mysql): spider替换流程 #10105
1 parent 9ffa917 commit be742d1

File tree

13 files changed

+537
-372
lines changed

13 files changed

+537
-372
lines changed

dbm-ui/backend/db_meta/api/cluster/tendbcluster/handler.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from backend.flow.utils.cc_manage import CcManage
3636
from backend.flow.utils.mysql.mysql_module_operate import MysqlCCTopoOperator
3737
from backend.flow.utils.spider.spider_act_dataclass import ShardInfo
38+
from backend.flow.utils.spider.spider_bk_config import get_spider_version_and_charset
3839

3940

4041
class TenDBClusterClusterHandler(ClusterHandler):
@@ -172,26 +173,30 @@ def add_spiders(
172173
cls,
173174
cluster_id: int,
174175
creator: str,
175-
spider_version: str,
176176
add_spiders: list,
177177
spider_role: Optional[TenDBClusterSpiderRole],
178178
resource_spec: dict,
179179
is_slave_cluster_create: bool,
180180
new_slave_domain: str = None,
181+
new_db_module_id: int = 0,
181182
):
182183
"""
183184
对已有的集群添加spider的元信息
184185
因为从集群添加的行为spider-slave扩容行为基本类似,所以这里作为一个公共方法,对域名处理根据不同单据类型做不同的处理
185186
@param cluster_id: 待关联的集群id
186187
@param creator: 提单的用户名称
187-
@param spider_version: 待加入的spider版本号(包括小版本信息)
188188
@param add_spiders: 待加入的spider机器信息
189189
@param spider_role: 待加入spider的角色
190190
@param resource_spec: 待加入spider的规格
191191
@param is_slave_cluster_create: 代表这次是否是添加从集群
192192
@param new_slave_domain: 如果是添加从集群场景,这里代表新的从域名信息
193+
@param new_db_module_id: new_db_module_id代表新的模块ID,默认为0,代表延用cluster信息的模块ID做依据
193194
"""
194195
cluster = Cluster.objects.get(id=cluster_id)
196+
db_module_id = new_db_module_id if new_db_module_id else cluster.db_module_id
197+
spider_charset, spider_version = get_spider_version_and_charset(
198+
bk_biz_id=cluster.bk_biz_id, db_module_id=db_module_id
199+
)
195200

196201
# 录入机器
197202
machines = []

dbm-ui/backend/flow/engine/bamboo/scene/common/get_file_list.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def mysql_proxy_upgrade_package(self, pkg_id: str) -> list:
178178
f"{env.BKREPO_PROJECT}/{env.BKREPO_BUCKET}/{proxy_pkg.path}",
179179
]
180180

181-
def spider_upgrade_package(self, pkg_id: str) -> list:
181+
def spider_upgrade_package(self, pkg_id: int) -> list:
182182
"""
183183
spider 升级需要的安装包列表
184184
"""

dbm-ui/backend/flow/engine/bamboo/scene/spider/common/common_sub_flow.py

+40-28
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
CtlSwitchToSlaveKwargs,
5858
DropSpiderRoutingKwargs,
5959
)
60+
from backend.flow.utils.spider.spider_bk_config import get_spider_version_and_charset
6061
from backend.flow.utils.spider.spider_db_meta import SpiderDBMeta
6162

6263
"""
@@ -72,6 +73,8 @@ def add_spider_slaves_sub_flow(
7273
parent_global_data: dict,
7374
is_clone_user: bool = True,
7475
slave_domain: str = None,
76+
new_pkg_id: int = 0,
77+
new_db_module_id: int = 0,
7578
):
7679
"""
7780
定义对原有的TenDB cluster集群添加spider slave节点的公共子流程
@@ -82,7 +85,9 @@ def add_spider_slaves_sub_flow(
8285
@param root_id: flow流程的root_id
8386
@param parent_global_data: 本次子流程的对应上层流程的全局只读上下文
8487
@param uid: 单据id
85-
@param is_clone_user 是否克隆权限
88+
@param is_clone_user 是否克隆权限, 区分一些单据场景。
89+
@param new_pkg_id 如果是做升级部署,需要传新版本的介质包,默认为0,表示不升级部署
90+
@param new_db_module_id 如果是做升级部署,需要传新的DB模块ID,默认为0,表示不升级部署
8691
"""
8792
tdbctl_pass = get_random_string(length=10)
8893

@@ -99,7 +104,15 @@ def add_spider_slaves_sub_flow(
99104
)[0]
100105

101106
parent_global_data["spider_ports"] = [tmp_spider.port]
102-
parent_global_data["db_module_id"] = cluster.db_module_id
107+
# 获取版本和字符集信息
108+
parent_global_data["db_module_id"] = new_db_module_id if new_db_module_id else cluster.db_module_id
109+
parent_global_data["spider_charset"], parent_global_data["spider_version"] = get_spider_version_and_charset(
110+
bk_biz_id=cluster.bk_biz_id, db_module_id=parent_global_data["db_module_id"]
111+
)
112+
# spider slave 不安装备份程序,只解压
113+
parent_global_data["untar_only"] = True
114+
115+
# 声明子流程
103116
sub_pipeline = SubBuilder(root_id=root_id, data=parent_global_data)
104117

105118
# 拼接执行原子任务活动节点需要的通用的私有参数结构体, 减少代码重复率,但引用时注意内部参数值传递的问题
@@ -125,20 +138,21 @@ def add_spider_slaves_sub_flow(
125138
)
126139

127140
# 阶段1 下发spider安装介质包
128-
if parent_global_data.get("pkg_id"):
129-
pkg_id = parent_global_data["pkg_id"]
141+
if new_pkg_id:
142+
# 代表升级部署,根据新的pkg_id下发介质包
130143
sub_pipeline.add_act(
131144
act_name=_("下发spider安装介质"),
132145
act_component_code=TransFileComponent.code,
133146
kwargs=asdict(
134147
DownloadMediaKwargs(
135148
bk_cloud_id=cluster.bk_cloud_id,
136149
exec_ip=[ip_info["ip"] for ip_info in add_spider_slaves],
137-
file_list=GetFileList(db_type=DBType.MySQL).spider_upgrade_package(pkg_id=pkg_id),
150+
file_list=GetFileList(db_type=DBType.MySQL).spider_upgrade_package(pkg_id=new_pkg_id),
138151
)
139152
),
140153
)
141154
else:
155+
# 根据继承的版本名称,或者介质包
142156
sub_pipeline.add_act(
143157
act_name=_("下发spider安装介质"),
144158
act_component_code=TransFileComponent.code,
@@ -260,6 +274,8 @@ def add_spider_masters_sub_flow(
260274
uid: str,
261275
parent_global_data: dict,
262276
is_add_spider_mnt: bool,
277+
new_pkg_id: int = 0,
278+
new_db_module_id: int = 0,
263279
):
264280
"""
265281
定义对原有的TenDB cluster集群添加spider master节点的公共子流程
@@ -271,14 +287,24 @@ def add_spider_masters_sub_flow(
271287
@param parent_global_data: 本次子流程的对应上层流程的全局只读上下文
272288
@param is_add_spider_mnt: 表示这次添加spider 运维节点,如果是则True,不是则False
273289
@param uid: 单据uid
290+
@param new_pkg_id 如果是做升级部署,需要传新版本的介质包,默认为0,表示不升级部署
291+
@param new_db_module_id 如果是做升级部署,需要传新的DB模块ID,默认为0,表示不升级部署
274292
"""
275293
tag = "mnt"
276294
tdbctl_pass = get_random_string(length=10)
277295

278296
# 获取到集群对应的spider端口,作为这次的安装
279297
parent_global_data["spider_ports"] = [cluster.proxyinstance_set.first().port]
280298
parent_global_data["ctl_port"] = cluster.proxyinstance_set.first().admin_port
281-
parent_global_data["db_module_id"] = cluster.db_module_id
299+
300+
# 获取版本和字符集信息
301+
parent_global_data["db_module_id"] = new_db_module_id if new_db_module_id else cluster.db_module_id
302+
parent_global_data["spider_charset"], parent_global_data["spider_version"] = get_spider_version_and_charset(
303+
bk_biz_id=cluster.bk_biz_id, db_module_id=parent_global_data["db_module_id"]
304+
)
305+
parent_global_data["ctl_charset"] = parent_global_data["spider_charset"]
306+
307+
# 声明子流程
282308
sub_pipeline = SubBuilder(root_id=root_id, data=parent_global_data)
283309

284310
# 拼接执行原子任务活动节点需要的通用的私有参数结构体, 减少代码重复率,但引用时注意内部参数值传递的问题
@@ -303,16 +329,15 @@ def add_spider_masters_sub_flow(
303329
)
304330
)
305331
# 阶段1 下发spider安装介质包
306-
if parent_global_data.get("pkg_id"):
307-
pkg_id = parent_global_data["pkg_id"]
332+
if new_pkg_id:
308333
sub_pipeline.add_act(
309334
act_name=_("下发spider安装介质"),
310335
act_component_code=TransFileComponent.code,
311336
kwargs=asdict(
312337
DownloadMediaKwargs(
313338
bk_cloud_id=cluster.bk_cloud_id,
314339
exec_ip=[ip_info["ip"] for ip_info in add_spider_masters],
315-
file_list=GetFileList(db_type=DBType.MySQL).spider_upgrade_package(pkg_id=pkg_id),
340+
file_list=GetFileList(db_type=DBType.MySQL).spider_upgrade_package(pkg_id=new_pkg_id),
316341
)
317342
),
318343
)
@@ -344,17 +369,11 @@ def add_spider_masters_sub_flow(
344369
acts_list = []
345370
for spider in get_spider_master_incr(cluster, add_spider_masters):
346371
exec_act_kwargs.exec_ip = spider["ip"]
347-
if parent_global_data.get("pkg_id"):
348-
exec_act_kwargs.cluster = {
349-
"immutable_domain": cluster.immute_domain,
350-
"auto_incr_value": spider["incr_number"],
351-
"pkg_id": parent_global_data["pkg_id"],
352-
}
353-
else:
354-
exec_act_kwargs.cluster = {
355-
"immutable_domain": cluster.immute_domain,
356-
"auto_incr_value": spider["incr_number"],
357-
}
372+
exec_act_kwargs.cluster = {
373+
"immutable_domain": cluster.immute_domain,
374+
"auto_incr_value": spider["incr_number"],
375+
"pkg_id": new_pkg_id,
376+
}
358377
exec_act_kwargs.get_mysql_payload_func = MysqlActPayload.get_install_spider_payload.__name__
359378
acts_list.append(
360379
{
@@ -797,21 +816,14 @@ def reduce_ctls_routing(root_id: str, parent_global_data: dict, cluster: Cluster
797816

798817
if reduce_ctl_primary:
799818
# 选择新节点作为primary,过滤待回收的节点
800-
all_ctl = cluster.proxyinstance_set.filter(
801-
tendbclusterspiderext__spider_role=TenDBClusterSpiderRole.SPIDER_MASTER
802-
)
803-
804-
# 因为ctl集群是采用GTID+半同步数据同步,所以理论上选择任意一个从节点作为主,数据不会丢失
805-
new_ctl_primary = all_ctl.exclude(machine__ip__in=[ip_info.machine.ip for ip_info in reduce_ctls]).first()
806-
807819
sub_pipeline.add_act(
808820
act_name=_("切换ctl中控集群"),
809821
act_component_code=CtlSwitchToSlaveComponent.code,
810822
kwargs=asdict(
811823
CtlSwitchToSlaveKwargs(
812824
cluster_id=cluster.id,
813825
reduce_ctl_primary=reduce_ctl_primary,
814-
new_ctl_primary=f"{new_ctl_primary.machine.ip}{IP_PORT_DIVIDER}{new_ctl_primary.admin_port}",
826+
reduce_ctl_secondary_list=reduce_ctl_secondary_list,
815827
)
816828
),
817829
)

dbm-ui/backend/flow/engine/bamboo/scene/spider/spider_add_mnt.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from backend.flow.plugins.components.collections.spider.spider_db_meta import SpiderDBMetaComponent
2727
from backend.flow.utils.mysql.mysql_act_dataclass import DBMetaOPKwargs
2828
from backend.flow.utils.mysql.mysql_context_dataclass import SystemInfoContext
29-
from backend.flow.utils.spider.spider_bk_config import get_spider_version_and_charset
3029
from backend.flow.utils.spider.spider_db_meta import SpiderDBMeta
3130

3231
logger = logging.getLogger("flow")
@@ -67,14 +66,7 @@ def add_spider_mnt(self):
6766
raise ClusterNotExistException(
6867
cluster_id=info["cluster_id"], bk_biz_id=int(self.data["bk_biz_id"]), message=_("集群不存在")
6968
)
70-
# 通过bk—config获取版本号和字符集信息
71-
# 获取的是业务默认配置,不一定是集群当前配置
72-
spider_charset, spider_version = get_spider_version_and_charset(
73-
bk_biz_id=cluster.bk_biz_id, db_module_id=cluster.db_module_id
74-
)
75-
# 补充这次单据需要的隐形参数,spider版本以及字符集
76-
sub_flow_context["spider_charset"] = spider_charset
77-
sub_flow_context["spider_version"] = spider_version
69+
7870
# 启动子流程
7971
sub_pipeline = SubBuilder(root_id=self.root_id, data=copy.deepcopy(sub_flow_context))
8072
# 阶段1 根据场景执行添加spider-mnt子流程

dbm-ui/backend/flow/engine/bamboo/scene/spider/spider_add_nodes.py

+57-40
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from backend.flow.plugins.components.collections.spider.spider_db_meta import SpiderDBMetaComponent
2929
from backend.flow.utils.mysql.mysql_act_dataclass import DBMetaOPKwargs
3030
from backend.flow.utils.mysql.mysql_context_dataclass import SystemInfoContext
31-
from backend.flow.utils.spider.spider_bk_config import get_spider_version_and_charset
3231
from backend.flow.utils.spider.spider_db_meta import SpiderDBMeta
3332

3433
logger = logging.getLogger("flow")
@@ -57,53 +56,67 @@ def add_spider_nodes(self):
5756
pipeline = Builder(root_id=self.root_id, data=self.data)
5857
sub_pipelines = []
5958
for info in self.data["infos"]:
60-
# 拼接子流程需要全局参数
61-
sub_flow_context = copy.deepcopy(self.data)
62-
sub_flow_context.pop("infos")
63-
64-
# 获取对应集群相关对象
65-
try:
66-
cluster = Cluster.objects.get(id=info["cluster_id"], bk_biz_id=int(self.data["bk_biz_id"]))
67-
except Cluster.DoesNotExist:
68-
raise ClusterNotExistException(
69-
cluster_id=info["cluster_id"], bk_biz_id=int(self.data["bk_biz_id"]), message=_("集群不存在")
59+
sub_pipelines.append(
60+
self.add_spider_nodes_with_cluster(
61+
cluster_id=info["cluster_id"],
62+
add_spider_role=info["add_spider_role"],
63+
add_spider_hosts=info["spider_ip_list"],
7064
)
71-
72-
# 根据集群去bk-config获取对应spider版本和字符集
73-
spider_charset, spider_version = get_spider_version_and_charset(
74-
bk_biz_id=cluster.bk_biz_id, db_module_id=cluster.db_module_id
7565
)
7666

77-
# 拼接子流程的全局参数
78-
sub_flow_context.update(info)
67+
pipeline.add_parallel_sub_pipeline(sub_flow_list=sub_pipelines)
68+
pipeline.run_pipeline(init_trans_data_class=SystemInfoContext())
7969

80-
# 补充这次单据需要的隐形参数,spider版本以及字符集
81-
sub_flow_context["spider_charset"] = spider_charset
82-
sub_flow_context["spider_version"] = spider_version
70+
def add_spider_nodes_with_cluster(
71+
self,
72+
cluster_id: int,
73+
add_spider_role: TenDBClusterSpiderRole,
74+
add_spider_hosts: list,
75+
new_db_module_id: int = 0,
76+
new_pkg_id: int = 0,
77+
):
78+
"""
79+
定义添加节点的子流程
80+
"""
8381

84-
if info["add_spider_role"] == TenDBClusterSpiderRole.SPIDER_MASTER:
82+
# 获取对应集群相关对象
83+
try:
84+
cluster = Cluster.objects.get(id=cluster_id, bk_biz_id=int(self.data["bk_biz_id"]))
85+
except Cluster.DoesNotExist:
86+
raise ClusterNotExistException(
87+
cluster_id=cluster_id, bk_biz_id=int(self.data["bk_biz_id"]), message=_("集群不存在")
88+
)
8589

86-
# 加入spider-master 子流程
87-
sub_flow_context["ctl_charset"] = spider_charset
88-
sub_pipelines.append(self.add_spider_master_notes(sub_flow_context, cluster))
90+
# 补充这次单据需要的隐形参数,spider版本以及字符集
91+
sub_flow_context = {
92+
"uid": self.data["uid"],
93+
"bk_biz_id": cluster.bk_biz_id,
94+
"cluster_id": cluster.id,
95+
"created_by": self.data["created_by"],
96+
"ticket_type": self.data["ticket_type"],
97+
"spider_ip_list": add_spider_hosts,
98+
"new_db_module_id": new_db_module_id,
99+
}
89100

90-
elif info["add_spider_role"] == TenDBClusterSpiderRole.SPIDER_SLAVE:
101+
if add_spider_role == TenDBClusterSpiderRole.SPIDER_MASTER:
91102

92-
# 加入spider-slave 子流程
93-
sub_pipelines.append(self.add_spider_slave_notes(sub_flow_context, cluster))
103+
# 加入spider-master 子流程
104+
return self.add_spider_master_notes(sub_flow_context, cluster, new_db_module_id, new_pkg_id)
94105

95-
else:
96-
# 理论上不会出现,出现就中断这次流程构造
97-
raise NormalSpiderFlowException(
98-
message=_("[{}]This type of role addition is not supported".format(info["add_spider_role"]))
99-
)
100-
if not sub_pipelines:
101-
raise NormalSpiderFlowException(message=_("build spider-add-nodes-pipeline failed"))
106+
elif add_spider_role == TenDBClusterSpiderRole.SPIDER_SLAVE:
102107

103-
pipeline.add_parallel_sub_pipeline(sub_flow_list=sub_pipelines)
104-
pipeline.run_pipeline(init_trans_data_class=SystemInfoContext())
108+
# 加入spider-slave 子流程
109+
return self.add_spider_slave_notes(sub_flow_context, cluster, new_db_module_id, new_pkg_id)
110+
111+
else:
112+
# 理论上不会出现,出现就中断这次流程构造
113+
raise NormalSpiderFlowException(
114+
message=_("[{}]This type of role addition is not supported".format(add_spider_role))
115+
)
105116

106-
def add_spider_master_notes(self, sub_flow_context: dict, cluster: Cluster):
117+
def add_spider_master_notes(
118+
self, sub_flow_context: dict, cluster: Cluster, new_db_module_id: int = 0, new_pkg_id: int = 0
119+
):
107120
"""
108121
定义spider master集群部署子流程
109122
目前产品形态 spider专属一套集群,所以流程只支持spider单机单实例安装
@@ -121,6 +134,8 @@ def add_spider_master_notes(self, sub_flow_context: dict, cluster: Cluster):
121134
uid=sub_flow_context["uid"],
122135
parent_global_data=sub_flow_context,
123136
is_add_spider_mnt=False,
137+
new_db_module_id=new_db_module_id,
138+
new_pkg_id=new_pkg_id,
124139
)
125140
)
126141

@@ -144,13 +159,13 @@ def add_spider_master_notes(self, sub_flow_context: dict, cluster: Cluster):
144159
)
145160
return sub_pipeline.build_sub_process(sub_name=_("[{}]添加spider-master节点流程".format(cluster.name)))
146161

147-
def add_spider_slave_notes(self, sub_flow_context: dict, cluster: Cluster):
162+
def add_spider_slave_notes(
163+
self, sub_flow_context: dict, cluster: Cluster, new_db_module_id: int = 0, new_pkg_id: int = 0
164+
):
148165
"""
149166
添加spider-slave节点的子流程流程逻辑
150167
必须集群存在从集群,才能添加
151168
"""
152-
# spider slave 不安装备份程序,只解压
153-
sub_flow_context["untar_only"] = True
154169

155170
sub_pipeline = SubBuilder(root_id=self.root_id, data=copy.deepcopy(sub_flow_context))
156171

@@ -162,6 +177,8 @@ def add_spider_slave_notes(self, sub_flow_context: dict, cluster: Cluster):
162177
root_id=self.root_id,
163178
uid=sub_flow_context["uid"],
164179
parent_global_data=copy.deepcopy(sub_flow_context),
180+
new_db_module_id=new_db_module_id,
181+
new_pkg_id=new_pkg_id,
165182
)
166183
)
167184
# 阶段2 变更db_meta数据

0 commit comments

Comments
 (0)