/
types.go
295 lines (255 loc) · 8.03 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
// 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"
"fmt"
"time"
"github.com/m3db/m3/src/query/block"
"github.com/m3db/m3/src/query/cost"
"github.com/m3db/m3/src/query/models"
"github.com/m3db/m3/src/query/ts"
xtime "github.com/m3db/m3/src/x/time"
"github.com/uber-go/tally"
)
// 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
)
// 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
}
// 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"`
}
func (q *FetchQuery) String() string {
return q.Raw
}
// FetchOptions represents the options for fetch query.
type FetchOptions struct {
// Limit is the maximum number of series to return.
Limit int
// BlockType is the block type that the fetch function returns.
BlockType models.FetchedBlockType
// FanoutOptions are the options for the fetch namespace fanout.
FanoutOptions *FanoutOptions
// Enforcer is used to enforce resource limits on the number of datapoints
// used by a given query. Limits are imposed at time of decompression.
Enforcer cost.ChainedEnforcer
// Scope is used to report metrics about the fetch.
Scope tally.Scope
}
// 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
)
// NewFetchOptions creates a new fetch options.
func NewFetchOptions() *FetchOptions {
return &FetchOptions{
Limit: 0,
BlockType: models.TypeSingleBlock,
FanoutOptions: &FanoutOptions{
FanoutUnaggregated: FanoutDefault,
FanoutAggregated: FanoutDefault,
FanoutAggregatedOptimized: FanoutDefault,
},
Enforcer: cost.NoopChainedEnforcer(),
Scope: tally.NoopScope,
}
}
// Clone will clone and return the fetch options.
func (o *FetchOptions) Clone() *FetchOptions {
result := *o
return &result
}
// Querier handles queries against a storage.
type Querier interface {
// Fetch fetches timeseries data based on a query
Fetch(
ctx context.Context,
query *FetchQuery,
options *FetchOptions,
) (*FetchResult, error)
// FetchBlocks converts fetch results to storage blocks
FetchBlocks(
ctx context.Context,
query *FetchQuery,
options *FetchOptions,
) (block.Result, 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,
) (*CompleteTagsResult, error)
}
// WriteQuery represents the input timeseries that is written to the db
type WriteQuery struct {
Tags models.Tags
Datapoints ts.Datapoints
Unit xtime.Unit
Annotation []byte
Attributes Attributes
}
func (q *WriteQuery) String() string {
return string(q.Tags.ID())
}
// CompleteTagsQuery represents a query that returns an autocompleted
// set of tags that exist in the db
type CompleteTagsQuery struct {
CompleteNameOnly bool
FilterNameTags [][]byte
TagMatchers models.Matchers
Start time.Time
End time.Time
}
// SeriesMatchQuery represents a query that returns a set of series
// that match the query
type SeriesMatchQuery struct {
TagMatchers []models.Matchers
Start time.Time
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)
}
// CompletedTag is an autocompleted tag with a name and a list of possible values
type CompletedTag struct {
Name []byte
Values [][]byte
}
// CompleteTagsResult represents a set of autocompleted tag names and values
type CompleteTagsResult struct {
CompleteNameOnly bool
CompletedTags []CompletedTag
}
// CompleteTagsResultBuilder is a builder that accumulates and deduplicates
// incoming CompleteTagsResult values
type CompleteTagsResultBuilder interface {
Add(*CompleteTagsResult) error
Build() CompleteTagsResult
}
// Appender provides batched appends against a storage.
type Appender interface {
// Write value to the database for an ID
Write(ctx context.Context, query *WriteQuery) error
}
// SearchResults is the result from a search
type SearchResults struct {
Metrics models.Metrics
}
// FetchResult provides a fetch result and meta information
type FetchResult struct {
SeriesList ts.SeriesList // The aggregated list of results across all underlying storage calls
LocalOnly bool
HasNext bool
}
// QueryResult is the result from a query
type QueryResult struct {
FetchResult *FetchResult
Err error
}
// MetricsType is a type of stored metrics.
type MetricsType uint
const (
// UnaggregatedMetricsType is an unaggregated metrics type.
UnaggregatedMetricsType MetricsType = iota
// AggregatedMetricsType is an aggregated metrics type.
AggregatedMetricsType
// DefaultMetricsType is the default metrics type value.
DefaultMetricsType = UnaggregatedMetricsType
)
var (
validMetricsTypes = []MetricsType{
UnaggregatedMetricsType,
AggregatedMetricsType,
}
)
func (t MetricsType) String() string {
switch t {
case UnaggregatedMetricsType:
return "unaggregated"
case AggregatedMetricsType:
return "aggregated"
default:
return "unknown"
}
}
// Attributes is a set of stored metrics attributes.
type Attributes struct {
MetricsType MetricsType
Retention time.Duration
Resolution time.Duration
}