Skip to content

Commit

Permalink
refactor(pkg,testdata): optimize struct name,default value,unit test
Browse files Browse the repository at this point in the history
1. Rename struct TmConf to TmConfig.
2. Set the default value of global-transation-timeout to 60s.
3. Perfecting the unit test for TmConfig, cover all configuration items.
4. Execute goimports.sh in project root path.
  • Loading branch information
liushao committed Dec 11, 2022
1 parent ef1df34 commit fb0876d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 21 deletions.
4 changes: 2 additions & 2 deletions pkg/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ const (
)

type ClientConf struct {
Tmconf tm.TmConf `yaml:"tm" json:"tm,omitempty" property:"tm" koanf:"tm"`
TmConfig tm.TmConfig `yaml:"tm" json:"tm,omitempty" property:"tm" koanf:"tm"`
}

func (c *ClientConf) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
// TODO: RmConf RegisterFlagsWithPrefix
// TODO: Undo RegisterFlagsWithPrefix
// TODO: LoadBalance RegisterFlagsWithPrefix
c.Tmconf.RegisterFlagsWithPrefix(prefix+".tm", f)
c.TmConfig.RegisterFlagsWithPrefix(prefix+".tm", f)
}

type Config struct {
Expand Down
25 changes: 18 additions & 7 deletions pkg/client/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,21 @@ func TestLoadPath(t *testing.T) {
assert.Equal(t, time.Second*60, cfg.TCCConfig.FenceConfig.CleanPeriod)

assert.NotNil(t, cfg.ClientConfig)
assert.NotNil(t, cfg.ClientConfig.Tmconf)
assert.Equal(t, 5, cfg.ClientConfig.Tmconf.CommitRetryCount)
assert.Equal(t, time.Second*10, cfg.ClientConfig.Tmconf.DefaultGlobalTransactionTimeout)
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 := `{"client":{"tm":{"commit-retry-count":5,"default-global-transaction-timeout":"10s"}},"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)
Expand All @@ -55,9 +60,15 @@ func TestLoadJson(t *testing.T) {
assert.Equal(t, time.Second*80, cfg.TCCConfig.FenceConfig.CleanPeriod)

assert.NotNil(t, cfg.ClientConfig)
assert.NotNil(t, cfg.ClientConfig.Tmconf)
assert.Equal(t, 5, cfg.ClientConfig.Tmconf.CommitRetryCount)
assert.Equal(t, time.Second*10, cfg.ClientConfig.Tmconf.DefaultGlobalTransactionTimeout)
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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 27 additions & 10 deletions pkg/tm/config.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
/*
* 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 TmConf struct {
CommitRetryCount int `yaml:"commit-retry-count" json:"commit-retry-count,omitempty" property:"commit-retry-count" koanf:"commit-retry-count"`
RollbackRetryCount int `yaml:"rollback-retry-count" json:"rollback-retry-count,omitempty" property:"rollback-retry-count" koanf:"rollback-retry-count"`
DefaultGlobalTransactionTimeout time.Duration `yaml:"default-global-transaction-timeout" json:"default-global-transaction-timeout,omitempty" property:"default-global-transaction-timeout" koanf:"default-global-transaction-timeout"`
DegradeCheck bool `yaml:"degrade-check" json:"degrade-check,omitempty" property:"degrade-check" koanf:"degrade-check"`
DegradeCheckPeriod int `yaml:"degrade-check-period" json:"degrade-check-period,omitempty" property:"degrade-check-period" koanf:"degrade-check-period"`
DegradeCheckAllowTimes time.Duration `yaml:"degrade-check-allow-times" json:"degrade-check-allow-times,omitempty" property:"degrade-check-allow-times" koanf:"degrade-check-allow-times"`
InterceptorOrder int `yaml:"interceptor-order" json:"interceptor-order,omitempty" property:"interceptor-order" koanf:"interceptor-order"`
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 *TmConf) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
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", 10*time.Second, "The timeout for a 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.")
Expand Down
2 changes: 1 addition & 1 deletion testdata/conf/seatago.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fb0876d

Please sign in to comment.