-
Notifications
You must be signed in to change notification settings - Fork 453
/
client.go
367 lines (331 loc) · 11.3 KB
/
client.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// 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"
"math"
"sync"
"time"
"github.com/m3db/m3/src/aggregator/sharding"
"github.com/m3db/m3/src/cluster/placement"
"github.com/m3db/m3/src/cluster/shard"
"github.com/m3db/m3/src/metrics/metadata"
"github.com/m3db/m3/src/metrics/metric/aggregated"
"github.com/m3db/m3/src/metrics/metric/id"
"github.com/m3db/m3/src/metrics/metric/unaggregated"
"github.com/m3db/m3/src/x/clock"
xerrors "github.com/m3db/m3/src/x/errors"
"github.com/m3db/m3/src/x/instrument"
"github.com/uber-go/tally"
)
var (
errClientIsInitializedOrClosed = errors.New("client is already initialized or closed")
errClientIsUninitializedOrClosed = errors.New("client is uninitialized or closed")
)
// Client is a client capable of writing different types of metrics to the aggregation clients.
type Client interface {
// Init initializes the client.
Init() error
// WriteUntimedCounter writes untimed counter metrics.
WriteUntimedCounter(
counter unaggregated.Counter,
metadatas metadata.StagedMetadatas,
) error
// WriteUntimedBatchTimer writes untimed batch timer metrics.
WriteUntimedBatchTimer(
batchTimer unaggregated.BatchTimer,
metadatas metadata.StagedMetadatas,
) error
// WriteUntimedGauge writes untimed gauge metrics.
WriteUntimedGauge(
gauge unaggregated.Gauge,
metadatas metadata.StagedMetadatas,
) error
// WriteTimed writes timed metrics.
WriteTimed(
metric aggregated.Metric,
metadata metadata.TimedMetadata,
) error
// Flush flushes any remaining data buffered by the client.
Flush() error
// Close closes the client.
Close() error
}
// AdminClient is an administrative client capable of performing regular client operations
// as well as high-privilege operations such as internal communcations among aggregation
// servers that regular client is not permissioned to do.
type AdminClient interface {
Client
// WriteForwarded writes forwarded metrics.
WriteForwarded(
metric aggregated.ForwardedMetric,
metadata metadata.ForwardMetadata,
) error
}
type clientState int
const (
clientUninitialized clientState = iota
clientInitialized
clientClosed
)
type clientMetrics struct {
writeUntimedCounter instrument.MethodMetrics
writeUntimedBatchTimer instrument.MethodMetrics
writeUntimedGauge instrument.MethodMetrics
writeForwarded instrument.MethodMetrics
flush instrument.MethodMetrics
shardNotOwned tally.Counter
shardNotWriteable tally.Counter
}
func newClientMetrics(scope tally.Scope, sampleRate float64) clientMetrics {
return clientMetrics{
writeUntimedCounter: instrument.NewMethodMetrics(scope, "writeUntimedCounter", sampleRate),
writeUntimedBatchTimer: instrument.NewMethodMetrics(scope, "writeUntimedBatchTimer", sampleRate),
writeUntimedGauge: instrument.NewMethodMetrics(scope, "writeUntimedGauge", sampleRate),
writeForwarded: instrument.NewMethodMetrics(scope, "writeForwarded", sampleRate),
flush: instrument.NewMethodMetrics(scope, "flush", sampleRate),
shardNotOwned: scope.Counter("shard-not-owned"),
shardNotWriteable: scope.Counter("shard-not-writeable"),
}
}
// client partitions metrics and send them via different routes based on their partitions.
type client struct {
sync.RWMutex
opts Options
nowFn clock.NowFn
shardCutoverWarmupDuration time.Duration
shardCutoffLingerDuration time.Duration
writerMgr instanceWriterManager
shardFn sharding.ShardFn
placementWatcher placement.StagedPlacementWatcher
state clientState
metrics clientMetrics
}
// NewClient creates a new client.
func NewClient(opts Options) Client {
var (
instrumentOpts = opts.InstrumentOptions()
writerMgrScope = instrumentOpts.MetricsScope().SubScope("writer-manager")
writerMgrOpts = opts.SetInstrumentOptions(instrumentOpts.SetMetricsScope(writerMgrScope))
writerMgr = newInstanceWriterManager(writerMgrOpts)
)
onPlacementsAddedFn := func(placements []placement.Placement) {
for _, placement := range placements {
writerMgr.AddInstances(placement.Instances()) // nolint: errcheck
}
}
onPlacementsRemovedFn := func(placements []placement.Placement) {
for _, placement := range placements {
writerMgr.RemoveInstances(placement.Instances()) // nolint: errcheck
}
}
activeStagedPlacementOpts := placement.NewActiveStagedPlacementOptions().
SetClockOptions(opts.ClockOptions()).
SetOnPlacementsAddedFn(onPlacementsAddedFn).
SetOnPlacementsRemovedFn(onPlacementsRemovedFn)
placementWatcherOpts := opts.StagedPlacementWatcherOptions().SetActiveStagedPlacementOptions(activeStagedPlacementOpts)
placementWatcher := placement.NewStagedPlacementWatcher(placementWatcherOpts)
return &client{
opts: opts,
nowFn: opts.ClockOptions().NowFn(),
shardCutoverWarmupDuration: opts.ShardCutoverWarmupDuration(),
shardCutoffLingerDuration: opts.ShardCutoffLingerDuration(),
writerMgr: writerMgr,
shardFn: opts.ShardFn(),
placementWatcher: placementWatcher,
metrics: newClientMetrics(instrumentOpts.MetricsScope(), instrumentOpts.MetricsSamplingRate()),
}
}
func (c *client) Init() error {
c.Lock()
defer c.Unlock()
if c.state != clientUninitialized {
return errClientIsInitializedOrClosed
}
c.state = clientInitialized
return c.placementWatcher.Watch()
}
func (c *client) WriteUntimedCounter(
counter unaggregated.Counter,
metadatas metadata.StagedMetadatas,
) error {
callStart := c.nowFn()
payload := payloadUnion{
payloadType: untimedType,
untimed: untimedPayload{
metric: counter.ToUnion(),
metadatas: metadatas,
},
}
err := c.write(counter.ID, c.nowNanos(), payload)
c.metrics.writeUntimedCounter.ReportSuccessOrError(err, c.nowFn().Sub(callStart))
return err
}
func (c *client) WriteUntimedBatchTimer(
batchTimer unaggregated.BatchTimer,
metadatas metadata.StagedMetadatas,
) error {
callStart := c.nowFn()
payload := payloadUnion{
payloadType: untimedType,
untimed: untimedPayload{
metric: batchTimer.ToUnion(),
metadatas: metadatas,
},
}
err := c.write(batchTimer.ID, c.nowNanos(), payload)
c.metrics.writeUntimedBatchTimer.ReportSuccessOrError(err, c.nowFn().Sub(callStart))
return err
}
func (c *client) WriteUntimedGauge(
gauge unaggregated.Gauge,
metadatas metadata.StagedMetadatas,
) error {
callStart := c.nowFn()
payload := payloadUnion{
payloadType: untimedType,
untimed: untimedPayload{
metric: gauge.ToUnion(),
metadatas: metadatas,
},
}
err := c.write(gauge.ID, c.nowNanos(), payload)
c.metrics.writeUntimedGauge.ReportSuccessOrError(err, c.nowFn().Sub(callStart))
return err
}
func (c *client) WriteTimed(
metric aggregated.Metric,
metadata metadata.TimedMetadata,
) error {
callStart := c.nowFn()
payload := payloadUnion{
payloadType: timedType,
timed: timedPayload{
metric: metric,
metadata: metadata,
},
}
err := c.write(metric.ID, metric.TimeNanos, payload)
c.metrics.writeForwarded.ReportSuccessOrError(err, c.nowFn().Sub(callStart))
return err
}
func (c *client) WriteForwarded(
metric aggregated.ForwardedMetric,
metadata metadata.ForwardMetadata,
) error {
callStart := c.nowFn()
payload := payloadUnion{
payloadType: forwardedType,
forwarded: forwardedPayload{
metric: metric,
metadata: metadata,
},
}
err := c.write(metric.ID, metric.TimeNanos, payload)
c.metrics.writeForwarded.ReportSuccessOrError(err, c.nowFn().Sub(callStart))
return err
}
func (c *client) Flush() error {
callStart := c.nowFn()
c.RLock()
if c.state != clientInitialized {
c.RUnlock()
return errClientIsUninitializedOrClosed
}
err := c.writerMgr.Flush()
c.RUnlock()
c.metrics.flush.ReportSuccessOrError(err, c.nowFn().Sub(callStart))
return err
}
func (c *client) Close() error {
c.Lock()
defer c.Unlock()
if c.state != clientInitialized {
return errClientIsUninitializedOrClosed
}
c.state = clientClosed
c.placementWatcher.Unwatch() // nolint: errcheck
return c.writerMgr.Close()
}
func (c *client) write(metricID id.RawID, timeNanos int64, payload payloadUnion) error {
c.RLock()
if c.state != clientInitialized {
c.RUnlock()
return errClientIsUninitializedOrClosed
}
stagedPlacement, onStagedPlacementDoneFn, err := c.placementWatcher.ActiveStagedPlacement()
if err != nil {
c.RUnlock()
return err
}
placement, onPlacementDoneFn, err := stagedPlacement.ActivePlacement()
if err != nil {
onStagedPlacementDoneFn()
c.RUnlock()
return err
}
var (
shardID = c.shardFn(metricID, uint32(placement.NumShards()))
instances = placement.InstancesForShard(shardID)
multiErr = xerrors.NewMultiError()
)
for _, instance := range instances {
// NB(xichen): the shard should technically always be found because the instances
// are computed from the placement, but protect against errors here regardless.
shard, ok := instance.Shards().Shard(shardID)
if !ok {
err = fmt.Errorf("instance %s does not own shard %d", instance.ID(), shardID)
multiErr = multiErr.Add(err)
c.metrics.shardNotOwned.Inc(1)
continue
}
if !c.shouldWriteForShard(timeNanos, shard) {
c.metrics.shardNotWriteable.Inc(1)
continue
}
if err = c.writerMgr.Write(instance, shardID, payload); err != nil {
multiErr = multiErr.Add(err)
}
}
onPlacementDoneFn()
onStagedPlacementDoneFn()
c.RUnlock()
return multiErr.FinalError()
}
func (c *client) shouldWriteForShard(nowNanos int64, shard shard.Shard) bool {
writeEarliestNanos, writeLatestNanos := c.writeTimeRangeFor(shard)
return nowNanos >= writeEarliestNanos && nowNanos <= writeLatestNanos
}
// writeTimeRangeFor returns the time range for writes going to a given shard.
func (c *client) writeTimeRangeFor(shard shard.Shard) (int64, int64) {
var (
earliestNanos = int64(0)
latestNanos = int64(math.MaxInt64)
)
if cutoverNanos := shard.CutoverNanos(); cutoverNanos >= int64(c.shardCutoverWarmupDuration) {
earliestNanos = cutoverNanos - int64(c.shardCutoverWarmupDuration)
}
if cutoffNanos := shard.CutoffNanos(); cutoffNanos <= math.MaxInt64-int64(c.shardCutoffLingerDuration) {
latestNanos = cutoffNanos + int64(c.shardCutoffLingerDuration)
}
return earliestNanos, latestNanos
}
func (c *client) nowNanos() int64 { return c.nowFn().UnixNano() }