Skip to content

Commit

Permalink
feat(mongodb): cluster扩缩容容接入层 TencentBlueKing#3364
Browse files Browse the repository at this point in the history
  • Loading branch information
yyhenryyy committed Mar 1, 2024
1 parent 7006381 commit 887d79d
Show file tree
Hide file tree
Showing 13 changed files with 380 additions and 21 deletions.
5 changes: 1 addition & 4 deletions dbm-services/go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ github.com/rs/xid v1.2.1 h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc=
github.com/rs/zerolog v1.15.0 h1:uPRuwkWF4J6fGsJ2R0Gn2jB1EQiav9k3S6CSdygQJXY=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58 h1:nlG4Wa5+minh3S9LVFtNoY+GVRiudA2e3EVfcCi3RCA=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f h1:UFr9zpz4xgTnIE5yIMtWAMngCdZ9p/+q6lTbgelo80M=
github.com/safchain/ethtool v0.0.0-20210803160452-9aa261dae9b1 h1:ZFfeKAhIQiiOrQaI3/znw0gOmYpO28Tcu1YaqMa/jtQ=
github.com/sagikazarmark/crypt v0.10.0 h1:96E1qrToLBU6fGzo+PRRz7KGOc9FkYFiPnR3/zf8Smg=
Expand Down Expand Up @@ -1017,7 +1018,6 @@ golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWP
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220511200225-c6db032c6c88/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/image v0.0.0-20210216034530-4410531fe030 h1:lP9pYkih3DUSC641giIXa2XqfTIbbbRr0w2EOTA7wHA=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug=
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028 h1:4+4C/Iv2U4fMZBiMCc98MG1In4gJY5YRhtpDNeDeHWs=
Expand Down Expand Up @@ -1051,8 +1051,6 @@ golang.org/x/oauth2 v0.11.0/go.mod h1:LdF7O/8bLR/qWK9DrpXmbHLTouvRHK0SgJl0GmDBch
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190130150945-aca44879d564/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190922100055-0a153f010e69/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -1068,7 +1066,6 @@ golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220908150016-7ac13a9a928d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
import logging.config
from typing import Dict, Optional

from backend.flow.engine.bamboo.scene.common.builder import Builder
from backend.flow.engine.bamboo.scene.mongodb.sub_task import increase_mongos, reduce_mongos
from backend.flow.utils.mongodb.mongodb_dataclass import ActKwargs

logger = logging.getLogger("flow")


class ScaleMongoSFlow(object):
"""cluster增加或减少mongos的flow"""

def __init__(self, root_id: str, data: Optional[Dict]):
"""
传入参数
@param root_id : 任务流程定义的root_id
@param data : 单据传递过来的参数列表,是dict格式
"""

self.root_id = root_id
self.data = data
self.get_kwargs = ActKwargs()
self.get_kwargs.payload = data
self.get_kwargs.get_file_path()

def multi_cluster_mongos_flow(self, increase: bool):
"""
增加或减少mongos流程
"""

# 创建流程实例
pipeline = Builder(root_id=self.root_id, data=self.data)

# 根据不同的cluster进行增加mongos——子流程并行
sub_pipelines = []
for cluster in self.data["infos"]:
if increase:
sub_pipline = increase_mongos(
root_id=self.root_id, ticket_data=self.data, sub_kwargs=self.get_kwargs, info=cluster
)
else:
sub_pipline = reduce_mongos(
root_id=self.root_id, ticket_data=self.data, sub_kwargs=self.get_kwargs, info=cluster
)
sub_pipelines.append(sub_pipline)
pipeline.add_parallel_sub_pipeline(sub_flow_list=sub_pipelines)

# 运行流程
pipeline.run_pipeline()
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ def cluster_install_flow(self):

# mongos安装——子流程并行
self.get_kwargs.mongos_info = self.data["mongos"]
sub_pipline = mongos_install(root_id=self.root_id, ticket_data=self.data, sub_kwargs=self.get_kwargs)
sub_pipline = mongos_install(
root_id=self.root_id, ticket_data=self.data, sub_kwargs=self.get_kwargs, increase_mongos=False
)
pipeline.add_sub_pipeline(sub_flow=sub_pipline)

# 添加shard到cluster
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
from .cluster_replace import cluster_replace
from .deinstall import deinstall
from .exec_script import exec_script
from .increase_mongos import increase_mongos
from .instance_restart import instance_restart
from .mongod_replace import mongod_replace
from .mongos_install import mongos_install
from .mongos_replace import mongos_replace
from .reduce_mongos import reduce_mongos
from .replicaset_install import replicaset_install
from .replicaset_replace import replicaset_replace
from .user import user
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def mongo_deinstall_parallel(sub_get_kwargs: ActKwargs, nodes: list, instance_ty
return acts_list


def deinstall(root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKwargs, cluster_id: int) -> SubBuilder:
def deinstall(
root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKwargs, cluster_id: int, reduce_mongos: bool
) -> SubBuilder:
"""
单个cluster卸载流程
"""
Expand All @@ -54,7 +56,8 @@ def deinstall(root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKwargs,
sub_pipeline = SubBuilder(root_id=root_id, data=ticket_data)

# 获取单个cluster信息
sub_get_kwargs.get_cluster_info_deinstall(cluster_id=cluster_id)
if not reduce_mongos:
sub_get_kwargs.get_cluster_info_deinstall(cluster_id=cluster_id)

# mongo卸载
# 复制集卸载
Expand All @@ -65,7 +68,8 @@ def deinstall(root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKwargs,
nodes=sub_get_kwargs.payload["nodes"],
instance_type=MongoDBInstanceType.MongoD.value,
)
sub_pipeline.add_parallel_acts(acts_list=acts_list)
if acts_list:
sub_pipeline.add_parallel_acts(acts_list=acts_list)
# cluster卸载
elif sub_get_kwargs.payload["cluster_type"] == ClusterType.MongoShardedCluster.value:
# mongos卸载——并行
Expand All @@ -74,7 +78,8 @@ def deinstall(root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKwargs,
nodes=sub_get_kwargs.payload["mongos_nodes"],
instance_type=MongoDBInstanceType.MongoS.value,
)
sub_pipeline.add_parallel_acts(acts_list=acts_list)
if acts_list:
sub_pipeline.add_parallel_acts(acts_list=acts_list)
# shard卸载——并行
many_acts_list = []
for shard_nodes in sub_get_kwargs.payload["shards_nodes"]:
Expand All @@ -84,14 +89,16 @@ def deinstall(root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKwargs,
instance_type=MongoDBInstanceType.MongoD.value,
)
many_acts_list.extend(acts_list)
sub_pipeline.add_parallel_acts(acts_list=many_acts_list)
if many_acts_list:
sub_pipeline.add_parallel_acts(acts_list=many_acts_list)
# config卸载——并行
acts_list = mongo_deinstall_parallel(
sub_get_kwargs=sub_get_kwargs,
nodes=sub_get_kwargs.payload["config_nodes"],
instance_type=MongoDBInstanceType.MongoD.value,
)
sub_pipeline.add_parallel_acts(acts_list=acts_list)
if acts_list:
sub_pipeline.add_parallel_acts(acts_list=acts_list)

# 删除dns
kwargs = sub_get_kwargs.get_delete_domain_kwargs()
Expand All @@ -110,11 +117,12 @@ def deinstall(root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKwargs,
)

# 删除db_meta关系
kwargs = {"cluster_id": cluster_id}
sub_pipeline.add_act(
act_name=_("MongoDB-删除db_meta关系"),
act_component_code=ExecDeletePasswordFromDBOperationComponent.code,
kwargs=kwargs,
)
if not reduce_mongos:
kwargs = {"cluster_id": cluster_id}
sub_pipeline.add_act(
act_name=_("MongoDB-删除db_meta关系"),
act_component_code=ExecDeletePasswordFromDBOperationComponent.code,
kwargs=kwargs,
)

return sub_pipeline.build_sub_process(sub_name=_("MongoDB--卸载--cluster_id:{}".format(str(cluster_id))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""

from copy import deepcopy
from typing import Dict, Optional

from django.utils.translation import ugettext as _

from backend.flow.engine.bamboo.scene.common.builder import SubBuilder
from backend.flow.plugins.components.collections.mongodb.add_domain_to_dns import ExecAddDomainToDnsOperationComponent
from backend.flow.plugins.components.collections.mongodb.exec_actuator_job import ExecuteDBActuatorJobComponent
from backend.flow.plugins.components.collections.mongodb.mongos_scale_4_meta import MongosScaleMetaComponent
from backend.flow.plugins.components.collections.mongodb.send_media import ExecSendMediaOperationComponent
from backend.flow.utils.mongodb.mongodb_dataclass import ActKwargs

from .mongos_install import mongos_install


def increase_mongos(root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKwargs, info: dict) -> SubBuilder:
"""
cluster 增加mongos流程
info 表示cluster信息
"""

# 获取变量
sub_get_kwargs = deepcopy(sub_kwargs)

# 创建子流程
sub_pipeline = SubBuilder(root_id=root_id, data=ticket_data)

# 设置参数
sub_get_kwargs.payload["db_version"] = info["db_version"]

# 获取机器
sub_get_kwargs.get_host_scale_mongos(info=info, increase=True)

# 介质下发
kwargs = sub_get_kwargs.get_send_media_kwargs()
sub_pipeline.add_act(
act_name=_("MongoDB-介质下发"), act_component_code=ExecSendMediaOperationComponent.code, kwargs=kwargs
)

# 创建原子任务执行目录
kwargs = sub_get_kwargs.get_create_dir_kwargs()
sub_pipeline.add_act(
act_name=_("MongoDB-创建原子任务执行目录"), act_component_code=ExecuteDBActuatorJobComponent.code, kwargs=kwargs
)

# 获取信息
sub_get_kwargs.get_cluster_info_deinstall(cluster_id=info["cluster_id"])
sub_get_kwargs.payload["cluster_id"] = info["cluster_id"]
sub_get_kwargs.cluster_type = sub_get_kwargs.payload["cluster_type"]
sub_get_kwargs.mongos_info = info
sub_get_kwargs.mongos_info["nodes"] = info["mongos"]
sub_get_kwargs.mongos_info["port"] = sub_get_kwargs.payload["mongos_nodes"][0]["port"]
sub_get_kwargs.payload["config"] = {}
sub_get_kwargs.payload["config"]["nodes"] = sub_get_kwargs.payload["config_nodes"]
sub_get_kwargs.payload["config"]["port"] = sub_get_kwargs.payload["config_nodes"][0]["port"]
sub_get_kwargs.payload["app"] = sub_get_kwargs.payload["db_app_abbr"]
cluster_name = info["cluster_name"]
sub_get_kwargs.mongos_info["set_id"] = cluster_name
sub_get_kwargs.db_main_version = str(info["db_version"].split(".")[0])
sub_get_kwargs.payload["key_file"] = sub_get_kwargs.get_key_file(cluster_name=cluster_name)
sub_get_kwargs.payload["nodes"] = []
sub_get_kwargs.payload["nodes"].append(sub_get_kwargs.payload["mongos_nodes"][0])
sub_get_kwargs.payload["mongos"] = {}
sub_get_kwargs.payload["mongos"]["nodes"] = info["mongos"]
sub_get_kwargs.payload["mongos"]["domain"] = sub_get_kwargs.payload["mongos_nodes"][0]["domain"]
sub_get_kwargs.payload["mongos"]["port"] = sub_get_kwargs.payload["mongos_nodes"][0]["port"]

# 进行mongos安装——子流程
sub_sub_pipeline = mongos_install(
root_id=root_id,
ticket_data=ticket_data,
sub_kwargs=sub_get_kwargs,
increase_mongos=True,
)
sub_pipeline.add_sub_pipeline(sub_sub_pipeline)

# dns新增实例
kwargs = sub_get_kwargs.get_add_domain_to_dns_kwargs(cluster=True)
sub_pipeline.add_act(
act_name=_("MongoDB--添加domain到dns"),
act_component_code=ExecAddDomainToDnsOperationComponent.code,
kwargs=kwargs,
)

# 修改db_meta数据
kwargs = sub_get_kwargs.get_change_meta_mongos_kwargs(increase=True)
sub_pipeline.add_act(
act_name=_("MongoDB--修改meta"),
act_component_code=MongosScaleMetaComponent.code,
kwargs=kwargs,
)

return sub_pipeline.build_sub_process(sub_name=_("MongoDB--{}增加mongos".format(cluster_name)))
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
from backend.flow.utils.mongodb.mongodb_dataclass import ActKwargs


def mongos_install(root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKwargs) -> SubBuilder:
def mongos_install(
root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKwargs, increase_mongos: bool
) -> SubBuilder:
"""
多个mongos安装流程
"""
Expand All @@ -33,7 +35,7 @@ def mongos_install(root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKwa
# 创建子流程
sub_pipeline = SubBuilder(root_id=root_id, data=ticket_data)

# mongod安装——并行
# mongos安装——并行
acts_list = []
for node in sub_get_kwargs.mongos_info["nodes"]:
kwargs = sub_get_kwargs.get_install_mongos_kwargs(node=node, replace=False)
Expand All @@ -56,6 +58,10 @@ def mongos_install(root_id: str, ticket_data: Optional[Dict], sub_kwargs: ActKwa
],
info=sub_get_kwargs.mongos_info,
)
# 增加mongos获取密码
if increase_mongos:
kwargs["create"] = False
kwargs = sub_get_kwargs.get_password_from_db(info=kwargs)
sub_pipeline.add_act(
act_name=_("MongoDB--保存dba用户及额外管理用户密码"),
act_component_code=ExecAddPasswordToDBOperationComponent.code,
Expand Down
Loading

0 comments on commit 887d79d

Please sign in to comment.