forked from TencentBlueKing/blueking-dbm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(redis): redis单据联调 TencentBlueKing#7872
# Reviewed, transaction id: 24024
- Loading branch information
Showing
3 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
dbm-ui/backend/ticket/builders/redis/redis_cluster_ins_migrate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available. | ||
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
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. | ||
""" | ||
from django.utils.translation import ugettext_lazy as _ | ||
from rest_framework import serializers | ||
|
||
from backend.db_services.dbbase.constants import IpSource | ||
from backend.flow.engine.controller.redis import RedisController | ||
from backend.ticket import builders | ||
from backend.ticket.builders.redis.base import BaseRedisTicketFlowBuilder | ||
from backend.ticket.constants import TicketType | ||
|
||
|
||
class RedisClusterInsMigrateDetailSerializer(serializers.Serializer): | ||
class RedisClusterInsMigrateItemSerializer(serializers.Serializer): | ||
cluster_id = serializers.IntegerField(help_text=_("集群ID")) | ||
resource_spec = serializers.JSONField(help_text=_("资源规格")) | ||
old_nodes = serializers.JSONField(help_text=_("旧节点信息集合")) | ||
|
||
ip_source = serializers.ChoiceField( | ||
help_text=_("主机来源"), choices=IpSource.get_choices(), default=IpSource.RESOURCE_POOL | ||
) | ||
infos = serializers.ListSerializer(help_text=_("实例迁移单据详情"), child=RedisClusterInsMigrateItemSerializer()) | ||
|
||
def validate(self, attrs): | ||
return attrs | ||
|
||
|
||
class RedisClusterInsMigrateBuilder(builders.FlowParamBuilder): | ||
controller = RedisController.redis_cluster_ins_migrate | ||
|
||
def format_ticket_data(self): | ||
for item in self.ticket_data["infos"]: | ||
item["src_master"] = f'{item["old_nodes"]["master"][0]["ip"]}:{item["old_nodes"]["master"][0]["port"]}' | ||
item["src_slave"] = f'{item["old_nodes"]["slave"][0]["ip"]}:{item["old_nodes"]["slave"][0]["port"]}' | ||
|
||
|
||
class RedisClusterInstanceApplyResourceParamBuilder(builders.ResourceApplyParamBuilder): | ||
def format(self): | ||
self.patch_info_affinity_location(roles=["backend_group"]) | ||
|
||
def post_callback(self): | ||
next_flow = self.ticket.next_flow() | ||
ticket_data = next_flow.details["ticket_data"] | ||
for item in ticket_data["infos"]: | ||
item["src_master"] = f'{item["old_nodes"]["master"][0]["ip"]}:{item["old_nodes"]["master"][0]["port"]}' | ||
item["src_slave"] = f'{item["old_nodes"]["slave"][0]["ip"]}:{item["old_nodes"]["slave"][0]["port"]}' | ||
item["dest_master"] = f'{ticket_data["nodes"]["backend_group"][0]["master"]["ip"]}' | ||
item["dest_slave"] = f'{ticket_data["nodes"]["backend_group"][0]["slave"]["ip"]}' | ||
next_flow.save(update_fields=["details"]) | ||
|
||
|
||
@builders.BuilderFactory.register(TicketType.REDIS_CLUSTER_INS_MIGRATE) | ||
class RedisClusterInsMigrateBuilder(BaseRedisTicketFlowBuilder): | ||
serializer = RedisClusterInsMigrateDetailSerializer | ||
inner_flow_builder = RedisClusterInsMigrateBuilder | ||
resource_apply_builder = RedisClusterInstanceApplyResourceParamBuilder | ||
inner_flow_name = _("Redis 集群指定实例迁移") |
91 changes: 91 additions & 0 deletions
91
dbm-ui/backend/ticket/builders/redis/redis_single_ins_migrate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available. | ||
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
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. | ||
""" | ||
from django.utils.translation import ugettext_lazy as _ | ||
from rest_framework import serializers | ||
|
||
from backend.db_meta.enums import ClusterType | ||
from backend.db_meta.models import Cluster | ||
from backend.db_services.dbbase.constants import IpSource | ||
from backend.flow.engine.controller.redis import RedisController | ||
from backend.ticket import builders | ||
from backend.ticket.builders.common.base import fetch_cluster_ids | ||
from backend.ticket.builders.redis.base import BaseRedisTicketFlowBuilder | ||
from backend.ticket.constants import TicketType | ||
|
||
|
||
class RedisSingleInsMigrateDetailSerializer(serializers.Serializer): | ||
class RedisSingleInsMigrateItemSerializer(serializers.Serializer): | ||
cluster_id = serializers.IntegerField(help_text=_("集群ID")) | ||
resource_spec = serializers.JSONField(help_text=_("资源规格")) | ||
old_nodes = serializers.JSONField(help_text=_("旧节点信息集合")) | ||
|
||
ip_source = serializers.ChoiceField( | ||
help_text=_("主机来源"), choices=IpSource.get_choices(), default=IpSource.RESOURCE_POOL | ||
) | ||
infos = serializers.ListSerializer(help_text=_("实例迁移单据详情"), child=RedisSingleInsMigrateItemSerializer()) | ||
|
||
def validate(self, attrs): | ||
self.validate_cluster_can_access(attrs) | ||
return attrs | ||
|
||
def validate_cluster_can_access(self, attrs): | ||
"""校验集群状态是否可以提单""" | ||
clusters = Cluster.objects.filter(id__in=fetch_cluster_ids(details=attrs)) | ||
|
||
for cluster in clusters: | ||
if cluster.cluster_type != ClusterType.RedisInstance: | ||
raise serializers.ValidationError( | ||
_(f"redis cluster[{cluster.id}] type is not {ClusterType.RedisInstance}") | ||
) | ||
|
||
|
||
class RedisSingleInsMigrateBuilder(builders.FlowParamBuilder): | ||
controller = RedisController.redis_single_ins_migrate | ||
|
||
def format_ticket_data(self): | ||
cluster_id = self.ticket_data["infos"][0]["cluster_id"] | ||
cluster = Cluster.objects.get(id=cluster_id) | ||
self.ticket_data.update( | ||
cluster_id=cluster.id, bk_cloud_id=cluster.bk_cloud_id, db_version=cluster.major_version | ||
) | ||
|
||
|
||
class RedisSingleInstanceApplyResourceParamBuilder(builders.ResourceApplyParamBuilder): | ||
def format(self): | ||
self.patch_info_affinity_location(roles=["backend_group"]) | ||
|
||
def patch_instance_migrate_info(self, ticket_data): | ||
"""补充实例迁移的信息""" | ||
cluster_num, machine_group = len(ticket_data["infos"]), len(ticket_data["nodes"]["backend_group"]) | ||
for index, info in enumerate(ticket_data["infos"]): | ||
backend_group = ticket_data["nodes"]["backend_group"][index % machine_group] | ||
info.update( | ||
backend_group=backend_group, | ||
# maxmemory = 机器内存 * 0.9 / 单机实例数 * 1024 * 1024 (MB --> 字节) | ||
maxmemory=int(1024 * 1024 * backend_group["master"]["bk_mem"] * 0.9 / (cluster_num / machine_group)), | ||
src_master=f'{info["old_nodes"]["master"][0]["ip"]}:{info["old_nodes"]["master"][0]["port"]}', | ||
src_slave=f'{info["old_nodes"]["slave"][0]["ip"]}:{info["old_nodes"]["slave"][0]["port"]}', | ||
dest_master=f'{ticket_data["nodes"]["backend_group"][0]["master"]["ip"]}', | ||
dest_slave=f'{ticket_data["nodes"]["backend_group"][0]["slave"]["ip"]}', | ||
) | ||
|
||
def post_callback(self): | ||
next_flow = self.ticket.next_flow() | ||
self.patch_instance_migrate_info(next_flow.details["ticket_data"]) | ||
next_flow.save(update_fields=["details"]) | ||
|
||
|
||
@builders.BuilderFactory.register(TicketType.REDIS_SINGLE_INS_MIGRATE) | ||
class RedisSingleInsMigrateBuilder(BaseRedisTicketFlowBuilder): | ||
serializer = RedisSingleInsMigrateDetailSerializer | ||
inner_flow_builder = RedisSingleInsMigrateBuilder | ||
resource_apply_builder = RedisSingleInstanceApplyResourceParamBuilder | ||
inner_flow_name = _("Redis 主从指定实例迁移") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters