vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/tabletenv/config_test.go (about) 1 /* 2 Copyright 2020 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package tabletenv 18 19 import ( 20 "testing" 21 "time" 22 23 "github.com/spf13/pflag" 24 "github.com/stretchr/testify/assert" 25 "github.com/stretchr/testify/require" 26 27 "vitess.io/vitess/go/test/utils" 28 "vitess.io/vitess/go/vt/dbconfigs" 29 "vitess.io/vitess/go/yaml2" 30 ) 31 32 func TestConfigParse(t *testing.T) { 33 cfg := TabletConfig{ 34 DB: &dbconfigs.DBConfigs{ 35 Socket: "a", 36 App: dbconfigs.UserConfig{ 37 User: "b", 38 }, 39 Dba: dbconfigs.UserConfig{ 40 User: "c", 41 }, 42 }, 43 OltpReadPool: ConnPoolConfig{ 44 Size: 16, 45 TimeoutSeconds: 10, 46 IdleTimeoutSeconds: 20, 47 PrefillParallelism: 30, 48 MaxWaiters: 40, 49 MaxLifetimeSeconds: 50, 50 }, 51 RowStreamer: RowStreamerConfig{ 52 MaxInnoDBTrxHistLen: 1000, 53 MaxMySQLReplLagSecs: 400, 54 }, 55 } 56 gotBytes, err := yaml2.Marshal(&cfg) 57 require.NoError(t, err) 58 wantBytes := `db: 59 allprivs: 60 password: '****' 61 app: 62 password: '****' 63 user: b 64 appdebug: 65 password: '****' 66 dba: 67 password: '****' 68 user: c 69 filtered: 70 password: '****' 71 repl: 72 password: '****' 73 socket: a 74 gracePeriods: {} 75 healthcheck: {} 76 hotRowProtection: {} 77 olap: {} 78 olapReadPool: {} 79 oltp: {} 80 oltpReadPool: 81 idleTimeoutSeconds: 20 82 maxLifetimeSeconds: 50 83 maxWaiters: 40 84 prefillParallelism: 30 85 size: 16 86 timeoutSeconds: 10 87 replicationTracker: {} 88 rowStreamer: 89 maxInnoDBTrxHistLen: 1000 90 maxMySQLReplLagSecs: 400 91 txPool: {} 92 ` 93 assert.Equal(t, wantBytes, string(gotBytes)) 94 95 // Make sure things already set don't get overwritten, 96 // and thing specified do overwrite. 97 // OltpReadPool.TimeoutSeconds should not get overwritten. 98 // DB.App.User should not get overwritten. 99 // DB.Dba.User should get overwritten. 100 inBytes := []byte(`db: 101 socket: a 102 dba: 103 user: c 104 oltpReadPool: 105 size: 16 106 idleTimeoutSeconds: 20 107 prefillParallelism: 30 108 maxWaiters: 40 109 maxLifetimeSeconds: 50 110 `) 111 gotCfg := cfg 112 gotCfg.DB = cfg.DB.Clone() 113 gotCfg.DB.Dba = dbconfigs.UserConfig{} 114 err = yaml2.Unmarshal(inBytes, &gotCfg) 115 require.NoError(t, err) 116 assert.Equal(t, cfg, gotCfg) 117 } 118 119 func TestDefaultConfig(t *testing.T) { 120 gotBytes, err := yaml2.Marshal(NewDefaultConfig()) 121 require.NoError(t, err) 122 want := `cacheResultFields: true 123 consolidator: enable 124 consolidatorStreamQuerySize: 2097152 125 consolidatorStreamTotalSize: 134217728 126 gracePeriods: {} 127 healthcheck: 128 degradedThresholdSeconds: 30 129 intervalSeconds: 20 130 unhealthyThresholdSeconds: 7200 131 hotRowProtection: 132 maxConcurrency: 5 133 maxGlobalQueueSize: 1000 134 maxQueueSize: 20 135 mode: disable 136 messagePostponeParallelism: 4 137 olap: 138 txTimeoutSeconds: 30 139 olapReadPool: 140 idleTimeoutSeconds: 1800 141 size: 200 142 oltp: 143 maxRows: 10000 144 queryTimeoutSeconds: 30 145 txTimeoutSeconds: 30 146 oltpReadPool: 147 idleTimeoutSeconds: 1800 148 maxWaiters: 5000 149 size: 16 150 queryCacheLFU: true 151 queryCacheMemory: 33554432 152 queryCacheSize: 5000 153 replicationTracker: 154 heartbeatIntervalSeconds: 0.25 155 mode: disable 156 rowStreamer: 157 maxInnoDBTrxHistLen: 1000000 158 maxMySQLReplLagSecs: 43200 159 schemaReloadIntervalSeconds: 1800 160 signalSchemaChangeReloadIntervalSeconds: 5 161 signalWhenSchemaChange: true 162 streamBufferSize: 32768 163 txPool: 164 idleTimeoutSeconds: 1800 165 maxWaiters: 5000 166 size: 20 167 timeoutSeconds: 1 168 ` 169 utils.MustMatch(t, want, string(gotBytes)) 170 } 171 172 func TestClone(t *testing.T) { 173 queryLogHandler = "" 174 txLogHandler = "" 175 176 cfg1 := &TabletConfig{ 177 OltpReadPool: ConnPoolConfig{ 178 Size: 16, 179 TimeoutSeconds: 10, 180 IdleTimeoutSeconds: 20, 181 PrefillParallelism: 30, 182 MaxWaiters: 40, 183 MaxLifetimeSeconds: 50, 184 }, 185 RowStreamer: RowStreamerConfig{ 186 MaxInnoDBTrxHistLen: 1000000, 187 MaxMySQLReplLagSecs: 43200, 188 }, 189 } 190 cfg2 := cfg1.Clone() 191 assert.Equal(t, cfg1, cfg2) 192 cfg1.OltpReadPool.Size = 10 193 assert.NotEqual(t, cfg1, cfg2) 194 } 195 196 func TestFlags(t *testing.T) { 197 fs := pflag.NewFlagSet("TestFlags", pflag.ContinueOnError) 198 registerTabletEnvFlags(fs) 199 want := *NewDefaultConfig() 200 want.DB = &dbconfigs.DBConfigs{} 201 assert.Equal(t, want.DB, currentConfig.DB) 202 assert.Equal(t, want, currentConfig) 203 204 // Simple Init. 205 Init() 206 want.OlapReadPool.IdleTimeoutSeconds = 1800 207 want.TxPool.IdleTimeoutSeconds = 1800 208 want.HotRowProtection.Mode = Disable 209 want.Consolidator = Enable 210 want.Healthcheck.IntervalSeconds = 20 211 want.Healthcheck.DegradedThresholdSeconds = 30 212 want.Healthcheck.UnhealthyThresholdSeconds = 7200 213 want.ReplicationTracker.HeartbeatIntervalSeconds = 1 214 want.ReplicationTracker.Mode = Disable 215 assert.Equal(t, want.DB, currentConfig.DB) 216 assert.Equal(t, want, currentConfig) 217 218 enableHotRowProtection = true 219 enableHotRowProtectionDryRun = true 220 Init() 221 want.HotRowProtection.Mode = Dryrun 222 assert.Equal(t, want, currentConfig) 223 224 enableHotRowProtection = true 225 enableHotRowProtectionDryRun = false 226 Init() 227 want.HotRowProtection.Mode = Enable 228 assert.Equal(t, want, currentConfig) 229 230 enableHotRowProtection = false 231 enableHotRowProtectionDryRun = true 232 Init() 233 want.HotRowProtection.Mode = Disable 234 assert.Equal(t, want, currentConfig) 235 236 enableHotRowProtection = false 237 enableHotRowProtectionDryRun = false 238 Init() 239 want.HotRowProtection.Mode = Disable 240 assert.Equal(t, want, currentConfig) 241 242 enableConsolidator = true 243 enableConsolidatorReplicas = true 244 Init() 245 want.Consolidator = NotOnPrimary 246 assert.Equal(t, want, currentConfig) 247 248 enableConsolidator = true 249 enableConsolidatorReplicas = false 250 Init() 251 want.Consolidator = Enable 252 assert.Equal(t, want, currentConfig) 253 254 enableConsolidator = false 255 enableConsolidatorReplicas = true 256 Init() 257 want.Consolidator = NotOnPrimary 258 assert.Equal(t, want, currentConfig) 259 260 enableConsolidator = false 261 enableConsolidatorReplicas = false 262 Init() 263 want.Consolidator = Disable 264 assert.Equal(t, want, currentConfig) 265 266 enableHeartbeat = true 267 heartbeatInterval = 1 * time.Second 268 currentConfig.ReplicationTracker.Mode = "" 269 currentConfig.ReplicationTracker.HeartbeatIntervalSeconds = 0 270 Init() 271 want.ReplicationTracker.Mode = Heartbeat 272 want.ReplicationTracker.HeartbeatIntervalSeconds = 1 273 assert.Equal(t, want, currentConfig) 274 275 enableHeartbeat = false 276 heartbeatInterval = 1 * time.Second 277 currentConfig.ReplicationTracker.Mode = "" 278 currentConfig.ReplicationTracker.HeartbeatIntervalSeconds = 0 279 Init() 280 want.ReplicationTracker.Mode = Disable 281 want.ReplicationTracker.HeartbeatIntervalSeconds = 1 282 assert.Equal(t, want, currentConfig) 283 284 enableReplicationReporter = true 285 heartbeatInterval = 1 * time.Second 286 currentConfig.ReplicationTracker.Mode = "" 287 currentConfig.ReplicationTracker.HeartbeatIntervalSeconds = 0 288 Init() 289 want.ReplicationTracker.Mode = Polling 290 want.ReplicationTracker.HeartbeatIntervalSeconds = 1 291 assert.Equal(t, want, currentConfig) 292 293 healthCheckInterval = 1 * time.Second 294 currentConfig.Healthcheck.IntervalSeconds = 0 295 Init() 296 want.Healthcheck.IntervalSeconds = 1 297 assert.Equal(t, want, currentConfig) 298 299 degradedThreshold = 2 * time.Second 300 currentConfig.Healthcheck.DegradedThresholdSeconds = 0 301 Init() 302 want.Healthcheck.DegradedThresholdSeconds = 2 303 assert.Equal(t, want, currentConfig) 304 305 unhealthyThreshold = 3 * time.Second 306 currentConfig.Healthcheck.UnhealthyThresholdSeconds = 0 307 Init() 308 want.Healthcheck.UnhealthyThresholdSeconds = 3 309 assert.Equal(t, want, currentConfig) 310 311 transitionGracePeriod = 4 * time.Second 312 currentConfig.GracePeriods.TransitionSeconds = 0 313 Init() 314 want.GracePeriods.TransitionSeconds = 4 315 assert.Equal(t, want, currentConfig) 316 317 currentConfig.SanitizeLogMessages = false 318 Init() 319 want.SanitizeLogMessages = false 320 assert.Equal(t, want, currentConfig) 321 322 currentConfig.SanitizeLogMessages = true 323 Init() 324 want.SanitizeLogMessages = true 325 assert.Equal(t, want, currentConfig) 326 }