diff --git a/dbm-services/mysql/db-partition/handler/handler.go b/dbm-services/mysql/db-partition/handler/handler.go index 1957ab9d6d..06b553224a 100644 --- a/dbm-services/mysql/db-partition/handler/handler.go +++ b/dbm-services/mysql/db-partition/handler/handler.go @@ -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 @@ -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 diff --git a/dbm-services/mysql/db-partition/main.go b/dbm-services/mysql/db-partition/main.go index 238e77974e..56c50079df 100644 --- a/dbm-services/mysql/db-partition/main.go +++ b/dbm-services/mysql/db-partition/main.go @@ -4,10 +4,6 @@ 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" @@ -15,6 +11,10 @@ import ( "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" diff --git a/dbm-services/mysql/db-partition/router/router.go b/dbm-services/mysql/db-partition/router/router.go index 2738acf5e8..3689e3d4b6 100644 --- a/dbm-services/mysql/db-partition/router/router.go +++ b/dbm-services/mysql/db-partition/router/router.go @@ -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) diff --git a/dbm-services/mysql/db-partition/service/manage_config.go b/dbm-services/mysql/db-partition/service/manage_config.go index 9ece0b2cf5..27d4b672c9 100644 --- a/dbm-services/mysql/db-partition/service/manage_config.go +++ b/dbm-services/mysql/db-partition/service/manage_config.go @@ -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 { @@ -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++ { diff --git a/dbm-services/mysql/db-partition/service/manage_config_object.go b/dbm-services/mysql/db-partition/service/manage_config_object.go index 3e5100e429..f464df6673 100644 --- a/dbm-services/mysql/db-partition/service/manage_config_object.go +++ b/dbm-services/mysql/db-partition/service/manage_config_object.go @@ -16,6 +16,7 @@ const SpiderPartitionCronLogTable = "spider_partition_cron_log" const online = "online" const offline = "offline" +const offlinewithclu = "offlinewithclu" const extraTime = 15 // MysqlManageLogsTable TODO @@ -99,6 +100,7 @@ type DisablePartitionInput struct { ClusterType string `json:"cluster_type"` Operator string `json:"operator"` Ids []int `json:"ids"` + ClusterIds []int `json:"cluster_ids"` } // EnablePartitionInput TODO @@ -106,6 +108,7 @@ type EnablePartitionInput struct { ClusterType string `json:"cluster_type"` Operator string `json:"operator"` Ids []int `json:"ids"` + ClusterIds []int `json:"cluster_ids"` } // ManageLog 审计分区管理行为