-
Notifications
You must be signed in to change notification settings - Fork 451
/
types.go
408 lines (301 loc) · 12.7 KB
/
types.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright (c) 2016 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 msgpack
import (
"bytes"
"io"
"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/metrics/policy"
"github.com/m3db/m3/src/x/pool"
)
// Buffer is a byte buffer.
type Buffer interface {
// Buffer returns the bytes buffer.
Buffer() *bytes.Buffer
// Bytes returns the buffered bytes.
Bytes() []byte
// Reset resets the buffer.
Reset()
// Close closes the buffer.
Close()
}
// Encoder is an encoder.
type Encoder interface {
// EncodeInt64 encodes an int64 value.
EncodeInt64(value int64) error
// EncodeBool encodes a boolean value.
EncodeBool(value bool) error
// EncodeFloat64 encodes a float64 value.
EncodeFloat64(value float64) error
// EncodeBytes encodes a byte slice.
EncodeBytes(value []byte) error
// EncodeBytesLen encodes the length of a byte slice.
EncodeBytesLen(value int) error
// EncodeArrayLen encodes the length of an array.
EncodeArrayLen(value int) error
}
// BufferedEncoder is an encoder backed by byte buffers.
type BufferedEncoder interface {
Buffer
Encoder
}
// BufferedEncoderAlloc allocates a bufferer encoder.
type BufferedEncoderAlloc func() BufferedEncoder
// BufferedEncoderPool is a pool of buffered encoders.
type BufferedEncoderPool interface {
// Init initializes the buffered encoder pool.
Init(alloc BufferedEncoderAlloc)
// Get returns a buffered encoder from the pool.
Get() BufferedEncoder
// Put puts a buffered encoder into the pool.
Put(enc BufferedEncoder)
}
// BufferedEncoderPoolOptions provides options for buffered encoder pools.
type BufferedEncoderPoolOptions interface {
// SetMaxCapacity sets the maximum capacity of buffers that can be returned to the pool.
SetMaxCapacity(value int) BufferedEncoderPoolOptions
// MaxBufferCapacity returns the maximum capacity of buffers that can be returned to the pool.
MaxCapacity() int
// SetObjectPoolOptions sets the object pool options.
SetObjectPoolOptions(value pool.ObjectPoolOptions) BufferedEncoderPoolOptions
// ObjectPoolOptions returns the object pool options.
ObjectPoolOptions() pool.ObjectPoolOptions
}
// encoderBase is the base encoder interface.
type encoderBase interface {
// Encoder returns the encoder.
encoder() BufferedEncoder
// err returns the error encountered during encoding, if any.
err() error
// reset resets the encoder.
reset(encoder BufferedEncoder)
// resetData resets the encoder data.
resetData()
// encodeStoragePolicy encodes a storage policy.
encodeStoragePolicy(p policy.StoragePolicy)
// encodePolicy encodes a policy.
encodePolicy(p policy.Policy)
// encodeVersion encodes a version.
encodeVersion(version int)
// encodeObjectType encodes an object type.
encodeObjectType(objType objectType)
// encodeNumObjectFields encodes the number of object fields.
encodeNumObjectFields(numFields int)
// encodeRawID encodes a raw ID.
encodeRawID(id id.RawID)
// encodeChunkedID encodes a chunked ID.
encodeChunkedID(id id.ChunkedID)
// encodeVarint encodes an integer value as varint.
encodeVarint(value int64)
// encodeBool encodes a boolean value.
encodeBool(value bool)
// encodeFloat64 encodes a float64 value.
encodeFloat64(value float64)
// encodeBytes encodes a byte slice.
encodeBytes(value []byte)
// encodeBytesLen encodes the length of a byte slice.
encodeBytesLen(value int)
// encodeArrayLen encodes the length of an array.
encodeArrayLen(value int)
}
// iteratorBase is the base iterator interface.
type iteratorBase interface {
// Reset resets the iterator.
reset(reader io.Reader)
// err returns the error encountered during decoding, if any.
err() error
// setErr sets the iterator error.
setErr(err error)
// reader returns the buffered reader.
reader() bufReader
// decodeStoragePolicy decodes a storage policy.
decodeStoragePolicy() policy.StoragePolicy
// decodePolicy decodes a policy.
decodePolicy() policy.Policy
// decodeVersion decodes a version.
decodeVersion() int
// decodeObjectType decodes an object type.
decodeObjectType() objectType
// decodeNumObjectFields decodes the number of object fields.
decodeNumObjectFields() int
// decodeRawID decodes a raw ID.
decodeRawID() id.RawID
// decodeVarint decodes a variable-width integer value.
decodeVarint() int64
// decodeBool decodes a boolean value.
decodeBool() bool
// decodeFloat64 decodes a float64 value.
decodeFloat64() float64
// decodeBytes decodes a byte slice.
decodeBytes() []byte
// decodeBytesLen decodes the length of a byte slice.
decodeBytesLen() int
// decodeArrayLen decodes the length of an array.
decodeArrayLen() int
// skip skips given number of fields if applicable.
skip(numFields int)
// checkNumFieldsForType decodes and compares the number of actual fields with
// the number of expected fields for a given object type.
checkNumFieldsForType(objType objectType) (int, int, bool)
// checkExpectedNumFieldsForType compares the given number of actual fields with
// the number of expected fields for a given object type.
checkExpectedNumFieldsForType(objType objectType, numActualFields int) (int, bool)
}
// UnaggregatedEncoder is an encoder for encoding different types of unaggregated metrics.
type UnaggregatedEncoder interface {
// EncodeCounter encodes a counter.
EncodeCounter(c unaggregated.Counter) error
// EncodeBatchTimer encodes a batch timer.
EncodeBatchTimer(bt unaggregated.BatchTimer) error
// EncodeGauge encodes a gauge.
EncodeGauge(g unaggregated.Gauge) error
// EncodeCounterWithPoliciesList encodes a counter with applicable policies list.
EncodeCounterWithPoliciesList(cp unaggregated.CounterWithPoliciesList) error
// EncodeBatchTimerWithPoliciesList encodes a batched timer with applicable policies list.
EncodeBatchTimerWithPoliciesList(btp unaggregated.BatchTimerWithPoliciesList) error
// EncodeGaugeWithPoliciesList encodes a gauge with applicable policies list.
EncodeGaugeWithPoliciesList(gp unaggregated.GaugeWithPoliciesList) error
// Encoder returns the encoder.
Encoder() BufferedEncoder
// Reset resets the encoder.
Reset(encoder BufferedEncoder)
}
// UnaggregatedIterator is an iterator for decoding different types of unaggregated metrics.
type UnaggregatedIterator interface {
// Next returns true if there are more items to decode.
Next() bool
// Metric returns the current metric.
// The returned value remains valid until the next Next() call.
Metric() unaggregated.MetricUnion
// PoliciesList returns the current applicable policies list.
// The returned value remains valid until the next Next() call.
PoliciesList() policy.PoliciesList
// Err returns the error encountered during decoding, if any.
Err() error
// Reset resets the iterator.
Reset(reader io.Reader)
// Close closes the iterator.
Close()
}
// UnaggregatedIteratorOptions provide options for unaggregated iterators.
type UnaggregatedIteratorOptions interface {
// SetIgnoreHigherVersion determines whether the iterator ignores messages
// with higher-than-supported version.
SetIgnoreHigherVersion(value bool) UnaggregatedIteratorOptions
// IgnoreHigherVersion returns whether the iterator ignores messages with
// higher-than-supported version.
IgnoreHigherVersion() bool
// SetReaderBufferSize sets the reader buffer size.
SetReaderBufferSize(value int) UnaggregatedIteratorOptions
// ReaderBufferSize returns the reader buffer size.
ReaderBufferSize() int
// SetLargeFloatsSize determines whether a float slice is considered a "large"
// slice and therefore resort to the pool for allocating that slice.
SetLargeFloatsSize(value int) UnaggregatedIteratorOptions
// LargeFloatsSize returns whether a float slice is considered a "large"
// slice and therefore resort to the pool for allocating that slice.
LargeFloatsSize() int
// SetLargeFloatsPool sets the large floats pool.
SetLargeFloatsPool(value pool.FloatsPool) UnaggregatedIteratorOptions
// LargeFloatsPool returns the large floats pool.
LargeFloatsPool() pool.FloatsPool
// SetIteratorPool sets the unaggregated iterator pool.
SetIteratorPool(value UnaggregatedIteratorPool) UnaggregatedIteratorOptions
// IteratorPool returns the unaggregated iterator pool.
IteratorPool() UnaggregatedIteratorPool
}
// UnaggregatedIteratorAlloc allocates an unaggregated iterator.
type UnaggregatedIteratorAlloc func() UnaggregatedIterator
// UnaggregatedIteratorPool is a pool of unaggregated iterators.
type UnaggregatedIteratorPool interface {
// Init initializes the unaggregated iterator pool.
Init(alloc UnaggregatedIteratorAlloc)
// Get returns an unaggregated iterator from the pool.
Get() UnaggregatedIterator
// Put puts an unaggregated iterator into the pool.
Put(it UnaggregatedIterator)
}
// AggregatedEncoder is an encoder for encoding aggregated metrics.
type AggregatedEncoder interface {
// EncodeMetricWithStoragePolicy encodes a metric with an applicable storage policy.
EncodeMetricWithStoragePolicy(mp aggregated.MetricWithStoragePolicy) error
// EncodeMetricWithStoragePolicyAndEncodeTime encodes a metric with an applicable
// storage policy, alongside the time at which encoding happens.
EncodeMetricWithStoragePolicyAndEncodeTime(
mp aggregated.MetricWithStoragePolicy,
encodedAtNanos int64,
) error
// EncodeChunkedMetricWithStoragePolicy encodes a chunked metric with an applicable storage policy.
EncodeChunkedMetricWithStoragePolicy(cmp aggregated.ChunkedMetricWithStoragePolicy) error
// EncodeChunkedMetricWithStoragePolicyAndEncodeTime encodes a chunked metric with
// an applicable storage policy, alongside the time at which encoding happens.
EncodeChunkedMetricWithStoragePolicyAndEncodeTime(
cmp aggregated.ChunkedMetricWithStoragePolicy,
encodedAtNanos int64,
) error
// Encoder returns the encoder.
Encoder() BufferedEncoder
// Reset resets the encoder.
Reset(encoder BufferedEncoder)
}
// AggregatedIterator is an iterator for decoding aggregated metrics.
type AggregatedIterator interface {
// Next returns true if there are more metrics to decode.
Next() bool
// Value returns the current raw metric, the corresponding policy, and timestamp at
// which the metric and the policy were encoded if applicable.
Value() (aggregated.RawMetric, policy.StoragePolicy, int64)
// Err returns the error encountered during decoding, if any.
Err() error
// Reset resets the iterator.
Reset(reader io.Reader)
// Close closes the iterator.
Close()
}
// AggregatedIteratorOptions provide options for aggregated iterators.
type AggregatedIteratorOptions interface {
// SetIgnoreHigherVersion determines whether the iterator ignores messages
// with higher-than-supported version.
SetIgnoreHigherVersion(value bool) AggregatedIteratorOptions
// IgnoreHigherVersion returns whether the iterator ignores messages with
// higher-than-supported version.
IgnoreHigherVersion() bool
// SetReaderBufferSize sets the reader buffer size.
SetReaderBufferSize(value int) AggregatedIteratorOptions
// ReaderBufferSize returns the reader buffer size.
ReaderBufferSize() int
// SetIteratorPool sets the aggregated iterator pool.
SetIteratorPool(value AggregatedIteratorPool) AggregatedIteratorOptions
// IteratorPool returns the aggregated iterator pool.
IteratorPool() AggregatedIteratorPool
}
// AggregatedIteratorAlloc allocates an aggregated iterator.
type AggregatedIteratorAlloc func() AggregatedIterator
// AggregatedIteratorPool is a pool of aggregated iterators.
type AggregatedIteratorPool interface {
// Init initializes the aggregated iterator pool.
Init(alloc AggregatedIteratorAlloc)
// Get returns an aggregated iterator from the pool.
Get() AggregatedIterator
// Put puts an aggregated iterator into the pool.
Put(it AggregatedIterator)
}