github.com/KinWaiYuen/client-go/v2@v2.5.4/config/config.go (about) 1 // Copyright 2021 TiKV Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // NOTE: The code in this file is based on code from the 16 // TiDB project, licensed under the Apache License v 2.0 17 // 18 // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/config/config.go 19 // 20 21 // Copyright 2021 PingCAP, Inc. 22 // 23 // Licensed under the Apache License, Version 2.0 (the "License"); 24 // you may not use this file except in compliance with the License. 25 // You may obtain a copy of the License at 26 // 27 // http://www.apache.org/licenses/LICENSE-2.0 28 // 29 // Unless required by applicable law or agreed to in writing, software 30 // distributed under the License is distributed on an "AS IS" BASIS, 31 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 32 // See the License for the specific language governing permissions and 33 // limitations under the License. 34 35 package config 36 37 import ( 38 "fmt" 39 "net/url" 40 "strings" 41 "sync/atomic" 42 43 "github.com/KinWaiYuen/client-go/v2/internal/logutil" 44 "github.com/KinWaiYuen/client-go/v2/oracle" 45 "github.com/KinWaiYuen/client-go/v2/util" 46 "github.com/pingcap/errors" 47 "go.uber.org/zap" 48 ) 49 50 var ( 51 globalConf atomic.Value 52 ) 53 54 const ( 55 // DefStoresRefreshInterval is the default value of StoresRefreshInterval 56 DefStoresRefreshInterval = 60 57 ) 58 59 func init() { 60 conf := DefaultConfig() 61 StoreGlobalConfig(&conf) 62 } 63 64 // Config contains configuration options. 65 type Config struct { 66 CommitterConcurrency int 67 MaxTxnTTL uint64 68 TiKVClient TiKVClient 69 Security Security 70 PDClient PDClient 71 PessimisticTxn PessimisticTxn 72 TxnLocalLatches TxnLocalLatches 73 // StoresRefreshInterval indicates the interval of refreshing stores info, the unit is second. 74 StoresRefreshInterval uint64 75 OpenTracingEnable bool 76 Path string 77 EnableForwarding bool 78 TxnScope string 79 EnableAsyncCommit bool 80 Enable1PC bool 81 } 82 83 // DefaultConfig returns the default configuration. 84 func DefaultConfig() Config { 85 return Config{ 86 CommitterConcurrency: 128, 87 MaxTxnTTL: 60 * 60 * 1000, // 1hour 88 TiKVClient: DefaultTiKVClient(), 89 PDClient: DefaultPDClient(), 90 TxnLocalLatches: DefaultTxnLocalLatches(), 91 StoresRefreshInterval: DefStoresRefreshInterval, 92 OpenTracingEnable: false, 93 Path: "", 94 EnableForwarding: false, 95 TxnScope: "", 96 EnableAsyncCommit: false, 97 Enable1PC: false, 98 } 99 } 100 101 // PDClient is the config for PD client. 102 type PDClient struct { 103 // PDServerTimeout is the max time which PD client will wait for the PD server in seconds. 104 PDServerTimeout uint `toml:"pd-server-timeout" json:"pd-server-timeout"` 105 } 106 107 // DefaultPDClient returns the default configuration for PDClient 108 func DefaultPDClient() PDClient { 109 return PDClient{ 110 PDServerTimeout: 3, 111 } 112 } 113 114 // TxnLocalLatches is the TxnLocalLatches section of the config. 115 type TxnLocalLatches struct { 116 Enabled bool `toml:"-" json:"-"` 117 Capacity uint `toml:"-" json:"-"` 118 } 119 120 // DefaultTxnLocalLatches returns the default configuration for TxnLocalLatches 121 func DefaultTxnLocalLatches() TxnLocalLatches { 122 return TxnLocalLatches{ 123 Enabled: false, 124 Capacity: 0, 125 } 126 } 127 128 // Valid returns true if the configuration is valid. 129 func (c *TxnLocalLatches) Valid() error { 130 if c.Enabled && c.Capacity == 0 { 131 return fmt.Errorf("txn-local-latches.capacity can not be 0") 132 } 133 return nil 134 } 135 136 // PessimisticTxn is the config for pessimistic transaction. 137 type PessimisticTxn struct { 138 // The max count of retry for a single statement in a pessimistic transaction. 139 MaxRetryCount uint `toml:"max-retry-count" json:"max-retry-count"` 140 } 141 142 // GetGlobalConfig returns the global configuration for this server. 143 // It should store configuration from command line and configuration file. 144 // Other parts of the system can read the global configuration use this function. 145 func GetGlobalConfig() *Config { 146 return globalConf.Load().(*Config) 147 } 148 149 // StoreGlobalConfig stores a new config to the globalConf. It mostly uses in the test to avoid some data races. 150 func StoreGlobalConfig(config *Config) { 151 globalConf.Store(config) 152 } 153 154 // UpdateGlobal updates the global config, and provide a restore function that can be used to restore to the original. 155 func UpdateGlobal(f func(conf *Config)) func() { 156 g := GetGlobalConfig() 157 restore := func() { 158 StoreGlobalConfig(g) 159 } 160 newConf := *g 161 f(&newConf) 162 StoreGlobalConfig(&newConf) 163 return restore 164 } 165 166 // GetTxnScopeFromConfig extracts @@txn_scope value from the config. 167 func GetTxnScopeFromConfig() string { 168 var txnScope string 169 if kvcfg := GetGlobalConfig(); kvcfg != nil { 170 txnScope = kvcfg.TxnScope 171 } 172 if val, err := util.EvalFailpoint("injectTxnScope"); err == nil { 173 txnScope = val.(string) 174 } 175 if len(txnScope) > 0 { 176 return txnScope 177 } 178 return oracle.GlobalTxnScope 179 } 180 181 // ParsePath parses this path. 182 // Path example: tikv://etcd-node1:port,etcd-node2:port?cluster=1&disableGC=false 183 func ParsePath(path string) (etcdAddrs []string, disableGC bool, err error) { 184 var u *url.URL 185 u, err = url.Parse(path) 186 if err != nil { 187 err = errors.Trace(err) 188 return 189 } 190 if strings.ToLower(u.Scheme) != "tikv" { 191 err = errors.Errorf("Uri scheme expected [tikv] but found [%s]", u.Scheme) 192 logutil.BgLogger().Error("parsePath error", zap.Error(err)) 193 return 194 } 195 switch strings.ToLower(u.Query().Get("disableGC")) { 196 case "true": 197 disableGC = true 198 case "false", "": 199 default: 200 err = errors.New("disableGC flag should be true/false") 201 return 202 } 203 etcdAddrs = strings.Split(u.Host, ",") 204 return 205 }