From 2a12a86bea318d460411d60ef2130afc3157c656 Mon Sep 17 00:00:00 2001 From: liushao <505786909@qq.com> Date: Mon, 12 Dec 2022 17:41:08 +0800 Subject: [PATCH] feat: add tm config (#398) feat(pkg/client,pkg/config): add tm config --- pkg/client/config.go | 22 ++++++++-- pkg/client/config_test.go | 24 ++++++++++- .../builder/mysql_insert_undo_log_builder.go | 2 +- pkg/tm/config.go | 43 +++++++++++++++++++ testdata/conf/seatago.yml | 2 +- 5 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 pkg/tm/config.go diff --git a/pkg/client/config.go b/pkg/client/config.go index 584423fd1..73e6e808a 100644 --- a/pkg/client/config.go +++ b/pkg/client/config.go @@ -26,6 +26,8 @@ import ( "runtime" "strings" + "github.com/seata/seata-go/pkg/tm" + "github.com/knadh/koanf" "github.com/knadh/koanf/parsers/json" "github.com/knadh/koanf/parsers/toml" @@ -47,12 +49,25 @@ const ( ymlSuffix = "yml" ) +type ClientConfig struct { + TmConfig tm.TmConfig `yaml:"tm" json:"tm,omitempty" koanf:"tm"` +} + +func (c *ClientConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) { + // TODO: RmConf RegisterFlagsWithPrefix + // TODO: Undo RegisterFlagsWithPrefix + // TODO: LoadBalance RegisterFlagsWithPrefix + c.TmConfig.RegisterFlagsWithPrefix(prefix+".tm", f) +} + type Config struct { - TCCConfig tcc.Config `yaml:"tcc" json:"tcc" koanf:"tcc"` + TCCConfig tcc.Config `yaml:"tcc" json:"tcc" koanf:"tcc"` + ClientConfig ClientConfig `yaml:"client" json:"client" koanf:"client"` } func (c *Config) RegisterFlags(f *flag.FlagSet) { c.TCCConfig.FenceConfig.RegisterFlagsWithPrefix("tcc", f) + c.ClientConfig.RegisterFlagsWithPrefix("client", f) } type loaderConf struct { @@ -108,7 +123,7 @@ func getJsonConfigResolver(bytes []byte) *koanf.Koanf { return k } -//resolverFilePath resolver file path +// resolverFilePath resolver file path // eg: give a ./conf/seatago.yaml return seatago and yaml func resolverFilePath(path string) (name, suffix string) { paths := strings.Split(path, "/") @@ -175,7 +190,6 @@ func newLoaderConf(configFilePath string) *loaderConf { // absolutePath get absolut path func absolutePath(inPath string) string { - if inPath == "$HOME" || strings.HasPrefix(inPath, "$HOME"+string(os.PathSeparator)) { inPath = userHomeDir() + inPath[5:] } @@ -192,7 +206,7 @@ func absolutePath(inPath string) string { return "" } -//userHomeDir get gopath +// userHomeDir get gopath func userHomeDir() string { if runtime.GOOS == "windows" { home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") diff --git a/pkg/client/config_test.go b/pkg/client/config_test.go index 9e53a6f82..06dd38d0c 100644 --- a/pkg/client/config_test.go +++ b/pkg/client/config_test.go @@ -34,12 +34,23 @@ func TestLoadPath(t *testing.T) { assert.Equal(t, "tcc_fence_log_test", cfg.TCCConfig.FenceConfig.LogTableName) assert.Equal(t, time.Second*60, cfg.TCCConfig.FenceConfig.CleanPeriod) + + assert.NotNil(t, cfg.ClientConfig) + assert.NotNil(t, cfg.ClientConfig.TmConfig) + assert.Equal(t, 5, cfg.ClientConfig.TmConfig.CommitRetryCount) + assert.Equal(t, 5, cfg.ClientConfig.TmConfig.RollbackRetryCount) + assert.Equal(t, time.Second*60, cfg.ClientConfig.TmConfig.DefaultGlobalTransactionTimeout) + assert.Equal(t, false, cfg.ClientConfig.TmConfig.DegradeCheck) + assert.Equal(t, 2000, cfg.ClientConfig.TmConfig.DegradeCheckPeriod) + assert.Equal(t, time.Second*10, cfg.ClientConfig.TmConfig.DegradeCheckAllowTimes) + assert.Equal(t, -2147482648, cfg.ClientConfig.TmConfig.InterceptorOrder) + // reset flag.CommandLine flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) } func TestLoadJson(t *testing.T) { - confJson := `{"tcc":{"fence":{"log-table-name":"tcc_fence_log_test2","clean-period":80000000000}}}` + confJson := `{"client":{"tm":{"commit-retry-count":5,"rollback-retry-count":5,"default-global-transaction-timeout":"60s","degrade-check":false,"degrade-check-period":2000,"degrade-check-allow-times":"10s","interceptor-order":-2147482648}},"tcc":{"fence":{"log-table-name":"tcc_fence_log_test2","clean-period":80000000000}}}` cfg := LoadJson([]byte(confJson)) assert.NotNil(t, cfg) assert.NotNil(t, cfg.TCCConfig) @@ -47,6 +58,17 @@ func TestLoadJson(t *testing.T) { assert.Equal(t, "tcc_fence_log_test2", cfg.TCCConfig.FenceConfig.LogTableName) assert.Equal(t, time.Second*80, cfg.TCCConfig.FenceConfig.CleanPeriod) + + assert.NotNil(t, cfg.ClientConfig) + assert.NotNil(t, cfg.ClientConfig.TmConfig) + assert.Equal(t, 5, cfg.ClientConfig.TmConfig.CommitRetryCount) + assert.Equal(t, 5, cfg.ClientConfig.TmConfig.RollbackRetryCount) + assert.Equal(t, time.Second*60, cfg.ClientConfig.TmConfig.DefaultGlobalTransactionTimeout) + assert.Equal(t, false, cfg.ClientConfig.TmConfig.DegradeCheck) + assert.Equal(t, 2000, cfg.ClientConfig.TmConfig.DegradeCheckPeriod) + assert.Equal(t, time.Second*10, cfg.ClientConfig.TmConfig.DegradeCheckAllowTimes) + assert.Equal(t, -2147482648, cfg.ClientConfig.TmConfig.InterceptorOrder) + // reset flag.CommandLine flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) } diff --git a/pkg/datasource/sql/undo/builder/mysql_insert_undo_log_builder.go b/pkg/datasource/sql/undo/builder/mysql_insert_undo_log_builder.go index af434e5a8..de2d998d6 100644 --- a/pkg/datasource/sql/undo/builder/mysql_insert_undo_log_builder.go +++ b/pkg/datasource/sql/undo/builder/mysql_insert_undo_log_builder.go @@ -186,7 +186,7 @@ func (u *MySQLInsertUndoLogBuilder) getPkValues(execCtx *types.ExecContext, pars return pkValuesMap, nil } -//containsPK the columns contains table meta pk +// containsPK the columns contains table meta pk func (u *MySQLInsertUndoLogBuilder) containsPK(meta types.TableMeta, parseCtx *types.ParseContext) bool { pkColumnNameList := meta.GetPrimaryKeyOnlyName() if len(pkColumnNameList) == 0 { diff --git a/pkg/tm/config.go b/pkg/tm/config.go new file mode 100644 index 000000000..b940910c3 --- /dev/null +++ b/pkg/tm/config.go @@ -0,0 +1,43 @@ +/* + * 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 tm + +import ( + "flag" + "time" +) + +type TmConfig struct { + CommitRetryCount int `yaml:"commit-retry-count" json:"commit-retry-count" koanf:"commit-retry-count"` + RollbackRetryCount int `yaml:"rollback-retry-count" json:"rollback-retry-count" koanf:"rollback-retry-count"` + DefaultGlobalTransactionTimeout time.Duration `yaml:"default-global-transaction-timeout" json:"default-global-transaction-timeout,omitempty" koanf:"default-global-transaction-timeout"` + DegradeCheck bool `yaml:"degrade-check" json:"degrade-check" koanf:"degrade-check"` + DegradeCheckPeriod int `yaml:"degrade-check-period" json:"degrade-check-period" koanf:"degrade-check-period"` + DegradeCheckAllowTimes time.Duration `yaml:"degrade-check-allow-times" json:"degrade-check-allow-times" koanf:"degrade-check-allow-times"` + InterceptorOrder int `yaml:"interceptor-order" json:"interceptor-order" koanf:"interceptor-order"` +} + +func (cfg *TmConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) { + f.IntVar(&cfg.CommitRetryCount, prefix+".commit-retry-count", 5, "The maximum number of retries when commit global transaction.") + f.IntVar(&cfg.RollbackRetryCount, prefix+".rollback-retry-count", 5, "The maximum number of retries when rollback global transaction.") + f.DurationVar(&cfg.DefaultGlobalTransactionTimeout, prefix+".default-global-transaction-timeout", 60*time.Second, "The timeout for a global transaction.") + f.BoolVar(&cfg.DegradeCheck, prefix+".degrade-check", false, "The switch for degrade check.") + f.IntVar(&cfg.DegradeCheckPeriod, prefix+".degrade-check-period", 2000, "The period for degrade checking.") + f.DurationVar(&cfg.DegradeCheckAllowTimes, prefix+".degrade-check-allow-times", 10*time.Second, "The duration allowed for degrade checking.") + f.IntVar(&cfg.InterceptorOrder, prefix+".interceptor-order", -2147482648, "The order of interceptor.") +} diff --git a/testdata/conf/seatago.yml b/testdata/conf/seatago.yml index 1b29f28a9..92c46eab6 100644 --- a/testdata/conf/seatago.yml +++ b/testdata/conf/seatago.yml @@ -50,7 +50,7 @@ seata: tm: commit-retry-count: 5 rollback-retry-count: 5 - default-global-transaction-timeout: 10s + default-global-transaction-timeout: 60s degrade-check: false degrade-check-period: 2000 degrade-check-allow-times: 10s