-
Notifications
You must be signed in to change notification settings - Fork 459
/
Copy pathconfig.go
299 lines (254 loc) · 9.6 KB
/
config.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
// 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 local
import (
goerrors "errors"
"fmt"
"sync"
"time"
"github.com/m3db/m3/src/dbnode/client"
"github.com/m3db/m3/src/query/storage"
"github.com/m3db/m3/src/query/stores/m3db"
"github.com/m3db/m3x/ident"
)
var (
errNotAggregatedClusterNamespace = goerrors.New("not an aggregated cluster namespace")
errBothNamespaceTypeNewAndDeprecatedFieldsSet = goerrors.New("cannot specify both deprecated and non-deprecated fields for namespace type")
defaultNewClientConfigurationParams = client.ConfigurationParameters{}
)
// ClustersStaticConfiguration is a set of static cluster configurations.
type ClustersStaticConfiguration []ClusterStaticConfiguration
// NewClientFromConfig is a method that can be set on
// ClusterStaticConfiguration to allow overriding the client initialization.
type NewClientFromConfig func(
cfg client.Configuration,
params client.ConfigurationParameters,
custom ...client.CustomOption,
) (client.Client, error)
// ClusterStaticConfiguration is a static cluster configuration.
type ClusterStaticConfiguration struct {
NewClientFromConfig NewClientFromConfig
Namespaces []ClusterStaticNamespaceConfiguration `yaml:"namespaces"`
Client client.Configuration `yaml:"client"`
}
func (c ClusterStaticConfiguration) newClient(
params client.ConfigurationParameters,
custom ...client.CustomOption,
) (client.Client, error) {
if c.NewClientFromConfig != nil {
return c.NewClientFromConfig(c.Client, params, custom...)
}
return c.Client.NewClient(params, custom...)
}
// ClusterStaticNamespaceConfiguration describes the namespaces in a
// static cluster.
type ClusterStaticNamespaceConfiguration struct {
// Namespace is namespace in the cluster that is specified.
Namespace string `yaml:"namespace"`
// Type is the type of values stored by the namespace, current
// supported values are "unaggregated" or "aggregated".
Type storage.MetricsType `yaml:"type"`
// Retention is the length of which values are stored by the namespace.
Retention time.Duration `yaml:"retention" validate:"nonzero"`
// Resolution is the frequency of which values are stored by the namespace.
Resolution time.Duration `yaml:"resolution" validate:"min=0"`
// Downsample is the configuration for downsampling options to use with
// the namespace.
Downsample *DownsampleClusterStaticNamespaceConfiguration `yaml:"downsample"`
// StorageMetricsType is the namespace type.
//
// Deprecated: Use "Type" field when specifying config instead, it is
// invalid to use both.
StorageMetricsType storage.MetricsType `yaml:"storageMetricsType"`
}
func (c ClusterStaticNamespaceConfiguration) metricsType() (storage.MetricsType, error) {
result := storage.DefaultMetricsType
if c.Type != storage.DefaultMetricsType && c.StorageMetricsType != storage.DefaultMetricsType {
// Don't allow both to not be default
return result, errBothNamespaceTypeNewAndDeprecatedFieldsSet
}
if c.Type != storage.DefaultMetricsType {
// New field value set
return c.Type, nil
}
if c.StorageMetricsType != storage.DefaultMetricsType {
// Deprecated field value set
return c.StorageMetricsType, nil
}
// Both are default
return result, nil
}
func (c ClusterStaticNamespaceConfiguration) downsampleOptions() (
ClusterNamespaceDownsampleOptions,
error,
) {
nsType, err := c.metricsType()
if err != nil {
return ClusterNamespaceDownsampleOptions{}, err
}
if nsType != storage.AggregatedMetricsType {
return ClusterNamespaceDownsampleOptions{}, errNotAggregatedClusterNamespace
}
if c.Downsample == nil {
return defaultClusterNamespaceDownsampleOptions, nil
}
return c.Downsample.downsampleOptions(), nil
}
// DownsampleClusterStaticNamespaceConfiguration is configuration
// specified for downsampling options on an aggregated cluster namespace.
type DownsampleClusterStaticNamespaceConfiguration struct {
All bool `yaml:"all"`
}
func (c DownsampleClusterStaticNamespaceConfiguration) downsampleOptions() ClusterNamespaceDownsampleOptions {
return ClusterNamespaceDownsampleOptions(c)
}
type unaggregatedClusterNamespaceConfiguration struct {
client client.Client
namespace ClusterStaticNamespaceConfiguration
result clusterConnectResult
}
type aggregatedClusterNamespacesConfiguration struct {
client client.Client
namespaces []ClusterStaticNamespaceConfiguration
result clusterConnectResult
}
type clusterConnectResult struct {
session client.Session
err error
}
// ClustersStaticConfigurationOptions are options to use when
// constructing clusters from config.
type ClustersStaticConfigurationOptions struct {
AsyncSessions bool
}
// NewClusters instantiates a new Clusters instance.
func (c ClustersStaticConfiguration) NewClusters(
opts ClustersStaticConfigurationOptions,
) (Clusters, error) {
var (
numUnaggregatedClusterNamespaces int
numAggregatedClusterNamespaces int
unaggregatedClusterNamespaceCfg = &unaggregatedClusterNamespaceConfiguration{}
aggregatedClusterNamespacesCfgs []*aggregatedClusterNamespacesConfiguration
unaggregatedClusterNamespace UnaggregatedClusterNamespaceDefinition
aggregatedClusterNamespaces []AggregatedClusterNamespaceDefinition
)
for _, clusterCfg := range c {
client, err := clusterCfg.newClient(defaultNewClientConfigurationParams)
if err != nil {
return nil, err
}
aggregatedClusterNamespacesCfg := &aggregatedClusterNamespacesConfiguration{
client: client,
}
for _, n := range clusterCfg.Namespaces {
nsType, err := n.metricsType()
if err != nil {
return nil, err
}
switch nsType {
case storage.UnaggregatedMetricsType:
numUnaggregatedClusterNamespaces++
if numUnaggregatedClusterNamespaces > 1 {
return nil, fmt.Errorf("only one unaggregated cluster namespace "+
"can be specified: specified %d", numUnaggregatedClusterNamespaces)
}
unaggregatedClusterNamespaceCfg.client = client
unaggregatedClusterNamespaceCfg.namespace = n
case storage.AggregatedMetricsType:
numAggregatedClusterNamespaces++
aggregatedClusterNamespacesCfg.namespaces =
append(aggregatedClusterNamespacesCfg.namespaces, n)
default:
return nil, fmt.Errorf("unknown storage metrics type: %v", nsType)
}
}
if len(aggregatedClusterNamespacesCfg.namespaces) > 0 {
aggregatedClusterNamespacesCfgs =
append(aggregatedClusterNamespacesCfgs, aggregatedClusterNamespacesCfg)
}
}
if numUnaggregatedClusterNamespaces != 1 {
return nil, fmt.Errorf("one unaggregated cluster namespace "+
"must be specified: specified %d", numUnaggregatedClusterNamespaces)
}
// Connect to all clusters in parallel
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
cfg := unaggregatedClusterNamespaceCfg
if !opts.AsyncSessions {
cfg.result.session, cfg.result.err = cfg.client.DefaultSession()
} else {
cfg.result.session = m3db.NewAsyncSession(func() (client.Client, error) {
return cfg.client, nil
}, nil)
}
}()
for _, cfg := range aggregatedClusterNamespacesCfgs {
cfg := cfg // Capture var
wg.Add(1)
go func() {
defer wg.Done()
if !opts.AsyncSessions {
cfg.result.session, cfg.result.err = cfg.client.DefaultSession()
} else {
cfg.result.session = m3db.NewAsyncSession(func() (client.Client, error) {
return cfg.client, nil
}, nil)
}
}()
}
// Wait
wg.Wait()
if unaggregatedClusterNamespaceCfg.result.err != nil {
return nil, fmt.Errorf("could not connect to unaggregated cluster: %v",
unaggregatedClusterNamespaceCfg.result.err)
}
unaggregatedClusterNamespace = UnaggregatedClusterNamespaceDefinition{
NamespaceID: ident.StringID(unaggregatedClusterNamespaceCfg.namespace.Namespace),
Session: unaggregatedClusterNamespaceCfg.result.session,
Retention: unaggregatedClusterNamespaceCfg.namespace.Retention,
}
for i, cfg := range aggregatedClusterNamespacesCfgs {
if cfg.result.err != nil {
return nil, fmt.Errorf("could not connect to aggregated cluster #%d: %v",
i, cfg.result.err)
}
for _, n := range cfg.namespaces {
downsampleOpts, err := n.downsampleOptions()
if err != nil {
return nil, fmt.Errorf("error parse downsample options for cluster #%d namespace %s: %v",
i, n.Namespace, err)
}
def := AggregatedClusterNamespaceDefinition{
NamespaceID: ident.StringID(n.Namespace),
Session: cfg.result.session,
Retention: n.Retention,
Resolution: n.Resolution,
Downsample: &downsampleOpts,
}
aggregatedClusterNamespaces = append(aggregatedClusterNamespaces, def)
}
}
return NewClusters(unaggregatedClusterNamespace,
aggregatedClusterNamespaces...)
}