github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cmd/server_test.go (about) 1 // Copyright 2020 PingCAP, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package cmd 15 16 import ( 17 "fmt" 18 "io/ioutil" 19 "path/filepath" 20 "time" 21 22 "github.com/pingcap/check" 23 "github.com/pingcap/ticdc/pkg/config" 24 "github.com/pingcap/ticdc/pkg/util/testleak" 25 ticonfig "github.com/pingcap/tidb/config" 26 "github.com/spf13/cobra" 27 ) 28 29 type serverSuite struct{} 30 31 var _ = check.Suite(&serverSuite{}) 32 33 func (s *serverSuite) TestPatchTiDBConf(c *check.C) { 34 defer testleak.AfterTest(c)() 35 patchTiDBConf() 36 cfg := ticonfig.GetGlobalConfig() 37 c.Assert(cfg.TiKVClient.MaxBatchSize, check.Equals, uint(0)) 38 } 39 40 func (s *serverSuite) TestDataDirServerConfig(c *check.C) { 41 defer testleak.AfterTest(c)() 42 cmd := new(cobra.Command) 43 initServerCmd(cmd) 44 c.Assert(cmd.ParseFlags([]string{}), check.IsNil) 45 cfg, err := loadAndVerifyServerConfig(cmd) 46 c.Assert(err, check.IsNil) 47 c.Assert(cfg, check.NotNil) 48 // data dir default to "" 49 c.Assert(cfg.DataDir, check.Equals, "") 50 c.Assert(cfg.Sorter.SortDir, check.Equals, filepath.Join("", "/tmp/sorter")) 51 52 dataDir := c.MkDir() 53 cmd = new(cobra.Command) 54 initServerCmd(cmd) 55 c.Assert(cmd.ParseFlags([]string{"--data-dir=" + dataDir}), check.IsNil) 56 cfg, err = loadAndVerifyServerConfig(cmd) 57 c.Assert(err, check.IsNil) 58 c.Assert(cfg, check.NotNil) 59 c.Assert(cfg.DataDir, check.Equals, dataDir) 60 // sorter-dir is not set yet 61 c.Assert(cfg.Sorter.SortDir, check.Equals, "/tmp/sorter") 62 } 63 64 func (s *serverSuite) TestLoadAndVerifyServerConfig(c *check.C) { 65 defer testleak.AfterTest(c)() 66 // test default flag values 67 cmd := new(cobra.Command) 68 initServerCmd(cmd) 69 c.Assert(cmd.ParseFlags([]string{}), check.IsNil) 70 cfg, err := loadAndVerifyServerConfig(cmd) 71 c.Assert(err, check.IsNil) 72 c.Assert(cfg, check.NotNil) 73 74 defcfg := config.GetDefaultServerConfig() 75 c.Assert(defcfg.ValidateAndAdjust(), check.IsNil) 76 c.Assert(cfg, check.DeepEquals, defcfg) 77 c.Assert(serverPdAddr, check.Equals, "http://127.0.0.1:2379") 78 79 // test empty PD address 80 cmd = new(cobra.Command) 81 initServerCmd(cmd) 82 c.Assert(cmd.ParseFlags([]string{"--pd="}), check.IsNil) 83 _, err = loadAndVerifyServerConfig(cmd) 84 c.Assert(err, check.ErrorMatches, ".*empty PD address.*") 85 86 // test invalid PD address 87 cmd = new(cobra.Command) 88 initServerCmd(cmd) 89 c.Assert(cmd.ParseFlags([]string{"--pd=aa"}), check.IsNil) 90 _, err = loadAndVerifyServerConfig(cmd) 91 c.Assert(err, check.ErrorMatches, ".*PD endpoint should be a valid http or https URL.*") 92 93 // test invalid PD address(without host) 94 cmd = new(cobra.Command) 95 initServerCmd(cmd) 96 c.Assert(cmd.ParseFlags([]string{"--pd=http://"}), check.IsNil) 97 _, err = loadAndVerifyServerConfig(cmd) 98 c.Assert(err, check.ErrorMatches, ".*PD endpoint should be a valid http or https URL.*") 99 100 // test missing certificate 101 cmd = new(cobra.Command) 102 initServerCmd(cmd) 103 c.Assert(cmd.ParseFlags([]string{"--pd=https://aa"}), check.IsNil) 104 _, err = loadAndVerifyServerConfig(cmd) 105 c.Assert(err, check.ErrorMatches, ".*PD endpoint scheme is https, please provide certificate.*") 106 107 // test undefined flag 108 cmd = new(cobra.Command) 109 initServerCmd(cmd) 110 c.Assert(cmd.ParseFlags([]string{"--PD="}), check.ErrorMatches, ".*unknown flag: --PD.*") 111 _, err = loadAndVerifyServerConfig(cmd) 112 c.Assert(err, check.IsNil) 113 114 // test flags without config file 115 dataDir := c.MkDir() 116 cmd = new(cobra.Command) 117 initServerCmd(cmd) 118 c.Assert(cmd.ParseFlags([]string{ 119 "--addr", "127.5.5.1:8833", 120 "--advertise-addr", "127.5.5.1:7777", 121 "--log-file", "/root/cdc.log", 122 "--log-level", "debug", 123 "--data-dir", dataDir, 124 "--gc-ttl", "10", 125 "--tz", "UTC", 126 "--owner-flush-interval", "150ms", 127 "--processor-flush-interval", "150ms", 128 "--cert", "bb", 129 "--key", "cc", 130 "--cert-allowed-cn", "dd,ee", 131 "--sorter-chunk-size-limit", "50000000", 132 "--sorter-max-memory-consumption", "60000", 133 "--sorter-max-memory-percentage", "70", 134 "--sorter-num-concurrent-worker", "80", 135 "--sorter-num-workerpool-goroutine", "90", 136 "--sort-dir", "/tmp/just_a_test", 137 }), check.IsNil) 138 cfg, err = loadAndVerifyServerConfig(cmd) 139 c.Assert(err, check.IsNil) 140 c.Assert(cfg, check.DeepEquals, &config.ServerConfig{ 141 Addr: "127.5.5.1:8833", 142 AdvertiseAddr: "127.5.5.1:7777", 143 LogFile: "/root/cdc.log", 144 LogLevel: "debug", 145 Log: &config.LogConfig{ 146 File: &config.LogFileConfig{ 147 MaxSize: 300, 148 MaxDays: 0, 149 MaxBackups: 0, 150 }, 151 }, 152 DataDir: dataDir, 153 GcTTL: 10, 154 TZ: "UTC", 155 CaptureSessionTTL: 10, 156 OwnerFlushInterval: config.TomlDuration(150 * time.Millisecond), 157 ProcessorFlushInterval: config.TomlDuration(150 * time.Millisecond), 158 Sorter: &config.SorterConfig{ 159 NumConcurrentWorker: 80, 160 ChunkSizeLimit: 50000000, 161 MaxMemoryPressure: 70, 162 MaxMemoryConsumption: 60000, 163 NumWorkerPoolGoroutine: 90, 164 SortDir: config.DefaultSortDir, 165 }, 166 Security: &config.SecurityConfig{ 167 CertPath: "bb", 168 KeyPath: "cc", 169 CertAllowedCN: []string{"dd", "ee"}, 170 }, 171 PerTableMemoryQuota: 20 * 1024 * 1024, // 20M 172 KVClient: &config.KVClientConfig{ 173 WorkerConcurrent: 8, 174 WorkerPoolSize: 0, 175 RegionScanLimit: 40, 176 }, 177 }) 178 179 // test decode config file 180 dataDir = c.MkDir() 181 tmpDir := c.MkDir() 182 configPath := filepath.Join(tmpDir, "ticdc.toml") 183 configContent := fmt.Sprintf(` 184 addr = "128.0.0.1:1234" 185 advertise-addr = "127.0.0.1:1111" 186 187 log-file = "/root/cdc1.log" 188 log-level = "warn" 189 190 data-dir = "%+v" 191 gc-ttl = 500 192 tz = "US" 193 capture-session-ttl = 10 194 195 owner-flush-interval = "600ms" 196 processor-flush-interval = "600ms" 197 198 [log.file] 199 max-size = 200 200 max-days = 1 201 max-backups = 1 202 203 [sorter] 204 chunk-size-limit = 10000000 205 max-memory-consumption = 2000000 206 max-memory-percentage = 3 207 num-concurrent-worker = 4 208 num-workerpool-goroutine = 5 209 sort-dir = "/tmp/just_a_test" 210 `, dataDir) 211 err = ioutil.WriteFile(configPath, []byte(configContent), 0o644) 212 c.Assert(err, check.IsNil) 213 cmd = new(cobra.Command) 214 initServerCmd(cmd) 215 c.Assert(cmd.ParseFlags([]string{"--config", configPath}), check.IsNil) 216 cfg, err = loadAndVerifyServerConfig(cmd) 217 c.Assert(err, check.IsNil) 218 c.Assert(cfg, check.DeepEquals, &config.ServerConfig{ 219 Addr: "128.0.0.1:1234", 220 AdvertiseAddr: "127.0.0.1:1111", 221 LogFile: "/root/cdc1.log", 222 LogLevel: "warn", 223 Log: &config.LogConfig{ 224 File: &config.LogFileConfig{ 225 MaxSize: 200, 226 MaxDays: 1, 227 MaxBackups: 1, 228 }, 229 }, 230 DataDir: dataDir, 231 GcTTL: 500, 232 TZ: "US", 233 CaptureSessionTTL: 10, 234 OwnerFlushInterval: config.TomlDuration(600 * time.Millisecond), 235 ProcessorFlushInterval: config.TomlDuration(600 * time.Millisecond), 236 Sorter: &config.SorterConfig{ 237 NumConcurrentWorker: 4, 238 ChunkSizeLimit: 10000000, 239 MaxMemoryPressure: 3, 240 MaxMemoryConsumption: 2000000, 241 NumWorkerPoolGoroutine: 5, 242 SortDir: config.DefaultSortDir, 243 }, 244 Security: &config.SecurityConfig{}, 245 PerTableMemoryQuota: 20 * 1024 * 1024, // 20M 246 KVClient: &config.KVClientConfig{ 247 WorkerConcurrent: 8, 248 WorkerPoolSize: 0, 249 RegionScanLimit: 40, 250 }, 251 }) 252 253 configContent = configContent + ` 254 [security] 255 ca-path = "aa" 256 cert-path = "bb" 257 key-path = "cc" 258 cert-allowed-cn = ["dd","ee"] 259 ` 260 err = ioutil.WriteFile(configPath, []byte(configContent), 0o644) 261 c.Assert(err, check.IsNil) 262 cmd = new(cobra.Command) 263 initServerCmd(cmd) 264 c.Assert(cmd.ParseFlags([]string{ 265 "--addr", "127.5.5.1:8833", 266 "--log-file", "/root/cdc.log", 267 "--log-level", "debug", 268 "--data-dir", dataDir, 269 "--gc-ttl", "10", 270 "--tz", "UTC", 271 "--owner-flush-interval", "150ms", 272 "--processor-flush-interval", "150ms", 273 "--ca", "", 274 "--sorter-chunk-size-limit", "50000000", 275 "--sorter-max-memory-consumption", "60000000", 276 "--sorter-max-memory-percentage", "70", 277 "--sorter-num-concurrent-worker", "3", 278 "--config", configPath, 279 }), check.IsNil) 280 cfg, err = loadAndVerifyServerConfig(cmd) 281 c.Assert(err, check.IsNil) 282 c.Assert(cfg, check.DeepEquals, &config.ServerConfig{ 283 Addr: "127.5.5.1:8833", 284 AdvertiseAddr: "127.0.0.1:1111", 285 LogFile: "/root/cdc.log", 286 LogLevel: "debug", 287 Log: &config.LogConfig{ 288 File: &config.LogFileConfig{ 289 MaxSize: 200, 290 MaxDays: 1, 291 MaxBackups: 1, 292 }, 293 }, 294 DataDir: dataDir, 295 GcTTL: 10, 296 TZ: "UTC", 297 CaptureSessionTTL: 10, 298 OwnerFlushInterval: config.TomlDuration(150 * time.Millisecond), 299 ProcessorFlushInterval: config.TomlDuration(150 * time.Millisecond), 300 Sorter: &config.SorterConfig{ 301 NumConcurrentWorker: 3, 302 ChunkSizeLimit: 50000000, 303 MaxMemoryPressure: 70, 304 MaxMemoryConsumption: 60000000, 305 NumWorkerPoolGoroutine: 5, 306 SortDir: config.DefaultSortDir, 307 }, 308 Security: &config.SecurityConfig{ 309 CertPath: "bb", 310 KeyPath: "cc", 311 CertAllowedCN: []string{"dd", "ee"}, 312 }, 313 PerTableMemoryQuota: 20 * 1024 * 1024, // 20M 314 KVClient: &config.KVClientConfig{ 315 WorkerConcurrent: 8, 316 WorkerPoolSize: 0, 317 RegionScanLimit: 40, 318 }, 319 }) 320 }