Skip to content

Commit

Permalink
fix(backend): redis 补充整机替换亲和性 #9192
Browse files Browse the repository at this point in the history
# Reviewed, transaction id: 29813
  • Loading branch information
ygcyao committed Jan 22, 2025
1 parent d50a6da commit 4d39e17
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
24 changes: 22 additions & 2 deletions dbm-ui/backend/ticket/builders/redis/redis_toolbox_add_slave.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
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 itertools

from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers

from backend.configuration.constants import AffinityEnum
from backend.db_meta.enums import InstanceRole, InstanceStatus
from backend.db_meta.models import Cluster, Machine, StorageInstanceTuple
from backend.db_services.dbbase.constants import IpSource
Expand Down Expand Up @@ -86,16 +89,33 @@ def patch_ticket_detail(self):
bk_host_id__in=master_hosts
)
}
cluster_ids = list(itertools.chain(*[infos["cluster_ids"] for infos in self.ticket.details["infos"]]))
id__cluster = {cluster.id: cluster for cluster in Cluster.objects.filter(id__in=cluster_ids)}

for info in self.ticket.details["infos"]:
info["resource_spec"] = {}
# 取第一个cluster即可,即使是多集群,也是单机多实例的情况
cluster = id__cluster[info["cluster_ids"][0]]
for pair in info["pairs"]:
# 申请的 new slave, 需要和当前集群中的 master 不同机房;
master_machine = id__machine[pair["redis_master"]["bk_host_id"]]
pair["redis_slave"]["location_spec"] = {
"city": master_machine.bk_city.logical_city.name,
"sub_zone_ids": [master_machine.bk_sub_zone_id],
"include_or_exclue": False,
"sub_zone_ids": [],
}
pair["redis_slave"].update(affinity=cluster.disaster_tolerance_level)
if cluster.disaster_tolerance_level == AffinityEnum.CROS_SUBZONE:
pair["redis_slave"]["location_spec"].update(
sub_zone_ids=[master_machine.bk_sub_zone_id], include_or_exclue=False
)

elif cluster.disaster_tolerance_level in [
AffinityEnum.SAME_SUBZONE,
AffinityEnum.SAME_SUBZONE_CROSS_SWTICH,
]:
pair["redis_slave"]["location_spec"].update(
sub_zone_ids=[master_machine.bk_sub_zone_id], include_or_exclue=True
)
info["resource_spec"][pair["redis_master"]["ip"]] = pair["redis_slave"]

self.ticket.save(update_fields=["details"])
17 changes: 15 additions & 2 deletions dbm-ui/backend/ticket/builders/redis/redis_toolbox_cut_off.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,24 @@ def patch_ticket_detail(self):
"count": 1,
"location_spec": {
"city": cluster.region,
"sub_zone_ids": [redis_master.machine.bk_sub_zone_id],
"include_or_exclue": False,
"sub_zone_ids": [],
},
"affinity": cluster.disaster_tolerance_level,
}

if cluster.disaster_tolerance_level == AffinityEnum.CROS_SUBZONE:
resource_spec[f"{role}_{role_host['ip']}"]["location_spec"].update(
sub_zone_ids=[redis_master.machine.bk_sub_zone_id], include_or_exclue=False
)

elif cluster.disaster_tolerance_level in [
AffinityEnum.SAME_SUBZONE,
AffinityEnum.SAME_SUBZONE_CROSS_SWTICH,
]:
resource_spec[f"{role}_{role_host['ip']}"]["location_spec"].update(
sub_zone_ids=[redis_master.machine.bk_sub_zone_id], include_or_exclue=True
)

info["resource_spec"] = resource_spec

self.ticket.save(update_fields=["details"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
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 itertools

from django.db.models import F, Prefetch
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 Cluster, Machine
from backend.db_services.dbbase.constants import IpSource
Expand Down Expand Up @@ -91,15 +94,31 @@ def format(self):
bk_host_id__in=master_hosts
)
}
cluster_ids = list(itertools.chain(*[infos["cluster_ids"] for infos in self.ticket.details["infos"]]))
id__cluster = {cluster.id: cluster for cluster in Cluster.objects.filter(id__in=cluster_ids)}

for info in self.ticket.details["infos"]:
cluster = id__cluster[info["cluster_ids"][0]]
# 申请新的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],
"include_or_exclue": False,
"sub_zone_ids": [],
}
info["resource_spec"]["sqlserver_ha"].update(affinity=cluster.disaster_tolerance_level)
if cluster.disaster_tolerance_level == AffinityEnum.CROS_SUBZONE:
info["resource_spec"]["sqlserver_ha"]["location_spec"].update(
sub_zone_ids=[master_machine.bk_sub_zone_id], include_or_exclue=False
)

elif cluster.disaster_tolerance_level in [
AffinityEnum.SAME_SUBZONE,
AffinityEnum.SAME_SUBZONE_CROSS_SWTICH,
]:
info["resource_spec"]["sqlserver_ha"]["location_spec"].update(
sub_zone_ids=[master_machine.bk_sub_zone_id], include_or_exclue=True
)
self.ticket.save(update_fields=["details"])

def post_callback(self):
Expand Down

0 comments on commit 4d39e17

Please sign in to comment.