github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/namespace/config_test.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package namespace 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/m3db/m3/src/dbnode/retention" 28 "github.com/m3db/m3/src/x/ident" 29 30 "github.com/stretchr/testify/require" 31 yaml "gopkg.in/yaml.v2" 32 ) 33 34 func TestRegistryConfig(t *testing.T) { 35 var ( 36 bootstrapEnabled = false 37 config = &MapConfiguration{ 38 Metadatas: []MetadataConfiguration{ 39 MetadataConfiguration{ 40 ID: "abc", 41 Retention: retention.Configuration{ 42 BlockSize: time.Hour, 43 RetentionPeriod: time.Hour, 44 BufferFuture: time.Minute, 45 BufferPast: time.Minute, 46 }, 47 }, 48 MetadataConfiguration{ 49 ID: "cde", 50 BootstrapEnabled: &bootstrapEnabled, 51 Retention: retention.Configuration{ 52 BlockSize: time.Hour, 53 RetentionPeriod: time.Hour, 54 BufferFuture: time.Minute, 55 BufferPast: time.Minute, 56 }, 57 Index: IndexConfiguration{ 58 Enabled: true, 59 BlockSize: time.Hour, 60 }, 61 }, 62 }, 63 } 64 ) 65 66 nsMap, err := config.Map() 67 require.NoError(t, err) 68 md, err := nsMap.Get(ident.StringID("abc")) 69 require.NoError(t, err) 70 mdd, err := config.Metadatas[0].Metadata() 71 require.NoError(t, err) 72 require.Equal(t, mdd.ID().String(), md.ID().String()) 73 require.Equal(t, mdd.Options(), md.Options()) 74 75 md, err = nsMap.Get(ident.StringID("cde")) 76 require.NoError(t, err) 77 mdd, err = config.Metadatas[1].Metadata() 78 require.NoError(t, err) 79 require.Equal(t, mdd.ID().String(), md.ID().String()) 80 require.Equal(t, mdd.Options(), md.Options()) 81 82 _, err = nsMap.Get(ident.StringID("otherstring")) 83 require.Error(t, err) 84 } 85 86 func TestMetadataConfig(t *testing.T) { 87 var ( 88 id = "someLongString" 89 bootstrapEnabled = true 90 flushEnabled = false 91 writesToCommitLog = true 92 cleanupEnabled = false 93 repairEnabled = false 94 retention = retention.Configuration{ 95 BlockSize: time.Hour, 96 RetentionPeriod: time.Hour, 97 BufferFuture: time.Minute, 98 BufferPast: time.Minute, 99 } 100 index = IndexConfiguration{ 101 Enabled: true, 102 BlockSize: time.Hour, 103 } 104 config = &MetadataConfiguration{ 105 ID: id, 106 BootstrapEnabled: &bootstrapEnabled, 107 FlushEnabled: &flushEnabled, 108 WritesToCommitLog: &writesToCommitLog, 109 CleanupEnabled: &cleanupEnabled, 110 RepairEnabled: &repairEnabled, 111 Retention: retention, 112 Index: index, 113 } 114 ) 115 116 metadata, err := config.Metadata() 117 require.NoError(t, err) 118 require.Equal(t, id, metadata.ID().String()) 119 120 opts := metadata.Options() 121 require.Equal(t, bootstrapEnabled, opts.BootstrapEnabled()) 122 require.Equal(t, flushEnabled, opts.FlushEnabled()) 123 require.Equal(t, writesToCommitLog, opts.WritesToCommitLog()) 124 require.Equal(t, cleanupEnabled, opts.CleanupEnabled()) 125 require.Equal(t, repairEnabled, opts.RepairEnabled()) 126 require.Equal(t, retention.Options(), opts.RetentionOptions()) 127 require.Equal(t, index.Options(), opts.IndexOptions()) 128 } 129 130 func TestRegistryConfigFromBytes(t *testing.T) { 131 yamlBytes := []byte(` 132 metadatas: 133 - id: "testmetrics" 134 bootstrapEnabled: false 135 flushEnabled: false 136 writesToCommitLog: false 137 cleanupEnabled: false 138 repairEnabled: false 139 retention: 140 retentionPeriod: 8h 141 blockSize: 2h 142 bufferFuture: 10m 143 bufferPast: 10m 144 - id: "metrics-10s:2d" 145 bootstrapEnabled: true 146 flushEnabled: true 147 writesToCommitLog: true 148 cleanupEnabled: true 149 repairEnabled: true 150 retention: 151 retentionPeriod: 48h 152 blockSize: 2h 153 bufferFuture: 10m 154 bufferPast: 10m 155 - id: "metrics-1m:40d" 156 bootstrapEnabled: true 157 flushEnabled: true 158 writesToCommitLog: true 159 cleanupEnabled: true 160 repairEnabled: true 161 retention: 162 retentionPeriod: 960h 163 blockSize: 12h 164 bufferFuture: 10m 165 bufferPast: 10m 166 index: 167 enabled: true 168 blockSize: 24h 169 `) 170 171 var conf MapConfiguration 172 require.NoError(t, yaml.Unmarshal(yamlBytes, &conf)) 173 174 nsMap, err := conf.Map() 175 require.NoError(t, err) 176 mds := nsMap.Metadatas() 177 require.Equal(t, 3, len(mds)) 178 179 testmetrics := ident.StringID("testmetrics") 180 ns, err := nsMap.Get(testmetrics) 181 require.NoError(t, err) 182 require.True(t, ns.ID().Equal(testmetrics)) 183 opts := ns.Options() 184 require.Equal(t, false, opts.BootstrapEnabled()) 185 require.Equal(t, false, opts.FlushEnabled()) 186 require.Equal(t, false, opts.WritesToCommitLog()) 187 require.Equal(t, false, opts.CleanupEnabled()) 188 require.Equal(t, false, opts.RepairEnabled()) 189 require.Equal(t, false, opts.IndexOptions().Enabled()) 190 testRetentionOpts := retention.NewOptions(). 191 SetRetentionPeriod(8 * time.Hour). 192 SetBlockSize(2 * time.Hour). 193 SetBufferFuture(10 * time.Minute). 194 SetBufferPast(10 * time.Minute) 195 require.True(t, testRetentionOpts.Equal(opts.RetentionOptions())) 196 197 metrics2d := ident.StringID("metrics-10s:2d") 198 ns, err = nsMap.Get(metrics2d) 199 require.NoError(t, err) 200 require.True(t, ns.ID().Equal(metrics2d)) 201 opts = ns.Options() 202 require.Equal(t, true, opts.BootstrapEnabled()) 203 require.Equal(t, true, opts.FlushEnabled()) 204 require.Equal(t, true, opts.WritesToCommitLog()) 205 require.Equal(t, true, opts.CleanupEnabled()) 206 require.Equal(t, true, opts.RepairEnabled()) 207 require.Equal(t, false, opts.IndexOptions().Enabled()) 208 testRetentionOpts = retention.NewOptions(). 209 SetRetentionPeriod(48 * time.Hour). 210 SetBlockSize(2 * time.Hour). 211 SetBufferFuture(10 * time.Minute). 212 SetBufferPast(10 * time.Minute) 213 require.True(t, testRetentionOpts.Equal(opts.RetentionOptions())) 214 215 metrics40d := ident.StringID("metrics-1m:40d") 216 ns, err = nsMap.Get(metrics40d) 217 require.NoError(t, err) 218 require.True(t, ns.ID().Equal(metrics40d)) 219 opts = ns.Options() 220 require.Equal(t, true, opts.BootstrapEnabled()) 221 require.Equal(t, true, opts.FlushEnabled()) 222 require.Equal(t, true, opts.WritesToCommitLog()) 223 require.Equal(t, true, opts.CleanupEnabled()) 224 require.Equal(t, true, opts.RepairEnabled()) 225 require.Equal(t, true, opts.IndexOptions().Enabled()) 226 require.Equal(t, 24*time.Hour, opts.IndexOptions().BlockSize()) 227 testRetentionOpts = retention.NewOptions(). 228 SetRetentionPeriod(960 * time.Hour). 229 SetBlockSize(12 * time.Hour). 230 SetBufferFuture(10 * time.Minute). 231 SetBufferPast(10 * time.Minute) 232 require.True(t, testRetentionOpts.Equal(opts.RetentionOptions())) 233 234 }