Skip to content

Commit

Permalink
Merge pull request #385 from actiontech/issue_329_1
Browse files Browse the repository at this point in the history
Issue 329
  • Loading branch information
sjjian authored Mar 11, 2022
2 parents d710ec4 + 844ea62 commit a57213f
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 132 deletions.
1 change: 1 addition & 0 deletions sqle/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
105 changes: 86 additions & 19 deletions sqle/api/controller/v1/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/actiontech/sqle/sqle/utils"

"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -45,7 +46,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: convertParamsToInstanceAdditionalParamRes(params),
}

res.Metas = append(res.Metas, meta)
}
return c.JSON(http.StatusOK, res)
}

func convertParamsToInstanceAdditionalParamRes(params params.Params) []*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 {
Expand Down Expand Up @@ -94,14 +121,24 @@ func CreateInstance(c echo.Context) error {
if req.DBType == "" {
req.DBType = driver.DriverTypeMySQL
}

additionalParams := driver.AllAdditionalParams()[req.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))
}
}

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: additionalParams,
}
// set default workflow template
if req.WorkflowTemplateName == "" {
Expand Down Expand Up @@ -200,12 +237,13 @@ type GetInstanceResV1 struct {

func convertInstanceToRes(instance *model.Instance) 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
Expand All @@ -224,6 +262,14 @@ func convertInstanceToRes(instance *model.Instance) InstanceResV1 {
}
instanceResV1.Roles = roleNames
}
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", param.Value),
})
}
return instanceResV1
}

Expand Down Expand Up @@ -413,6 +459,17 @@ func UpdateInstance(c echo.Context) error {
}
}

if req.AdditionalParams != nil {
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"] = additionalParams
}

err = s.UpdateInstanceById(instance.ID, updateMap)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
Expand Down Expand Up @@ -608,12 +665,22 @@ func CheckInstanceIsConnectable(c echo.Context) error {
if req.DBType == "" {
req.DBType = driver.DriverTypeMySQL
}

additionalParams := driver.AllAdditionalParams()[req.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))
}
}

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: additionalParams,
}
return checkInstanceIsConnectable(c, instance)
}
Expand Down
9 changes: 5 additions & 4 deletions sqle/api/controller/v1/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,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: inst.AdditionalParams,

DatabaseName: database,
}
Expand Down
36 changes: 31 additions & 5 deletions sqle/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Params
additionalParamsMu sync.RWMutex
)

const (
Expand All @@ -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.Params

// DatabaseName is the default database to connect.
DatabaseName string
Expand Down Expand Up @@ -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.Params) {
_, exist := drivers[name]
if exist {
panic("duplicated driver name")
Expand All @@ -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.Params)
}
additionalParams[name] = ap
additionalParamsMu.Unlock()
}

type DriverNotSupportedError struct {
Expand Down Expand Up @@ -197,6 +209,17 @@ func AllDrivers() []string {
return driverNames
}

func AllAdditionalParams() map[string] /*driver name*/ params.Params {
additionalParamsMu.RLock()
defer additionalParamsMu.RUnlock()

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")

// Driver is a interface that must be implemented by a database.
Expand Down Expand Up @@ -244,6 +267,9 @@ type Registerer interface {

// Rules returns all rules that plugin supported.
Rules() []*Rule

// AdditionalParams returns all additional params that plugin supported.
AdditionalParams() params.Params
}

// Node is a interface which unify SQL ast tree. It produce by Driver.Parse.
Expand Down
7 changes: 4 additions & 3 deletions sqle/driver/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"fmt"
"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"
Expand All @@ -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.Params{})

if err := LoadPtTemplateFromFile("./scripts/pt-online-schema-change.template"); err != nil {
panic(err)
Expand Down
27 changes: 15 additions & 12 deletions sqle/driver/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.ConvertParamToProtoParam(config.DSN.AdditionalParams),

// database is to open.
Database: config.DSN.DatabaseName,
Expand All @@ -160,7 +161,7 @@ func InitPlugins(pluginDir string) error {

}

Register(pluginMeta.Name, handler, driverRules)
Register(pluginMeta.Name, handler, driverRules, proto.ConvertProtoParamToParam(pluginMeta.GetAdditionalParams()))

log.Logger().WithFields(logrus.Fields{
"plugin_name": pluginMeta.Name,
Expand Down Expand Up @@ -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.ConvertProtoParamToParam(req.GetDsn().GetAdditionalParams()),
}
}

Expand Down Expand Up @@ -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.ConvertParamToProtoParam(d.r.AdditionalParams()),
}, nil
}

Expand Down
Loading

0 comments on commit a57213f

Please sign in to comment.