github.com/m3db/m3@v1.5.0/src/aggregator/client/config_test.go (about) 1 // Copyright (c) 2018 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 client 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/m3db/m3/src/aggregator/sharding" 28 m3clusterclient "github.com/m3db/m3/src/cluster/client" 29 "github.com/m3db/m3/src/cluster/kv" 30 "github.com/m3db/m3/src/cluster/kv/mem" 31 "github.com/m3db/m3/src/x/clock" 32 "github.com/m3db/m3/src/x/instrument" 33 xio "github.com/m3db/m3/src/x/io" 34 "github.com/m3db/m3/src/x/pool" 35 36 "github.com/golang/mock/gomock" 37 "github.com/stretchr/testify/require" 38 yaml "gopkg.in/yaml.v2" 39 ) 40 41 var testClientConfig = ` 42 placementKV: 43 zone: testZone 44 environment: testEnvironment 45 namespace: testNamespace 46 placementWatcher: 47 key: testWatchKey 48 initWatchTimeout: 15s 49 hashType: murmur32 50 shardCutoverWarmupDuration: 10m 51 shardCutoffLingerDuration: 1m 52 encoder: 53 initBufferSize: 100 54 maxMessageSize: 50000000 55 bytesPool: 56 buckets: 57 - capacity: 16 58 count: 10 59 - capacity: 32 60 count: 20 61 watermark: 62 low: 0.001 63 high: 0.01 64 flushWorkerCount: 10 65 forceFlushEvery: 123s 66 maxBatchSize: 42 67 maxTimerBatchSize: 140 68 queueSize: 1000 69 queueDropType: oldest 70 connection: 71 connectionTimeout: 1s 72 connectionKeepAlive: true 73 writeTimeout: 1s 74 initReconnectThreshold: 2 75 maxReconnectThreshold: 5000 76 reconnectThresholdMultiplier: 2 77 maxReconnectDuration: 1m 78 writeRetries: 79 initialBackoff: 100ms 80 maxBackoff: 1s 81 maxRetries: 2 82 jitter: true 83 ` 84 85 func TestConfigUnmarshal(t *testing.T) { 86 var cfg Configuration 87 require.NoError(t, yaml.Unmarshal([]byte(testClientConfig), &cfg)) 88 89 require.Equal(t, "testZone", cfg.PlacementKV.Zone) 90 require.Equal(t, "testEnvironment", cfg.PlacementKV.Environment) 91 require.Equal(t, "testNamespace", cfg.PlacementKV.Namespace) 92 require.Equal(t, "testWatchKey", cfg.Watcher.Key) 93 require.Equal(t, 15*time.Second, cfg.Watcher.InitWatchTimeout) 94 require.Equal(t, sharding.Murmur32Hash, *cfg.HashType) 95 require.Equal(t, 10*time.Minute, *cfg.ShardCutoverWarmupDuration) 96 require.Equal(t, time.Minute, *cfg.ShardCutoffLingerDuration) 97 require.Equal(t, 100, *cfg.Encoder.InitBufferSize) 98 require.Equal(t, 50000000, *cfg.Encoder.MaxMessageSize) 99 require.Equal(t, []pool.BucketConfiguration{ 100 {Count: 10, Capacity: 16}, 101 {Count: 20, Capacity: 32}, 102 }, cfg.Encoder.BytesPool.Buckets) 103 require.Equal(t, 0.001, cfg.Encoder.BytesPool.Watermark.RefillLowWatermark) 104 require.Equal(t, 0.01, cfg.Encoder.BytesPool.Watermark.RefillHighWatermark) 105 require.Equal(t, 10, cfg.FlushWorkerCount) 106 require.Equal(t, 123*time.Second, cfg.ForceFlushEvery) 107 require.Equal(t, 140, cfg.MaxTimerBatchSize) 108 require.Equal(t, 42, cfg.MaxBatchSize) 109 require.Equal(t, 1000, cfg.QueueSize) 110 require.Equal(t, DropOldest, *cfg.QueueDropType) 111 require.Equal(t, time.Second, cfg.Connection.ConnectionTimeout) 112 require.Equal(t, true, *cfg.Connection.ConnectionKeepAlive) 113 require.Equal(t, time.Second, cfg.Connection.WriteTimeout) 114 require.Equal(t, 2, cfg.Connection.InitReconnectThreshold) 115 require.Equal(t, 5000, cfg.Connection.MaxReconnectThreshold) 116 require.Equal(t, 2, cfg.Connection.ReconnectThresholdMultiplier) 117 require.Equal(t, time.Minute, *cfg.Connection.MaxReconnectDuration) 118 require.NotNil(t, cfg.Connection.WriteRetries) 119 require.Equal(t, 100*time.Millisecond, cfg.Connection.WriteRetries.InitialBackoff) 120 require.Equal(t, time.Second, cfg.Connection.WriteRetries.MaxBackoff) 121 require.Equal(t, 2, cfg.Connection.WriteRetries.MaxRetries) 122 require.Equal(t, true, *cfg.Connection.WriteRetries.Jitter) 123 require.Nil(t, cfg.Connection.WriteRetries.Forever) 124 } 125 126 func TestNewClientOptions(t *testing.T) { 127 ctrl := gomock.NewController(t) 128 defer ctrl.Finish() 129 130 var cfg Configuration 131 require.NoError(t, yaml.Unmarshal([]byte(testClientConfig), &cfg)) 132 133 expectedKvOpts := kv.NewOverrideOptions(). 134 SetZone("testZone"). 135 SetEnvironment("testEnvironment"). 136 SetNamespace("testNamespace") 137 store := mem.NewStore() 138 kvClient := m3clusterclient.NewMockClient(ctrl) 139 kvClient.EXPECT().Store(expectedKvOpts).Return(store, nil) 140 clockOpts := clock.NewOptions() 141 instrumentOpts := instrument.NewOptions() 142 rwOpts := xio.NewOptions() 143 opts, err := cfg.newClientOptions(kvClient, clockOpts, instrumentOpts, rwOpts) 144 require.NoError(t, err) 145 146 // Verify the constructed options match expectations. 147 require.True(t, instrumentOpts == opts.InstrumentOptions()) 148 require.Equal(t, 100, opts.EncoderOptions().InitBufferSize()) 149 require.Equal(t, 50000000, opts.EncoderOptions().MaxMessageSize()) 150 require.NotNil(t, opts.EncoderOptions().BytesPool()) 151 require.NotNil(t, opts.ShardFn()) 152 require.Equal(t, "testWatchKey", opts.WatcherOptions().StagedPlacementKey()) 153 require.True(t, store == opts.WatcherOptions().StagedPlacementStore()) 154 require.Equal(t, 10*time.Minute, opts.ShardCutoverWarmupDuration()) 155 require.Equal(t, time.Minute, opts.ShardCutoffLingerDuration()) 156 require.Equal(t, 10, opts.FlushWorkerCount()) 157 require.Equal(t, 123*time.Second, opts.ForceFlushEvery()) 158 require.Equal(t, 140, opts.MaxTimerBatchSize()) 159 require.Equal(t, 42, opts.MaxBatchSize()) 160 require.Equal(t, DropOldest, opts.QueueDropType()) 161 require.Equal(t, time.Second, opts.ConnectionOptions().ConnectionTimeout()) 162 require.Equal(t, true, opts.ConnectionOptions().ConnectionKeepAlive()) 163 require.Equal(t, time.Second, opts.ConnectionOptions().WriteTimeout()) 164 require.Equal(t, 2, opts.ConnectionOptions().InitReconnectThreshold()) 165 require.Equal(t, 5000, opts.ConnectionOptions().MaxReconnectThreshold()) 166 require.Equal(t, 2, opts.ConnectionOptions().ReconnectThresholdMultiplier()) 167 require.Equal(t, time.Minute, opts.ConnectionOptions().MaxReconnectDuration()) 168 require.Equal(t, 100*time.Millisecond, opts.ConnectionOptions().WriteRetryOptions().InitialBackoff()) 169 require.Equal(t, 2.0, opts.ConnectionOptions().WriteRetryOptions().BackoffFactor()) 170 require.Equal(t, time.Second, opts.ConnectionOptions().WriteRetryOptions().MaxBackoff()) 171 require.Equal(t, 2, opts.ConnectionOptions().WriteRetryOptions().MaxRetries()) 172 require.Equal(t, true, opts.ConnectionOptions().WriteRetryOptions().Jitter()) 173 require.Equal(t, false, opts.ConnectionOptions().WriteRetryOptions().Forever()) 174 }