From 921b61007ff098ace321b0f1063fd0125c2ac60b Mon Sep 17 00:00:00 2001 From: seanlook Date: Mon, 13 Jan 2025 17:08:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(backend):=20dbconfig=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E8=BD=AC=E7=A7=BB=E4=B8=9A=E5=8A=A1api=20#9043?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbm-services/common/db-config/README.md | 21 ++++++- .../db-config/internal/api/config_version.go | 13 +++++ .../handler/simple/change_bk_biz_id.go | 44 +++++++++++++++ .../internal/handler/simple/simple.go | 1 + .../service/simpleconfig/change_bk_biz_id.go | 56 +++++++++++++++++++ .../mysql-dbbackup/cmd/subcmd_spider.go | 6 +- .../ibdstatistic/ibd_statistic.go | 2 +- dbm-ui/backend/components/dbconfig/client.py | 5 ++ .../local_tasks/db_meta/constants.py | 8 +-- 9 files changed, 145 insertions(+), 11 deletions(-) create mode 100644 dbm-services/common/db-config/internal/handler/simple/change_bk_biz_id.go create mode 100644 dbm-services/common/db-config/internal/service/simpleconfig/change_bk_biz_id.go diff --git a/dbm-services/common/db-config/README.md b/dbm-services/common/db-config/README.md index 2fefa051ad..a6c2edd08c 100644 --- a/dbm-services/common/db-config/README.md +++ b/dbm-services/common/db-config/README.md @@ -201,4 +201,23 @@ Usage of ./bkconfigsvr: tb_config_file_def: 配置类型和配置文件定义 tb_config_name_def: 平台配置项定义 tb_config_node: 业务、模块、集群等配置项 -tb_config_versioned: 已发布配置文件版本 \ No newline at end of file +tb_config_versioned: 已发布配置文件版本 + + +## 转移业务 api示例: +``` +# request +curl http://x.x.x.x/bkconfig/v1/version/change-bkbizid -d \ +'{ + "bk_biz_id": "123", + "new_bk_biz_id": "456", + "cluster_domains": ["xx1.yy.zz", "xx2.yy.zz"] +}' + +# response +{ + "code":0, + "message":"", + "data":{"clusters_affected":0, "cluster_received":2} +} +``` \ No newline at end of file diff --git a/dbm-services/common/db-config/internal/api/config_version.go b/dbm-services/common/db-config/internal/api/config_version.go index 5182b3b499..b72be5d000 100644 --- a/dbm-services/common/db-config/internal/api/config_version.go +++ b/dbm-services/common/db-config/internal/api/config_version.go @@ -60,3 +60,16 @@ type ListConfigVersionsResp struct { VersionLatest string `json:"published"` BaseLevelDef } // @name ListConfigVersionsResp + +// ChangeBkBizIdReq change bk_biz_id request body +type ChangeBkBizIdReq struct { + BKBizIDDef + NewBKBizID string `json:"new_bk_biz_id" form:"new_bk_biz_id" validate:"required" example:"2345"` + ClusterDomains []string `json:"cluster_domains" form:"cluster_domains" validate:"required"` +} + +// ChangeBkBizIdResp change bk_biz_id response body +type ChangeBkBizIdResp struct { + ClustersAffected int64 `json:"clusters_affected" form:"clusters_affected"` + ClusterReceived int `json:"cluster_received" form:"cluster_received"` +} diff --git a/dbm-services/common/db-config/internal/handler/simple/change_bk_biz_id.go b/dbm-services/common/db-config/internal/handler/simple/change_bk_biz_id.go new file mode 100644 index 0000000000..744fd80ba2 --- /dev/null +++ b/dbm-services/common/db-config/internal/handler/simple/change_bk_biz_id.go @@ -0,0 +1,44 @@ +package simple + +import ( + "bk-dbconfig/internal/api" + "bk-dbconfig/internal/handler" + "bk-dbconfig/internal/service/simpleconfig" + + "github.com/gin-gonic/gin" + + "dbm-services/common/go-pubpkg/validate" +) + +// ChangeBizBizId godoc +// +// @Summary 修改集群的业务 +// @Description 修改集群的业务归属 +// @Tags config_version +// @Accept json +// @Produce json +// @Param body body api.ChangeBkBizIdReq true "change bk_biz_id for clusters" +// @Success 200 {object} api.ChangeBkBizIdResp +// @Failure 400 {object} api.HTTPClientErrResp +// @Router /bkconfig/v1/version/change_bk_biz_id [post] +func (cf *Config) ChangeBizBizId(ctx *gin.Context) { + var r api.ChangeBkBizIdReq + var resp *api.ChangeBkBizIdResp + var err error + if err = ctx.BindJSON(&r); err != nil { + handler.SendResponse(ctx, err, nil) + return + } + if err := validate.GoValidateStruct(r, false); err != nil { + handler.SendResponse(ctx, err, nil) + return + } + //r.ClusterDomains = cmutil.UniqueStrings(r.ClusterDomains) + if resp, err = simpleconfig.ChangeBkBizId(&r, ""); err != nil { + handler.SendResponse(ctx, err, nil) + return + } + resp.ClusterReceived = len(r.ClusterDomains) + handler.SendResponse(ctx, nil, resp) + return +} diff --git a/dbm-services/common/db-config/internal/handler/simple/simple.go b/dbm-services/common/db-config/internal/handler/simple/simple.go index 34f34a63c3..994ed0587f 100644 --- a/dbm-services/common/db-config/internal/handler/simple/simple.go +++ b/dbm-services/common/db-config/internal/handler/simple/simple.go @@ -35,6 +35,7 @@ func (cf *Config) Routes() []*gin.RouteInfo { {Method: http.MethodPost, Path: "/version/applied", HandlerFunc: cf.VersionApplyStat}, {Method: http.MethodPost, Path: "/version/status", HandlerFunc: cf.VersionStat}, {Method: http.MethodPost, Path: "/version/applyitem", HandlerFunc: cf.ItemApply}, + {Method: http.MethodPost, Path: "/version/change-bkbizid", HandlerFunc: cf.ChangeBizBizId}, // config_item {Method: http.MethodPost, Path: "/confitem/query", HandlerFunc: cf.MergeAndGetConfigItems}, diff --git a/dbm-services/common/db-config/internal/service/simpleconfig/change_bk_biz_id.go b/dbm-services/common/db-config/internal/service/simpleconfig/change_bk_biz_id.go new file mode 100644 index 0000000000..0076f55f3f --- /dev/null +++ b/dbm-services/common/db-config/internal/service/simpleconfig/change_bk_biz_id.go @@ -0,0 +1,56 @@ +// 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. + +package simpleconfig + +import ( + "bk-dbconfig/internal/api" + "bk-dbconfig/internal/repository/model" + "bk-dbconfig/pkg/constvar" + + "gorm.io/gorm" +) + +// ChangeBkBizId 修改集群的业务 id +// 返回影响 tb_config_versioned 表的记录数 +func ChangeBkBizId(r *api.ChangeBkBizIdReq, opUser string) (*api.ChangeBkBizIdResp, error) { + var rowsAffected int64 + txErr := model.DB.Self.Transaction(func(tx *gorm.DB) error { + updateVersioned := tx.Model(&model.ConfigVersionedModel{}). + Where("bk_biz_id = ? and level_value in ? and level_name = ?", + r.BKBizID, r.ClusterDomains, constvar.LevelCluster). + Update("bk_biz_id", r.NewBKBizID) + if err := updateVersioned.Error; err != nil { + return err + } else { + rowsAffected = updateVersioned.RowsAffected + } + + updateConfigNode := tx.Model(&model.ConfigModel{}). + Where("bk_biz_id = ? and level_value in ? and level_name = ?", + r.BKBizID, r.ClusterDomains, constvar.LevelCluster). + Update("bk_biz_id", r.NewBKBizID) + if err := updateConfigNode.Error; err != nil { + return err + } + + updateFileNode := tx.Model(&model.ConfigFileNodeModel{}). + Where("bk_biz_id = ? and level_value in ? and level_name = ?", + r.BKBizID, r.ClusterDomains, constvar.LevelCluster). + Update("bk_biz_id", r.NewBKBizID) + if err := updateFileNode.Error; err != nil { + return err + } + return nil + }) + + if txErr == nil { + return &api.ChangeBkBizIdResp{ClustersAffected: rowsAffected}, nil + } + return nil, txErr +} diff --git a/dbm-services/mysql/db-tools/mysql-dbbackup/cmd/subcmd_spider.go b/dbm-services/mysql/db-tools/mysql-dbbackup/cmd/subcmd_spider.go index 8fd0e6f87e..9e35d6e2fc 100644 --- a/dbm-services/mysql/db-tools/mysql-dbbackup/cmd/subcmd_spider.go +++ b/dbm-services/mysql/db-tools/mysql-dbbackup/cmd/subcmd_spider.go @@ -137,11 +137,7 @@ var spiderCheckCmd = &cobra.Command{ if err != nil { return err } - configFile, err := findSpiderBackupConfigFile(cnfFiles) - if err != nil { - return err - } - publicConfigs, err := batchParseCnfFiles([]string{configFile}) + publicConfigs, err := batchParseCnfFiles(cnfFiles) if err != nil { return err } diff --git a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/ibdstatistic/ibd_statistic.go b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/ibdstatistic/ibd_statistic.go index 60d0cc5960..95ac8c88db 100644 --- a/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/ibdstatistic/ibd_statistic.go +++ b/dbm-services/mysql/db-tools/mysql-monitor/pkg/itemscollect/ibdstatistic/ibd_statistic.go @@ -115,7 +115,7 @@ func (c *ibdStatistic) Run() (msg string, err error) { } } } - + //reportMetrics() err = reportLog2(dbTableSize, dbSize) if err != nil { return "", err diff --git a/dbm-ui/backend/components/dbconfig/client.py b/dbm-ui/backend/components/dbconfig/client.py index 143f593303..75fcce9a88 100644 --- a/dbm-ui/backend/components/dbconfig/client.py +++ b/dbm-ui/backend/components/dbconfig/client.py @@ -83,6 +83,11 @@ def __init__(self): url="bkconfig/v1/version/generate", description=_("查询实例配置文件模版"), ) + self.change_bk_biz_id = self.generate_data_api( + method="POST", + url="bkconfig/v1/version/change-bkbizid", + description=_("集群转移业务"), + ) DBConfigApi = _DBConfigApi() diff --git a/dbm-ui/backend/db_periodic_task/local_tasks/db_meta/constants.py b/dbm-ui/backend/db_periodic_task/local_tasks/db_meta/constants.py index 74567976e6..ba72a12af2 100644 --- a/dbm-ui/backend/db_periodic_task/local_tasks/db_meta/constants.py +++ b/dbm-ui/backend/db_periodic_task/local_tasks/db_meta/constants.py @@ -69,13 +69,13 @@ "used": """sum by (cluster_domain) ( max by (cluster_domain, ip) ( max_over_time( - bkmonitor:exporter_dbm_mysqld_exporter:mysql_datadir_df_used_mb{instance_role="backend_master",%s}[5m] + bkmonitor:exporter_dbm_mysqld_exporter:mysql_datadir_df_used_mb{instance_role="backend_master",%s}[124m] ) * 1024 * 1024 ))""", "total": """sum by (cluster_domain) ( max by (cluster_domain, ip) ( max_over_time( - bkmonitor:exporter_dbm_mysqld_exporter:mysql_datadir_df_total_mb{instance_role="backend_master",%s}[5m] + bkmonitor:exporter_dbm_mysqld_exporter:mysql_datadir_df_total_mb{instance_role="backend_master",%s}[124m] ) * 1024 * 1024 ))""", }, @@ -84,12 +84,12 @@ "used": """sum by (cluster_domain) ( avg by (cluster_domain, instance) ( avg_over_time( - bkmonitor:exporter_dbm_mysqld_exporter:mysql_datadir_du_used_mb{instance_role="remote_master",%s}[5m] + bkmonitor:exporter_dbm_mysqld_exporter:mysql_datadir_du_used_mb{instance_role="remote_master",%s}[124m] ) * 1024 * 1024))""", "total": """sum by (cluster_domain) ( avg by (cluster_domain, ip) ( avg_over_time( - bkmonitor:exporter_dbm_mysqld_exporter:mysql_datadir_df_total_mb{instance_role="remote_master",%s}[5m] + bkmonitor:exporter_dbm_mysqld_exporter:mysql_datadir_df_total_mb{instance_role="remote_master",%s}[124m] ) * 1024 * 1024))""", }, ClusterType.SqlserverSingle: {