Skip to content

Commit

Permalink
fix(backend): dbconfig增加转移业务api #9043
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlook authored and iSecloud committed Jan 14, 2025
1 parent 98de8f3 commit 921b610
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 11 deletions.
21 changes: 20 additions & 1 deletion dbm-services/common/db-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,23 @@ Usage of ./bkconfigsvr:
tb_config_file_def: 配置类型和配置文件定义
tb_config_name_def: 平台配置项定义
tb_config_node: 业务、模块、集群等配置项
tb_config_versioned: 已发布配置文件版本
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}
}
```
13 changes: 13 additions & 0 deletions dbm-services/common/db-config/internal/api/config_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (c *ibdStatistic) Run() (msg string, err error) {
}
}
}

//reportMetrics()
err = reportLog2(dbTableSize, dbSize)
if err != nil {
return "", err
Expand Down
5 changes: 5 additions & 0 deletions dbm-ui/backend/components/dbconfig/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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
))""",
},
Expand All @@ -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: {
Expand Down

0 comments on commit 921b610

Please sign in to comment.