Skip to content

Commit

Permalink
fix: 集群维度启用/禁用分区配置 #3465
Browse files Browse the repository at this point in the history
  • Loading branch information
xfan0805 authored and iSecloud committed Mar 6, 2024
1 parent 636b135 commit 156ef83
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 4 deletions.
40 changes: 40 additions & 0 deletions dbm-services/mysql/db-partition/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,26 @@ func DisablePartition(r *gin.Context) {
return
}

// DisablePartitionByCluster 用于集群禁用时停止分区,标志为 offlinewithclu
func DisablePartitionByCluster(r *gin.Context) {
var input service.DisablePartitionInput
if err := r.ShouldBind(&input); err != nil {
err = errno.ErrReadEntity.Add(err.Error())
slog.Error(err.Error())
SendResponse(r, err, nil)
return
}
slog.Info(fmt.Sprintf("ids: %v, operator: %s", input.Ids, input.Operator))
err := input.DisablePartitionConfigByCluster()
if err != nil {
slog.Error(err.Error())
SendResponse(r, errors.New(fmt.Sprintf("分区禁用失败!%s", err.Error())), nil)
return
}
SendResponse(r, nil, "分区禁用成功!")
return
}

// EnablePartition TODO
func EnablePartition(r *gin.Context) {
var input service.EnablePartitionInput
Expand All @@ -199,6 +219,26 @@ func EnablePartition(r *gin.Context) {
return
}

// EnablePartitionByCluster 集群启用时启用分区
func EnablePartitionByCluster(r *gin.Context) {
var input service.EnablePartitionInput
if err := r.ShouldBind(&input); err != nil {
err = errno.ErrReadEntity.Add(err.Error())
slog.Error(err.Error())
SendResponse(r, err, nil)
return
}
slog.Info(fmt.Sprintf("ids: %v, operator: %s", input.Ids, input.Operator))
err := input.EnablePartitionByCluster()
if err != nil {
slog.Error(err.Error())
SendResponse(r, errors.New(fmt.Sprintf("分区启用失败!%s", err.Error())), nil)
return
}
SendResponse(r, nil, "分区启用成功!")
return
}

// UpdatePartitionsConfig TODO
func UpdatePartitionsConfig(r *gin.Context) {
var input service.CreatePartitionsInput
Expand Down
8 changes: 4 additions & 4 deletions dbm-services/mysql/db-partition/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"net/http"
"os"

"dbm-services/common/go-pubpkg/apm/metric"
"dbm-services/common/go-pubpkg/apm/trace"
"dbm-services/mysql/db-partition/monitor"

"github.com/gin-gonic/gin"
"github.com/golang-migrate/migrate/v4"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
"golang.org/x/exp/slog"

"dbm-services/common/go-pubpkg/apm/metric"
"dbm-services/common/go-pubpkg/apm/trace"
"dbm-services/mysql/db-partition/monitor"

"dbm-services/mysql/db-partition/assests"
"dbm-services/mysql/db-partition/cron"
"dbm-services/mysql/db-partition/model"
Expand Down
2 changes: 2 additions & 0 deletions dbm-services/mysql/db-partition/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func RegisterRouter(engine *gin.Engine) {
p.POST("/dry_run", handler.DryRun)
p.POST("/disable_partition", handler.DisablePartition)
p.POST("/enable_partition", handler.EnablePartition)
p.POST("/disable_partition_cluster", handler.DisablePartitionByCluster)
p.POST("/enable_partition_cluster", handler.EnablePartitionByCluster)
// 更新分区配置
p.POST("/update_conf", handler.UpdatePartitionsConfig)
p.POST("/create_log", handler.CreatePartitionLog)
Expand Down
72 changes: 72 additions & 0 deletions dbm-services/mysql/db-partition/service/manage_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,42 @@ func (m *DisablePartitionInput) DisablePartitionConfig() error {
return nil
}

// DisablePartitionConfigByCluster TODO
func (m *DisablePartitionInput) DisablePartitionConfigByCluster() error {
if len(m.ClusterIds) == 0 {
return errno.ConfigIdIsEmpty
}
var tbName string
// 判断是mysql集群还是spider集群
var logTbName string
switch strings.ToLower(m.ClusterType) {
case Tendbha, Tendbsingle:
tbName = MysqlPartitionConfig
logTbName = MysqlManageLogsTable
case Tendbcluster:
tbName = SpiderPartitionConfig
logTbName = SpiderManageLogsTable
default:
return errors.New("不支持的db类型")
}
var list []string
for _, item := range m.ClusterIds {
list = append(list, strconv.FormatInt(int64(item), 10))

}
db := model.DB.Self.Table(tbName)
result := db.
Where(fmt.Sprintf("cluster_id in (%s)", strings.Join(list, ","))).
Update("phase", offlinewithclu)
if result.Error != nil {
return result.Error
}
for _, id := range m.Ids {
CreateManageLog(tbName, logTbName, id, "DisableByCluster", m.Operator)
}
return nil
}

// EnablePartitionConfig TODO
func (m *EnablePartitionInput) EnablePartitionConfig() error {
if len(m.Ids) == 0 {
Expand Down Expand Up @@ -525,6 +561,42 @@ func (m *EnablePartitionInput) EnablePartitionConfig() error {
return nil
}

// EnablePartitionByCluster TODO
func (m *EnablePartitionInput) EnablePartitionByCluster() error {
if len(m.ClusterIds) == 0 {
return errno.ConfigIdIsEmpty
}
var tbName string
// 判断是mysql集群还是spider集群
var logTbName string
switch strings.ToLower(m.ClusterType) {
case Tendbha, Tendbsingle:
tbName = MysqlPartitionConfig
logTbName = MysqlManageLogsTable
case Tendbcluster:
tbName = SpiderPartitionConfig
logTbName = SpiderManageLogsTable
default:
return errors.New("不支持的db类型")
}
var list []string
for _, item := range m.ClusterIds {
list = append(list, strconv.FormatInt(int64(item), 10))

}
db := model.DB.Self.Table(tbName)
result := db.
Where(fmt.Sprintf("cluster_id in (%s)", strings.Join(list, ","))).
Update("phase", online)
if result.Error != nil {
return result.Error
}
for _, id := range m.Ids {
CreateManageLog(tbName, logTbName, id, "EnableByCluster", m.Operator)
}
return nil
}

func (m *CreatePartitionsInput) compareWithSameArray() (warnings []string, err error) {
l := len(m.DbLikes)
for i := 0; i < l; i++ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const SpiderPartitionCronLogTable = "spider_partition_cron_log"

const online = "online"
const offline = "offline"
const offlinewithclu = "offlinewithclu"
const extraTime = 15

// MysqlManageLogsTable TODO
Expand Down Expand Up @@ -99,13 +100,15 @@ type DisablePartitionInput struct {
ClusterType string `json:"cluster_type"`
Operator string `json:"operator"`
Ids []int `json:"ids"`
ClusterIds []int `json:"cluster_ids"`
}

// EnablePartitionInput TODO
type EnablePartitionInput struct {
ClusterType string `json:"cluster_type"`
Operator string `json:"operator"`
Ids []int `json:"ids"`
ClusterIds []int `json:"cluster_ids"`
}

// ManageLog 审计分区管理行为
Expand Down

0 comments on commit 156ef83

Please sign in to comment.