forked from m3db/m3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
197 lines (164 loc) · 6.89 KB
/
server.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
// 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 config
import (
"time"
"github.com/m3db/m3/src/aggregator/server/http"
"github.com/m3db/m3/src/aggregator/server/rawtcp"
"github.com/m3db/m3/src/metrics/encoding/msgpack"
"github.com/m3db/m3/src/metrics/encoding/protobuf"
"github.com/m3db/m3/src/x/instrument"
"github.com/m3db/m3/src/x/pool"
"github.com/m3db/m3/src/x/retry"
xserver "github.com/m3db/m3/src/x/server"
)
// RawTCPServerConfiguration contains raw TCP server configuration.
type RawTCPServerConfiguration struct {
// Raw TCP server listening address.
ListenAddress string `yaml:"listenAddress" validate:"nonzero"`
// Error log limit per second.
ErrorLogLimitPerSecond *int64 `yaml:"errorLogLimitPerSecond"`
// Whether keep alives are enabled on connections.
KeepAliveEnabled *bool `yaml:"keepAliveEnabled"`
// KeepAlive period.
KeepAlivePeriod *time.Duration `yaml:"keepAlivePeriod"`
// Retry mechanism configuration.
Retry retry.Configuration `yaml:"retry"`
// Read buffer size.
ReadBufferSize *int `yaml:"readBufferSize"`
// Msgpack iterator configuration.
MsgpackIterator msgpackUnaggregatedIteratorConfiguration `yaml:"msgpackIterator"`
// Protobuf iterator configuration.
ProtobufIterator protobufUnaggregatedIteratorConfiguration `yaml:"protobufIterator"`
}
// NewServerOptions create a new set of raw TCP server options.
func (c *RawTCPServerConfiguration) NewServerOptions(
instrumentOpts instrument.Options,
) rawtcp.Options {
opts := rawtcp.NewOptions().SetInstrumentOptions(instrumentOpts)
// Set server options.
serverOpts := xserver.NewOptions().
SetInstrumentOptions(instrumentOpts).
SetRetryOptions(c.Retry.NewOptions(instrumentOpts.MetricsScope()))
if c.KeepAliveEnabled != nil {
serverOpts = serverOpts.SetTCPConnectionKeepAlive(*c.KeepAliveEnabled)
}
if c.KeepAlivePeriod != nil {
serverOpts = serverOpts.SetTCPConnectionKeepAlivePeriod(*c.KeepAlivePeriod)
}
opts = opts.SetServerOptions(serverOpts)
// Set msgpack iterator options.
msgpackItOpts := c.MsgpackIterator.NewOptions(instrumentOpts)
opts = opts.SetMsgpackUnaggregatedIteratorOptions(msgpackItOpts)
// Set protobuf iterator options.
protobufItOpts := c.ProtobufIterator.NewOptions(instrumentOpts)
opts = opts.SetProtobufUnaggregatedIteratorOptions(protobufItOpts)
if c.ReadBufferSize != nil {
opts = opts.SetReadBufferSize(*c.ReadBufferSize)
}
if c.ErrorLogLimitPerSecond != nil {
opts = opts.SetErrorLogLimitPerSecond(*c.ErrorLogLimitPerSecond)
}
return opts
}
// msgpackUnaggregatedIteratorConfiguration contains configuration for msgpack unaggregated iterator.
type msgpackUnaggregatedIteratorConfiguration struct {
// Whether to ignore encoded data streams whose version is higher than the current known version.
IgnoreHigherVersion *bool `yaml:"ignoreHigherVersion"`
// Reader buffer size.
ReaderBufferSize *int `yaml:"readerBufferSize"`
// Whether a float slice is considered a "large" slice and therefore resort to
// the large floats pool for allocating that slice.
LargeFloatsSize *int `yaml:"largeFloatsSize"`
// Pool of large float slices.
LargeFloatsPool pool.BucketizedPoolConfiguration `yaml:"largeFloatsPool"`
}
func (c *msgpackUnaggregatedIteratorConfiguration) NewOptions(
instrumentOpts instrument.Options,
) msgpack.UnaggregatedIteratorOptions {
scope := instrumentOpts.MetricsScope()
opts := msgpack.NewUnaggregatedIteratorOptions()
if c.IgnoreHigherVersion != nil {
opts = opts.SetIgnoreHigherVersion(*c.IgnoreHigherVersion)
}
if c.ReaderBufferSize != nil {
opts = opts.SetReaderBufferSize(*c.ReaderBufferSize)
}
if c.LargeFloatsSize != nil {
opts = opts.SetLargeFloatsSize(*c.LargeFloatsSize)
}
// NB(xichen): intentionally not using the same floats pool used for computing
// timer quantiles to accommodate different usage patterns and reduce contention.
iOpts := instrumentOpts.SetMetricsScope(scope.SubScope("large-floats-pool"))
largeFloatsPoolOpts := c.LargeFloatsPool.NewObjectPoolOptions(iOpts)
largeFloatsPool := pool.NewFloatsPool(c.LargeFloatsPool.NewBuckets(), largeFloatsPoolOpts)
opts = opts.SetLargeFloatsPool(largeFloatsPool)
largeFloatsPool.Init()
return opts
}
// protobufUnaggregatedIteratorConfiguration contains configuration for protobuf unaggregated iterator.
type protobufUnaggregatedIteratorConfiguration struct {
// Initial buffer size.
InitBufferSize *int `yaml:"initBufferSize"`
// Maximum message size.
MaxMessageSize *int `yaml:"maxMessageSize"`
// Bytes pool.
BytesPool pool.BucketizedPoolConfiguration `yaml:"bytesPool"`
}
func (c *protobufUnaggregatedIteratorConfiguration) NewOptions(
instrumentOpts instrument.Options,
) protobuf.UnaggregatedOptions {
opts := protobuf.NewUnaggregatedOptions()
if c.InitBufferSize != nil {
opts = opts.SetInitBufferSize(*c.InitBufferSize)
}
if c.MaxMessageSize != nil {
opts = opts.SetMaxMessageSize(*c.MaxMessageSize)
}
// Set bytes pool.
scope := instrumentOpts.MetricsScope()
iOpts := instrumentOpts.SetMetricsScope(scope.SubScope("bytes-pool"))
objectPoolOpts := c.BytesPool.NewObjectPoolOptions(iOpts)
buckets := c.BytesPool.NewBuckets()
bytesPool := pool.NewBytesPool(buckets, objectPoolOpts)
opts = opts.SetBytesPool(bytesPool)
bytesPool.Init()
return opts
}
// HTTPServerConfiguration contains http server configuration.
type HTTPServerConfiguration struct {
// HTTP server listening address.
ListenAddress string `yaml:"listenAddress" validate:"nonzero"`
// HTTP server read timeout.
ReadTimeout time.Duration `yaml:"readTimeout"`
// HTTP server write timeout.
WriteTimeout time.Duration `yaml:"writeTimeout"`
}
// NewServerOptions create a new set of http server options.
func (c *HTTPServerConfiguration) NewServerOptions() http.Options {
opts := http.NewOptions()
if c.ReadTimeout != 0 {
opts = opts.SetReadTimeout(c.ReadTimeout)
}
if c.WriteTimeout != 0 {
opts = opts.SetWriteTimeout(c.WriteTimeout)
}
return opts
}