Skip to content

Commit

Permalink
fix(backend): 修复mysql部署资源池申请参数 #7369
Browse files Browse the repository at this point in the history
  • Loading branch information
iSecloud committed Oct 16, 2024
1 parent 3eba049 commit fc57a88
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 34 deletions.
8 changes: 6 additions & 2 deletions dbm-ui/backend/db_services/partition/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ class PartitionListSerializer(serializers.Serializer):
immute_domains = serializers.CharField(help_text=_("集群域名"), required=False)
dblikes = serializers.CharField(help_text=_("匹配库"), required=False)
tblikes = serializers.CharField(help_text=_("匹配表"), required=False)
ids = serializers.CharField(help_text=_("策略ID"), required=False)

limit = serializers.IntegerField(required=False, default=10)
offset = serializers.IntegerField(required=False, default=0)

def validate(self, attrs):
filter_fields = ["immute_domains", "dblikes", "tblikes"]
filter_fields = ["immute_domains", "dblikes", "tblikes", "ids"]
# 将过滤参数转为list
for field in filter_fields:
if field in attrs:
attrs[field] = attrs[field].split(",")

# id过滤类型为int
if attrs.get("ids"):
attrs["ids"] = list(map(int, attrs["ids"]))
return attrs


Expand Down
2 changes: 1 addition & 1 deletion dbm-ui/backend/db_services/partition/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _update_log_status(log_list):
*ActionEnum.get_match_actions(
"tendbcluster_partition", exclude=[ActionEnum.TENDBCLUSTER_PARTITION_CREATE]
),
*[ActionEnum.TENDBCLUSTER_PARTITION_ENABLE_DISABLE],
ActionEnum.TENDBCLUSTER_PARTITION_ENABLE_DISABLE,
]
if d["cluster_type"] == ClusterType.TenDBCluster
else [*ActionEnum.get_match_actions("mysql_partition", exclude=[ActionEnum.MYSQL_PARTITION_CREATE])],
Expand Down
41 changes: 14 additions & 27 deletions dbm-ui/backend/ticket/builders/mysql/mysql_ha_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +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 typing import Dict, List

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

from backend.components import DBConfigApi
from backend.components.dbconfig import constants as dbconf_const
from backend.configuration.constants import MASTER_DOMAIN_INITIAL_VALUE, SLAVE_DOMAIN_INITIAL_VALUE, AffinityEnum
from backend.db_meta.enums import ClusterType
from backend.db_meta.models import AppCache, DBModule
Expand All @@ -26,11 +25,11 @@
from backend.ticket.builders.mysql.base import BaseMySQLHATicketFlowBuilder
from backend.ticket.builders.mysql.mysql_single_apply import (
MysqlSingleApplyDetailSerializer,
MysqlSingleApplyFlowBuilder,
MysqlSingleApplyFlowParamBuilder,
MysqlSingleApplyResourceParamBuilder,
)
from backend.ticket.constants import TicketType
from backend.ticket.exceptions import TicketParamsVerifyException


class MysqlHAApplyDetailSerializer(MysqlSingleApplyDetailSerializer):
Expand All @@ -47,6 +46,7 @@ class MysqlHAApplyDetailSerializer(MysqlSingleApplyDetailSerializer):

def validate(self, attrs):
super().validate(attrs)

# 验证输入的机器数量是否预期
if attrs["ip_source"] == IpSource.RESOURCE_POOL:
return attrs
Expand Down Expand Up @@ -104,48 +104,35 @@ def insert_ip_into_apply_infos(cls, ticket_data, apply_infos: List[Dict]):
proxy_nodes = ticket_data["nodes"]["proxy"]
for index, apply_info in enumerate(apply_infos):
# 每组集群需要两个后端 IP 和两个 Proxy IP
start = index * 2
end = (index + 1) * 2
start, end = index * 2, (index + 1) * 2
apply_info["mysql_ip_list"] = backend_nodes[start:end]
apply_info["proxy_ip_list"] = proxy_nodes[start:end]


class MysqlHaApplyResourceParamBuilder(MysqlSingleApplyResourceParamBuilder):
@classmethod
def insert_ip_into_apply_infos(cls, ticket_data, apply_infos: List[Dict]):
backend_nodes = [[group["master"], group["slave"]] for group in ticket_data["nodes"]["backend_group"]]
ticket_data["nodes"]["backend"] = list(itertools.chain(*backend_nodes))
MysqlHAApplyFlowParamBuilder.insert_ip_into_apply_infos(ticket_data, apply_infos)

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

MysqlHAApplyFlowParamBuilder.insert_ip_into_apply_infos(self.ticket.details, apply_infos)

self.insert_ip_into_apply_infos(next_flow.details["ticket_data"], apply_infos)
next_flow.details["ticket_data"].update(apply_infos=apply_infos)
next_flow.save(update_fields=["details"])


@BuilderFactory.register(
TicketType.MYSQL_HA_APPLY, is_apply=True, cluster_type=ClusterType.TenDBHA, iam=ActionEnum.MYSQL_APPLY
)
class MysqlHAApplyFlowBuilder(BaseMySQLHATicketFlowBuilder):
class MysqlHAApplyFlowBuilder(BaseMySQLHATicketFlowBuilder, MysqlSingleApplyFlowBuilder):
serializer = MysqlHAApplyDetailSerializer
inner_flow_builder = MysqlHAApplyFlowParamBuilder
inner_flow_name = _("MySQL高可用部署执行")
resource_apply_builder = MysqlHaApplyResourceParamBuilder

def patch_ticket_detail(self):
# 补充数据库版本和字符集
db_config = DBConfigApi.query_conf_item(
{
"bk_biz_id": str(self.ticket.bk_biz_id),
"level_name": dbconf_const.LevelName.MODULE,
"level_value": str(self.ticket.details["db_module_id"]),
"conf_file": dbconf_const.DEPLOY_FILE_NAME,
"conf_type": dbconf_const.ConfType.DEPLOY,
"namespace": ClusterType.TenDBHA,
"format": dbconf_const.FormatType.MAP,
}
)["content"]

# 校验配置是否存在
if not db_config.get("db_version") or not db_config.get("charset"):
raise TicketParamsVerifyException(_("获取数据库字符集或版本失败,请检查获取参数, db_config: {}").format(db_config))

self.ticket.update_details(db_version=db_config.get("db_version"), charset=db_config.get("charset"))
super().patch_dbconfig(cluster_type=ClusterType.TenDBHA)
super().patch_ticket_detail()
12 changes: 8 additions & 4 deletions dbm-ui/backend/ticket/builders/mysql/mysql_single_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def format_cluster_domains(self) -> List[Dict[str, str]]:
@classmethod
def insert_ip_into_apply_infos(cls, ticket_data, apply_infos: List[Dict]):
# 适配手动输入和资源池导入的角色类型
backend_nodes = ticket_data["nodes"]["backend"] or ticket_data["nodes"]["single"]
backend_nodes = ticket_data["nodes"]["backend"]
for index, apply_info in enumerate(apply_infos):
apply_info["new_ip"] = backend_nodes[index]

Expand Down Expand Up @@ -204,7 +204,7 @@ class MysqlSingleApplyFlowBuilder(BaseMySQLSingleTicketFlowBuilder):
inner_flow_name = _("MySQL单节点部署执行")
resource_apply_builder = MysqlSingleApplyResourceParamBuilder

def patch_ticket_detail(self):
def patch_dbconfig(self, cluster_type):
# 补充数据库版本和字符集
db_config = DBConfigApi.query_conf_item(
{
Expand All @@ -213,13 +213,17 @@ def patch_ticket_detail(self):
"level_value": str(self.ticket.details["db_module_id"]),
"conf_file": dbconf_const.DEPLOY_FILE_NAME,
"conf_type": dbconf_const.ConfType.DEPLOY,
"namespace": ClusterType.TenDBSingle,
"namespace": cluster_type,
"format": dbconf_const.FormatType.MAP,
}
)["content"]

# 校验配置是否存在
if not db_config.get("db_version") or not db_config.get("charset"):
raise TicketParamsVerifyException(_("获取数据库字符集或版本失败,请检查获取参数, db_config: {}").format(db_config))
raise TicketParamsVerifyException(_("获取数据库配置失败,请检查获取参数db_config: {}").format(db_config))

self.ticket.update_details(db_version=db_config.get("db_version"), charset=db_config.get("charset"))

def patch_ticket_detail(self):
self.patch_dbconfig(cluster_type=ClusterType.TenDBSingle)
super().patch_ticket_detail()

0 comments on commit fc57a88

Please sign in to comment.