Skip to content

Commit

Permalink
fix(backend): tendbcluster重建slave补充亲和性 TencentBlueKing#7431
Browse files Browse the repository at this point in the history
  • Loading branch information
iSecloud committed Oct 21, 2024
1 parent e8b3bb5 commit dc9578c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
7 changes: 0 additions & 7 deletions dbm-ui/backend/ticket/builders/common/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from backend.db_meta.enums import AccessLayer, ClusterPhase, ClusterType, InstanceInnerRole, InstanceStatus
from backend.db_meta.enums.comm import SystemTagEnum
from backend.db_meta.models import Cluster, ExtraProcessInstance, Machine, ProxyInstance, Spec, StorageInstance
from backend.db_services.ipchooser.constants import BkOsType
from backend.db_services.ipchooser.query.resource import ResourceQueryHelper
from backend.db_services.mysql.cluster.handlers import ClusterServiceHandler
from backend.db_services.mysql.dumper.handlers import DumperHandler
Expand Down Expand Up @@ -533,12 +532,6 @@ def format(self):
if info.get("bk_cloud_id") and info.get("bk_biz_id"):
continue

# 获取系统版本跟类型
os_names = info.get("system_version")
os_type = BkOsType.db_type_to_os_type(TicketType.get_db_type_by_ticket(self.ticket.ticket_type))
# 增加os_names和os_type过滤
info["resource_params"] = {"os_names": os_names, "os_type": os_type}

# 默认从集群中获取云区域ID和业务ID
cluster_id = info.get("cluster_id") or info.get("src_cluster") or info.get("cluster_ids")[0]
bk_cloud_id = id__clusters[cluster_id].bk_cloud_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def format(self):
for info in self.ticket.details["infos"]:
# 申请新的slave, 需要和当前集群中的master处于不同机房;
master_machine = id__machine[formatted_dict[info["old_slave_host"]["bk_host_id"]]]
# TODO: 还有补充操作系统
info["resource_spec"]["sqlserver_ha"]["location_spec"] = {
"city": master_machine.bk_city.logical_city.name,
"sub_zone_ids": [master_machine.bk_sub_zone_id],
Expand Down
27 changes: 27 additions & 0 deletions dbm-ui/backend/ticket/builders/tendbcluster/tendb_restore_slave.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers

from backend.configuration.constants import AffinityEnum
from backend.db_meta.enums import ClusterType
from backend.db_meta.models import StorageInstance
from backend.db_services.dbbase.constants import IpSource
from backend.flow.engine.controller.spider import SpiderController
from backend.ticket import builders
Expand All @@ -21,6 +23,7 @@
from backend.ticket.builders.mysql.mysql_restore_slave import MysqlRestoreSlaveDetailSerializer
from backend.ticket.builders.tendbcluster.base import BaseTendbTicketFlowBuilder
from backend.ticket.constants import TicketType
from backend.utils.basic import get_target_items_from_details


class TendbClusterRestoreSlaveDetailSerializer(MysqlRestoreSlaveDetailSerializer):
Expand Down Expand Up @@ -63,6 +66,30 @@ def format_ticket_data(self):


class TendbClusterRestoreSlaveResourceParamBuilder(BaseOperateResourceParamBuilder):
def patch_slave_subzone(self):
# 对于亲和性为跨园区的,slave和master需要在不同园区
slave_host_ids = get_target_items_from_details(self.ticket.details, match_keys=["bk_host_id"])
slaves = StorageInstance.objects.prefetch_related("as_receiver__ejector__machine", "machine").filter(
machine__bk_host_id__in=slave_host_ids, cluster_type=ClusterType.TenDBCluster
)
slave_host_map = {slave.machine.bk_host_id: slave for slave in slaves}
for info in self.ticket_data["infos"]:
resource_spec = info["resource_spec"]["new_slave"]
slave = slave_host_map[info["old_slave"]["bk_host_id"]]
master_subzone_id = slave.as_receiver.get().ejector.machine.bk_sub_zone_id
# 同城跨园区,要求slave和master在不同subzone
if resource_spec["affinity"] == AffinityEnum.CROS_SUBZONE:
resource_spec["location_spec"].update(sub_zone_ids=[master_subzone_id], include_or_exclue=False)
# 同城同园区,要求slave和master在一个subzone
elif resource_spec["affinity"] in [AffinityEnum.SAME_SUBZONE, AffinityEnum.SAME_SUBZONE_CROSS_SWTICH]:
resource_spec["location_spec"].update(sub_zone_ids=[master_subzone_id], include_or_exclue=True)

def format(self):
# 补充亲和性和城市信息
super().patch_info_affinity_location(roles=["new_slave"])
# 补充slave园区申请
self.patch_slave_subzone()

def post_callback(self):
next_flow = self.ticket.next_flow()
ticket_data = next_flow.details["ticket_data"]
Expand Down

0 comments on commit dc9578c

Please sign in to comment.