/
types.go
346 lines (310 loc) · 11.5 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
// 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 storage
import (
"context"
"errors"
"fmt"
"time"
"github.com/m3db/m3/src/metrics/policy"
"github.com/m3db/m3/src/query/block"
"github.com/m3db/m3/src/query/generated/proto/prompb"
"github.com/m3db/m3/src/query/models"
"github.com/m3db/m3/src/query/storage/m3/consolidators"
"github.com/m3db/m3/src/query/storage/m3/storagemetadata"
"github.com/m3db/m3/src/query/ts"
xtime "github.com/m3db/m3/src/x/time"
"github.com/uber-go/tally"
)
var (
errWriteQueryNoDatapoints = errors.New("write query with no datapoints")
)
// Type describes the type of storage.
type Type int
const (
// TypeLocalDC is for storages that reside in the local datacenter.
TypeLocalDC Type = iota
// TypeRemoteDC is for storages that reside in a remote datacenter.
TypeRemoteDC
// TypeMultiDC is for storages that will aggregate multiple datacenters.
TypeMultiDC
// TypeDebug is for storages that are used for debugging purposes.
TypeDebug
)
// ErrorBehavior describes what this storage type should do on error. This is
// used for determining how to proceed when encountering an error in a fanout
// storage situation.
type ErrorBehavior uint8
const (
// BehaviorFail is for storages that should fail the entire query when queries
// against this storage fail.
BehaviorFail ErrorBehavior = iota
// BehaviorWarn is for storages that should only warn of incomplete results on
// failure.
BehaviorWarn
// BehaviorContainer is for storages that contain substorages. It is necessary
// to look at the returned error to determine if it's a failing error or
// a warning error.
BehaviorContainer
)
// Storage provides an interface for reading and writing to the tsdb.
type Storage interface {
Querier
Appender
// Type identifies the type of the underlying storage.
Type() Type
// Close is used to close the underlying storage and free up resources.
Close() error
// ErrorBehavior dictates what fanout storage should do when this storage
// encounters an error.
ErrorBehavior() ErrorBehavior
// Name gives the plaintext name for this storage, used for logging purposes.
Name() string
}
// Query is an interface for a M3DB query.
type Query interface {
fmt.Stringer
// nolint
query()
}
func (q *FetchQuery) query() {}
func (q *WriteQuery) query() {}
// FetchQuery represents the input query which is fetched from M3DB.
type FetchQuery struct {
Raw string
TagMatchers models.Matchers `json:"matchers"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
Interval time.Duration `json:"interval"`
}
// FetchOptions represents the options for fetch query.
type FetchOptions struct {
// Remote is set when this fetch is originated by a remote grpc call.
Remote bool
// SeriesLimit is the maximum number of series to return.
SeriesLimit int
// InstanceMultiple is how much to increase the per database instance series limit.
InstanceMultiple float32
// DocsLimit is the maximum number of docs to return.
DocsLimit int
// RangeLimit is the maximum time range to return.
RangeLimit time.Duration
// ReturnedSeriesLimit is the maximum number of series to return.
ReturnedSeriesLimit int
// ReturnedDatapointsLimit is the maximum number of datapoints to return.
ReturnedDatapointsLimit int
// ReturnedSeriesMetadataLimit is the maximum number of series metadata to return.
ReturnedSeriesMetadataLimit int
// RequireExhaustive results in an error if the query exceeds the series limit.
RequireExhaustive bool
// RequireNoWait results in an error if the query execution must wait for permits.
RequireNoWait bool
// BlockType is the block type that the fetch function returns.
BlockType models.FetchedBlockType
// FanoutOptions are the options for the fetch namespace fanout.
FanoutOptions *FanoutOptions
// RestrictQueryOptions restricts the fetch to a specific set of
// conditions.
RestrictQueryOptions *RestrictQueryOptions
// Step is the configured step size.
Step time.Duration
// LookbackDuration if set overrides the default lookback duration.
LookbackDuration *time.Duration
// Scope is used to report metrics about the fetch.
Scope tally.Scope
// Timeout is the timeout for the request.
Timeout time.Duration
// Source is the source for the query.
Source []byte
}
// FanoutOptions describes which namespaces should be fanned out to for
// the query.
type FanoutOptions struct {
// FanoutUnaggregated describes the fanout options for
// unaggregated namespaces.
FanoutUnaggregated FanoutOption
// FanoutAggregated describes the fanout options for
// aggregated namespaces.
FanoutAggregated FanoutOption
// FanoutAggregatedOptimized describes the fanout options for the
// aggregated namespace optimization.
FanoutAggregatedOptimized FanoutOption
}
// FanoutOption describes the fanout option.
type FanoutOption uint
const (
// FanoutDefault defaults to the fanout option.
FanoutDefault FanoutOption = iota
// FanoutForceDisable forces disabling fanout.
FanoutForceDisable
// FanoutForceEnable forces enabling fanout.
FanoutForceEnable
)
// RestrictByType are specific restrictions to stick to a single data type.
type RestrictByType struct {
// MetricsType restricts the type of metrics being returned.
MetricsType storagemetadata.MetricsType
// StoragePolicy is required if metrics type is not unaggregated
// to specify which storage policy metrics should be returned from.
StoragePolicy policy.StoragePolicy
}
// RestrictByTag are specific restrictions to enforce behavior for given
// tags.
type RestrictByTag struct {
// Restrict is a set of override matchers to apply to a fetch
// regardless of the existing fetch matchers, they should replace any
// existing matchers part of a fetch if they collide.
Restrict models.Matchers
// Strip is a set of tag names to strip from the response.
//
// NB: If this is unset, but Restrict is set, all tag names appearing in any
// of the Restrict matchers are removed.
Strip [][]byte
}
// RestrictQueryOptions restricts the query to a specific set of conditions.
type RestrictQueryOptions struct {
// RestrictByType are specific restrictions to stick to a single data type.
RestrictByType *RestrictByType
// RestrictByTag are specific restrictions to enforce behavior for given
// tags.
RestrictByTag *RestrictByTag
// RestrictByTypes are specific restrictions to query from specified data
// types.
RestrictByTypes []*RestrictByType
}
// Querier handles queries against a storage.
type Querier interface {
// FetchProm fetches decompressed timeseries data based on a query in a
// Prometheus-compatible format.
// TODO: take in an accumulator of some sort rather than returning
// necessarily as a Prom result.
FetchProm(
ctx context.Context,
query *FetchQuery,
options *FetchOptions,
) (PromResult, error)
// FetchBlocks fetches timeseries as blocks based on a query.
FetchBlocks(
ctx context.Context,
query *FetchQuery,
options *FetchOptions,
) (block.Result, error)
FetchCompressed(
ctx context.Context,
query *FetchQuery,
options *FetchOptions,
) (consolidators.MultiFetchResult, error)
// SearchSeries returns series IDs matching the current query.
SearchSeries(
ctx context.Context,
query *FetchQuery,
options *FetchOptions,
) (*SearchResults, error)
// CompleteTags returns autocompleted tag results.
CompleteTags(
ctx context.Context,
query *CompleteTagsQuery,
options *FetchOptions,
) (*consolidators.CompleteTagsResult, error)
// QueryStorageMetadataAttributes returns the storage metadata
// attributes for a query.
QueryStorageMetadataAttributes(
ctx context.Context,
queryStart, queryEnd time.Time,
opts *FetchOptions,
) ([]storagemetadata.Attributes, error)
}
// WriteQuery represents the input timeseries that is written to the database.
// TODO: rename WriteQuery to WriteRequest or something similar.
type WriteQuery struct {
// opts as a field allows the options to be unexported
// and the Validate method on WriteQueryOptions to be reused.
opts WriteQueryOptions
}
// WriteQueryOptions is a set of options to use to construct a write query.
// These are passed by options so that they can be validated when creating
// a write query, which helps knowing a constructed write query is valid.
type WriteQueryOptions struct {
Tags models.Tags
Datapoints ts.Datapoints
Unit xtime.Unit
Annotation []byte
Attributes storagemetadata.Attributes
}
// CompleteTagsQuery represents a query that returns an autocompleted
// set of tags.
type CompleteTagsQuery struct {
// CompleteNameOnly indicates if the query should return only tag names, or
// tag names and values.
CompleteNameOnly bool
// FilterNameTags is a list of tags to filter results by. If this is empty, no
// filtering is applied.
FilterNameTags [][]byte
// TagMatchers is the search criteria for the query.
TagMatchers models.Matchers
// Start is the inclusive start for the query.
Start xtime.UnixNano
// End is the exclusive end for the query.
End xtime.UnixNano
}
// SeriesMatchQuery represents a query that returns a set of series
// that match the query.
type SeriesMatchQuery struct {
// TagMatchers is the search criteria for the query.
TagMatchers []models.Matchers
// Start is the inclusive start for the query.
Start time.Time
// End is the exclusive end for the query.
End time.Time
}
func (q *CompleteTagsQuery) String() string {
if q.CompleteNameOnly {
return fmt.Sprintf("completing tag name for query %s", q.TagMatchers)
}
return fmt.Sprintf("completing tag values for query %s", q.TagMatchers)
}
// Appender provides batched appends against a storage.
type Appender interface {
// Write writes a batched set of datapoints to storage based on the provided
// query.
Write(ctx context.Context, query *WriteQuery) error
}
// SearchResults is the result from a search.
type SearchResults struct {
// Metrics is the list of search results.
Metrics models.Metrics
// Metadata describes any metadata for the Fetch operation.
Metadata block.ResultMetadata
}
// FetchResult provides a decompressed fetch result and meta information.
type FetchResult struct {
// SeriesList is the list of decompressed and computed series after fetch
// query execution.
SeriesList ts.SeriesList
// Metadata describes any metadata for the operation.
Metadata block.ResultMetadata
}
// PromResult is a Prometheus-compatible result type.
type PromResult struct {
// PromResult is the result, in Prometheus protobuf format.
PromResult *prompb.QueryResult
// ResultMetadata is the metadata for the result.
Metadata block.ResultMetadata
}