Skip to content

Commit

Permalink
feat(mongodb): mongos自愈,mongo实例下架 TencentBlueKing#7010
Browse files Browse the repository at this point in the history
  • Loading branch information
yyhenryyy committed Oct 18, 2024
1 parent 0cedc6f commit 9cd8a04
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
specific language governing permissions and limitations under the License.
"""
import logging
import datetime

from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

from django.utils.crypto import get_random_string
from backend.configuration.constants import DBType
from backend.configuration.models.dba import DBAdministrator
from backend.db_services.dbbase.constants import IpSource
from backend.db_services.redis.autofix.models import RedisAutofixCore
from backend.ticket.constants import TicketType
from backend.ticket.models import Ticket
from backend.db_services.redis.autofix.enums import AutofixStatus
from backend.utils.time import datetime2str

logger = logging.getLogger("root")

Expand All @@ -44,6 +49,7 @@ def get_resource_spec(mongos_list: list, mongod_list: list) -> dict:
mongod["ip"]: {
"spec_id": mongod["spec_id"],
"count": 1,
"spec_config": mongod["spec_config"],
"Location_spec": {"city": mongod["city"], "sub_zone_ids": [mongod["bk_sub_zone_id"]]},
}
}
Expand Down Expand Up @@ -88,10 +94,15 @@ def mongo_create_ticket(cluster: RedisAutofixCore, cluster_ids: list, mongos_lis
}

# 创建单据
Ticket.create_ticket(
ticket = Ticket.create_ticket(
ticket_type=TicketType.MONGODB_AUTOFIX.value,
creator=mongodb_dba[0],
bk_biz_id=cluster.bk_biz_id,
remark=_("自动发起-自愈任务-{}".format(cluster.immute_domain)),
details=details,
)
cluster.ticket_id = ticket.id
cluster.status_version = get_random_string(12)
cluster.deal_status = AutofixStatus.AF_WFLOW.value
cluster.update_at = datetime2str(datetime.datetime.now(timezone.utc))
cluster.save(update_fields=["ticket_id", "status_version", "deal_status", "update_at"])
33 changes: 18 additions & 15 deletions dbm-ui/backend/flow/engine/bamboo/scene/mongodb/mongodb_autofix.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from backend.db_meta.enums import ClusterType
from backend.flow.utils.mongodb.mongodb_repo import MongoRepository

from . import mongodb_cluster_autofix
from . import mongodb_replace

logger = logging.getLogger("flow")
Expand All @@ -31,11 +32,12 @@ def __init__(self, root_id: str, data: Optional[Dict]):

self.root_id = root_id
self.data = data
self.autofix_info = self.data["infos"][0]

def get_public_data(self) -> Dict:
"""参数公共部分"""

bk_biz_id = self.data["infos"]["bk_biz_id"]
bk_biz_id = self.autofix_info["bk_biz_id"]
return {
"bk_biz_id": bk_biz_id,
"uid": self.data["uid"],
Expand All @@ -48,8 +50,9 @@ def shard_get_data(self) -> Dict:
"""分片集群获取参数"""

flow_parameter = self.get_public_data()
bk_cloud_id = self.data["infos"]["bk_cloud_id"]
cluster_id = self.data["infos"]["cluster_ids"][0]

bk_cloud_id = self.autofix_info["bk_cloud_id"]
cluster_id = self.autofix_info["cluster_ids"][0]
cluster_info = MongoRepository().fetch_one_cluster(withDomain=False, id=cluster_id)
config = cluster_info.get_config()
shards = cluster_info.get_shards()
Expand All @@ -58,33 +61,33 @@ def shard_get_data(self) -> Dict:
mongo_config = []
mongodb = []
# 获取mongos参数
for mongos in self.data["infos"]["mongos_list"]:
for mongos in self.autofix_info["mongos_list"]:
mongos.append(
{
"ip": mongos["ip"],
"bk_cloud_id": bk_cloud_id,
"spec_id": mongos["spec_id"],
"down": True,
"spec_config": mongos["spec_config"],
"target": self.data["infos"][mongos["ip"]],
"target": self.autofix_info[mongos["ip"]],
"instances": [
{
"cluster_id": cluster_id,
"db_version": cluster_info.major_version,
"domain": self.data["infos"]["immute_domain"],
"domain": self.autofix_info["immute_domain"],
"port": int(cluster_info.get_mongos()[0].port),
}
],
}
)
for mongod in self.data["infos"]["mongod_list"]:
for mongod in self.autofix_info["mongod_list"]:
ip_info = {
"ip": mongod["ip"],
"bk_cloud_id": bk_cloud_id,
"spec_id": mongod["spec_id"],
"down": True,
"spec_config": mongod["spec_config"],
"target": self.data["infos"][mongod["ip"]],
"target": self.autofix_info[mongod["ip"]],
"instances": [],
}
instances = []
Expand Down Expand Up @@ -131,9 +134,9 @@ def rs_get_data(self) -> Dict:
"""副本集获取参数"""

flow_parameter = self.get_public_data()
bk_cloud_id = self.data["infos"]["bk_cloud_id"]
cluster_ids = self.data["infos"]["cluster_ids"]
for mongod in self.data["infos"]["mongod_list"]:
bk_cloud_id = self.autofix_info["bk_cloud_id"]
cluster_ids = self.autofix_info["cluster_ids"]
for mongod in self.autofix_info["mongod_list"]:
instances = []
for cluster_id in cluster_ids:
cluster_info = MongoRepository().fetch_one_cluster(withDomain=True, id=cluster_id)
Expand All @@ -155,7 +158,7 @@ def rs_get_data(self) -> Dict:
"spec_id": mongod["spec_id"],
"down": True,
"spec_config": mongod["spec_config"],
"target": self.data["infos"][mongod["ip"]],
"target": self.autofix_info[mongod["ip"]],
"instances": instances,
}
)
Expand All @@ -165,8 +168,8 @@ def autofix(self):
"""进行自愈"""

# 副本集
if self.data["infos"]["cluster_type"] == ClusterType.MongoReplicaSet.value:
if self.autofix_info["cluster_type"] == ClusterType.MongoReplicaSet.value:
mongodb_replace.MongoReplaceFlow(self.root_id, self.rs_get_data()).multi_host_replace_flow()
# 分片集群
elif self.data["infos"]["cluster_type"] == ClusterType.MongoShardedCluster.value:
mongodb_replace.MongoReplaceFlow(self.root_id, self.shard_get_data()).multi_host_replace_flow()
elif self.autofix_info["cluster_type"] == ClusterType.MongoShardedCluster.value:
mongodb_cluster_autofix.MongoClusterAutofixFlow(self.root_id, self.shard_get_data()).cluster_autofix_flow()
2 changes: 2 additions & 0 deletions dbm-ui/backend/ticket/builders/mongodb/mongo_autofix.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ class MongoDBAutofixFlowBuilder(BaseMongoShardedTicketFlowBuilder):
inner_flow_builder = MongoDBAutofixFlowParamBuilder
inner_flow_name = _("MongoDB 故障自愈")
resource_batch_apply_builder = MongoDBAutofixResourceParamBuilder
default_need_itsm = True
default_need_manual_confirm = True

0 comments on commit 9cd8a04

Please sign in to comment.