refactor(manager): modify mysql table schema, orm json tag. (#283)

Signed-off-by: hanson.yj <hanson.yj@alibaba-inc.com>

Co-authored-by: hanson.yj <hanson.yj@alibaba-inc.com>
This commit is contained in:
yangjun289519474 2021-06-04 11:03:44 +08:00 committed by Gaius
parent 64807eed8d
commit 1fb834edc2
No known key found for this signature in database
GPG Key ID: 8B4E5D1290FA2FFB
12 changed files with 91 additions and 79 deletions

1
go.mod
View File

@ -21,6 +21,7 @@ require (
github.com/golang/protobuf v1.4.3 github.com/golang/protobuf v1.4.3
github.com/google/uuid v1.1.5 github.com/google/uuid v1.1.5
github.com/gorilla/mux v1.7.3 github.com/gorilla/mux v1.7.3
github.com/iancoleman/strcase v0.1.3
github.com/mailru/easyjson v0.7.7 // indirect github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-runewidth v0.0.9 github.com/mattn/go-runewidth v0.0.9
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db

2
go.sum
View File

@ -231,6 +231,8 @@ github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0m
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/iancoleman/strcase v0.1.3 h1:dJBk1m2/qjL1twPLf68JND55vvivMupZ4wIzE8CTdBw=
github.com/iancoleman/strcase v0.1.3/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=

View File

@ -200,8 +200,8 @@ func (handler *Handler) ListSchedulerClusters(ctx *gin.Context) {
} }
func checkSchedulerClusterValidate(cluster *types.SchedulerCluster) (err error) { func checkSchedulerClusterValidate(cluster *types.SchedulerCluster) (err error) {
var schedulerConfigMap map[string]string var configMap map[string]string
err = json.Unmarshal([]byte(cluster.SchedulerConfig), &schedulerConfigMap) err = json.Unmarshal([]byte(cluster.Config), &configMap)
if err != nil { if err != nil {
err = errors.New("unmarshal scheduler_config error: scheduler_config must map[string]string") err = errors.New("unmarshal scheduler_config error: scheduler_config must map[string]string")
return return

View File

@ -2,7 +2,7 @@ package types
type SchedulerCluster struct { type SchedulerCluster struct {
ClusterID string `json:"cluster_id" binding:"omitempty"` ClusterID string `json:"cluster_id" binding:"omitempty"`
SchedulerConfig string `json:"scheduler_config" binding:"required"` Config string `json:"config" binding:"required"`
ClientConfig string `json:"client_config" binding:"required"` ClientConfig string `json:"client_config" binding:"required"`
Version int64 `json:"version" binding:"omitempty"` Version int64 `json:"version" binding:"omitempty"`
Creator string `json:"creator" binding:"omitempty"` Creator string `json:"creator" binding:"omitempty"`

View File

@ -42,7 +42,7 @@ func (suite *ServerTestSuite) testDefaultSchedulerCluster() *types.SchedulerClus
clientConfigBytes, _ := json.Marshal(&clientConfigMap) clientConfigBytes, _ := json.Marshal(&clientConfigMap)
return &types.SchedulerCluster{ return &types.SchedulerCluster{
SchedulerConfig: string(schedulerConfigBytes), Config: string(schedulerConfigBytes),
ClientConfig: string(clientConfigBytes), ClientConfig: string(clientConfigBytes),
} }
} }
@ -269,7 +269,7 @@ func (suite *ServerTestSuite) TestGetSchedulerClusterConfig() {
assert.Nil(err) assert.Nil(err)
assert.NotNil(ret) assert.NotNil(ret)
cfg := ret.GetSchedulerConfig() cfg := ret.GetSchedulerConfig()
assert.Equal(cluster.SchedulerConfig, cfg.ClusterConfig) assert.Equal(cluster.Config, cfg.ClusterConfig)
assert.Equal(cluster.ClientConfig, cfg.ClientConfig) assert.Equal(cluster.ClientConfig, cfg.ClientConfig)
} }
@ -313,13 +313,13 @@ func (suite *ServerTestSuite) TestGetSchedulerClusterConfig() {
{ {
var schedulerConfigMap map[string]string var schedulerConfigMap map[string]string
err := json.Unmarshal([]byte(cluster.SchedulerConfig), &schedulerConfigMap) err := json.Unmarshal([]byte(cluster.Config), &schedulerConfigMap)
assert.Nil(err) assert.Nil(err)
schedulerConfigMap["CDN_CLUSTER_ID"] = cdnCluster.ClusterID schedulerConfigMap["CDN_CLUSTER_ID"] = cdnCluster.ClusterID
schedulerConfigByte, err := json.Marshal(schedulerConfigMap) schedulerConfigByte, err := json.Marshal(schedulerConfigMap)
assert.Nil(err) assert.Nil(err)
cluster.SchedulerConfig = string(schedulerConfigByte) cluster.Config = string(schedulerConfigByte)
suite.server.ms.UpdateSchedulerCluster(context.TODO(), cluster) suite.server.ms.UpdateSchedulerCluster(context.TODO(), cluster)
} }
@ -334,7 +334,7 @@ func (suite *ServerTestSuite) TestGetSchedulerClusterConfig() {
assert.Nil(err) assert.Nil(err)
assert.NotNil(ret) assert.NotNil(ret)
cfg := ret.GetSchedulerConfig() cfg := ret.GetSchedulerConfig()
assert.Equal(cluster.SchedulerConfig, cfg.ClusterConfig) assert.Equal(cluster.Config, cfg.ClusterConfig)
assert.Equal(cluster.ClientConfig, cfg.ClientConfig) assert.Equal(cluster.ClientConfig, cfg.ClientConfig)
cdnHost := cfg.GetCdnHosts() cdnHost := cfg.GetCdnHosts()
@ -407,25 +407,25 @@ func (suite *ServerTestSuite) TestSchedulerCluster() {
assert.NotNil(ret) assert.NotNil(ret)
assert.Nil(err) assert.Nil(err)
assert.Equal(cluster.ClusterID, ret.ClusterID) assert.Equal(cluster.ClusterID, ret.ClusterID)
assert.Equal(cluster.SchedulerConfig, ret.SchedulerConfig) assert.Equal(cluster.Config, ret.Config)
assert.Equal(cluster.ClientConfig, ret.ClientConfig) assert.Equal(cluster.ClientConfig, ret.ClientConfig)
} }
{ {
var schedulerConfigMap map[string]string var schedulerConfigMap map[string]string
err := json.Unmarshal([]byte(cluster.SchedulerConfig), &schedulerConfigMap) err := json.Unmarshal([]byte(cluster.Config), &schedulerConfigMap)
assert.Nil(err) assert.Nil(err)
schedulerConfigMap["schedulerConfig_a"] = "schedulerConfig_a_update" schedulerConfigMap["schedulerConfig_a"] = "schedulerConfig_a_update"
schedulerConfigByte, err := json.Marshal(schedulerConfigMap) schedulerConfigByte, err := json.Marshal(schedulerConfigMap)
assert.Nil(err) assert.Nil(err)
cluster.SchedulerConfig = string(schedulerConfigByte) cluster.Config = string(schedulerConfigByte)
ret, err := suite.server.ms.UpdateSchedulerCluster(context.TODO(), cluster) ret, err := suite.server.ms.UpdateSchedulerCluster(context.TODO(), cluster)
assert.NotNil(ret) assert.NotNil(ret)
assert.Nil(err) assert.Nil(err)
assert.Equal(cluster.ClusterID, ret.ClusterID) assert.Equal(cluster.ClusterID, ret.ClusterID)
assert.Equal(cluster.SchedulerConfig, ret.SchedulerConfig) assert.Equal(cluster.Config, ret.Config)
assert.Equal(cluster.ClientConfig, ret.ClientConfig) assert.Equal(cluster.ClientConfig, ret.ClientConfig)
} }
@ -436,7 +436,7 @@ func (suite *ServerTestSuite) TestSchedulerCluster() {
for i, c := range ret { for i, c := range ret {
if cluster.ClusterID == c.ClusterID { if cluster.ClusterID == c.ClusterID {
assert.Equal(cluster.ClusterID, ret[i].ClusterID) assert.Equal(cluster.ClusterID, ret[i].ClusterID)
assert.Equal(cluster.SchedulerConfig, ret[i].SchedulerConfig) assert.Equal(cluster.Config, ret[i].Config)
assert.Equal(cluster.ClientConfig, ret[i].ClientConfig) assert.Equal(cluster.ClientConfig, ret[i].ClientConfig)
} }
} }

View File

@ -142,17 +142,17 @@ func (ms *ManagerServer) GetClusterConfig(ctx context.Context, req *manager.GetC
} else if manager.ResourceType_Scheduler == req.GetType() { } else if manager.ResourceType_Scheduler == req.GetType() {
schedulerInstance := interInstance.(*types.SchedulerInstance) schedulerInstance := interInstance.(*types.SchedulerInstance)
schedulerCluster := interCluster.(*types.SchedulerCluster) schedulerCluster := interCluster.(*types.SchedulerCluster)
schedulerConfigsMap := make(map[string]interface{}) configsMap := make(map[string]interface{})
err := json.Unmarshal([]byte(schedulerCluster.SchedulerConfig), &schedulerConfigsMap) err := json.Unmarshal([]byte(schedulerCluster.Config), &configsMap)
if err != nil { if err != nil {
return nil, err return nil, err
} }
CDNClusterID, exist := schedulerConfigsMap["CDN_CLUSTER_ID"] CDNClusterID, exist := configsMap["CDN_CLUSTER_ID"]
if !exist { if !exist {
return &manager.ClusterConfig{Config: &manager.ClusterConfig_SchedulerConfig{SchedulerConfig: &manager.SchedulerConfig{ return &manager.ClusterConfig{Config: &manager.ClusterConfig_SchedulerConfig{SchedulerConfig: &manager.SchedulerConfig{
ClusterId: schedulerCluster.ClusterID, ClusterId: schedulerCluster.ClusterID,
ClusterConfig: schedulerCluster.SchedulerConfig, ClusterConfig: schedulerCluster.Config,
ClientConfig: schedulerCluster.ClientConfig, ClientConfig: schedulerCluster.ClientConfig,
ClusterVersion: schedulerCluster.Version, ClusterVersion: schedulerCluster.Version,
InstanceConfig: schedulerInstance.IDC, // todo InstanceConfig format InstanceConfig: schedulerInstance.IDC, // todo InstanceConfig format
@ -218,7 +218,7 @@ func (ms *ManagerServer) GetClusterConfig(ctx context.Context, req *manager.GetC
return &manager.ClusterConfig{Config: &manager.ClusterConfig_SchedulerConfig{SchedulerConfig: &manager.SchedulerConfig{ return &manager.ClusterConfig{Config: &manager.ClusterConfig_SchedulerConfig{SchedulerConfig: &manager.SchedulerConfig{
ClusterId: schedulerCluster.ClusterID, ClusterId: schedulerCluster.ClusterID,
ClusterConfig: schedulerCluster.SchedulerConfig, ClusterConfig: schedulerCluster.Config,
ClientConfig: schedulerCluster.ClientConfig, ClientConfig: schedulerCluster.ClientConfig,
ClusterVersion: schedulerCluster.Version, ClusterVersion: schedulerCluster.Version,
InstanceConfig: schedulerInstance.IDC, // todo InstanceConfig format InstanceConfig: schedulerInstance.IDC, // todo InstanceConfig format

View File

@ -15,10 +15,12 @@ import (
type CDNClusterTable struct { type CDNClusterTable struct {
ID uint `gorm:"column:id;primaryKey"` ID uint `gorm:"column:id;primaryKey"`
ClusterID string `gorm:"column:cluster_id;unique;size:63"` ClusterID string `gorm:"column:cluster_id;unique;size:63"`
Config string `gorm:"column:config;size:4095"` Config string `gorm:"column:config;type:text;not null"`
Creator string `gorm:"column:creator;size:31"` Creator string `gorm:"column:creator;size:31"`
Modifier string `gorm:"column:modifier;size:31"` Modifier string `gorm:"column:modifier;size:31"`
Enable bool `gorm:"column:enable;type:bool;"`
Version int64 `gorm:"column:version"` Version int64 `gorm:"column:version"`
Description string `gorm:"column:description;size:1023"`
CreatedAt time.Time `gorm:"column:created_at"` CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"` UpdatedAt time.Time `gorm:"column:updated_at"`
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index"` DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index"`

View File

@ -17,7 +17,7 @@ type CDNInstanceTable struct {
InstanceID string `gorm:"column:instance_id;unique;size:63"` InstanceID string `gorm:"column:instance_id;unique;size:63"`
ClusterID string `gorm:"column:cluster_id;size:63"` ClusterID string `gorm:"column:cluster_id;size:63"`
IDC string `gorm:"column:idc;size:63"` IDC string `gorm:"column:idc;size:63"`
Location string `gorm:"column:location;size:4095"` Location string `gorm:"column:location;type:text;not null"`
HostName string `gorm:"column:host_name;size:63"` HostName string `gorm:"column:host_name;size:63"`
IP string `gorm:"column:ip;size:31"` IP string `gorm:"column:ip;size:31"`
Port int32 `gorm:"column:port"` Port int32 `gorm:"column:port"`
@ -25,6 +25,7 @@ type CDNInstanceTable struct {
DownPort int32 `gorm:"column:down_port"` DownPort int32 `gorm:"column:down_port"`
State string `gorm:"column:state;size:15"` State string `gorm:"column:state;size:15"`
Version int64 `gorm:"column:version"` Version int64 `gorm:"column:version"`
Description string `gorm:"column:description;size:1023"`
CreatedAt time.Time `gorm:"column:created_at"` CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"` UpdatedAt time.Time `gorm:"column:updated_at"`
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index"` DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index"`

View File

@ -8,6 +8,7 @@ import (
"d7y.io/dragonfly/v2/manager/store" "d7y.io/dragonfly/v2/manager/store"
"d7y.io/dragonfly/v2/pkg/dfcodes" "d7y.io/dragonfly/v2/pkg/dfcodes"
"d7y.io/dragonfly/v2/pkg/dferrors" "d7y.io/dragonfly/v2/pkg/dferrors"
"github.com/iancoleman/strcase"
"github.com/xo/dburl" "github.com/xo/dburl"
"gorm.io/driver/mysql" "gorm.io/driver/mysql"
"gorm.io/gorm" "gorm.io/gorm"
@ -65,7 +66,7 @@ func newOrmStore(cfg *config.StoreConfig) (*ormStore, error) {
} }
for t, f := range ormTables { for t, f := range ormTables {
table := t.String() table := strcase.ToSnake(t.String())
s, err := f(db, table) s, err := f(db, table)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -15,11 +15,13 @@ import (
type SchedulerClusterTable struct { type SchedulerClusterTable struct {
ID uint `gorm:"column:id;primaryKey"` ID uint `gorm:"column:id;primaryKey"`
ClusterID string `gorm:"column:cluster_id;unique;size:63"` ClusterID string `gorm:"column:cluster_id;unique;size:63"`
SchedulerConfig string `gorm:"column:scheduler_config;size:4095"` Config string `gorm:"column:config;type:text;not null"`
ClientConfig string `gorm:"column:client_config;size:4095"` ClientConfig string `gorm:"column:client_config;type:text;not null"`
Creator string `gorm:"column:creator;size:31"` Creator string `gorm:"column:creator;size:31"`
Modifier string `gorm:"column:modifier;size:31"` Modifier string `gorm:"column:modifier;size:31"`
Enable bool `gorm:"column:enable;type:bool"`
Version int64 `gorm:"column:version"` Version int64 `gorm:"column:version"`
Description string `gorm:"column:description;size:1023"`
CreatedAt time.Time `gorm:"column:created_at"` CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"` UpdatedAt time.Time `gorm:"column:updated_at"`
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index"` DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index"`
@ -49,7 +51,7 @@ func NewSchedulerClusterStore(db *gorm.DB, table string) (store.Store, error) {
func SchedulerClusterToTable(t *types.SchedulerCluster) *SchedulerClusterTable { func SchedulerClusterToTable(t *types.SchedulerCluster) *SchedulerClusterTable {
return &SchedulerClusterTable{ return &SchedulerClusterTable{
ClusterID: t.ClusterID, ClusterID: t.ClusterID,
SchedulerConfig: t.SchedulerConfig, Config: t.Config,
ClientConfig: t.ClientConfig, ClientConfig: t.ClientConfig,
Creator: t.Creator, Creator: t.Creator,
Modifier: t.Modifier, Modifier: t.Modifier,
@ -60,7 +62,7 @@ func SchedulerClusterToTable(t *types.SchedulerCluster) *SchedulerClusterTable {
func SchedulerClusterToSchema(t *SchedulerClusterTable) *types.SchedulerCluster { func SchedulerClusterToSchema(t *SchedulerClusterTable) *types.SchedulerCluster {
return &types.SchedulerCluster{ return &types.SchedulerCluster{
ClusterID: t.ClusterID, ClusterID: t.ClusterID,
SchedulerConfig: t.SchedulerConfig, Config: t.Config,
ClientConfig: t.ClientConfig, ClientConfig: t.ClientConfig,
Creator: t.Creator, Creator: t.Creator,
Modifier: t.Modifier, Modifier: t.Modifier,
@ -71,7 +73,7 @@ func SchedulerClusterToSchema(t *SchedulerClusterTable) *types.SchedulerCluster
func (s *SchedulerClusterStore) updateSchemaToTable(new, old *SchedulerClusterTable) *SchedulerClusterTable { func (s *SchedulerClusterStore) updateSchemaToTable(new, old *SchedulerClusterTable) *SchedulerClusterTable {
new.ID = old.ID new.ID = old.ID
if new.SchedulerConfig != old.SchedulerConfig || new.ClientConfig != old.ClientConfig { if new.Config != old.Config || new.ClientConfig != old.ClientConfig {
new.Version = time.Now().UnixNano() new.Version = time.Now().UnixNano()
} else { } else {
new.Version = old.Version new.Version = old.Version

View File

@ -17,15 +17,16 @@ type SchedulerInstanceTable struct {
InstanceID string `gorm:"column:instance_id;unique;size:63"` InstanceID string `gorm:"column:instance_id;unique;size:63"`
ClusterID string `gorm:"column:cluster_id;size:63"` ClusterID string `gorm:"column:cluster_id;size:63"`
SecurityDomain string `gorm:"column:security_domain;size:63"` SecurityDomain string `gorm:"column:security_domain;size:63"`
VIPs string `gorm:"column:vips;size:4095"` VIPs string `gorm:"column:vips;type:text;not null"`
IDC string `gorm:"column:idc;size:63"` IDC string `gorm:"column:idc;size:63"`
Location string `gorm:"column:location;size:4095"` Location string `gorm:"column:location;type:text;not null"`
NetConfig string `gorm:"column:net_config;size:4095"` NetConfig string `gorm:"column:net_config;type:text;not null"`
HostName string `gorm:"column:host_name;size:63"` HostName string `gorm:"column:host_name;size:63"`
IP string `gorm:"column:ip;size:31"` IP string `gorm:"column:ip;size:31"`
Port int32 `gorm:"column:port"` Port int32 `gorm:"column:port"`
State string `gorm:"column:state;size:15"` State string `gorm:"column:state;size:15"`
Version int64 `gorm:"column:version"` Version int64 `gorm:"column:version"`
Description string `gorm:"column:description;size:1023"`
CreatedAt time.Time `gorm:"column:created_at"` CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"` UpdatedAt time.Time `gorm:"column:updated_at"`
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index"` DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index"`

View File

@ -16,10 +16,12 @@ type SecurityDomainTable struct {
ID uint `gorm:"column:id;primaryKey"` ID uint `gorm:"column:id;primaryKey"`
SecurityDomain string `gorm:"column:security_domain;unique;size:63"` SecurityDomain string `gorm:"column:security_domain;unique;size:63"`
DisplayName string `gorm:"column:display_name;size:63"` DisplayName string `gorm:"column:display_name;size:63"`
ProxyDomain string `gorm:"column:proxy_domain;size:4095"` ProxyDomain string `gorm:"column:proxy_domain;type:text;not null"`
Config string `gorm:"column:config;type:text;not null"`
Creator string `gorm:"column:creator;size:31"` Creator string `gorm:"column:creator;size:31"`
Modifier string `gorm:"column:modifier;size:31"` Modifier string `gorm:"column:modifier;size:31"`
Version int64 `gorm:"column:version"` Version int64 `gorm:"column:version"`
Description string `gorm:"column:description;size:1023"`
CreatedAt time.Time `gorm:"column:created_at"` CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"` UpdatedAt time.Time `gorm:"column:updated_at"`
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index"` DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index"`