github.com/m3db/m3@v1.5.0/src/cmd/services/m3aggregator/config/server.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package config
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/m3db/m3/src/aggregator/server/http"
    27  	"github.com/m3db/m3/src/aggregator/server/m3msg"
    28  	"github.com/m3db/m3/src/aggregator/server/rawtcp"
    29  	"github.com/m3db/m3/src/metrics/encoding/protobuf"
    30  	"github.com/m3db/m3/src/msg/consumer"
    31  	"github.com/m3db/m3/src/x/instrument"
    32  	"github.com/m3db/m3/src/x/pool"
    33  	"github.com/m3db/m3/src/x/retry"
    34  	xserver "github.com/m3db/m3/src/x/server"
    35  )
    36  
    37  // M3MsgServerConfiguration contains M3Msg server configuration.
    38  type M3MsgServerConfiguration struct {
    39  	// Server is the server configuration.
    40  	Server xserver.Configuration `yaml:"server"`
    41  
    42  	// Consumer is the M3Msg consumer configuration.
    43  	Consumer consumer.Configuration `yaml:"consumer"`
    44  }
    45  
    46  // NewServerOptions creates a new set of M3Msg server options.
    47  func (c *M3MsgServerConfiguration) NewServerOptions(
    48  	instrumentOpts instrument.Options,
    49  ) (m3msg.Options, error) {
    50  	opts := m3msg.NewOptions().
    51  		SetInstrumentOptions(instrumentOpts).
    52  		SetServerOptions(c.Server.NewOptions(instrumentOpts)).
    53  		SetConsumerOptions(c.Consumer.NewOptions(instrumentOpts))
    54  	if err := opts.Validate(); err != nil {
    55  		return nil, err
    56  	}
    57  	return opts, nil
    58  }
    59  
    60  // RawTCPServerConfiguration contains raw TCP server configuration.
    61  type RawTCPServerConfiguration struct {
    62  	// Raw TCP server listening address.
    63  	ListenAddress string `yaml:"listenAddress" validate:"nonzero"`
    64  
    65  	// Error log limit per second.
    66  	ErrorLogLimitPerSecond *int64 `yaml:"errorLogLimitPerSecond"`
    67  
    68  	// Whether keep alives are enabled on connections.
    69  	KeepAliveEnabled *bool `yaml:"keepAliveEnabled"`
    70  
    71  	// KeepAlive period.
    72  	KeepAlivePeriod *time.Duration `yaml:"keepAlivePeriod"`
    73  
    74  	// Retry mechanism configuration.
    75  	Retry retry.Configuration `yaml:"retry"`
    76  
    77  	// Read buffer size.
    78  	ReadBufferSize *int `yaml:"readBufferSize"`
    79  
    80  	// Protobuf iterator configuration.
    81  	ProtobufIterator protobufUnaggregatedIteratorConfiguration `yaml:"protobufIterator"`
    82  }
    83  
    84  // NewServerOptions create a new set of raw TCP server options.
    85  func (c *RawTCPServerConfiguration) NewServerOptions(
    86  	instrumentOpts instrument.Options,
    87  ) rawtcp.Options {
    88  	opts := rawtcp.NewOptions().SetInstrumentOptions(instrumentOpts)
    89  
    90  	// Set server options.
    91  	serverOpts := xserver.NewOptions().
    92  		SetInstrumentOptions(instrumentOpts).
    93  		SetRetryOptions(c.Retry.NewOptions(instrumentOpts.MetricsScope()))
    94  	if c.KeepAliveEnabled != nil {
    95  		serverOpts = serverOpts.SetTCPConnectionKeepAlive(*c.KeepAliveEnabled)
    96  	}
    97  	if c.KeepAlivePeriod != nil {
    98  		serverOpts = serverOpts.SetTCPConnectionKeepAlivePeriod(*c.KeepAlivePeriod)
    99  	}
   100  	opts = opts.SetServerOptions(serverOpts)
   101  
   102  	// Set protobuf iterator options.
   103  	protobufItOpts := c.ProtobufIterator.NewOptions(instrumentOpts)
   104  	opts = opts.SetProtobufUnaggregatedIteratorOptions(protobufItOpts)
   105  
   106  	if c.ReadBufferSize != nil {
   107  		opts = opts.SetReadBufferSize(*c.ReadBufferSize)
   108  	}
   109  	if c.ErrorLogLimitPerSecond != nil {
   110  		opts = opts.SetErrorLogLimitPerSecond(*c.ErrorLogLimitPerSecond)
   111  	}
   112  	return opts
   113  }
   114  
   115  // protobufUnaggregatedIteratorConfiguration contains configuration for protobuf unaggregated iterator.
   116  type protobufUnaggregatedIteratorConfiguration struct {
   117  	// Initial buffer size.
   118  	InitBufferSize *int `yaml:"initBufferSize"`
   119  
   120  	// Maximum message size.
   121  	MaxMessageSize *int `yaml:"maxMessageSize"`
   122  
   123  	// Bytes pool.
   124  	BytesPool pool.BucketizedPoolConfiguration `yaml:"bytesPool"`
   125  }
   126  
   127  func (c *protobufUnaggregatedIteratorConfiguration) NewOptions(
   128  	instrumentOpts instrument.Options,
   129  ) protobuf.UnaggregatedOptions {
   130  	opts := protobuf.NewUnaggregatedOptions()
   131  	if c.InitBufferSize != nil {
   132  		opts = opts.SetInitBufferSize(*c.InitBufferSize)
   133  	}
   134  	if c.MaxMessageSize != nil {
   135  		opts = opts.SetMaxMessageSize(*c.MaxMessageSize)
   136  	}
   137  
   138  	// Set bytes pool.
   139  	scope := instrumentOpts.MetricsScope()
   140  	iOpts := instrumentOpts.SetMetricsScope(scope.SubScope("bytes-pool"))
   141  	objectPoolOpts := c.BytesPool.NewObjectPoolOptions(iOpts)
   142  	buckets := c.BytesPool.NewBuckets()
   143  	bytesPool := pool.NewBytesPool(buckets, objectPoolOpts)
   144  	opts = opts.SetBytesPool(bytesPool)
   145  	bytesPool.Init()
   146  
   147  	return opts
   148  }
   149  
   150  // HTTPServerConfiguration contains http server configuration.
   151  type HTTPServerConfiguration struct {
   152  	// HTTP server listening address.
   153  	ListenAddress string `yaml:"listenAddress" validate:"nonzero"`
   154  
   155  	// HTTP server read timeout.
   156  	ReadTimeout time.Duration `yaml:"readTimeout"`
   157  
   158  	// HTTP server write timeout.
   159  	WriteTimeout time.Duration `yaml:"writeTimeout"`
   160  }
   161  
   162  // NewServerOptions create a new set of http server options.
   163  func (c *HTTPServerConfiguration) NewServerOptions() http.Options {
   164  	opts := http.NewOptions()
   165  	if c.ReadTimeout != 0 {
   166  		opts = opts.SetReadTimeout(c.ReadTimeout)
   167  	}
   168  	if c.WriteTimeout != 0 {
   169  		opts = opts.SetWriteTimeout(c.WriteTimeout)
   170  	}
   171  	return opts
   172  }