Skip to content

Commit fc16bd7

Browse files
xfan0805iSecloud
authored andcommitted
fix: 分区查询优化 TencentBlueKing#8652
1 parent f7eb9b8 commit fc16bd7

File tree

9 files changed

+224
-149
lines changed

9 files changed

+224
-149
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SET NAMES utf8;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
SET NAMES utf8;
2+
CREATE TABLE `partition_customization_config` (
3+
`id` int(11) NOT NULL AUTO_INCREMENT,
4+
`bk_biz_id` int(11) NOT NULL,
5+
`immute_domain` varchar(255) NOT NULL DEFAULT '',
6+
`partition_column` varchar(255) NOT NULL DEFAULT '',
7+
PRIMARY KEY (`id`),
8+
KEY `bk_biz_id` (`bk_biz_id`)
9+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

dbm-services/mysql/db-partition/handler/handler.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
// Package handler TODO
1+
// Package handler TODOG
22
package handler
33

44
import (
5-
"dbm-services/mysql/db-partition/model"
6-
"dbm-services/mysql/db-partition/monitor"
75
"errors"
86
"fmt"
97
"log/slog"
108
"net/http"
119
_ "runtime/debug" // debug TODO
1210
"time"
1311

12+
"dbm-services/mysql/db-partition/model"
13+
"dbm-services/mysql/db-partition/monitor"
14+
1415
cron_pkg "github.com/robfig/cron/v3"
1516

1617
"dbm-services/common/go-pubpkg/errno"
@@ -185,7 +186,7 @@ func DisablePartitionByCluster(r *gin.Context) {
185186
SendResponse(r, err, nil)
186187
return
187188
}
188-
slog.Info(fmt.Sprintf("ids: %v, operator: %s", input.Ids, input.Operator))
189+
slog.Info(fmt.Sprintf("cluster_ids: %v, operator: %s", input.ClusterIds, input.Operator))
189190
err := input.DisablePartitionConfigByCluster()
190191
if err != nil {
191192
slog.Error(err.Error())

dbm-services/mysql/db-partition/main.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package main
22

33
import (
4+
"net/http"
5+
"os"
6+
47
"dbm-services/mysql/db-partition/monitor"
58
"dbm-services/mysql/db-partition/service"
69
"dbm-services/mysql/db-partition/util"
7-
"net/http"
8-
"os"
910

1011
"github.com/gin-gonic/gin"
1112
"github.com/golang-migrate/migrate/v4"
@@ -82,4 +83,5 @@ func init() {
8283
model.DB.Init()
8384
model.InitClient()
8485
model.InitBkRepo()
86+
model.InitCustimazation()
8587
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
3+
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
4+
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at https://opensource.org/licenses/MIT
6+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
7+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
8+
* specific language governing permissions and limitations under the License.
9+
*/
10+
11+
package model
12+
13+
import (
14+
"log/slog"
15+
16+
"gorm.io/gorm"
17+
"gorm.io/gorm/logger"
18+
)
19+
20+
var CustimazationMap map[int64]string
21+
22+
// Custimazation TODO
23+
type Custimazation struct {
24+
id int `gorm:"column:id"`
25+
BkBizId int64 `json:"bk_biz_id" gorm:"column:bk_biz_id"`
26+
PartitionColumn string `json:"partition_column" gorm:"column:partition_column"`
27+
ImmuteDomain string `json:"immute_domain" gorm:"column:immute_domain"`
28+
}
29+
30+
func InitCustimazation() {
31+
CustimazationMap = make(map[int64]string)
32+
custimazations := []Custimazation{}
33+
result := DB.Self.Session(&gorm.Session{
34+
Logger: logger.Default.LogMode(logger.Info),
35+
}).Table("partition_customization_config").Find(&custimazations)
36+
if result.Error != nil {
37+
slog.Error("定制化配置读取失败!", result.Error)
38+
}
39+
for _, cs := range custimazations {
40+
if cs.ImmuteDomain != "" {
41+
CustimazationMap[cs.BkBizId] = cs.ImmuteDomain
42+
} else if cs.PartitionColumn != "" {
43+
CustimazationMap[cs.BkBizId] = cs.PartitionColumn
44+
}
45+
}
46+
}

dbm-services/mysql/db-partition/service/check_partition_base_func.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,9 @@ func (config *PartitionConfig) GetDbTableInfo(fromCron bool, host Host) (ptlist
146146
slog.Error("GetDbTableInfo", sql, err.Error())
147147
return nil, err
148148
}
149-
// (1)兼容【分区字段为空】的历史问题,对于某些特殊的分区类型,旧系统已经不在页面上支持,所以旧系统有意将分区字段留空,
150-
// 使其无法在页面编辑,避免改变了其他所属分区类别,因此无法核对比较,但不影响新增和删除分区。
151-
// (2)兼容web、dnf业务的特殊定制类型,分区字段类型为int,但是系统记录为timestamp,因此无法核对比较,但不影响新增和删除分区。
152-
// (3)兼容minigame业务的特殊定制类型,分区类型为0,但是实际定义与分区类型存在差异,因此无法核对比较,但不影响新增和删除分区。
153-
webCustomization := config.BkBizId == 159 && config.PartitionColumn == "Fcreate_time"
154-
iegamsCustomization := config.BkBizId == 5016839 && config.PartitionColumn == "Fcreate_time"
155-
minigameCustomization := config.BkBizId == 121 && config.ImmuteDomain == "gamedb.game-record.minigame.db"
156-
dnfCustomization := config.BkBizId == 105 && config.PartitionColumn == "occ_date"
157-
if config.PartitionColumn != "" && !webCustomization && !iegamsCustomization &&
158-
!minigameCustomization && !dnfCustomization {
149+
150+
flag := config.CustomizationCheck()
151+
if flag {
159152
// 分区表至少会有一个分区
160153
for _, v := range output.CmdResults[0].TableData {
161154
// 如果发现分区字段、分区间隔与规则不符合,需要重新做分区,页面调整了分区规则
@@ -215,6 +208,19 @@ func (config *PartitionConfig) GetDbTableInfo(fromCron bool, host Host) (ptlist
215208
return ptlist, nil
216209
}
217210

211+
func (config *PartitionConfig) CustomizationCheck() (flag bool) {
212+
// (1)兼容【分区字段为空】的历史问题,对于某些特殊的分区类型,旧系统已经不在页面上支持,所以旧系统有意将分区字段留空,
213+
// 使其无法在页面编辑,避免改变了其他所属分区类别,因此无法核对比较,但不影响新增和删除分区。
214+
// (2)兼容web、dnf业务的特殊定制类型,分区字段类型为int,但是系统记录为timestamp,因此无法核对比较,但不影响新增和删除分区。
215+
// (3)兼容minigame业务的特殊定制类型,分区类型为0,但是实际定义与分区类型存在差异,因此无法核对比较,但不影响新增和删除分区。
216+
var custFlag bool
217+
if val, ok := model.CustimazationMap[config.BkBizId]; ok {
218+
custFlag = config.PartitionColumn == val || config.ImmuteDomain == val
219+
}
220+
flag = config.PartitionColumn != "" && !custFlag
221+
return flag
222+
}
223+
218224
func CheckPartitionExpression(expression, method, column string, partitionType int) (bool, error) {
219225
columnWithBackquote := fmt.Sprintf("`%s`", column)
220226
switch partitionType {

dbm-services/mysql/db-partition/service/cron_basic_func.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ package service
1212

1313
import (
1414
"context"
15-
"dbm-services/common/go-pubpkg/errno"
16-
"dbm-services/mysql/db-partition/util"
1715
"encoding/json"
1816
"fmt"
1917
"log/slog"
@@ -23,6 +21,9 @@ import (
2321
"sync"
2422
"time"
2523

24+
"dbm-services/common/go-pubpkg/errno"
25+
"dbm-services/mysql/db-partition/util"
26+
2627
"golang.org/x/time/rate"
2728

2829
"dbm-services/mysql/db-partition/model"
@@ -357,7 +358,8 @@ func GetTendbclusterInstances(cluster string) (map[string][]SpiderNode, int, err
357358
domain := tmp[0]
358359
port, _ := strconv.Atoi(tmp[1])
359360
cloud, _ := strconv.Atoi(tmp[2])
360-
address := fmt.Sprintf("%s:%d", domain, port)
361+
// port加1000,去任意中控查询集群信息
362+
address := fmt.Sprintf("%s:%d", domain, port+1000)
361363
var splitCnt int
362364
var tdbctlPrimary string
363365
// 查询tdbctl

0 commit comments

Comments
 (0)