Skip to content

Commit

Permalink
feat(backend): mysql备份单据联调 TencentBlueKing#7906
Browse files Browse the repository at this point in the history
# Reviewed, transaction id: 24278
  • Loading branch information
peterxucai committed Nov 20, 2024
1 parent 0a4ae10 commit 7f9516a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 23 deletions.
8 changes: 4 additions & 4 deletions dbm-ui/backend/ticket/builders/mysql/mysql_db_table_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __validate_cluster_id_unique(cluster_ids) -> str:
"""
集群 id 不能重复出现
"""
dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids) if cnt > 1]
dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids).items() if cnt > 1]
if dup_cluster_ids:
return _(
"重复输入集群: {}".format(
Expand All @@ -89,7 +89,7 @@ def __validate_cluster_type(cluster_ids) -> str:
cluster_types = []
for cluster_obj in Cluster.objects.filter(pk__in=cluster_ids):
if cluster_obj.cluster_type not in [ClusterType.TenDBHA, ClusterType.TenDBSingle]:
bad.append(_("不支持的集群类型 {} {}".format(cluster_obj.immute_domain, cluster_obj.cluster_type)))
bad.append(str(_("不支持的集群类型 {} {}".format(cluster_obj.immute_domain, cluster_obj.cluster_type))))

cluster_types.append(cluster_obj.cluster_type)

Expand All @@ -107,7 +107,7 @@ def __validate_cluster_exists(cluster_ids) -> str:
exists_cluster_ids = list(
Cluster.objects.filter(
pk__in=cluster_ids, cluster_type__in=[ClusterType.TenDBHA, ClusterType.TenDBSingle]
).values_list("cluster_id", flat=True)
).values_list("id", flat=True)
)
not_exists_cluster_ids = list(set(cluster_ids) - set(exists_cluster_ids))
if not_exists_cluster_ids:
Expand All @@ -128,7 +128,7 @@ def __validate_cluster_status(cluster_ids) -> str:
).exists()
):
bad.append(_("{} 缺少状态正常的 standby slave".format(cluster_obj.immute_domain)))
elif cluster_obj.storageinstance_set.filter(status=InstanceStatus.RUNNING).exists():
elif not cluster_obj.storageinstance_set.filter(status=InstanceStatus.RUNNING).exists():
bad.append(_("{} 缺少状态正常的存储实例".format(cluster_obj.immute_domain)))

if bad:
Expand Down
24 changes: 14 additions & 10 deletions dbm-ui/backend/ticket/builders/mysql/mysql_full_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __validate_cluster_id_unique(cluster_ids) -> str:
"""
集群 id 不能重复出现
"""
dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids) if cnt > 1]
dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids).items() if cnt > 1]
if dup_cluster_ids:
return _(
"重复输入集群: {}".format(
Expand All @@ -81,7 +81,7 @@ def __validate_cluster_type(cluster_ids) -> str:
cluster_types = []
for cluster_obj in Cluster.objects.filter(pk__in=cluster_ids):
if cluster_obj.cluster_type not in [ClusterType.TenDBHA, ClusterType.TenDBSingle]:
bad.append(_("不支持的集群类型 {} {}".format(cluster_obj.immute_domain, cluster_obj.cluster_type)))
bad.append(str(_("不支持的集群类型 {} {}".format(cluster_obj.immute_domain, cluster_obj.cluster_type))))

cluster_types.append(cluster_obj.cluster_type)

Expand All @@ -101,15 +101,17 @@ def __validate_backup_local(attrs) -> str:
cluster_obj = Cluster.objects.get(pk=cluster_id)

if cluster_obj.cluster_type == ClusterType.TenDBSingle and backup_local != InstanceInnerRole.ORPHAN:
bad.append(_("{} 备份位置只能是 {}".format(cluster_obj.immute_domain, InstanceInnerRole.ORPHAN)))
bad.append(str(_("{} 备份位置只能是 {}".format(cluster_obj.immute_domain, InstanceInnerRole.ORPHAN))))
elif cluster_obj.cluster_type == ClusterType.TenDBHA and backup_local not in [
InstanceInnerRole.MASTER,
InstanceInnerRole.SLAVE,
]:
bad.append(
_(
"{} 备份位置只能是 {}".format(
cluster_obj.immute_domain, [InstanceInnerRole.MASTER, InstanceInnerRole.SLAVE]
str(
_(
"{} 备份位置只能是 {}".format(
cluster_obj.immute_domain, [InstanceInnerRole.MASTER, InstanceInnerRole.SLAVE]
)
)
)
)
Expand All @@ -129,10 +131,12 @@ def __validate_cluster_status(attrs) -> str:
cluster=cluster_obj, instance_inner_role=backup_local, is_stand_by=True, status=InstanceStatus.RUNNING
).exists():
bad.append(
_(
"{} 没找到正常的 {} 实例".format(
cluster_obj.immute_domain,
backup_local,
str(
_(
"{} 没找到正常的 {} 实例".format(
cluster_obj.immute_domain,
backup_local,
)
)
)
)
Expand Down
33 changes: 28 additions & 5 deletions dbm-ui/backend/ticket/builders/tendbcluster/db_table_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,27 @@ class TenDBClusterDBTableBackupInfoSerializer(serializers.Serializer):

infos = serializers.ListSerializer(help_text=_("库表备份信息"), child=TenDBClusterDBTableBackupInfoSerializer())

@classmethod
def get_backup_local_params(cls, info):
"""
对备份位置进行提取,
两种情况:remote/spider_mnt::127.0.0.1
"""
divider = "::"
if divider not in info["backup_local"]:
return info

backup_local, spider_mnt_address = info["backup_local"].split(divider)
info["backup_local"] = backup_local
info["spider_mnt_address"] = spider_mnt_address

return info

def validate(self, attrs):

for cluster_info in attrs["infos"]:
self.get_backup_local_params(cluster_info)

# 集群不允许重复
cluster_ids = [info["cluster_id"] for info in attrs["infos"]]

Expand All @@ -60,6 +80,9 @@ def validate(self, attrs):
if msg:
errors.append(msg)

if errors:
raise serializers.ValidationError(errors)

# 库表选择器校验
super().validate_database_table_selector(attrs, role_key="backup_local")
return attrs
Expand All @@ -69,7 +92,7 @@ def __validate_cluster_id_unique(cluster_ids) -> str:
"""
集群 id 不能重复出现
"""
dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids) if cnt > 1]
dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids).items() if cnt > 1]
if dup_cluster_ids:
return _(
"重复输入集群: {}".format(
Expand Down Expand Up @@ -97,7 +120,7 @@ def __validate_cluster_exists(cluster_ids) -> str:
"""
exists_cluster_ids = list(
Cluster.objects.filter(pk__in=cluster_ids, cluster_type=ClusterType.TenDBCluster).values_list(
"cluster_id", flat=True
"id", flat=True
)
)
not_exists_cluster_ids = list(set(cluster_ids) - set(exists_cluster_ids))
Expand All @@ -111,10 +134,10 @@ def __validate_backup_local(attrs):
for info in attrs["infos"]:
backup_local = info["backup_local"]
if backup_local not in ["remote", "spider_mnt"]:
bad.append(_("不支持的备份位置 {}".format(backup_local)))
bad.append(str(_("不支持的备份位置 {}".format(backup_local))))

if backup_local == "spider_mnt" and "spider_mnt_address" not in info:
bad.append(_("缺少 spider_mnt_address"))
bad.append(str(_("缺少 spider_mnt_address")))

if bad:
return ", ".join(list(set(bad)))
Expand All @@ -136,7 +159,7 @@ def __validate_cluster_status(attrs):
elif (
backup_local == "remote"
and cluster_obj.storageinstance_set.filter(
InstanceInnerRole.SLAVE,
instance_inner_role=InstanceInnerRole.SLAVE.value,
is_stand_by=True,
)
.exclude(status=InstanceStatus.RUNNING)
Expand Down
31 changes: 27 additions & 4 deletions dbm-ui/backend/ticket/builders/tendbcluster/full_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,27 @@ class TenDBClusterFullBackupInfoSerializer(serializers.Serializer):

infos = serializers.ListSerializer(child=TenDBClusterFullBackupInfoSerializer())

@classmethod
def get_backup_local_params(cls, info):
"""
对备份位置进行提取,
两种情况:remote/spider_mnt::127.0.0.1
"""
divider = "::"
if divider not in info["backup_local"]:
return info

backup_local, spider_mnt_address = info["backup_local"].split(divider)
info["backup_local"] = backup_local
info["spider_mnt_address"] = spider_mnt_address

return info

def validate(self, attrs):

for cluster_info in attrs["infos"]:
self.get_backup_local_params(cluster_info)

cluster_ids = [info["cluster_id"] for info in attrs["infos"]]

errors = []
Expand All @@ -58,14 +78,17 @@ def validate(self, attrs):
if msg:
errors.append(msg)

if errors:
raise serializers.ValidationError(errors)

return attrs

@staticmethod
def __validate_cluster_id_unique(cluster_ids) -> str:
"""
集群 id 不能重复出现
"""
dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids) if cnt > 1]
dup_cluster_ids = [cid for cid, cnt in collections.Counter(cluster_ids).items() if cnt > 1]
if dup_cluster_ids:
return _(
"重复输入集群: {}".format(
Expand Down Expand Up @@ -93,7 +116,7 @@ def __validate_cluster_exists(cluster_ids) -> str:
"""
exists_cluster_ids = list(
Cluster.objects.filter(pk__in=cluster_ids, cluster_type=ClusterType.TenDBCluster).values_list(
"cluster_id", flat=True
"id", flat=True
)
)
not_exists_cluster_ids = list(set(cluster_ids) - set(exists_cluster_ids))
Expand All @@ -107,10 +130,10 @@ def __validate_backup_local(attrs):
for info in attrs["infos"]:
backup_local = info["backup_local"]
if backup_local not in ["master", "slave", "spider_mnt"]:
bad.append(_("不支持的备份位置 {}".format(backup_local)))
bad.append(str(_("不支持的备份位置 {}".format(backup_local))))

if backup_local == "spider_mnt" and "spider_mnt_address" not in info:
bad.append(_("缺少 spider_mnt_address"))
bad.append(str(_("缺少 spider_mnt_address")))

if bad:
return ", ".join(list(set(bad)))
Expand Down

0 comments on commit 7f9516a

Please sign in to comment.