github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/cmd/config/config_util_test.go (about) 1 // Copyright 2020 WHTCORPS INC, 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 config 15 16 import ( 17 "encoding/json" 18 "fmt" 19 "io/ioutil" 20 "os" 21 "path/filepath" 22 "reflect" 23 24 "github.com/BurntSushi/toml" 25 . "github.com/whtcorpsinc/check" 26 ) 27 28 func (s *testConfigSuite) TestCloneConf(c *C) { 29 c1, err := CloneConf(&defaultConf) 30 c.Assert(err, IsNil) 31 c2, err := CloneConf(c1) 32 c.Assert(err, IsNil) 33 c.Assert(reflect.DeepEqual(c1, c2), IsTrue) 34 35 c1.CausetStore = "abc" 36 c1.Port = 2333 37 c1.Log.EnableSlowLog = !c1.Log.EnableSlowLog 38 c1.RepairTableList = append(c1.RepairTableList, "abc") 39 c.Assert(c1.CausetStore, Not(Equals), c2.CausetStore) 40 c.Assert(c1.Port, Not(Equals), c2.Port) 41 c.Assert(c1.Log.EnableSlowLog, Not(Equals), c2.Log.EnableSlowLog) 42 c.Assert(fmt.Sprintf("%v", c1.RepairTableList), Not(Equals), fmt.Sprintf("%v", c2.RepairTableList)) 43 } 44 45 func (s *testConfigSuite) TestMergeConfigItems(c *C) { 46 oriConf, _ := CloneConf(&defaultConf) 47 oldConf, _ := CloneConf(oriConf) 48 newConf, _ := CloneConf(oldConf) 49 50 // allowed 51 newConf.Performance.MaxProcs = 123 52 newConf.Performance.MaxMemory = 123 53 newConf.Performance.CrossJoin = false 54 newConf.Performance.FeedbackProbability = 123 55 newConf.Performance.QueryFeedbackLimit = 123 56 newConf.Performance.PseudoEstimateRatio = 123 57 newConf.OOMCausetAction = "panic" 58 newConf.MemQuotaQuery = 123 59 newConf.EinsteinDBClient.StoreLimit = 123 60 61 // rejected 62 newConf.CausetStore = "tiflash" 63 newConf.Port = 2333 64 newConf.AdvertiseAddress = "1.2.3.4" 65 newConf.Log.SlowThreshold = 2345 66 67 as, rs := MergeConfigItems(oldConf, newConf) 68 c.Assert(len(as), Equals, 10) 69 c.Assert(len(rs), Equals, 3) 70 for _, a := range as { 71 _, ok := dynamicConfigItems[a] 72 c.Assert(ok, IsTrue) 73 } 74 for _, a := range rs { 75 _, ok := dynamicConfigItems[a] 76 c.Assert(ok, IsFalse) 77 } 78 79 c.Assert(oldConf.Performance.MaxProcs, Equals, newConf.Performance.MaxProcs) 80 c.Assert(oldConf.Performance.MaxMemory, Equals, newConf.Performance.MaxMemory) 81 c.Assert(oldConf.Performance.CrossJoin, Equals, newConf.Performance.CrossJoin) 82 c.Assert(oldConf.Performance.FeedbackProbability, Equals, newConf.Performance.FeedbackProbability) 83 c.Assert(oldConf.Performance.QueryFeedbackLimit, Equals, newConf.Performance.QueryFeedbackLimit) 84 c.Assert(oldConf.Performance.PseudoEstimateRatio, Equals, newConf.Performance.PseudoEstimateRatio) 85 c.Assert(oldConf.OOMCausetAction, Equals, newConf.OOMCausetAction) 86 c.Assert(oldConf.MemQuotaQuery, Equals, newConf.MemQuotaQuery) 87 c.Assert(oldConf.EinsteinDBClient.StoreLimit, Equals, newConf.EinsteinDBClient.StoreLimit) 88 c.Assert(oldConf.Log.SlowThreshold, Equals, newConf.Log.SlowThreshold) 89 90 c.Assert(oldConf.CausetStore, Equals, oriConf.CausetStore) 91 c.Assert(oldConf.Port, Equals, oriConf.Port) 92 c.Assert(oldConf.AdvertiseAddress, Equals, oriConf.AdvertiseAddress) 93 } 94 95 func (s *testConfigSuite) TestAtomicWriteConfig(c *C) { 96 conf, _ := CloneConf(&defaultConf) 97 confPath := filepath.Join(os.TemFIDelir(), "test-write-config.toml") 98 conf.Performance.MaxMemory = 123 99 conf.Performance.MaxProcs = 234 100 conf.Performance.PseudoEstimateRatio = 3.45 101 c.Assert(atomicWriteConfig(conf, confPath), IsNil) 102 103 content, err := ioutil.ReadFile(confPath) 104 c.Assert(err, IsNil) 105 dconf, err := decodeConfig(string(content)) 106 c.Assert(err, IsNil) 107 c.Assert(dconf.Performance.MaxMemory, Equals, uint64(123)) 108 c.Assert(dconf.Performance.MaxProcs, Equals, uint(234)) 109 c.Assert(dconf.Performance.PseudoEstimateRatio, Equals, 3.45) 110 111 conf.Performance.MaxMemory = 321 112 conf.Performance.MaxProcs = 432 113 conf.Performance.PseudoEstimateRatio = 54.3 114 c.Assert(atomicWriteConfig(conf, confPath), IsNil) 115 116 content, err = ioutil.ReadFile(confPath) 117 c.Assert(err, IsNil) 118 dconf, err = decodeConfig(string(content)) 119 c.Assert(err, IsNil) 120 c.Assert(dconf.Performance.MaxMemory, Equals, uint64(321)) 121 c.Assert(dconf.Performance.MaxProcs, Equals, uint(432)) 122 c.Assert(dconf.Performance.PseudoEstimateRatio, Equals, 54.3) 123 } 124 125 func (s *testConfigSuite) TestFlattenConfig(c *C) { 126 toJSONStr := func(v interface{}) string { 127 str, err := json.Marshal(v) 128 c.Assert(err, IsNil) 129 return string(str) 130 } 131 132 jsonConf := `{ 133 "k0": 233333, 134 "k1": "v1", 135 "k2": ["v2-1", "v2-2", "v2-3"], 136 "k3": [{"k3-1":"v3-1"}, {"k3-2":"v3-2"}, {"k3-3":"v3-3"}], 137 "k4": { 138 "k4-1": [1, 2, 3, 4], 139 "k4-2": [5, 6, 7, 8], 140 "k4-3": [666] 141 }}` 142 nested := make(map[string]interface{}) 143 c.Assert(json.Unmarshal([]byte(jsonConf), &nested), IsNil) 144 flatMap := FlattenConfigItems(nested) 145 c.Assert(len(flatMap), Equals, 7) 146 c.Assert(toJSONStr(flatMap["k0"]), Equals, "233333") 147 c.Assert(flatMap["k1"], Equals, "v1") 148 c.Assert(toJSONStr(flatMap["k2"]), Equals, `["v2-1","v2-2","v2-3"]`) 149 c.Assert(toJSONStr(flatMap["k3"]), Equals, `[{"k3-1":"v3-1"},{"k3-2":"v3-2"},{"k3-3":"v3-3"}]`) 150 c.Assert(toJSONStr(flatMap["k4.k4-1"]), Equals, `[1,2,3,4]`) 151 c.Assert(toJSONStr(flatMap["k4.k4-2"]), Equals, `[5,6,7,8]`) 152 c.Assert(toJSONStr(flatMap["k4.k4-3"]), Equals, `[666]`) 153 154 tomlConf := ` 155 port=4000 156 [log] 157 level='info' 158 format='text' 159 [isolation-read] 160 engines = ["einsteindb", "tiflash", "milevadb"] 161 ` 162 nested = make(map[string]interface{}) 163 c.Assert(toml.Unmarshal([]byte(tomlConf), &nested), IsNil) 164 flatMap = FlattenConfigItems(nested) 165 c.Assert(len(flatMap), Equals, 4) 166 c.Assert(toJSONStr(flatMap["port"]), Equals, "4000") 167 c.Assert(toJSONStr(flatMap["log.level"]), Equals, `"info"`) 168 c.Assert(toJSONStr(flatMap["log.format"]), Equals, `"text"`) 169 c.Assert(toJSONStr(flatMap["isolation-read.engines"]), Equals, `["einsteindb","tiflash","milevadb"]`) 170 }