-
Notifications
You must be signed in to change notification settings - Fork 451
/
cluster_resolver.go
362 lines (318 loc) · 13.1 KB
/
cluster_resolver.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
// Copyright (c) 2019 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 m3
import (
"fmt"
"sort"
"time"
"github.com/m3db/m3/src/query/storage"
"github.com/m3db/m3/src/query/storage/m3/consolidators"
"github.com/m3db/m3/src/query/storage/m3/storagemetadata"
)
type unaggregatedNamespaceType uint8
const (
partiallySatisfiesRange unaggregatedNamespaceType = iota
fullySatisfiesRange
disabled
)
type unaggregatedNamespaceDetails struct {
satisfies unaggregatedNamespaceType
clusterNamespace ClusterNamespace
retention time.Duration
}
// resolveUnaggregatedNamespaceForQuery determines if the unaggregated namespace
// should be used, and if so, determines if it fully satisfies the query range.
func resolveUnaggregatedNamespaceForQuery(
now, start time.Time,
unaggregated ClusterNamespace,
opts *storage.FanoutOptions,
) unaggregatedNamespaceDetails {
if opts.FanoutUnaggregated == storage.FanoutForceDisable {
return unaggregatedNamespaceDetails{satisfies: disabled}
}
retention := unaggregated.Options().Attributes().Retention
unaggregatedStart := now.Add(-1 * retention)
if unaggregatedStart.Before(start) || unaggregatedStart.Equal(start) {
return unaggregatedNamespaceDetails{
clusterNamespace: unaggregated,
retention: retention,
satisfies: fullySatisfiesRange,
}
}
return unaggregatedNamespaceDetails{
clusterNamespace: unaggregated,
retention: retention,
satisfies: partiallySatisfiesRange,
}
}
// resolveClusterNamespacesForQuery returns the namespaces that need to be
// fanned out to depending on the query time and the namespaces configured.
func resolveClusterNamespacesForQuery(
now, start, end time.Time,
clusters Clusters,
opts *storage.FanoutOptions,
restrict *storage.RestrictQueryOptions,
) (consolidators.QueryFanoutType, ClusterNamespaces, error) {
if typeRestrict := restrict.GetRestrictByType(); typeRestrict != nil {
// If a specific restriction is set, then attempt to satisfy.
return resolveClusterNamespacesForQueryWithRestrictQueryOptions(now,
start, clusters, *typeRestrict)
}
// First check if the unaggregated cluster can fully satisfy the query range.
// If so, return it and shortcircuit, as unaggregated will necessarily have
// every metric.
unaggregated := resolveUnaggregatedNamespaceForQuery(now, start,
clusters.UnaggregatedClusterNamespace(), opts)
if unaggregated.satisfies == fullySatisfiesRange {
return consolidators.NamespaceCoversAllQueryRange,
ClusterNamespaces{unaggregated.clusterNamespace},
nil
}
if opts.FanoutAggregated == storage.FanoutForceDisable {
if unaggregated.satisfies == partiallySatisfiesRange {
return consolidators.NamespaceCoversPartialQueryRange,
ClusterNamespaces{unaggregated.clusterNamespace}, nil
}
return consolidators.NamespaceInvalid, nil, errUnaggregatedAndAggregatedDisabled
}
// The filter function will drop namespaces which do not cover the entire
// query range from contention.
//
// NB: if fanout aggregation is forced on, the filter instead forces clusters
// that do not cover the range to be set as partially aggregated.
coversRangeFilter := newCoversRangeFilter(coversRangeFilterOptions{
now: now,
queryStart: start,
})
// Filter aggregated namespaces by filter function and options.
var r reusedAggregatedNamespaceSlices
r = aggregatedNamespaces(clusters.ClusterNamespaces(), r, coversRangeFilter, opts)
// If any of the aggregated clusters have a complete set of metrics, use
// those that have the smallest resolutions, supplemented by lower resolution
// partially aggregated metrics.
if len(r.completeAggregated) > 0 {
sort.Stable(ClusterNamespacesByResolutionAsc(r.completeAggregated))
// Take most granular complete aggregated namespace.
result := r.completeAggregated[:1]
completedAttrs := result[0].Options().Attributes()
// Also include any finer grain partially aggregated namespaces that
// may contain a matching metric.
for _, n := range r.partialAggregated {
if n.Options().Attributes().Resolution < completedAttrs.Resolution {
// More granular resolution.
result = append(result, n)
}
}
return consolidators.NamespaceCoversAllQueryRange, result, nil
}
// No complete aggregated namespaces can definitely fulfill the query,
// so take the longest retention completed aggregated namespace to return
// as much data as possible, along with any partially aggregated namespaces
// that have either same retention and lower resolution or longer retention
// than the complete aggregated namespace.
r = aggregatedNamespaces(clusters.ClusterNamespaces(), r, nil, opts)
if len(r.completeAggregated) == 0 {
// Absolutely no complete aggregated namespaces, need to fanout to all
// partial aggregated namespaces as well as the unaggregated cluster
// as we have no idea which has the longest retention.
result := r.partialAggregated
// If unaggregated namespace can partially satisfy this range, add it as a
// fanout contender.
if unaggregated.satisfies == partiallySatisfiesRange {
result = append(result, unaggregated.clusterNamespace)
}
// If any namespace currently in contention does not cover the entire query
// range, set query fanout type to namespaceCoversPartialQueryRange.
for _, n := range result {
if !coversRangeFilter(n) {
return consolidators.NamespaceCoversPartialQueryRange, result, nil
}
}
// Otherwise, all namespaces cover the query range.
return consolidators.NamespaceCoversAllQueryRange, result, nil
}
// Return the longest retention aggregated namespace and
// any potentially more granular or longer retention partial
// aggregated namespaces.
sort.Stable(sort.Reverse(ClusterNamespacesByRetentionAsc(r.completeAggregated)))
// Take longest retention complete aggregated namespace or the unaggregated
// cluster if that is longer than the longest aggregated namespace.
result := r.completeAggregated[:1]
completedAttrs := result[0].Options().Attributes()
if unaggregated.satisfies == partiallySatisfiesRange {
if completedAttrs.Retention <= unaggregated.retention {
// If the longest aggregated cluster for some reason has lower retention
// than the unaggregated cluster then we prefer the unaggregated cluster
// as it has a complete data set and is always the most granular.
result[0] = unaggregated.clusterNamespace
completedAttrs = unaggregated.clusterNamespace.Options().Attributes()
}
}
// Take any partially aggregated namespaces with longer retention or
// same retention with more granular resolution that may contain
// a matching metric.
for _, n := range r.partialAggregated {
if n.Options().Attributes().Retention > completedAttrs.Retention {
// Higher retention.
result = append(result, n)
} else if n.Options().Attributes().Retention == completedAttrs.Retention &&
n.Options().Attributes().Resolution < completedAttrs.Resolution {
// Same retention but more granular resolution.
result = append(result, n)
}
}
return consolidators.NamespaceCoversPartialQueryRange, result, nil
}
type reusedAggregatedNamespaceSlices struct {
completeAggregated []ClusterNamespace
partialAggregated []ClusterNamespace
}
func (slices reusedAggregatedNamespaceSlices) reset(
size int,
) reusedAggregatedNamespaceSlices {
// Initialize arrays if yet uninitialized.
if slices.completeAggregated == nil {
slices.completeAggregated = make([]ClusterNamespace, 0, size)
} else {
slices.completeAggregated = slices.completeAggregated[:0]
}
if slices.partialAggregated == nil {
slices.partialAggregated = make([]ClusterNamespace, 0, size)
} else {
slices.partialAggregated = slices.partialAggregated[:0]
}
return slices
}
// aggregatedNamespaces filters out clusters that do not meet the filter
// condition, and organizes remaining clusters in two lists if possible.
//
// NB: If fanout aggregation is disabled, no clusters will be returned as either
// partial or complete candidates. If fanout aggregation is forced to enabled
// then no filter is applied, and all namespaces are considered viable. In this
// case, the filter is used to determine if returned namespaces have the
// complete set of metrics.
//
// NB: If fanout optimization is enabled, add any aggregated namespaces that
// have a complete set of metrics to the completeAggregated slice list. If this
// optimization is disabled, or if none of the aggregated namespaces are
// guaranteed to have a complete set of all metrics, they are added to the
// partialAggregated list.
func aggregatedNamespaces(
all ClusterNamespaces,
slices reusedAggregatedNamespaceSlices,
filter func(ClusterNamespace) bool,
opts *storage.FanoutOptions,
) reusedAggregatedNamespaceSlices {
// Reset reused slices.
slices = slices.reset(len(all))
// Otherwise the default and force enable is to fanout and treat
// the aggregated namespaces differently (depending on whether they
// have all the data).
for _, namespace := range all {
nsOpts := namespace.Options()
if nsOpts.Attributes().MetricsType != storagemetadata.AggregatedMetricsType {
// Not an aggregated cluster.
continue
}
if filter != nil && !filter(namespace) {
// Fails to satisfy filter.
continue
}
// If not optimizing fanout to aggregated namespaces, set all aggregated
// namespaces satisfying the filter as partially aggregated, as all metrics
// do not necessarily appear in all namespaces, depending on configuration.
if opts.FanoutAggregatedOptimized == storage.FanoutForceDisable {
slices.partialAggregated = append(slices.partialAggregated, namespace)
continue
}
// Otherwise, check downsample options for the namespace and determine if
// this namespace is set as containing all metrics.
downsampleOpts, err := nsOpts.DownsampleOptions()
if err != nil {
continue
}
if downsampleOpts.All {
// This namespace has a complete set of metrics. Ensure that it passes
// the filter if it was a forced addition, otherwise it may be too short
// to cover the entire range and should be considered a partial result.
slices.completeAggregated = append(slices.completeAggregated, namespace)
continue
}
// This namespace does not necessarily have a complete set of metrics.
slices.partialAggregated = append(slices.partialAggregated, namespace)
}
return slices
}
// resolveClusterNamespacesForQueryWithRestrictQueryOptions returns the cluster
// namespace referred to by the restrict fetch options or an error if it
// cannot be found.
func resolveClusterNamespacesForQueryWithRestrictQueryOptions(
now, start time.Time,
clusters Clusters,
restrict storage.RestrictByType,
) (consolidators.QueryFanoutType, ClusterNamespaces, error) {
coversRangeFilter := newCoversRangeFilter(coversRangeFilterOptions{
now: now,
queryStart: start,
})
result := func(
namespace ClusterNamespace,
err error,
) (consolidators.QueryFanoutType, ClusterNamespaces, error) {
if err != nil {
return 0, nil, err
}
if coversRangeFilter(namespace) {
return consolidators.NamespaceCoversAllQueryRange,
ClusterNamespaces{namespace}, nil
}
return consolidators.NamespaceCoversPartialQueryRange,
ClusterNamespaces{namespace}, nil
}
switch restrict.MetricsType {
case storagemetadata.UnaggregatedMetricsType:
return result(clusters.UnaggregatedClusterNamespace(), nil)
case storagemetadata.AggregatedMetricsType:
ns, ok := clusters.AggregatedClusterNamespace(RetentionResolution{
Retention: restrict.StoragePolicy.Retention().Duration(),
Resolution: restrict.StoragePolicy.Resolution().Window,
})
if !ok {
return result(nil,
fmt.Errorf("could not find namespace for storage policy: %v",
restrict.StoragePolicy.String()))
}
return result(ns, nil)
default:
return result(nil,
fmt.Errorf("unrecognized metrics type: %v", restrict.MetricsType))
}
}
type coversRangeFilterOptions struct {
now time.Time
queryStart time.Time
}
func newCoversRangeFilter(opts coversRangeFilterOptions) func(namespace ClusterNamespace) bool {
return func(namespace ClusterNamespace) bool {
// Include only if can fulfill the entire time range of the query
clusterStart := opts.now.Add(-1 * namespace.Options().Attributes().Retention)
return !clusterStart.After(opts.queryStart)
}
}