Browse Source

ftr: optimize the datasource init (#447)

* optimize the datasource init
tags/v1.1.0
FengZhang GitHub 2 years ago
parent
commit
47929ca2f0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 88 additions and 43 deletions
  1. +12
    -2
      pkg/client/client.go
  2. +24
    -0
      pkg/datasource/init.go
  3. +5
    -0
      pkg/datasource/sql/conn_at_test.go
  4. +1
    -1
      pkg/datasource/sql/driver.go
  5. +1
    -1
      pkg/datasource/sql/exec/at/at_executor.go
  6. +0
    -6
      pkg/datasource/sql/exec/executor.go
  7. +1
    -1
      pkg/datasource/sql/exec/xa/default.go
  8. +3
    -2
      pkg/datasource/sql/hook/logger_hook.go
  9. +2
    -2
      pkg/datasource/sql/hook/undo_log_hook.go
  10. +38
    -7
      pkg/datasource/sql/plugin.go
  11. +0
    -4
      pkg/datasource/sql/undo/builder/mysql_delete_undo_log_builder.go
  12. +0
    -4
      pkg/datasource/sql/undo/builder/mysql_insert_undo_log_builder.go
  13. +0
    -4
      pkg/datasource/sql/undo/builder/mysql_insertonduplicate_update_undo_log_builder.go
  14. +0
    -4
      pkg/datasource/sql/undo/builder/mysql_multi_undo_log_builder.go
  15. +0
    -4
      pkg/datasource/sql/undo/builder/mysql_update_undo_log_builder.go
  16. +1
    -1
      pkg/datasource/sql/undo/mysql/default.go

+ 12
- 2
pkg/client/client.go View File

@@ -20,6 +20,8 @@ package client
import (
"sync"

"github.com/seata/seata-go/pkg/datasource"

at "github.com/seata/seata-go/pkg/datasource/sql"
"github.com/seata/seata-go/pkg/integration"
"github.com/seata/seata-go/pkg/remoting/getty"
@@ -41,11 +43,13 @@ func InitPath(configFilePath string) {

initRmClient(cfg)
initTmClient(cfg)
initDatasource(cfg)
}

var (
onceInitTmClient sync.Once
onceInitRmClient sync.Once
onceInitTmClient sync.Once
onceInitRmClient sync.Once
onceInitDatasource sync.Once
)

// InitTmClient init client tm client
@@ -80,3 +84,9 @@ func initRmClient(cfg *Config) {
at.InitAT(cfg.ClientConfig.UndoConfig, cfg.AsyncWorkerConfig)
})
}

func initDatasource(cfg *Config) {
onceInitDatasource.Do(func() {
datasource.Init()
})
}

+ 24
- 0
pkg/datasource/init.go View File

@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package datasource

import sql2 "github.com/seata/seata-go/pkg/datasource/sql"

func Init() {
sql2.Init()
}

+ 5
- 0
pkg/datasource/sql/conn_at_test.go View File

@@ -33,6 +33,11 @@ import (
"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
Init()
m.Run()
}

func initAtConnTestResource(t *testing.T) (*gomock.Controller, *sql.DB, *mockSQLInterceptor, *mockTxHook) {
ctrl := gomock.NewController(t)



+ 1
- 1
pkg/datasource/sql/driver.go View File

@@ -41,7 +41,7 @@ const (
SeataXAMySQLDriver = "seata-xa-mysql"
)

func init() {
func initDriver() {
sql.Register(SeataATMySQLDriver, &seataATDriver{
seataDriver: &seataDriver{
transType: types.ATMode,


+ 1
- 1
pkg/datasource/sql/exec/at/at_executor.go View File

@@ -28,7 +28,7 @@ import (
"github.com/seata/seata-go/pkg/datasource/sql/types"
)

func init() {
func Init() {
exec.RegisterATExecutor(types.DBTypeMySQL, func() exec.SQLExecutor { return &AtExecutor{} })
}



+ 0
- 6
pkg/datasource/sql/exec/executor.go View File

@@ -23,14 +23,8 @@ import (

"github.com/seata/seata-go/pkg/datasource/sql/parser"
"github.com/seata/seata-go/pkg/datasource/sql/types"
"github.com/seata/seata-go/pkg/datasource/sql/undo"
"github.com/seata/seata-go/pkg/datasource/sql/undo/builder"
)

func init() {
undo.RegisterUndoLogBuilder(types.MultiExecutor, builder.GetMySQLMultiUndoLogBuilder)
}

var (
atExecutors = make(map[types.DBType]func() SQLExecutor)
xaExecutors = make(map[types.DBType]func() SQLExecutor)


+ 1
- 1
pkg/datasource/sql/exec/xa/default.go View File

@@ -22,7 +22,7 @@ import (
"github.com/seata/seata-go/pkg/datasource/sql/types"
)

func init() {
func Init() {
exec.RegisterXAExecutor(types.DBTypeMySQL, func() exec.SQLExecutor {
return &XAExecutor{}
})


+ 3
- 2
pkg/datasource/sql/hook/logger_hook.go View File

@@ -21,13 +21,14 @@ import (
"context"

"github.com/seata/seata-go/pkg/datasource/sql/exec"

"github.com/seata/seata-go/pkg/datasource/sql/types"
"github.com/seata/seata-go/pkg/util/log"
"go.uber.org/zap"
)

func init() {
exec.RegisterHook(&loggerSQLHook{})
func NewLoggerSQLHook() exec.SQLHook {
return &loggerSQLHook{}
}

type loggerSQLHook struct{}


+ 2
- 2
pkg/datasource/sql/hook/undo_log_hook.go View File

@@ -27,8 +27,8 @@ import (
"github.com/seata/seata-go/pkg/tm"
)

func init() {
exec.RegisterHook(&undoLogSQLHook{})
func NewUndoLogSQLHook() exec.SQLHook {
return &undoLogSQLHook{}
}

type undoLogSQLHook struct {


+ 38
- 7
pkg/datasource/sql/plugin.go View File

@@ -18,12 +18,43 @@
package sql

import (
_ "github.com/seata/seata-go/pkg/datasource/sql/hook"
"github.com/seata/seata-go/pkg/datasource/sql/exec"
"github.com/seata/seata-go/pkg/datasource/sql/exec/at"
"github.com/seata/seata-go/pkg/datasource/sql/exec/xa"
"github.com/seata/seata-go/pkg/datasource/sql/hook"
"github.com/seata/seata-go/pkg/datasource/sql/types"
"github.com/seata/seata-go/pkg/datasource/sql/undo"
"github.com/seata/seata-go/pkg/datasource/sql/undo/builder"
"github.com/seata/seata-go/pkg/datasource/sql/undo/mysql"
)

// mysql 相关插件
_ "github.com/seata/seata-go/pkg/datasource/sql/datasource/mysql"
_ "github.com/seata/seata-go/pkg/datasource/sql/undo/mysql"
func Init() {
hookRegister()
executorRegister()
undoInit()
initDriver()
}

_ "github.com/seata/seata-go/pkg/datasource/sql/exec/at"
_ "github.com/seata/seata-go/pkg/datasource/sql/exec/xa"
)
func hookRegister() {
exec.RegisterHook(hook.NewLoggerSQLHook())
exec.RegisterHook(hook.NewUndoLogSQLHook())
}

func executorRegister() {
at.Init()
xa.Init()
}

func undoInit() {
mysqlUndoLogInit()
}

func mysqlUndoLogInit() {
mysql.InitUndoLogManager()

undo.RegisterUndoLogBuilder(types.DeleteExecutor, builder.GetMySQLDeleteUndoLogBuilder)
undo.RegisterUndoLogBuilder(types.InsertExecutor, builder.GetMySQLInsertUndoLogBuilder)
undo.RegisterUndoLogBuilder(types.InsertOnDuplicateExecutor, builder.GetMySQLInsertOnDuplicateUndoLogBuilder)
undo.RegisterUndoLogBuilder(types.MultiExecutor, builder.GetMySQLMultiUndoLogBuilder)
undo.RegisterUndoLogBuilder(types.UpdateExecutor, builder.GetMySQLUpdateUndoLogBuilder)
}

+ 0
- 4
pkg/datasource/sql/undo/builder/mysql_delete_undo_log_builder.go View File

@@ -33,10 +33,6 @@ import (
"github.com/seata/seata-go/pkg/util/log"
)

func init() {
undo.RegisterUndoLogBuilder(types.DeleteExecutor, GetMySQLDeleteUndoLogBuilder)
}

type MySQLDeleteUndoLogBuilder struct {
BasicUndoLogBuilder
}


+ 0
- 4
pkg/datasource/sql/undo/builder/mysql_insert_undo_log_builder.go View File

@@ -34,10 +34,6 @@ const (
SqlPlaceholder = "?"
)

func init() {
undo.RegisterUndoLogBuilder(types.InsertExecutor, GetMySQLInsertUndoLogBuilder)
}

type MySQLInsertUndoLogBuilder struct {
BasicUndoLogBuilder
// InsertResult after insert sql


+ 0
- 4
pkg/datasource/sql/undo/builder/mysql_insertonduplicate_update_undo_log_builder.go View File

@@ -30,10 +30,6 @@ import (
"github.com/seata/seata-go/pkg/util/log"
)

func init() {
undo.RegisterUndoLogBuilder(types.InsertOnDuplicateExecutor, GetMySQLInsertOnDuplicateUndoLogBuilder)
}

type MySQLInsertOnDuplicateUndoLogBuilder struct {
MySQLInsertUndoLogBuilder
BeforeSelectSql string


+ 0
- 4
pkg/datasource/sql/undo/builder/mysql_multi_undo_log_builder.go View File

@@ -24,10 +24,6 @@ import (
"github.com/seata/seata-go/pkg/datasource/sql/undo"
)

func init() {
undo.RegisterUndoLogBuilder(types.MultiExecutor, GetMySQLMultiUndoLogBuilder)
}

type MySQLMultiUndoLogBuilder struct {
BasicUndoLogBuilder
beforeImages []*types.RecordImage


+ 0
- 4
pkg/datasource/sql/undo/builder/mysql_update_undo_log_builder.go View File

@@ -39,10 +39,6 @@ const (
OnlyCareUpdateColumns = true
)

func init() {
undo.RegisterUndoLogBuilder(types.UpdateExecutor, GetMySQLUpdateUndoLogBuilder)
}

type MySQLUpdateUndoLogBuilder struct {
BasicUndoLogBuilder
}


+ 1
- 1
pkg/datasource/sql/undo/mysql/default.go View File

@@ -23,7 +23,7 @@ import (
"github.com/seata/seata-go/pkg/datasource/sql/undo/base"
)

func init() {
func InitUndoLogManager() {
if err := undo.RegisterUndoLogManager(&undoLogManager{Base: base.NewBaseUndoLogManager()}); err != nil {
panic(errors.WithStack(err))
}


Loading…
Cancel
Save