From fb31e201a56e4f30cbbebf8492477fa90e415317 Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Thu, 10 Mar 2022 18:04:43 +0800 Subject: [PATCH 01/13] Modify proto file --- sqle/driver/proto/driver.pb.go | 134 +++++++++++++++++++-------------- sqle/driver/proto/driver.proto | 4 +- 2 files changed, 79 insertions(+), 59 deletions(-) diff --git a/sqle/driver/proto/driver.pb.go b/sqle/driver/proto/driver.pb.go index db17b5f033..f20172cc9b 100644 --- a/sqle/driver/proto/driver.pb.go +++ b/sqle/driver/proto/driver.pb.go @@ -51,11 +51,12 @@ var _ = math.Inf const _ = proto1.ProtoPackageIsVersion2 // please upgrade the proto package type DSN struct { - Host string `protobuf:"bytes,1,opt,name=host" json:"host,omitempty"` - Port string `protobuf:"bytes,2,opt,name=port" json:"port,omitempty"` - User string `protobuf:"bytes,3,opt,name=user" json:"user,omitempty"` - Password string `protobuf:"bytes,4,opt,name=password" json:"password,omitempty"` - Database string `protobuf:"bytes,5,opt,name=database" json:"database,omitempty"` + Host string `protobuf:"bytes,1,opt,name=host" json:"host,omitempty"` + Port string `protobuf:"bytes,2,opt,name=port" json:"port,omitempty"` + User string `protobuf:"bytes,3,opt,name=user" json:"user,omitempty"` + Password string `protobuf:"bytes,4,opt,name=password" json:"password,omitempty"` + Database string `protobuf:"bytes,5,opt,name=database" json:"database,omitempty"` + AdditionalParams []*Param `protobuf:"bytes,6,rep,name=additionalParams" json:"additionalParams,omitempty"` } func (m *DSN) Reset() { *m = DSN{} } @@ -98,6 +99,13 @@ func (m *DSN) GetDatabase() string { return "" } +func (m *DSN) GetAdditionalParams() []*Param { + if m != nil { + return m.AdditionalParams + } + return nil +} + type Rule struct { Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"` Desc string `protobuf:"bytes,2,opt,name=desc" json:"desc,omitempty"` @@ -491,8 +499,9 @@ func (m *GenRollbackSQLResponse) GetReason() string { } type MetasResponse struct { - Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"` - Rules []*Rule `protobuf:"bytes,2,rep,name=rules" json:"rules,omitempty"` + Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"` + Rules []*Rule `protobuf:"bytes,2,rep,name=rules" json:"rules,omitempty"` + AdditionalParams []*Param `protobuf:"bytes,3,rep,name=additionalParams" json:"additionalParams,omitempty"` } func (m *MetasResponse) Reset() { *m = MetasResponse{} } @@ -514,6 +523,13 @@ func (m *MetasResponse) GetRules() []*Rule { return nil } +func (m *MetasResponse) GetAdditionalParams() []*Param { + if m != nil { + return m.AdditionalParams + } + return nil +} + func init() { proto1.RegisterType((*DSN)(nil), "proto.DSN") proto1.RegisterType((*Rule)(nil), "proto.Rule") @@ -918,55 +934,57 @@ var _Driver_serviceDesc = grpc.ServiceDesc{ func init() { proto1.RegisterFile("driver.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 797 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x54, 0xdd, 0x4e, 0xf3, 0x46, - 0x10, 0x55, 0x62, 0x3b, 0x7c, 0x19, 0x87, 0x0a, 0x16, 0x8a, 0xac, 0x88, 0x4a, 0x61, 0x69, 0xa5, - 0xa0, 0x52, 0x50, 0xc3, 0x25, 0xe2, 0x02, 0x1a, 0x5a, 0x21, 0x95, 0x28, 0x35, 0x48, 0x95, 0x7a, - 0x67, 0xe2, 0x21, 0x8d, 0x70, 0x6c, 0x67, 0x77, 0x0d, 0xc9, 0xdb, 0xf4, 0x19, 0xfa, 0x64, 0x7d, - 0x84, 0x6a, 0xff, 0x1c, 0x9b, 0xc0, 0x77, 0xe5, 0x99, 0x33, 0xb3, 0x33, 0x67, 0xd7, 0x67, 0x06, - 0x3a, 0x31, 0x9b, 0xbd, 0x22, 0x3b, 0xcb, 0x59, 0x26, 0x32, 0xe2, 0xa9, 0x0f, 0x5d, 0x81, 0x33, - 0x7c, 0x18, 0x11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x29, 0x6a, 0x32, 0xbb, 0xfb, 0xc6, 0xab, 0xb7, 0xeb, 0x83, - 0x2b, 0x17, 0x01, 0xb1, 0x6f, 0x52, 0xd9, 0x0a, 0xdd, 0xda, 0x79, 0x72, 0x0c, 0xde, 0x2f, 0x49, - 0xc6, 0xf1, 0x5d, 0xd9, 0x7a, 0x12, 0x05, 0x77, 0x3c, 0x4b, 0xa7, 0x5f, 0xcd, 0x39, 0x07, 0x57, - 0xaa, 0xbf, 0x6c, 0x59, 0xd9, 0x17, 0xdd, 0x8f, 0xc6, 0x83, 0x9c, 0x40, 0xf3, 0x71, 0x49, 0x76, - 0x4c, 0xa8, 0x1c, 0xc2, 0xee, 0x6e, 0x05, 0x31, 0xa9, 0x17, 0xd0, 0x2e, 0x07, 0xe8, 0x1d, 0x89, - 0xc0, 0xae, 0xb5, 0x8d, 0x01, 0x1b, 0xa8, 0x2d, 0xcb, 0x91, 0xec, 0xad, 0x97, 0x70, 0x39, 0x50, - 0xe5, 0xbb, 0xd5, 0x95, 0x3e, 0x00, 0x4f, 0xc9, 0xa7, 0x3c, 0x53, 0x15, 0x75, 0x79, 0xa6, 0x2e, - 0xc4, 0x7b, 0xf8, 0xa6, 0xae, 0x17, 0x72, 0x68, 0xf2, 0x3e, 0x54, 0x5c, 0xf7, 0xbb, 0x4f, 0xa2, - 0xba, 0xdc, 0x0d, 0xfc, 0xf5, 0xe5, 0xec, 0xfc, 0x52, 0xa5, 0x3c, 0xb5, 0xd4, 0xe7, 0xe2, 0xff, - 0x00, 0x00, 0x00, 0xff, 0xff, 0x58, 0x6b, 0xde, 0xcc, 0x64, 0x07, 0x00, 0x00, + // 822 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x54, 0xdb, 0x6a, 0xe4, 0x46, + 0x10, 0x45, 0x23, 0x69, 0xbc, 0x53, 0x1a, 0x07, 0xbb, 0xd7, 0x59, 0xc4, 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xb6, 0xc9, 0x8d, 0x16, 0x9b, 0xbe, 0xf5, 0x45, 0x32, 0x87, 0x50, + 0x4f, 0x5e, 0x73, 0xa6, 0xab, 0x87, 0xe6, 0x4c, 0x7f, 0x86, 0x3f, 0xc1, 0x17, 0xfd, 0x51, 0x23, + 0x87, 0x36, 0x6f, 0xe3, 0xb0, 0x4e, 0xbe, 0xda, 0x12, 0x35, 0xe5, 0x8e, 0xe1, 0xb7, 0x67, 0x47, + 0x6f, 0xdf, 0xeb, 0x94, 0xeb, 0xa1, 0xfe, 0xbc, 0xfb, 0x37, 0x00, 0x00, 0xff, 0xff, 0x12, 0x6c, + 0xb3, 0x02, 0xda, 0x07, 0x00, 0x00, } diff --git a/sqle/driver/proto/driver.proto b/sqle/driver/proto/driver.proto index a764f72d29..e144bc3c86 100644 --- a/sqle/driver/proto/driver.proto +++ b/sqle/driver/proto/driver.proto @@ -27,12 +27,13 @@ message DSN { string user = 3; string password = 4; string database = 5; + repeated Param additionalParams = 6; } message Rule { string name = 1; string desc = 2; - string value = 3 [deprecated=true]; + string value = 3 [deprecated = true]; string level = 4; string category = 5; repeated Param params = 6; @@ -114,6 +115,7 @@ message GenRollbackSQLResponse { message MetasResponse { string name = 1; repeated Rule rules = 2; + repeated Param additionalParams = 3; } From c323fc6056336e83df2dde1b22f51e3256728c73 Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Thu, 10 Mar 2022 18:05:30 +0800 Subject: [PATCH 02/13] Modify model --- sqle/model/instance.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sqle/model/instance.go b/sqle/model/instance.go index d4fed35d1a..ad2f71505a 100644 --- a/sqle/model/instance.go +++ b/sqle/model/instance.go @@ -1,6 +1,7 @@ package model import ( + "encoding/json" "fmt" "strings" @@ -23,6 +24,7 @@ type Instance struct { SecretPassword string `json:"secret_password" gorm:"column:db_password; not null"` Desc string `json:"desc" example:"this is a instance"` WorkflowTemplateId uint `json:"workflow_template_id"` + AdditionalParams string `json:"additional_params" gorm:"type:text"` // relation table Roles []*Role `json:"-" gorm:"many2many:instance_role;"` @@ -30,6 +32,12 @@ type Instance struct { WorkflowTemplate *WorkflowTemplate `gorm:"foreignkey:WorkflowTemplateId"` } +func (i *Instance) GetAdditionalParams() map[string]interface{} { + mp := make(map[string]interface{}) + _ = json.Unmarshal([]byte(i.AdditionalParams), &mp) + return mp +} + // BeforeSave is a hook implement gorm model before exec create func (i *Instance) BeforeSave() error { return i.encryptPassword() From 716486b2b1046a1f909ba933a39f280b4971654f Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Thu, 10 Mar 2022 18:05:51 +0800 Subject: [PATCH 03/13] add proto util --- sqle/driver/proto/util.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 sqle/driver/proto/util.go diff --git a/sqle/driver/proto/util.go b/sqle/driver/proto/util.go new file mode 100644 index 0000000000..ffc2a6ac5c --- /dev/null +++ b/sqle/driver/proto/util.go @@ -0,0 +1,37 @@ +package proto + +import ( + "github.com/actiontech/sqle/sqle/pkg/params" +) + +func ParamToProtoParam(p []*params.Param) []*Param { + pp := make([]*Param, len(p)) + for i, v := range p { + if v == nil { + continue + } + pp[i] = &Param{ + Key: v.Key, + Value: v.Value, + Desc: v.Desc, + Type: string(v.Type), + } + } + return pp +} + +func ProtoParamToParam(p []*Param) []*params.Param { + pp := make([]*params.Param, len(p)) + for i, v := range p { + if v == nil { + continue + } + pp[i] = ¶ms.Param{ + Key: v.Key, + Value: v.Value, + Desc: v.Desc, + Type: params.ParamType(v.Type), + } + } + return pp +} From 7f4c34421732ccca9609201b41c649445f20c2af Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Thu, 10 Mar 2022 18:06:44 +0800 Subject: [PATCH 04/13] implement interface --- sqle/api/app.go | 1 + sqle/api/controller/v1/instance.go | 115 +++++++++++++++++++++++------ sqle/api/controller/v1/util.go | 11 ++- 3 files changed, 102 insertions(+), 25 deletions(-) diff --git a/sqle/api/app.go b/sqle/api/app.go index 93b666f7d7..3cb530c045 100644 --- a/sqle/api/app.go +++ b/sqle/api/app.go @@ -99,6 +99,7 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config config.SqleConfi // instance v1Router.POST("/instances", v1.CreateInstance, AdminUserAllowed()) + v1Router.GET("/instance_additional_metas", v1.GetInstanceAdditionalMetas, AdminUserAllowed()) v1Router.DELETE("/instances/:instance_name/", v1.DeleteInstance, AdminUserAllowed()) v1Router.PATCH("/instances/:instance_name/", v1.UpdateInstance, AdminUserAllowed()) diff --git a/sqle/api/controller/v1/instance.go b/sqle/api/controller/v1/instance.go index fc73f9096b..d475e2a1c4 100644 --- a/sqle/api/controller/v1/instance.go +++ b/sqle/api/controller/v1/instance.go @@ -2,7 +2,9 @@ package v1 import ( "context" + "encoding/json" "fmt" + "github.com/actiontech/sqle/sqle/pkg/params" "net/http" "github.com/actiontech/sqle/sqle/api/controller" @@ -45,7 +47,33 @@ type InstanceAdditionalParamResV1 struct { // @Success 200 {object} v1.GetInstanceAdditionalMetasResV1 // @router /v1/instance_additional_metas [get] func GetInstanceAdditionalMetas(c echo.Context) error { - return nil + additionalParams := driver.AllAdditionalParams() + res := &GetInstanceAdditionalMetasResV1{ + BaseRes: controller.NewBaseReq(nil), + Metas: []*InstanceAdditionalMetaV1{}, + } + for name, params := range additionalParams { + meta := &InstanceAdditionalMetaV1{ + DBType: name, + Params: ParamsSliceToInstanceAdditionalParamResV1Slice(params), + } + + res.Metas = append(res.Metas, meta) + } + return c.JSON(http.StatusOK, res) +} + +func ParamsSliceToInstanceAdditionalParamResV1Slice(params []*params.Param) []*InstanceAdditionalParamResV1 { + res := make([]*InstanceAdditionalParamResV1, len(params)) + for i, param := range params { + res[i] = &InstanceAdditionalParamResV1{ + Name: param.Key, + Description: param.Desc, + Type: string(param.Type), + Value: param.Value, + } + } + return res } type CreateInstanceReqV1 struct { @@ -94,14 +122,25 @@ func CreateInstance(c echo.Context) error { if req.DBType == "" { req.DBType = driver.DriverTypeMySQL } + + additionalParams := map[string]interface{}{} + for _, param := range req.AdditionalParams { + additionalParams[param.Name] = param.Value + } + additionalParamsBytes, err := json.Marshal(additionalParams) + if err != nil { + return controller.JSONBaseErrorReq(c, err) + } + instance := &model.Instance{ - DbType: req.DBType, - Name: req.Name, - User: req.User, - Host: req.Host, - Port: req.Port, - Password: req.Password, - Desc: req.Desc, + DbType: req.DBType, + Name: req.Name, + User: req.User, + Host: req.Host, + Port: req.Port, + Password: req.Password, + Desc: req.Desc, + AdditionalParams: string(additionalParamsBytes), } // set default workflow template if req.WorkflowTemplateName == "" { @@ -198,14 +237,15 @@ type GetInstanceResV1 struct { Data InstanceResV1 `json:"data"` } -func convertInstanceToRes(instance *model.Instance) InstanceResV1 { +func convertInstanceToRes(instance *model.Instance, p []*params.Param) InstanceResV1 { instanceResV1 := InstanceResV1{ - Name: instance.Name, - Host: instance.Host, - Port: instance.Port, - User: instance.User, - Desc: instance.Desc, - DBType: instance.DbType, + Name: instance.Name, + Host: instance.Host, + Port: instance.Port, + User: instance.User, + Desc: instance.Desc, + DBType: instance.DbType, + AdditionalParams: []*InstanceAdditionalParamResV1{}, } if instance.WorkflowTemplate != nil { instanceResV1.WorkflowTemplateName = instance.WorkflowTemplate.Name @@ -224,6 +264,15 @@ func convertInstanceToRes(instance *model.Instance) InstanceResV1 { } instanceResV1.Roles = roleNames } + additionalParams := instance.GetAdditionalParams() + for _, param := range p { + instanceResV1.AdditionalParams = append(instanceResV1.AdditionalParams, &InstanceAdditionalParamResV1{ + Name: param.Key, + Description: param.Desc, + Type: string(param.Type), + Value: fmt.Sprintf("%v", additionalParams[param.Key]), + }) + } return instanceResV1 } @@ -252,9 +301,11 @@ func GetInstance(c echo.Context) error { return controller.JSONBaseErrorReq(c, err) } + additionalParams := driver.AllAdditionalParams()[instance.DbType] + return c.JSON(http.StatusOK, &GetInstanceResV1{ BaseRes: controller.NewBaseReq(nil), - Data: convertInstanceToRes(instance), + Data: convertInstanceToRes(instance, additionalParams), }) } @@ -412,6 +463,19 @@ func UpdateInstance(c echo.Context) error { return controller.JSONBaseErrorReq(c, err) } } + if req.AdditionalParams != nil { + paramMap := instance.GetAdditionalParams() + for _, param := range req.AdditionalParams { + if param.Value != "" { + paramMap[param.Name] = param.Value + } + } + paramBytes, err := json.Marshal(paramMap) + if err != nil { + return controller.JSONBaseErrorReq(c, err) + } + updateMap["additional_params"] = string(paramBytes) + } err = s.UpdateInstanceById(instance.ID, updateMap) if err != nil { @@ -608,12 +672,21 @@ func CheckInstanceIsConnectable(c echo.Context) error { if req.DBType == "" { req.DBType = driver.DriverTypeMySQL } + mp := map[string]interface{}{} + for _, param := range req.AdditionalParams { + mp[param.Name] = param.Value + } + paramBytes, err := json.Marshal(mp) + if err != nil { + return controller.JSONBaseErrorReq(c, err) + } instance := &model.Instance{ - DbType: req.DBType, - User: req.User, - Host: req.Host, - Port: req.Port, - Password: req.Password, + DbType: req.DBType, + User: req.User, + Host: req.Host, + Port: req.Port, + Password: req.Password, + AdditionalParams: string(paramBytes), } return checkInstanceIsConnectable(c, instance) } diff --git a/sqle/api/controller/v1/util.go b/sqle/api/controller/v1/util.go index 0498861565..ec5999501e 100644 --- a/sqle/api/controller/v1/util.go +++ b/sqle/api/controller/v1/util.go @@ -3,6 +3,8 @@ package v1 import ( "github.com/actiontech/sqle/sqle/driver" "github.com/actiontech/sqle/sqle/model" + "github.com/actiontech/sqle/sqle/pkg/params" + "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -13,10 +15,11 @@ func newDriverWithoutAudit(l *logrus.Entry, inst *model.Instance, database strin } dsn := &driver.DSN{ - Host: inst.Host, - Port: inst.Port, - User: inst.User, - Password: inst.Password, + Host: inst.Host, + Port: inst.Port, + User: inst.User, + Password: inst.Password, + AdditionalParams: params.NewParamSliceFromMap(inst.GetAdditionalParams()), DatabaseName: database, } From 6cc60e3428d2c61a1d582031a0701de7caf9dc8b Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Thu, 10 Mar 2022 18:07:35 +0800 Subject: [PATCH 05/13] Modify driver --- sqle/driver/driver.go | 31 ++++++++++++++++++++++++++----- sqle/driver/mysql/mysql.go | 3 ++- sqle/driver/plugin.go | 27 +++++++++++++++------------ sqle/pkg/driver/adaptor.go | 22 ++++++++++++++++++---- 4 files changed, 61 insertions(+), 22 deletions(-) diff --git a/sqle/driver/driver.go b/sqle/driver/driver.go index 5cfa47057c..884224ba00 100644 --- a/sqle/driver/driver.go +++ b/sqle/driver/driver.go @@ -21,6 +21,10 @@ var ( // rules store audit rules for each driver. rules map[string][]*Rule rulesMu sync.RWMutex + + // additionalParams store driver additional params + additionalParams map[string][]*params.Param + additionalParamsMu sync.RWMutex ) const ( @@ -35,10 +39,11 @@ const ( // DSN provide necessary information to connect to database. type DSN struct { - Host string - Port string - User string - Password string + Host string + Port string + User string + Password string + AdditionalParams []*params.Param // DatabaseName is the default database to connect. DatabaseName string @@ -141,7 +146,7 @@ type handler func(log *logrus.Entry, c *Config) (Driver, error) // // Register makes a database driver available by the provided driver name. // Driver's initialize handler and audit rules register by Register. -func Register(name string, h handler, rs []*Rule) { +func Register(name string, h handler, rs []*Rule, ap []*params.Param) { _, exist := drivers[name] if exist { panic("duplicated driver name") @@ -157,6 +162,13 @@ func Register(name string, h handler, rs []*Rule) { } rules[name] = rs rulesMu.Unlock() + + additionalParamsMu.Lock() + if additionalParams == nil { + additionalParams = make(map[string][]*params.Param) + } + additionalParams[name] = ap + additionalParamsMu.Unlock() } type DriverNotSupportedError struct { @@ -197,6 +209,12 @@ func AllDrivers() []string { return driverNames } +func AllAdditionalParams() map[string] /*driver name*/ []*params.Param { + additionalParamsMu.RLock() + defer additionalParamsMu.RUnlock() + return additionalParams +} + var ErrNodesCountExceedOne = errors.New("after parse, nodes count exceed one") // Driver is a interface that must be implemented by a database. @@ -244,6 +262,9 @@ type Registerer interface { // Rules returns all rules that plugin supported. Rules() []*Rule + + // AdditionalParams returns all additional params that plugin supported. + AdditionalParams() []*params.Param } // Node is a interface which unify SQL ast tree. It produce by Driver.Parse. diff --git a/sqle/driver/mysql/mysql.go b/sqle/driver/mysql/mysql.go index 4bdb667e32..9d95eb9663 100644 --- a/sqle/driver/mysql/mysql.go +++ b/sqle/driver/mysql/mysql.go @@ -5,6 +5,7 @@ import ( "database/sql" _driver "database/sql/driver" "fmt" + "github.com/actiontech/sqle/sqle/pkg/params" "strings" "github.com/actiontech/sqle/sqle/driver/mysql/optimizer/index" @@ -26,7 +27,7 @@ func init() { allRules[i] = &rulepkg.RuleHandlers[i].Rule } - driver.Register(driver.DriverTypeMySQL, newInspect, allRules) + driver.Register(driver.DriverTypeMySQL, newInspect, allRules, []*params.Param{}) if err := LoadPtTemplateFromFile("./scripts/pt-online-schema-change.template"); err != nil { panic(err) diff --git a/sqle/driver/plugin.go b/sqle/driver/plugin.go index a4c5836419..1c89ea2773 100644 --- a/sqle/driver/plugin.go +++ b/sqle/driver/plugin.go @@ -142,10 +142,11 @@ func InitPlugins(pluginDir string) error { } if config.DSN != nil { initRequest.Dsn = &proto.DSN{ - Host: config.DSN.Host, - Port: config.DSN.Port, - User: config.DSN.User, - Password: config.DSN.Password, + Host: config.DSN.Host, + Port: config.DSN.Port, + User: config.DSN.User, + Password: config.DSN.Password, + AdditionalParams: proto.ParamToProtoParam(config.DSN.AdditionalParams), // database is to open. Database: config.DSN.DatabaseName, @@ -160,7 +161,7 @@ func InitPlugins(pluginDir string) error { } - Register(pluginMeta.Name, handler, driverRules) + Register(pluginMeta.Name, handler, driverRules, proto.ProtoParamToParam(pluginMeta.GetAdditionalParams())) log.Logger().WithFields(logrus.Fields{ "plugin_name": pluginMeta.Name, @@ -330,11 +331,12 @@ func (d *driverGRPCServer) Init(ctx context.Context, req *proto.InitRequest) (*p var dsn *DSN if req.GetDsn() != nil { dsn = &DSN{ - Host: req.GetDsn().GetHost(), - Port: req.GetDsn().GetPort(), - User: req.GetDsn().GetUser(), - Password: req.GetDsn().GetPassword(), - DatabaseName: req.GetDsn().GetDatabase(), + Host: req.GetDsn().GetHost(), + Port: req.GetDsn().GetPort(), + User: req.GetDsn().GetUser(), + Password: req.GetDsn().GetPassword(), + DatabaseName: req.GetDsn().GetDatabase(), + AdditionalParams: proto.ProtoParamToParam(req.GetDsn().GetAdditionalParams()), } } @@ -455,8 +457,9 @@ func (d *driverGRPCServer) Metas(ctx context.Context, req *proto.Empty) (*proto. } return &proto.MetasResponse{ - Name: d.r.Name(), - Rules: protoRules, + Name: d.r.Name(), + Rules: protoRules, + AdditionalParams: proto.ParamToProtoParam(d.r.AdditionalParams()), }, nil } diff --git a/sqle/pkg/driver/adaptor.go b/sqle/pkg/driver/adaptor.go index 0f881b6ad0..e5ea474173 100644 --- a/sqle/pkg/driver/adaptor.go +++ b/sqle/pkg/driver/adaptor.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" _driver "database/sql/driver" + "github.com/actiontech/sqle/sqle/pkg/params" "os" "github.com/actiontech/sqle/sqle/driver" @@ -27,6 +28,8 @@ type Adaptor struct { ruleToRawHandler map[string] /*rule name*/ rawSQLRuleHandler ruleToASTHandler map[string] /*rule name*/ astSQLRuleHandler + additionalParams []*params.Param + ao *adaptorOptions } @@ -50,6 +53,7 @@ func NewAdaptor(dt Dialector) *Adaptor { }), ruleToRawHandler: make(map[string]rawSQLRuleHandler), ruleToASTHandler: make(map[string]astSQLRuleHandler), + additionalParams: []*params.Param{}, } } @@ -58,6 +62,10 @@ func (a *Adaptor) AddRule(r *driver.Rule, h rawSQLRuleHandler) { a.ruleToRawHandler[r.Name] = h } +func (a *Adaptor) AddAdditionalParams(p *params.Param) { + a.additionalParams = append(a.additionalParams, p) +} + func (a *Adaptor) AddRuleWithSQLParser(r *driver.Rule, h astSQLRuleHandler) { a.rules = append(a.rules, r) a.ruleToASTHandler[r.Name] = h @@ -83,8 +91,9 @@ func (a *Adaptor) Serve(opts ...AdaptorOption) { } r := ®istererImpl{ - dt: a.dt, - rules: a.rules, + dt: a.dt, + rules: a.rules, + additionalParams: a.additionalParams, } newDriver := func(cfg *driver.Config) driver.Driver { @@ -151,8 +160,9 @@ var _ driver.Driver = (*driverImpl)(nil) var _ driver.Registerer = (*registererImpl)(nil) type registererImpl struct { - dt Dialector - rules []*driver.Rule + dt Dialector + rules []*driver.Rule + additionalParams []*params.Param } func (r *registererImpl) Name() string { @@ -163,6 +173,10 @@ func (r *registererImpl) Rules() []*driver.Rule { return r.rules } +func (r *registererImpl) AdditionalParams() []*params.Param { + return r.additionalParams +} + type driverImpl struct { a *Adaptor db *sql.DB From 261030af940ce2f2cc8604333efad5d6891b50cd Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Thu, 10 Mar 2022 18:07:51 +0800 Subject: [PATCH 06/13] Modify param --- sqle/pkg/params/params.go | 11 +++++++++++ sqle/server/auditplan/task.go | 19 +++++++++++-------- sqle/server/sqled.go | 10 ++++++---- sqle/server/workflow_schedule.go | 12 +++++++----- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/sqle/pkg/params/params.go b/sqle/pkg/params/params.go index ffafe40b3e..f85e5a10dc 100644 --- a/sqle/pkg/params/params.go +++ b/sqle/pkg/params/params.go @@ -132,3 +132,14 @@ func (r *Params) Copy() Params { } return ps } + +func NewParamSliceFromMap(m map[string]interface{}) []*Param { + s := []*Param{} + for k, v := range m { + s = append(s, &Param{ + Key: k, + Value: fmt.Sprintf("%v", v), + }) + } + return s +} diff --git a/sqle/server/auditplan/task.go b/sqle/server/auditplan/task.go index c30f4d14d8..80e27ce7cb 100644 --- a/sqle/server/auditplan/task.go +++ b/sqle/server/auditplan/task.go @@ -8,15 +8,16 @@ import ( "sync" "time" - "github.com/actiontech/sqle/sqle/log" - "github.com/actiontech/sqle/sqle/utils" - "github.com/actiontech/sqle/sqle/driver" "github.com/actiontech/sqle/sqle/driver/mysql/executor" "github.com/actiontech/sqle/sqle/errors" + "github.com/actiontech/sqle/sqle/log" "github.com/actiontech/sqle/sqle/model" "github.com/actiontech/sqle/sqle/pkg/oracle" + "github.com/actiontech/sqle/sqle/pkg/params" "github.com/actiontech/sqle/sqle/server" + "github.com/actiontech/sqle/sqle/utils" + "github.com/sirupsen/logrus" ) @@ -329,11 +330,13 @@ func (at *SchemaMetaTask) collectorDo() { return } db, err := executor.NewExecutor(at.logger, &driver.DSN{ - Host: instance.Host, - Port: instance.Port, - User: instance.User, - Password: instance.Password, - DatabaseName: at.ap.InstanceDatabase}, + Host: instance.Host, + Port: instance.Port, + User: instance.User, + Password: instance.Password, + AdditionalParams: params.NewParamSliceFromMap(instance.GetAdditionalParams()), + DatabaseName: at.ap.InstanceDatabase, + }, at.ap.InstanceDatabase) if err != nil { at.logger.Errorf("connect to instance fail, error: %v", err) diff --git a/sqle/server/sqled.go b/sqle/server/sqled.go index 6a3f409a4c..572192af80 100644 --- a/sqle/server/sqled.go +++ b/sqle/server/sqled.go @@ -12,6 +12,7 @@ import ( "github.com/actiontech/sqle/sqle/errors" "github.com/actiontech/sqle/sqle/log" "github.com/actiontech/sqle/sqle/model" + "github.com/actiontech/sqle/sqle/pkg/params" xerrors "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -467,10 +468,11 @@ func newDriverWithAudit(l *logrus.Entry, inst *model.Instance, database string, modelRules, err = st.GetRulesFromRuleTemplateByName(templateName) } else { dsn = &driver.DSN{ - Host: inst.Host, - Port: inst.Port, - User: inst.User, - Password: inst.Password, + Host: inst.Host, + Port: inst.Port, + User: inst.User, + Password: inst.Password, + AdditionalParams: params.NewParamSliceFromMap(inst.GetAdditionalParams()), DatabaseName: database, } diff --git a/sqle/server/workflow_schedule.go b/sqle/server/workflow_schedule.go index 585c1e084d..4dd640dd28 100644 --- a/sqle/server/workflow_schedule.go +++ b/sqle/server/workflow_schedule.go @@ -10,6 +10,7 @@ import ( "github.com/actiontech/sqle/sqle/errors" "github.com/actiontech/sqle/sqle/log" "github.com/actiontech/sqle/sqle/model" + "github.com/actiontech/sqle/sqle/pkg/params" "github.com/sirupsen/logrus" ) @@ -84,11 +85,12 @@ func ExecuteWorkflow(workflow *model.Workflow, userId uint) error { // if instance is not connectable, exec sql must be failed; // commit action unable to retry, so don't to exec it. dsn := &driver.DSN{ - Host: task.Instance.Host, - Port: task.Instance.Port, - User: task.Instance.User, - Password: task.Instance.Password, - DatabaseName: task.Schema, + Host: task.Instance.Host, + Port: task.Instance.Port, + User: task.Instance.User, + Password: task.Instance.Password, + AdditionalParams: params.NewParamSliceFromMap(task.Instance.GetAdditionalParams()), + DatabaseName: task.Schema, } cfg, err := driver.NewConfig(dsn, nil) From 70eeb22f5f222c5b67141ceca9c53b09a51031cf Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Thu, 10 Mar 2022 18:11:07 +0800 Subject: [PATCH 07/13] Adjust the code format --- sqle/api/controller/v1/instance.go | 2 +- sqle/driver/mysql/mysql.go | 6 +++--- sqle/pkg/driver/adaptor.go | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/sqle/api/controller/v1/instance.go b/sqle/api/controller/v1/instance.go index d475e2a1c4..c3c45a7322 100644 --- a/sqle/api/controller/v1/instance.go +++ b/sqle/api/controller/v1/instance.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "github.com/actiontech/sqle/sqle/pkg/params" "net/http" "github.com/actiontech/sqle/sqle/api/controller" @@ -12,6 +11,7 @@ import ( "github.com/actiontech/sqle/sqle/errors" "github.com/actiontech/sqle/sqle/log" "github.com/actiontech/sqle/sqle/model" + "github.com/actiontech/sqle/sqle/pkg/params" "github.com/actiontech/sqle/sqle/utils" "github.com/labstack/echo/v4" diff --git a/sqle/driver/mysql/mysql.go b/sqle/driver/mysql/mysql.go index 9d95eb9663..6b21fca057 100644 --- a/sqle/driver/mysql/mysql.go +++ b/sqle/driver/mysql/mysql.go @@ -5,17 +5,17 @@ import ( "database/sql" _driver "database/sql/driver" "fmt" - "github.com/actiontech/sqle/sqle/pkg/params" "strings" - "github.com/actiontech/sqle/sqle/driver/mysql/optimizer/index" - "github.com/actiontech/sqle/sqle/driver" "github.com/actiontech/sqle/sqle/driver/mysql/executor" "github.com/actiontech/sqle/sqle/driver/mysql/onlineddl" + "github.com/actiontech/sqle/sqle/driver/mysql/optimizer/index" rulepkg "github.com/actiontech/sqle/sqle/driver/mysql/rule" "github.com/actiontech/sqle/sqle/driver/mysql/session" "github.com/actiontech/sqle/sqle/driver/mysql/util" + "github.com/actiontech/sqle/sqle/pkg/params" + "github.com/pingcap/parser/ast" "github.com/pkg/errors" "github.com/sirupsen/logrus" diff --git a/sqle/pkg/driver/adaptor.go b/sqle/pkg/driver/adaptor.go index e5ea474173..2509e5bfde 100644 --- a/sqle/pkg/driver/adaptor.go +++ b/sqle/pkg/driver/adaptor.go @@ -4,11 +4,12 @@ import ( "context" "database/sql" _driver "database/sql/driver" - "github.com/actiontech/sqle/sqle/pkg/params" "os" "github.com/actiontech/sqle/sqle/driver" + "github.com/actiontech/sqle/sqle/pkg/params" "github.com/actiontech/sqle/sqle/utils" + "github.com/hashicorp/go-hclog" "github.com/percona/go-mysql/query" "github.com/pkg/errors" From 1f908302cccdccd2be34f7d3921d7769d444dc35 Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Fri, 11 Mar 2022 11:56:18 +0800 Subject: [PATCH 08/13] update instance model --- sqle/api/controller/v1/instance.go | 66 +++++++++++++++++------------- sqle/api/controller/v1/util.go | 4 +- sqle/driver/driver.go | 8 ++-- sqle/driver/mysql/mysql.go | 2 +- sqle/model/instance.go | 28 +++++-------- sqle/pkg/params/params.go | 11 ----- sqle/server/auditplan/task.go | 3 +- sqle/server/sqled.go | 3 +- sqle/server/workflow_schedule.go | 3 +- 9 files changed, 58 insertions(+), 70 deletions(-) diff --git a/sqle/api/controller/v1/instance.go b/sqle/api/controller/v1/instance.go index c3c45a7322..79184257f1 100644 --- a/sqle/api/controller/v1/instance.go +++ b/sqle/api/controller/v1/instance.go @@ -2,7 +2,6 @@ package v1 import ( "context" - "encoding/json" "fmt" "net/http" @@ -123,13 +122,14 @@ func CreateInstance(c echo.Context) error { req.DBType = driver.DriverTypeMySQL } - additionalParams := map[string]interface{}{} - for _, param := range req.AdditionalParams { - additionalParams[param.Name] = param.Value - } - additionalParamsBytes, err := json.Marshal(additionalParams) - if err != nil { - return controller.JSONBaseErrorReq(c, err) + additionalParams := driver.AllAdditionalParams()[req.DBType] + for _, param := range additionalParams { + for _, additionalParam := range req.AdditionalParams { + if param.Key == additionalParam.Name { + param.Value = additionalParam.Value + break + } + } } instance := &model.Instance{ @@ -140,7 +140,7 @@ func CreateInstance(c echo.Context) error { Port: req.Port, Password: req.Password, Desc: req.Desc, - AdditionalParams: string(additionalParamsBytes), + AdditionalParams: additionalParams, } // set default workflow template if req.WorkflowTemplateName == "" { @@ -264,13 +264,12 @@ func convertInstanceToRes(instance *model.Instance, p []*params.Param) InstanceR } instanceResV1.Roles = roleNames } - additionalParams := instance.GetAdditionalParams() for _, param := range p { instanceResV1.AdditionalParams = append(instanceResV1.AdditionalParams, &InstanceAdditionalParamResV1{ Name: param.Key, Description: param.Desc, Type: string(param.Type), - Value: fmt.Sprintf("%v", additionalParams[param.Key]), + Value: fmt.Sprintf("%v", instance.AdditionalParams.GetParam(param.Key)), }) } return instanceResV1 @@ -463,18 +462,26 @@ func UpdateInstance(c echo.Context) error { return controller.JSONBaseErrorReq(c, err) } } + if req.AdditionalParams != nil { - paramMap := instance.GetAdditionalParams() - for _, param := range req.AdditionalParams { - if param.Value != "" { - paramMap[param.Name] = param.Value + instanceParams := instance.AdditionalParams + additionParams := driver.AllAdditionalParams()[instance.DbType] + + for _, reqParams := range req.AdditionalParams { + if reqParams.Value != "" && additionParams.GetParam(reqParams.Name) != nil { + err = instanceParams.SetParamValue(reqParams.Name, reqParams.Value) + if err != nil { + instanceParams = append(instanceParams, ¶ms.Param{ + Key: reqParams.Name, + Value: reqParams.Value, + Desc: additionParams.GetParam(reqParams.Name).Desc, + Type: additionParams.GetParam(reqParams.Name).Type, + }) + } } } - paramBytes, err := json.Marshal(paramMap) - if err != nil { - return controller.JSONBaseErrorReq(c, err) - } - updateMap["additional_params"] = string(paramBytes) + + updateMap["additional_params"] = instanceParams } err = s.UpdateInstanceById(instance.ID, updateMap) @@ -672,21 +679,24 @@ func CheckInstanceIsConnectable(c echo.Context) error { if req.DBType == "" { req.DBType = driver.DriverTypeMySQL } - mp := map[string]interface{}{} - for _, param := range req.AdditionalParams { - mp[param.Name] = param.Value - } - paramBytes, err := json.Marshal(mp) - if err != nil { - return controller.JSONBaseErrorReq(c, err) + + additionalParams := driver.AllAdditionalParams()[req.DBType] + for _, param := range additionalParams { + for _, additionalParam := range req.AdditionalParams { + if param.Key == additionalParam.Name { + param.Value = additionalParam.Value + break + } + } } + instance := &model.Instance{ DbType: req.DBType, User: req.User, Host: req.Host, Port: req.Port, Password: req.Password, - AdditionalParams: string(paramBytes), + AdditionalParams: additionalParams, } return checkInstanceIsConnectable(c, instance) } diff --git a/sqle/api/controller/v1/util.go b/sqle/api/controller/v1/util.go index ec5999501e..ff02f1490c 100644 --- a/sqle/api/controller/v1/util.go +++ b/sqle/api/controller/v1/util.go @@ -3,8 +3,6 @@ package v1 import ( "github.com/actiontech/sqle/sqle/driver" "github.com/actiontech/sqle/sqle/model" - "github.com/actiontech/sqle/sqle/pkg/params" - "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -19,7 +17,7 @@ func newDriverWithoutAudit(l *logrus.Entry, inst *model.Instance, database strin Port: inst.Port, User: inst.User, Password: inst.Password, - AdditionalParams: params.NewParamSliceFromMap(inst.GetAdditionalParams()), + AdditionalParams: inst.AdditionalParams, DatabaseName: database, } diff --git a/sqle/driver/driver.go b/sqle/driver/driver.go index 884224ba00..e3abed53ce 100644 --- a/sqle/driver/driver.go +++ b/sqle/driver/driver.go @@ -23,7 +23,7 @@ var ( rulesMu sync.RWMutex // additionalParams store driver additional params - additionalParams map[string][]*params.Param + additionalParams map[string]params.Params additionalParamsMu sync.RWMutex ) @@ -146,7 +146,7 @@ type handler func(log *logrus.Entry, c *Config) (Driver, error) // // Register makes a database driver available by the provided driver name. // Driver's initialize handler and audit rules register by Register. -func Register(name string, h handler, rs []*Rule, ap []*params.Param) { +func Register(name string, h handler, rs []*Rule, ap params.Params) { _, exist := drivers[name] if exist { panic("duplicated driver name") @@ -165,7 +165,7 @@ func Register(name string, h handler, rs []*Rule, ap []*params.Param) { additionalParamsMu.Lock() if additionalParams == nil { - additionalParams = make(map[string][]*params.Param) + additionalParams = make(map[string]params.Params) } additionalParams[name] = ap additionalParamsMu.Unlock() @@ -209,7 +209,7 @@ func AllDrivers() []string { return driverNames } -func AllAdditionalParams() map[string] /*driver name*/ []*params.Param { +func AllAdditionalParams() map[string] /*driver name*/ params.Params { additionalParamsMu.RLock() defer additionalParamsMu.RUnlock() return additionalParams diff --git a/sqle/driver/mysql/mysql.go b/sqle/driver/mysql/mysql.go index 6b21fca057..cfdf798aea 100644 --- a/sqle/driver/mysql/mysql.go +++ b/sqle/driver/mysql/mysql.go @@ -27,7 +27,7 @@ func init() { allRules[i] = &rulepkg.RuleHandlers[i].Rule } - driver.Register(driver.DriverTypeMySQL, newInspect, allRules, []*params.Param{}) + driver.Register(driver.DriverTypeMySQL, newInspect, allRules, params.Params{}) if err := LoadPtTemplateFromFile("./scripts/pt-online-schema-change.template"); err != nil { panic(err) diff --git a/sqle/model/instance.go b/sqle/model/instance.go index ad2f71505a..ff4f37cef6 100644 --- a/sqle/model/instance.go +++ b/sqle/model/instance.go @@ -1,8 +1,8 @@ package model import ( - "encoding/json" "fmt" + "github.com/actiontech/sqle/sqle/pkg/params" "strings" "github.com/actiontech/sqle/sqle/errors" @@ -15,16 +15,16 @@ import ( type Instance struct { Model // has created composite index: [id, name] by gorm#AddIndex - Name string `json:"name" gorm:"not null;index" example:""` - DbType string `json:"db_type" gorm:"column:db_type; not null" example:"mysql"` - Host string `json:"host" gorm:"column:db_host; not null" example:"10.10.10.10"` - Port string `json:"port" gorm:"column:db_port; not null" example:"3306"` - User string `json:"user" gorm:"column:db_user; not null" example:"root"` - Password string `json:"-" gorm:"-"` - SecretPassword string `json:"secret_password" gorm:"column:db_password; not null"` - Desc string `json:"desc" example:"this is a instance"` - WorkflowTemplateId uint `json:"workflow_template_id"` - AdditionalParams string `json:"additional_params" gorm:"type:text"` + Name string `json:"name" gorm:"not null;index" example:""` + DbType string `json:"db_type" gorm:"column:db_type; not null" example:"mysql"` + Host string `json:"host" gorm:"column:db_host; not null" example:"10.10.10.10"` + Port string `json:"port" gorm:"column:db_port; not null" example:"3306"` + User string `json:"user" gorm:"column:db_user; not null" example:"root"` + Password string `json:"-" gorm:"-"` + SecretPassword string `json:"secret_password" gorm:"column:db_password; not null"` + Desc string `json:"desc" example:"this is a instance"` + WorkflowTemplateId uint `json:"workflow_template_id"` + AdditionalParams params.Params `json:"params" gorm:"type:text"` // relation table Roles []*Role `json:"-" gorm:"many2many:instance_role;"` @@ -32,12 +32,6 @@ type Instance struct { WorkflowTemplate *WorkflowTemplate `gorm:"foreignkey:WorkflowTemplateId"` } -func (i *Instance) GetAdditionalParams() map[string]interface{} { - mp := make(map[string]interface{}) - _ = json.Unmarshal([]byte(i.AdditionalParams), &mp) - return mp -} - // BeforeSave is a hook implement gorm model before exec create func (i *Instance) BeforeSave() error { return i.encryptPassword() diff --git a/sqle/pkg/params/params.go b/sqle/pkg/params/params.go index f85e5a10dc..ffafe40b3e 100644 --- a/sqle/pkg/params/params.go +++ b/sqle/pkg/params/params.go @@ -132,14 +132,3 @@ func (r *Params) Copy() Params { } return ps } - -func NewParamSliceFromMap(m map[string]interface{}) []*Param { - s := []*Param{} - for k, v := range m { - s = append(s, &Param{ - Key: k, - Value: fmt.Sprintf("%v", v), - }) - } - return s -} diff --git a/sqle/server/auditplan/task.go b/sqle/server/auditplan/task.go index 80e27ce7cb..e638c20516 100644 --- a/sqle/server/auditplan/task.go +++ b/sqle/server/auditplan/task.go @@ -14,7 +14,6 @@ import ( "github.com/actiontech/sqle/sqle/log" "github.com/actiontech/sqle/sqle/model" "github.com/actiontech/sqle/sqle/pkg/oracle" - "github.com/actiontech/sqle/sqle/pkg/params" "github.com/actiontech/sqle/sqle/server" "github.com/actiontech/sqle/sqle/utils" @@ -334,7 +333,7 @@ func (at *SchemaMetaTask) collectorDo() { Port: instance.Port, User: instance.User, Password: instance.Password, - AdditionalParams: params.NewParamSliceFromMap(instance.GetAdditionalParams()), + AdditionalParams: instance.AdditionalParams, DatabaseName: at.ap.InstanceDatabase, }, at.ap.InstanceDatabase) diff --git a/sqle/server/sqled.go b/sqle/server/sqled.go index 572192af80..1e94e817e0 100644 --- a/sqle/server/sqled.go +++ b/sqle/server/sqled.go @@ -12,7 +12,6 @@ import ( "github.com/actiontech/sqle/sqle/errors" "github.com/actiontech/sqle/sqle/log" "github.com/actiontech/sqle/sqle/model" - "github.com/actiontech/sqle/sqle/pkg/params" xerrors "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -472,7 +471,7 @@ func newDriverWithAudit(l *logrus.Entry, inst *model.Instance, database string, Port: inst.Port, User: inst.User, Password: inst.Password, - AdditionalParams: params.NewParamSliceFromMap(inst.GetAdditionalParams()), + AdditionalParams: inst.AdditionalParams, DatabaseName: database, } diff --git a/sqle/server/workflow_schedule.go b/sqle/server/workflow_schedule.go index 4dd640dd28..7ed9059303 100644 --- a/sqle/server/workflow_schedule.go +++ b/sqle/server/workflow_schedule.go @@ -10,7 +10,6 @@ import ( "github.com/actiontech/sqle/sqle/errors" "github.com/actiontech/sqle/sqle/log" "github.com/actiontech/sqle/sqle/model" - "github.com/actiontech/sqle/sqle/pkg/params" "github.com/sirupsen/logrus" ) @@ -89,7 +88,7 @@ func ExecuteWorkflow(workflow *model.Workflow, userId uint) error { Port: task.Instance.Port, User: task.Instance.User, Password: task.Instance.Password, - AdditionalParams: params.NewParamSliceFromMap(task.Instance.GetAdditionalParams()), + AdditionalParams: task.Instance.AdditionalParams, DatabaseName: task.Schema, } From feaa09858181ddb542bb4b415f96168f40e8646e Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Fri, 11 Mar 2022 11:59:32 +0800 Subject: [PATCH 09/13] update instance model --- sqle/model/instance.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqle/model/instance.go b/sqle/model/instance.go index ff4f37cef6..1b2e002cb0 100644 --- a/sqle/model/instance.go +++ b/sqle/model/instance.go @@ -24,7 +24,7 @@ type Instance struct { SecretPassword string `json:"secret_password" gorm:"column:db_password; not null"` Desc string `json:"desc" example:"this is a instance"` WorkflowTemplateId uint `json:"workflow_template_id"` - AdditionalParams params.Params `json:"params" gorm:"type:text"` + AdditionalParams params.Params `json:"additional_params" gorm:"type:text"` // relation table Roles []*Role `json:"-" gorm:"many2many:instance_role;"` From 0338a73e7e3fa49dce60543606af2850ec6ac9f8 Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Fri, 11 Mar 2022 13:43:23 +0800 Subject: [PATCH 10/13] unite []*params.Param --- sqle/api/controller/v1/instance.go | 28 ++++++++++++---------------- sqle/driver/driver.go | 4 ++-- sqle/driver/plugin.go | 8 ++++---- sqle/driver/proto/util.go | 6 +++--- sqle/pkg/driver/adaptor.go | 8 ++++---- 5 files changed, 25 insertions(+), 29 deletions(-) diff --git a/sqle/api/controller/v1/instance.go b/sqle/api/controller/v1/instance.go index 79184257f1..f8b772c81f 100644 --- a/sqle/api/controller/v1/instance.go +++ b/sqle/api/controller/v1/instance.go @@ -54,7 +54,7 @@ func GetInstanceAdditionalMetas(c echo.Context) error { for name, params := range additionalParams { meta := &InstanceAdditionalMetaV1{ DBType: name, - Params: ParamsSliceToInstanceAdditionalParamResV1Slice(params), + Params: convertParamsToInstanceAdditionalParamRes(params), } res.Metas = append(res.Metas, meta) @@ -62,7 +62,7 @@ func GetInstanceAdditionalMetas(c echo.Context) error { return c.JSON(http.StatusOK, res) } -func ParamsSliceToInstanceAdditionalParamResV1Slice(params []*params.Param) []*InstanceAdditionalParamResV1 { +func convertParamsToInstanceAdditionalParamRes(params params.Params) []*InstanceAdditionalParamResV1 { res := make([]*InstanceAdditionalParamResV1, len(params)) for i, param := range params { res[i] = &InstanceAdditionalParamResV1{ @@ -123,12 +123,10 @@ func CreateInstance(c echo.Context) error { } additionalParams := driver.AllAdditionalParams()[req.DBType] - for _, param := range additionalParams { - for _, additionalParam := range req.AdditionalParams { - if param.Key == additionalParam.Name { - param.Value = additionalParam.Value - break - } + for _, additionalParam := range req.AdditionalParams { + err = additionalParams.SetParamValue(additionalParam.Name, additionalParam.Value) + if err != nil { + return controller.JSONBaseErrorReq(c, errors.New(errors.DataInvalid, err)) } } @@ -237,7 +235,7 @@ type GetInstanceResV1 struct { Data InstanceResV1 `json:"data"` } -func convertInstanceToRes(instance *model.Instance, p []*params.Param) InstanceResV1 { +func convertInstanceToRes(instance *model.Instance, allParam params.Params) InstanceResV1 { instanceResV1 := InstanceResV1{ Name: instance.Name, Host: instance.Host, @@ -264,7 +262,7 @@ func convertInstanceToRes(instance *model.Instance, p []*params.Param) InstanceR } instanceResV1.Roles = roleNames } - for _, param := range p { + for _, param := range allParam { instanceResV1.AdditionalParams = append(instanceResV1.AdditionalParams, &InstanceAdditionalParamResV1{ Name: param.Key, Description: param.Desc, @@ -681,12 +679,10 @@ func CheckInstanceIsConnectable(c echo.Context) error { } additionalParams := driver.AllAdditionalParams()[req.DBType] - for _, param := range additionalParams { - for _, additionalParam := range req.AdditionalParams { - if param.Key == additionalParam.Name { - param.Value = additionalParam.Value - break - } + for _, additionalParam := range req.AdditionalParams { + err := additionalParams.SetParamValue(additionalParam.Name, additionalParam.Value) + if err != nil { + return controller.JSONBaseErrorReq(c, errors.New(errors.DataInvalid, err)) } } diff --git a/sqle/driver/driver.go b/sqle/driver/driver.go index e3abed53ce..34a159e9fc 100644 --- a/sqle/driver/driver.go +++ b/sqle/driver/driver.go @@ -43,7 +43,7 @@ type DSN struct { Port string User string Password string - AdditionalParams []*params.Param + AdditionalParams params.Params // DatabaseName is the default database to connect. DatabaseName string @@ -264,7 +264,7 @@ type Registerer interface { Rules() []*Rule // AdditionalParams returns all additional params that plugin supported. - AdditionalParams() []*params.Param + AdditionalParams() params.Params } // Node is a interface which unify SQL ast tree. It produce by Driver.Parse. diff --git a/sqle/driver/plugin.go b/sqle/driver/plugin.go index 1c89ea2773..c90a22eed3 100644 --- a/sqle/driver/plugin.go +++ b/sqle/driver/plugin.go @@ -146,7 +146,7 @@ func InitPlugins(pluginDir string) error { Port: config.DSN.Port, User: config.DSN.User, Password: config.DSN.Password, - AdditionalParams: proto.ParamToProtoParam(config.DSN.AdditionalParams), + AdditionalParams: proto.ConvertParamToProtoParam(config.DSN.AdditionalParams), // database is to open. Database: config.DSN.DatabaseName, @@ -161,7 +161,7 @@ func InitPlugins(pluginDir string) error { } - Register(pluginMeta.Name, handler, driverRules, proto.ProtoParamToParam(pluginMeta.GetAdditionalParams())) + Register(pluginMeta.Name, handler, driverRules, proto.ConvertProtoParamToParam(pluginMeta.GetAdditionalParams())) log.Logger().WithFields(logrus.Fields{ "plugin_name": pluginMeta.Name, @@ -336,7 +336,7 @@ func (d *driverGRPCServer) Init(ctx context.Context, req *proto.InitRequest) (*p User: req.GetDsn().GetUser(), Password: req.GetDsn().GetPassword(), DatabaseName: req.GetDsn().GetDatabase(), - AdditionalParams: proto.ProtoParamToParam(req.GetDsn().GetAdditionalParams()), + AdditionalParams: proto.ConvertProtoParamToParam(req.GetDsn().GetAdditionalParams()), } } @@ -459,7 +459,7 @@ func (d *driverGRPCServer) Metas(ctx context.Context, req *proto.Empty) (*proto. return &proto.MetasResponse{ Name: d.r.Name(), Rules: protoRules, - AdditionalParams: proto.ParamToProtoParam(d.r.AdditionalParams()), + AdditionalParams: proto.ConvertParamToProtoParam(d.r.AdditionalParams()), }, nil } diff --git a/sqle/driver/proto/util.go b/sqle/driver/proto/util.go index ffc2a6ac5c..0d91eff7b2 100644 --- a/sqle/driver/proto/util.go +++ b/sqle/driver/proto/util.go @@ -4,7 +4,7 @@ import ( "github.com/actiontech/sqle/sqle/pkg/params" ) -func ParamToProtoParam(p []*params.Param) []*Param { +func ConvertParamToProtoParam(p params.Params) []*Param { pp := make([]*Param, len(p)) for i, v := range p { if v == nil { @@ -20,8 +20,8 @@ func ParamToProtoParam(p []*params.Param) []*Param { return pp } -func ProtoParamToParam(p []*Param) []*params.Param { - pp := make([]*params.Param, len(p)) +func ConvertProtoParamToParam(p []*Param) params.Params { + pp := make(params.Params, len(p)) for i, v := range p { if v == nil { continue diff --git a/sqle/pkg/driver/adaptor.go b/sqle/pkg/driver/adaptor.go index 2509e5bfde..6e7edfbf40 100644 --- a/sqle/pkg/driver/adaptor.go +++ b/sqle/pkg/driver/adaptor.go @@ -29,7 +29,7 @@ type Adaptor struct { ruleToRawHandler map[string] /*rule name*/ rawSQLRuleHandler ruleToASTHandler map[string] /*rule name*/ astSQLRuleHandler - additionalParams []*params.Param + additionalParams params.Params ao *adaptorOptions } @@ -54,7 +54,7 @@ func NewAdaptor(dt Dialector) *Adaptor { }), ruleToRawHandler: make(map[string]rawSQLRuleHandler), ruleToASTHandler: make(map[string]astSQLRuleHandler), - additionalParams: []*params.Param{}, + additionalParams: params.Params{}, } } @@ -163,7 +163,7 @@ var _ driver.Registerer = (*registererImpl)(nil) type registererImpl struct { dt Dialector rules []*driver.Rule - additionalParams []*params.Param + additionalParams params.Params } func (r *registererImpl) Name() string { @@ -174,7 +174,7 @@ func (r *registererImpl) Rules() []*driver.Rule { return r.rules } -func (r *registererImpl) AdditionalParams() []*params.Param { +func (r *registererImpl) AdditionalParams() params.Params { return r.additionalParams } From d74490a399216afcfb022ac89e763d185f371ec7 Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Fri, 11 Mar 2022 13:48:58 +0800 Subject: [PATCH 11/13] Clean up redundant parameters --- sqle/api/controller/v1/instance.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/sqle/api/controller/v1/instance.go b/sqle/api/controller/v1/instance.go index f8b772c81f..2655ff7618 100644 --- a/sqle/api/controller/v1/instance.go +++ b/sqle/api/controller/v1/instance.go @@ -235,7 +235,7 @@ type GetInstanceResV1 struct { Data InstanceResV1 `json:"data"` } -func convertInstanceToRes(instance *model.Instance, allParam params.Params) InstanceResV1 { +func convertInstanceToRes(instance *model.Instance) InstanceResV1 { instanceResV1 := InstanceResV1{ Name: instance.Name, Host: instance.Host, @@ -262,12 +262,12 @@ func convertInstanceToRes(instance *model.Instance, allParam params.Params) Inst } instanceResV1.Roles = roleNames } - for _, param := range allParam { + for _, param := range instance.AdditionalParams { instanceResV1.AdditionalParams = append(instanceResV1.AdditionalParams, &InstanceAdditionalParamResV1{ Name: param.Key, Description: param.Desc, Type: string(param.Type), - Value: fmt.Sprintf("%v", instance.AdditionalParams.GetParam(param.Key)), + Value: fmt.Sprintf("%v", param.Value), }) } return instanceResV1 @@ -298,11 +298,9 @@ func GetInstance(c echo.Context) error { return controller.JSONBaseErrorReq(c, err) } - additionalParams := driver.AllAdditionalParams()[instance.DbType] - return c.JSON(http.StatusOK, &GetInstanceResV1{ BaseRes: controller.NewBaseReq(nil), - Data: convertInstanceToRes(instance, additionalParams), + Data: convertInstanceToRes(instance), }) } From 04c0bffd0bf6be6e365b25d3907151a5c38b5014 Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Fri, 11 Mar 2022 16:20:54 +0800 Subject: [PATCH 12/13] fix bug --- sqle/api/controller/v1/instance.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sqle/api/controller/v1/instance.go b/sqle/api/controller/v1/instance.go index 2655ff7618..6b830d2c16 100644 --- a/sqle/api/controller/v1/instance.go +++ b/sqle/api/controller/v1/instance.go @@ -464,9 +464,12 @@ func UpdateInstance(c echo.Context) error { additionParams := driver.AllAdditionalParams()[instance.DbType] for _, reqParams := range req.AdditionalParams { - if reqParams.Value != "" && additionParams.GetParam(reqParams.Name) != nil { + if reqParams.Value != "" { err = instanceParams.SetParamValue(reqParams.Name, reqParams.Value) - if err != nil { + + if err != nil && instanceParams.GetParam(reqParams.Name) != nil { + return controller.JSONBaseErrorReq(c, errors.New(errors.DataInvalid, err)) + } else if additionParams.GetParam(reqParams.Name) != nil { instanceParams = append(instanceParams, ¶ms.Param{ Key: reqParams.Name, Value: reqParams.Value, From 844ea62c7ae768e771f5c42ee7fc3ccd55cac3e9 Mon Sep 17 00:00:00 2001 From: HuangWeiCen <1724659546@qq.com> Date: Fri, 11 Mar 2022 16:44:39 +0800 Subject: [PATCH 13/13] update copy params --- sqle/api/controller/v1/instance.go | 25 ++++++------------------- sqle/driver/driver.go | 7 ++++++- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/sqle/api/controller/v1/instance.go b/sqle/api/controller/v1/instance.go index 6b830d2c16..7520d4595c 100644 --- a/sqle/api/controller/v1/instance.go +++ b/sqle/api/controller/v1/instance.go @@ -460,27 +460,14 @@ func UpdateInstance(c echo.Context) error { } if req.AdditionalParams != nil { - instanceParams := instance.AdditionalParams - additionParams := driver.AllAdditionalParams()[instance.DbType] - - for _, reqParams := range req.AdditionalParams { - if reqParams.Value != "" { - err = instanceParams.SetParamValue(reqParams.Name, reqParams.Value) - - if err != nil && instanceParams.GetParam(reqParams.Name) != nil { - return controller.JSONBaseErrorReq(c, errors.New(errors.DataInvalid, err)) - } else if additionParams.GetParam(reqParams.Name) != nil { - instanceParams = append(instanceParams, ¶ms.Param{ - Key: reqParams.Name, - Value: reqParams.Value, - Desc: additionParams.GetParam(reqParams.Name).Desc, - Type: additionParams.GetParam(reqParams.Name).Type, - }) - } + additionalParams := driver.AllAdditionalParams()[instance.DbType] + for _, additionalParam := range req.AdditionalParams { + err = additionalParams.SetParamValue(additionalParam.Name, additionalParam.Value) + if err != nil { + return controller.JSONBaseErrorReq(c, errors.New(errors.DataInvalid, err)) } } - - updateMap["additional_params"] = instanceParams + updateMap["additional_params"] = additionalParams } err = s.UpdateInstanceById(instance.ID, updateMap) diff --git a/sqle/driver/driver.go b/sqle/driver/driver.go index 34a159e9fc..c2db578686 100644 --- a/sqle/driver/driver.go +++ b/sqle/driver/driver.go @@ -212,7 +212,12 @@ func AllDrivers() []string { func AllAdditionalParams() map[string] /*driver name*/ params.Params { additionalParamsMu.RLock() defer additionalParamsMu.RUnlock() - return additionalParams + + newParams := map[string]params.Params{} + for k, v := range additionalParams { + newParams[k] = v.Copy() + } + return newParams } var ErrNodesCountExceedOne = errors.New("after parse, nodes count exceed one")