Skip to content

Commit

Permalink
fix(mongodb): 迁移元数据在流程节点内判断副本集机器是否复用 #8184
Browse files Browse the repository at this point in the history
  • Loading branch information
yyhenryyy authored and iSecloud committed Nov 26, 2024
1 parent a3d0ff3 commit a0c47a1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
2 changes: 1 addition & 1 deletion dbm-ui/backend/db_services/dbresource/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from backend.components.dbresource.client import DBResourceApi
from backend.components.gse.client import GseApi
from backend.db_meta.enums.spec import SpecClusterType, SpecMachineType
from backend.db_meta.models import Spec, AppCache
from backend.db_meta.models import AppCache, Spec
from backend.db_services.dbresource.exceptions import SpecOperateException
from backend.db_services.ipchooser.query.resource import ResourceQueryHelper

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
from backend.flow.plugins.components.collections.common.install_nodeman_plugin import (
InstallNodemanPluginServiceComponent,
)
from backend.flow.plugins.components.collections.mongodb.add_relationship_to_meta import (
ExecAddRelationshipOperationComponent,
)
from backend.flow.plugins.components.collections.mongodb.exec_actuator_job import ExecuteDBActuatorJobComponent
from backend.flow.plugins.components.collections.mongodb.migrate_meta import MongoDBMigrateMetaComponent
from backend.flow.plugins.components.collections.mongodb.send_media import ExecSendMediaOperationComponent
Expand Down Expand Up @@ -83,7 +80,7 @@ def cluster_migrate(
kwargs = sub_get_kwargs.get_migrate_info()
sub_pipeline.add_act(
act_name=_("迁移meta"),
act_component_code=ExecAddRelationshipOperationComponent.code,
act_component_code=MongoDBMigrateMetaComponent.code,
kwargs=kwargs,
)

Expand Down
55 changes: 54 additions & 1 deletion dbm-ui/backend/flow/utils/mongodb/migrate_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
from backend.components import DBConfigApi, DnsApi
from backend.components.dbconfig.constants import LevelName, ReqType
from backend.configuration.handlers.dba import DBAdministratorHandler
from backend.db_meta.api.cluster.mongocluster import pkg_create_mongo_cluster
from backend.db_meta.api.cluster.mongorepset import pkg_create_mongoset
from backend.db_meta.enums import ClusterEntryType
from backend.db_meta.enums.cluster_type import ClusterType
from backend.db_meta.models import CLBEntryDetail, Cluster, ClusterEntry
from backend.db_meta.models import CLBEntryDetail, Cluster, ClusterEntry, Machine
from backend.flow.consts import DEFAULT_CONFIG_CONFIRM, DEFAULT_DB_MODULE_ID, MongoDBManagerUser
from backend.flow.utils import dns_manage
from backend.flow.utils.mongodb.mongodb_password import MongoDBPassword
Expand Down Expand Up @@ -154,6 +156,57 @@ def save_password(self):
)
return False

def migrate_cluster(self):
"""迁移集群"""

# 副本集判断机器是否复用
if self.info["cluster_type"] == ClusterType.MongoReplicaSet.value:
for node in self.info["replicaset_storages"]:
if Machine.objects.filter(ip=node["ip"], bk_cloud_id=node["bk_cloud_id"]).count() > 0:
self.info["skip_machine"] = True
break
# 写入meta
try:
if self.info["cluster_type"] == ClusterType.MongoReplicaSet.value:
pkg_create_mongoset(
bk_biz_id=self.info["bk_biz_id"],
name=self.info["name"],
immute_domain=self.info["immute_domain"],
alias=self.info["alias"],
major_version=self.info["major_version"],
storages=self.info["storages"],
creator=self.info["creator"],
bk_cloud_id=self.info["bk_cloud_id"],
db_module_id=self.info["db_module_id"],
region=self.info["region"],
skip_machine=self.info["skip_machine"],
spec_id=self.info["spec_id"],
spec_config=self.info["spec_config"],
disaster_tolerance_level=self.info.get("disaster_tolerance_level", "NONE"),
)
elif self.info["cluster_type"] == ClusterType.MongoShardedCluster.value:
pkg_create_mongo_cluster(
bk_biz_id=self.info["bk_biz_id"],
name=self.info["name"],
immute_domain=self.info["immute_domain"],
alias=self.info["alias"],
db_module_id=self.info["db_module_id"],
major_version=self.info["major_version"],
proxies=self.info["proxies"],
configs=self.info["configs"],
storages=self.info["storages"],
creator=self.info["creator"],
bk_cloud_id=self.info["bk_cloud_id"],
region=self.info["region"],
machine_specs=self.info["machine_specs"],
disaster_tolerance_level=self.info.get("disaster_tolerance_level", "NONE"),
)
except Exception as e:
logger.error("add relationship to meta fail, error:{}".format(str(e)))
return False
logger.info("add mongodb relationship to meta successfully")
return True

def change_domain_app(self):
"""修改dns的app字段"""

Expand Down
16 changes: 3 additions & 13 deletions dbm-ui/backend/flow/utils/mongodb/mongodb_migrate_dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from backend.db_meta.enums.cluster_type import ClusterType
from backend.db_meta.enums.instance_role import InstanceRole
from backend.db_meta.enums.spec import SpecClusterType, SpecMachineType
from backend.db_meta.models import Machine
from backend.db_meta.models.spec import Spec
from backend.flow.consts import (
DEFAULT_DB_MODULE_ID,
Expand Down Expand Up @@ -99,17 +98,6 @@ def __init__(self):
# os配置
self.os_conf: dict = None

def skip_machine(self):
"""副本集机器复用"""

if self.source_cluster_info.get("cluster_type") == ClusterType.MongoReplicaSet.value:
for node in self.source_cluster_info.get("storages"):
if Machine.objects.filter(ip=node["ip"], bk_cloud_id=node["bk_cloud_id"]).count() > 0:
self.source_cluster_info["skip_machine"] = True
break
else:
self.source_cluster_info["skip_machine"] = False

@staticmethod
def get_mongodb_spec_info(cluster_type: str, machine_type: str) -> dict:
"""获取机器规格"""
Expand Down Expand Up @@ -256,6 +244,7 @@ def get_migrate_info(self) -> dict:
"""获取迁移信息"""

info = {
"meta_func_name": MongoDBMigrateMeta.migrate_cluster.__name__,
"bk_biz_id": self.bk_biz_id,
"major_version": self.source_cluster_info.get("major_version"),
"creator": self.source_cluster_info.get("mongodb_dbas").split(",")[0],
Expand Down Expand Up @@ -290,7 +279,8 @@ def get_migrate_info(self) -> dict:
storages[index]["role"] = self.instance_role[index]
info.update(
{
"skip_machine": self.source_cluster_info.get("skip_machine"),
"replicaset_storages": self.source_cluster_info.get("storages"),
"skip_machine": False,
"immute_domain": immute_domain,
"name": self.source_cluster_info.get("replsetname"),
"alias": self.source_cluster_info.get("replsetname"),
Expand Down

0 comments on commit a0c47a1

Please sign in to comment.