github.com/m3db/m3@v1.5.0/src/cmd/services/m3coordinator/ingest/m3msg/config.go (about)

     1  // Copyright (c) 2018 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 ingestm3msg
    22  
    23  import (
    24  	"github.com/m3db/m3/src/query/models"
    25  	"github.com/m3db/m3/src/query/storage"
    26  	"github.com/m3db/m3/src/x/instrument"
    27  	"github.com/m3db/m3/src/x/pool"
    28  	"github.com/m3db/m3/src/x/retry"
    29  	"github.com/m3db/m3/src/x/sampler"
    30  	"github.com/m3db/m3/src/x/serialize"
    31  	xsync "github.com/m3db/m3/src/x/sync"
    32  )
    33  
    34  const defaultLogSampleRate = 0.01
    35  
    36  // Configuration configs the ingester.
    37  type Configuration struct {
    38  	WorkerPoolSize int                          `yaml:"workerPoolSize"`
    39  	OpPool         pool.ObjectPoolConfiguration `yaml:"opPool"`
    40  	Retry          retry.Configuration          `yaml:"retry"`
    41  	LogSampleRate  *float64                     `yaml:"logSampleRate" validate:"min=0.0,max=1.0"`
    42  }
    43  
    44  // NewIngester creates an ingester with an appender.
    45  func (cfg Configuration) NewIngester(
    46  	appender storage.Appender,
    47  	tagOptions models.TagOptions,
    48  	instrumentOptions instrument.Options,
    49  ) (*Ingester, error) {
    50  	opts, err := cfg.newOptions(appender, tagOptions, instrumentOptions)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return NewIngester(opts), nil
    55  }
    56  
    57  func (cfg Configuration) newOptions(
    58  	appender storage.Appender,
    59  	tagOptions models.TagOptions,
    60  	instrumentOptions instrument.Options,
    61  ) (Options, error) {
    62  	scope := instrumentOptions.MetricsScope().Tagged(
    63  		map[string]string{"component": "ingester"},
    64  	)
    65  	workers, err := xsync.NewPooledWorkerPool(
    66  		cfg.WorkerPoolSize,
    67  		xsync.NewPooledWorkerPoolOptions().
    68  			SetInstrumentOptions(instrumentOptions),
    69  	)
    70  	if err != nil {
    71  		return Options{}, err
    72  	}
    73  
    74  	workers.Init()
    75  	tagDecoderPool := serialize.NewTagDecoderPool(
    76  		serialize.NewTagDecoderOptions(serialize.TagDecoderOptionsConfig{}),
    77  		pool.NewObjectPoolOptions().
    78  			SetInstrumentOptions(instrumentOptions.
    79  				SetMetricsScope(instrumentOptions.MetricsScope().
    80  					SubScope("tag-decoder-pool"))),
    81  	)
    82  	tagDecoderPool.Init()
    83  
    84  	var logSampleRate = defaultLogSampleRate
    85  	if cfg.LogSampleRate != nil {
    86  		logSampleRate = *cfg.LogSampleRate
    87  	}
    88  	sampler, err := sampler.NewSampler(sampler.Rate(logSampleRate))
    89  	if err != nil {
    90  		return Options{}, err
    91  	}
    92  	return Options{
    93  		Appender:          appender,
    94  		Workers:           workers,
    95  		PoolOptions:       cfg.OpPool.NewObjectPoolOptions(instrumentOptions),
    96  		TagOptions:        tagOptions,
    97  		TagDecoderPool:    tagDecoderPool,
    98  		RetryOptions:      cfg.Retry.NewOptions(scope),
    99  		Sampler:           sampler,
   100  		InstrumentOptions: instrumentOptions,
   101  	}, nil
   102  }