-
Notifications
You must be signed in to change notification settings - Fork 450
/
config.go
311 lines (277 loc) · 10.4 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package client
import (
"errors"
"fmt"
"time"
"github.com/m3db/m3/src/aggregator/sharding"
m3clusterclient "github.com/m3db/m3/src/cluster/client"
"github.com/m3db/m3/src/cluster/kv"
"github.com/m3db/m3/src/cluster/placement"
"github.com/m3db/m3/src/metrics/encoding/protobuf"
producerconfig "github.com/m3db/m3/src/msg/producer/config"
"github.com/m3db/m3/src/x/clock"
"github.com/m3db/m3/src/x/instrument"
xio "github.com/m3db/m3/src/x/io"
"github.com/m3db/m3/src/x/pool"
"github.com/m3db/m3/src/x/retry"
"github.com/uber-go/tally"
)
var (
errNoM3MsgOptions = errors.New("m3msg aggregator client: missing m3msg options")
)
// Configuration contains client configuration.
type Configuration struct {
Type AggregatorClientType `yaml:"type"`
M3Msg *M3MsgConfiguration `yaml:"m3msg"`
PlacementKV *kv.OverrideConfiguration `yaml:"placementKV"`
PlacementWatcher *placement.WatcherConfiguration `yaml:"placementWatcher"`
HashType *sharding.HashType `yaml:"hashType"`
ShardCutoverWarmupDuration *time.Duration `yaml:"shardCutoverWarmupDuration"`
ShardCutoffLingerDuration *time.Duration `yaml:"shardCutoffLingerDuration"`
Encoder EncoderConfiguration `yaml:"encoder"`
FlushSize int `yaml:"flushSize"`
MaxBatchSize int `yaml:"maxBatchSize"`
MaxTimerBatchSize int `yaml:"maxTimerBatchSize"`
BatchFlushDeadline time.Duration `yaml:"batchFlushDeadline"`
QueueSize int `yaml:"queueSize"`
QueueDropType *DropType `yaml:"queueDropType"`
Connection ConnectionConfiguration `yaml:"connection"`
}
// NewAdminClient creates a new admin client.
func (c *Configuration) NewAdminClient(
kvClient m3clusterclient.Client,
clockOpts clock.Options,
instrumentOpts instrument.Options,
rwOpts xio.Options,
) (AdminClient, error) {
client, err := c.NewClient(kvClient, clockOpts, instrumentOpts, rwOpts)
if err != nil {
return nil, err
}
return client.(AdminClient), nil
}
// NewClient creates a new client.
func (c *Configuration) NewClient(
kvClient m3clusterclient.Client,
clockOpts clock.Options,
instrumentOpts instrument.Options,
rwOpts xio.Options,
) (Client, error) {
opts, err := c.newClientOptions(kvClient, clockOpts, instrumentOpts, rwOpts)
if err != nil {
return nil, err
}
return NewClient(opts)
}
var (
errLegacyClientNoPlacementKVConfig = errors.New("no placement KV config set")
errLegacyClientNoPlacementWatcherConfig = errors.New("no placement watcher config set")
)
func (c *Configuration) newClientOptions(
kvClient m3clusterclient.Client,
clockOpts clock.Options,
instrumentOpts instrument.Options,
rwOpts xio.Options,
) (Options, error) {
opts := NewOptions().
SetAggregatorClientType(c.Type).
SetClockOptions(clockOpts).
SetInstrumentOptions(instrumentOpts).
SetRWOptions(rwOpts)
switch c.Type {
case M3MsgAggregatorClient:
m3msgCfg := c.M3Msg
if m3msgCfg == nil {
return nil, errNoM3MsgOptions
}
m3msgOpts, err := m3msgCfg.NewM3MsgOptions(kvClient, instrumentOpts, rwOpts)
if err != nil {
return nil, err
}
// Allow M3Msg options to override the timer options for instrument options.
opts = opts.SetInstrumentOptions(
opts.InstrumentOptions().SetTimerOptions(m3msgOpts.TimerOptions()))
// Set the M3Msg options configured.
opts = opts.SetM3MsgOptions(m3msgOpts)
case LegacyAggregatorClient:
placementKV := c.PlacementKV
if placementKV == nil {
return nil, errLegacyClientNoPlacementKVConfig
}
placementWatcher := c.PlacementWatcher
if placementWatcher == nil {
return nil, errLegacyClientNoPlacementWatcherConfig
}
scope := instrumentOpts.MetricsScope()
connectionOpts := c.Connection.NewConnectionOptions(scope.SubScope("connection"))
kvOpts, err := placementKV.NewOverrideOptions()
if err != nil {
return nil, err
}
placementStore, err := kvClient.Store(kvOpts)
if err != nil {
return nil, err
}
iOpts := instrumentOpts.SetMetricsScope(scope.SubScope("encoder"))
encoderOpts := c.Encoder.NewEncoderOptions(iOpts)
iOpts = instrumentOpts.SetMetricsScope(scope.SubScope("placement-watcher"))
watcherOpts := placementWatcher.NewOptions(placementStore, iOpts)
// Get the shard fn.
hashType := sharding.DefaultHash
if c.HashType != nil {
hashType = *c.HashType
}
shardFn, err := hashType.ShardFn()
if err != nil {
return nil, err
}
opts = opts.SetStagedPlacementWatcherOptions(watcherOpts).
SetShardFn(shardFn).
SetEncoderOptions(encoderOpts).
SetConnectionOptions(connectionOpts)
if c.ShardCutoverWarmupDuration != nil {
opts = opts.SetShardCutoverWarmupDuration(*c.ShardCutoverWarmupDuration)
}
if c.ShardCutoffLingerDuration != nil {
opts = opts.SetShardCutoffLingerDuration(*c.ShardCutoffLingerDuration)
}
if c.FlushSize != 0 {
opts = opts.SetFlushSize(c.FlushSize)
}
if c.MaxBatchSize != 0 {
opts = opts.SetMaxBatchSize(c.MaxBatchSize)
}
if c.MaxTimerBatchSize != 0 {
opts = opts.SetMaxTimerBatchSize(c.MaxTimerBatchSize)
}
if c.BatchFlushDeadline != 0 {
opts = opts.SetBatchFlushDeadline(c.BatchFlushDeadline)
}
if c.QueueSize != 0 {
opts = opts.SetInstanceQueueSize(c.QueueSize)
}
if c.QueueDropType != nil {
opts = opts.SetQueueDropType(*c.QueueDropType)
}
default:
return nil, fmt.Errorf("unknown client type: %v", c.Type)
}
// Validate the options.
if err := opts.Validate(); err != nil {
return nil, err
}
return opts, nil
}
// ConnectionConfiguration contains the connection configuration.
type ConnectionConfiguration struct {
ConnectionTimeout time.Duration `yaml:"connectionTimeout"`
ConnectionKeepAlive *bool `yaml:"connectionKeepAlive"`
WriteTimeout time.Duration `yaml:"writeTimeout"`
InitReconnectThreshold int `yaml:"initReconnectThreshold"`
MaxReconnectThreshold int `yaml:"maxReconnectThreshold"`
ReconnectThresholdMultiplier int `yaml:"reconnectThresholdMultiplier"`
MaxReconnectDuration *time.Duration `yaml:"maxReconnectDuration"`
WriteRetries *retry.Configuration `yaml:"writeRetries"`
}
// NewConnectionOptions creates new connection options.
func (c *ConnectionConfiguration) NewConnectionOptions(scope tally.Scope) ConnectionOptions {
opts := NewConnectionOptions()
if c.ConnectionTimeout != 0 {
opts = opts.SetConnectionTimeout(c.ConnectionTimeout)
}
if c.ConnectionKeepAlive != nil {
opts = opts.SetConnectionKeepAlive(*c.ConnectionKeepAlive)
}
if c.WriteTimeout != 0 {
opts = opts.SetWriteTimeout(c.WriteTimeout)
}
if c.InitReconnectThreshold != 0 {
opts = opts.SetInitReconnectThreshold(c.InitReconnectThreshold)
}
if c.MaxReconnectThreshold != 0 {
opts = opts.SetMaxReconnectThreshold(c.MaxReconnectThreshold)
}
if c.ReconnectThresholdMultiplier != 0 {
opts = opts.SetReconnectThresholdMultiplier(c.ReconnectThresholdMultiplier)
}
if c.MaxReconnectDuration != nil {
opts = opts.SetMaxReconnectDuration(*c.MaxReconnectDuration)
}
if c.WriteRetries != nil {
retryOpts := c.WriteRetries.NewOptions(scope)
opts = opts.SetWriteRetryOptions(retryOpts)
}
return opts
}
// EncoderConfiguration configures the encoder.
type EncoderConfiguration struct {
InitBufferSize *int `yaml:"initBufferSize"`
MaxMessageSize *int `yaml:"maxMessageSize"`
BytesPool *pool.BucketizedPoolConfiguration `yaml:"bytesPool"`
}
// NewEncoderOptions create a new set of encoder options.
func (c *EncoderConfiguration) NewEncoderOptions(
instrumentOpts instrument.Options,
) protobuf.UnaggregatedOptions {
opts := protobuf.NewUnaggregatedOptions()
if c.InitBufferSize != nil {
opts = opts.SetInitBufferSize(*c.InitBufferSize)
}
if c.MaxMessageSize != nil {
opts = opts.SetMaxMessageSize(*c.MaxMessageSize)
}
if c.BytesPool != nil {
sizeBuckets := c.BytesPool.NewBuckets()
objectPoolOpts := c.BytesPool.NewObjectPoolOptions(instrumentOpts)
bytesPool := pool.NewBytesPool(sizeBuckets, objectPoolOpts)
opts = opts.SetBytesPool(bytesPool)
bytesPool.Init()
}
return opts
}
// M3MsgConfiguration contains the M3Msg client configuration, required
// if using M3Msg client type.
type M3MsgConfiguration struct {
Producer producerconfig.ProducerConfiguration `yaml:"producer"`
}
// NewM3MsgOptions returns new M3Msg options from configuration.
func (c *M3MsgConfiguration) NewM3MsgOptions(
kvClient m3clusterclient.Client,
instrumentOpts instrument.Options,
rwOpts xio.Options,
) (M3MsgOptions, error) {
opts := NewM3MsgOptions()
// For M3Msg clients we want to use the default timer options
// as defined by the default M3Msg options for low overhead
// timers.
instrumentOpts = instrumentOpts.SetTimerOptions(opts.TimerOptions())
producer, err := c.Producer.NewProducer(kvClient, instrumentOpts, rwOpts)
if err != nil {
return nil, err
}
opts = opts.SetProducer(producer)
// Validate the options.
if err := opts.Validate(); err != nil {
return nil, err
}
return opts, nil
}