Skip to content

Commit

Permalink
fix(redis): 自愈集群信息修复 #2592
Browse files Browse the repository at this point in the history
  • Loading branch information
xiepaup authored and zhangzhw8 committed Dec 15, 2023
1 parent d44658b commit 31d0917
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
2 changes: 1 addition & 1 deletion dbm-ui/backend/db_meta/api/cluster/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@ def query_cluster_by_hosts(hosts: List):

for machine in machine_cluster.values():
machine["ports"] = machine_ports.get(machine["ip"])
return machine_cluster.values()
return list(machine_cluster.values())
2 changes: 1 addition & 1 deletion dbm-ui/backend/db_services/redis/autofix/bill.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def create_ticket(cluster: RedisAutofixCore, redis_proxies: list, redis_slaves:
ticket_type=TicketType.REDIS_CLUSTER_AUTOFIX.value,
group=DBType.Redis.value,
status=TicketStatus.PENDING.value,
remark=_("自动发起-自愈任务-{cluster.immute_domain}"),
remark=_("自动发起-自愈任务-{}".format(cluster.immute_domain)),
details=details,
is_reviewed=True,
)
Expand Down
57 changes: 47 additions & 10 deletions dbm-ui/backend/ticket/builders/redis/redis_toolbox_autofix.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from rest_framework import serializers

from backend.configuration.constants import AffinityEnum
from backend.db_meta.enums import InstanceRole
from backend.db_meta.models import Cluster, StorageInstance
from backend.db_services.dbbase.constants import IpSource
from backend.flow.engine.controller.redis import RedisController
from backend.ticket import builders
Expand Down Expand Up @@ -52,12 +54,16 @@ def post_callback(self):
ticket_data = next_flow.details["ticket_data"]

for info_index, info in enumerate(self.ticket_data["infos"]):
for role in ["redis_master", "proxy", "redis_slave"]:
for role in [
InstanceRole.REDIS_MASTER.value,
InstanceRole.REDIS_PROXY.value,
InstanceRole.REDIS_SLAVE.value,
]:
role_hosts = info.get(role)
if not role_hosts:
continue

role_group = "backend_group" if role == "redis_master" else role
role_group = "backend_group" if role == InstanceRole.REDIS_MASTER.value else role
for role_host_index, role_host in enumerate(role_hosts):
role_host["target"] = nodes.get(f"{info_index}_{role_group}")[role_host_index]

Expand Down Expand Up @@ -88,18 +94,49 @@ def patch_ticket_detail(self):
super().patch_ticket_detail()

resource_spec = {}
cluster_ids = [infos["cluster_id"] 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"]:
for role in ["redis_master", "proxy", "redis_slave"]:
cluster = id__cluster[info["cluster_id"]]
for role in [
InstanceRole.REDIS_MASTER.value,
InstanceRole.REDIS_PROXY.value,
InstanceRole.REDIS_SLAVE.value,
]:
role_hosts = info.get(role)

if not role_hosts:
continue
role_group = "backend_group" if role == "redis_master" else role
role_group_affinity = AffinityEnum.CROS_SUBZONE if role_group == "backend_group" else AffinityEnum.NONE
resource_spec[role_group] = {
"spec_id": info[role][0]["spec_id"],
"count": len(role_hosts),
"affinity": role_group_affinity.value,
}

if role == InstanceRole.REDIS_MASTER.value:
# 如果替换角色是master,则是master/slave成对替换
resource_spec["backend_group"] = {
"spec_id": info[role][0]["spec_id"],
"count": len(role_hosts),
"location_spec": {"city": cluster.region, "sub_zone_ids": []},
"affinity": cluster.disaster_tolerance_level,
}
elif role == InstanceRole.REDIS_SLAVE.value:
# 如果是替换slave, 需要和当前集群中的配对的 master 不同机房
redis_slaves = StorageInstance.objects.prefetch_related("as_receiver", "machine").filter(
cluster=cluster, machine__ip__in=[host["ip"] for host in role_hosts]
)
ip__redis_slave = {slave.machine.ip: slave for slave in redis_slaves}
for role_host in role_hosts:
redis_master = ip__redis_slave[role_host["ip"]].as_receiver.get().ejector
resource_spec[f"{role}_{role_host['ip']}"] = {
"spec_id": role_host["spec_id"],
"count": 1,
"location_spec": {
"city": cluster.region,
"sub_zone_ids": [redis_master.machine.bk_sub_zone_id],
"include_or_exclue": False,
},
}
elif role == InstanceRole.REDIS_PROXY.value:
# TODO: proxy替换的亲和性需要衡量存量proxy的分布,暂时忽略
resource_spec[role] = {"spec_id": info[role][0]["spec_id"], "count": len(role_hosts)}

info["resource_spec"] = resource_spec

self.ticket.save(update_fields=["details"])

0 comments on commit 31d0917

Please sign in to comment.