-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
221 lines (193 loc) · 7.38 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
// Copyright (c) 2017 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 handler
import (
"errors"
"fmt"
"github.com/m3db/m3/src/aggregator/aggregator/handler/filter"
"github.com/m3db/m3/src/aggregator/aggregator/handler/writer"
"github.com/m3db/m3/src/aggregator/sharding"
"github.com/m3db/m3/src/cluster/client"
"github.com/m3db/m3/src/cluster/services"
"github.com/m3db/m3/src/metrics/policy"
"github.com/m3db/m3/src/msg/producer"
"github.com/m3db/m3/src/msg/producer/config"
"github.com/m3db/m3/src/x/instrument"
"github.com/m3db/m3/src/x/pool"
"go.uber.org/zap"
)
var (
errNoHandlerConfiguration = errors.New("no handler configuration")
errNoDynamicOrStaticBackendConfiguration = errors.New("neither dynamic nor static backend was configured")
errBothDynamicAndStaticBackendConfiguration = errors.New("both dynamic and static backend were configured")
)
// FlushHandlerConfiguration configures flush handlers.
type FlushHandlerConfiguration struct {
Handlers []flushHandlerConfiguration `yaml:"handlers" validate:"nonzero"`
}
// NewHandler creates a new flush handler based on the configuration.
func (c FlushHandlerConfiguration) NewHandler(
cs client.Client,
instrumentOpts instrument.Options,
) (Handler, error) {
if len(c.Handlers) == 0 {
return nil, errNoHandlerConfiguration
}
var (
handlers = make([]Handler, 0, len(c.Handlers))
)
for _, hc := range c.Handlers {
handler, err := hc.newHandler(cs, instrumentOpts)
if err != nil {
return nil, err
}
handlers = append(handlers, handler)
}
if len(handlers) == 1 {
return handlers[0], nil
}
return NewBroadcastHandler(handlers), nil
}
type writerConfiguration struct {
// Pool of buffered bytes.
BytesPool *pool.BucketizedPoolConfiguration `yaml:"bytesPool"`
// How frequent is the encoding time sampled and included in the payload.
EncodingTimeSamplingRate float64 `yaml:"encodingTimeSamplingRate" validate:"min=0.0,max=1.0"`
}
func (c writerConfiguration) NewWriterOptions(
instrumentOpts instrument.Options,
) writer.Options {
opts := writer.NewOptions().
SetInstrumentOptions(instrumentOpts).
SetEncodingTimeSamplingRate(c.EncodingTimeSamplingRate)
scope := instrumentOpts.MetricsScope()
iOpts := instrumentOpts.SetMetricsScope(scope.SubScope("buffered-encoder-pool"))
if c.BytesPool != nil {
iOpts := iOpts.SetMetricsScope(scope.Tagged(map[string]string{"pool": "buffered-bytes-pool"}))
bytesPool := pool.NewBytesPool(c.BytesPool.NewBuckets(), c.BytesPool.NewObjectPoolOptions(iOpts))
bytesPool.Init()
opts = opts.SetBytesPool(bytesPool)
}
return opts
}
type flushHandlerConfiguration struct {
// StaticBackend configures the backend.
StaticBackend *staticBackendConfiguration `yaml:"staticBackend"`
// DynamicBackend configures the dynamic backend.
DynamicBackend *dynamicBackendConfiguration `yaml:"dynamicBackend"`
}
func (c flushHandlerConfiguration) newHandler(
cs client.Client,
instrumentOpts instrument.Options,
) (Handler, error) {
if err := c.Validate(); err != nil {
return nil, err
}
if c.DynamicBackend != nil {
return c.DynamicBackend.newProtobufHandler(
cs,
instrumentOpts,
)
}
switch c.StaticBackend.Type {
case blackholeType:
return NewBlackholeHandler(), nil
case loggingType:
return NewLoggingHandler(instrumentOpts.Logger()), nil
default:
return nil, fmt.Errorf("unknown backend type %v", c.StaticBackend.Type)
}
}
func (c flushHandlerConfiguration) Validate() error {
if c.StaticBackend == nil && c.DynamicBackend == nil {
return errNoDynamicOrStaticBackendConfiguration
}
if c.StaticBackend != nil && c.DynamicBackend != nil {
return errBothDynamicAndStaticBackendConfiguration
}
return nil
}
type dynamicBackendConfiguration struct {
// Name of the backend.
Name string `yaml:"name"`
// Hashing function type.
HashType sharding.HashType `yaml:"hashType"`
// Producer configs the m3msg producer.
Producer config.ProducerConfiguration `yaml:"producer"`
// Filters configs the filter for consumer services.
Filters []consumerServiceFilterConfiguration `yaml:"filters"`
// Filters configs the filter for consumer services.
StoragePolicyFilters []storagePolicyFilterConfiguration `yaml:"storagePolicyFilters"`
// Writer configs the writer options.
Writer writerConfiguration `yaml:"writer"`
}
func (c *dynamicBackendConfiguration) newProtobufHandler(
cs client.Client,
instrumentOpts instrument.Options,
) (Handler, error) {
scope := instrumentOpts.MetricsScope().Tagged(map[string]string{
"backend": c.Name,
"component": "producer",
})
instrumentOpts = instrumentOpts.SetMetricsScope(scope)
p, err := c.Producer.NewProducer(cs, instrumentOpts)
if err != nil {
return nil, err
}
if err := p.Init(); err != nil {
return nil, err
}
logger := instrumentOpts.Logger()
for _, filter := range c.Filters {
sid, f := filter.NewConsumerServiceFilter()
p.RegisterFilter(sid, f)
logger.Info("registered filter for consumer service", zap.Stringer("service", sid))
}
for _, filter := range c.StoragePolicyFilters {
sid, f := filter.NewConsumerServiceFilter()
p.RegisterFilter(sid, f)
logger.Info("registered storage policy filter for consumer service",
zap.Any("policies", filter.StoragePolicies),
zap.Stringer("service", sid))
}
wOpts := c.Writer.NewWriterOptions(instrumentOpts)
instrumentOpts.Logger().Info("created flush handler with protobuf encoding", zap.String("name", c.Name))
return NewProtobufHandler(p, c.HashType, wOpts), nil
}
type storagePolicyFilterConfiguration struct {
ServiceID services.ServiceIDConfiguration `yaml:"serviceID" validate:"nonzero"`
StoragePolicies []policy.StoragePolicy `yaml:"storagePolicies" validate:"nonzero"`
}
func (c storagePolicyFilterConfiguration) NewConsumerServiceFilter() (services.ServiceID, producer.FilterFunc) {
return c.ServiceID.NewServiceID(), writer.NewStoragePolicyFilter(c.StoragePolicies)
}
type consumerServiceFilterConfiguration struct {
ServiceID services.ServiceIDConfiguration `yaml:"serviceID" validate:"nonzero"`
ShardSet sharding.ShardSet `yaml:"shardSet" validate:"nonzero"`
}
func (c consumerServiceFilterConfiguration) NewConsumerServiceFilter() (services.ServiceID, producer.FilterFunc) {
return c.ServiceID.NewServiceID(), filter.NewShardSetFilter(c.ShardSet)
}
type staticBackendConfiguration struct {
// Static backend type.
Type Type `yaml:"type"`
// Name of the backend.
Name string `yaml:"name"`
}