github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/runtime/runtime_options.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 runtime
    22  
    23  import (
    24  	"errors"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/dbnode/ratelimit"
    28  	"github.com/m3db/m3/src/dbnode/topology"
    29  )
    30  
    31  const (
    32  	// DefaultWriteConsistencyLevel is the default write consistency level
    33  	DefaultWriteConsistencyLevel = topology.ConsistencyLevelMajority
    34  
    35  	// DefaultReadConsistencyLevel is the default read consistency level
    36  	DefaultReadConsistencyLevel = topology.ReadConsistencyLevelUnstrictMajority
    37  
    38  	// DefaultBootstrapConsistencyLevel is the default bootstrap consistency level
    39  	DefaultBootstrapConsistencyLevel = topology.ReadConsistencyLevelMajority
    40  
    41  	defaultWriteNewSeriesAsync                  = false
    42  	defaultWriteNewSeriesBackoffDuration        = time.Duration(0)
    43  	defaultWriteNewSeriesLimitPerShardPerSecond = 0
    44  	defaultTickSeriesBatchSize                  = 512
    45  	defaultTickPerSeriesSleepDuration           = 100 * time.Microsecond
    46  	defaultTickMinimumInterval                  = 10 * time.Second
    47  	defaultTickCancellationCheckInterval        = time.Second
    48  	defaultMaxWiredBlocks                       = uint(1 << 16) // 65,536
    49  )
    50  
    51  var (
    52  	errWriteNewSeriesBackoffDurationIsNegative = errors.New(
    53  		"write new series backoff duration cannot be negative")
    54  	errWriteNewSeriesLimitPerShardPerSecondIsNegative = errors.New(
    55  		"write new series limit per shard per cannot be negative")
    56  	errTickSeriesBatchSizeMustBePositive = errors.New(
    57  		"tick series batch size must be positive")
    58  	errTickPerSeriesSleepDurationMustBePositive = errors.New(
    59  		"tick per series sleep duration must be positive")
    60  )
    61  
    62  type options struct {
    63  	persistRateLimitOpts                 ratelimit.Options
    64  	writeNewSeriesAsync                  bool
    65  	writeNewSeriesBackoffDuration        time.Duration
    66  	writeNewSeriesLimitPerShardPerSecond int
    67  	encodersPerBlockLimit                int
    68  	tickSeriesBatchSize                  int
    69  	tickPerSeriesSleepDuration           time.Duration
    70  	tickMinimumInterval                  time.Duration
    71  	maxWiredBlocks                       uint
    72  	clientBootstrapConsistencyLevel      topology.ReadConsistencyLevel
    73  	clientReadConsistencyLevel           topology.ReadConsistencyLevel
    74  	clientWriteConsistencyLevel          topology.ConsistencyLevel
    75  	tickCancellationCheckInterval        time.Duration
    76  }
    77  
    78  // NewOptions creates a new set of runtime options with defaults
    79  func NewOptions() Options {
    80  	return &options{
    81  		persistRateLimitOpts:                 ratelimit.NewOptions(),
    82  		writeNewSeriesAsync:                  defaultWriteNewSeriesAsync,
    83  		writeNewSeriesBackoffDuration:        defaultWriteNewSeriesBackoffDuration,
    84  		writeNewSeriesLimitPerShardPerSecond: defaultWriteNewSeriesLimitPerShardPerSecond,
    85  		tickSeriesBatchSize:                  defaultTickSeriesBatchSize,
    86  		tickPerSeriesSleepDuration:           defaultTickPerSeriesSleepDuration,
    87  		tickMinimumInterval:                  defaultTickMinimumInterval,
    88  		maxWiredBlocks:                       defaultMaxWiredBlocks,
    89  		clientBootstrapConsistencyLevel:      DefaultBootstrapConsistencyLevel,
    90  		clientReadConsistencyLevel:           DefaultReadConsistencyLevel,
    91  		clientWriteConsistencyLevel:          DefaultWriteConsistencyLevel,
    92  		tickCancellationCheckInterval:        defaultTickCancellationCheckInterval,
    93  	}
    94  }
    95  
    96  func (o *options) Validate() error {
    97  	// writeNewSeriesBackoffDuration can be zero to specify no backoff
    98  	if o.writeNewSeriesBackoffDuration < 0 {
    99  		return errWriteNewSeriesBackoffDurationIsNegative
   100  	}
   101  
   102  	// writeNewSeriesLimitPerShardPerSecond can be zero to specify that
   103  	// no limit should be enforced
   104  	if o.writeNewSeriesLimitPerShardPerSecond < 0 {
   105  		return errWriteNewSeriesLimitPerShardPerSecondIsNegative
   106  	}
   107  
   108  	if !(o.tickSeriesBatchSize > 0) {
   109  		return errTickSeriesBatchSizeMustBePositive
   110  	}
   111  
   112  	if !(o.tickPerSeriesSleepDuration > 0) {
   113  		return errTickPerSeriesSleepDurationMustBePositive
   114  	}
   115  
   116  	// tickMinimumInterval can be zero if user desires
   117  
   118  	return nil
   119  }
   120  
   121  func (o *options) SetPersistRateLimitOptions(value ratelimit.Options) Options {
   122  	opts := *o
   123  	opts.persistRateLimitOpts = value
   124  	return &opts
   125  }
   126  
   127  func (o *options) PersistRateLimitOptions() ratelimit.Options {
   128  	return o.persistRateLimitOpts
   129  }
   130  
   131  func (o *options) SetWriteNewSeriesAsync(value bool) Options {
   132  	opts := *o
   133  	opts.writeNewSeriesAsync = value
   134  	return &opts
   135  }
   136  
   137  func (o *options) WriteNewSeriesAsync() bool {
   138  	return o.writeNewSeriesAsync
   139  }
   140  
   141  func (o *options) SetWriteNewSeriesBackoffDuration(value time.Duration) Options {
   142  	opts := *o
   143  	opts.writeNewSeriesBackoffDuration = value
   144  	return &opts
   145  }
   146  
   147  func (o *options) WriteNewSeriesBackoffDuration() time.Duration {
   148  	return o.writeNewSeriesBackoffDuration
   149  }
   150  
   151  func (o *options) SetWriteNewSeriesLimitPerShardPerSecond(value int) Options {
   152  	opts := *o
   153  	opts.writeNewSeriesLimitPerShardPerSecond = value
   154  	return &opts
   155  }
   156  
   157  func (o *options) WriteNewSeriesLimitPerShardPerSecond() int {
   158  	return o.writeNewSeriesLimitPerShardPerSecond
   159  }
   160  
   161  func (o *options) SetEncodersPerBlockLimit(value int) Options {
   162  	opts := *o
   163  	opts.encodersPerBlockLimit = value
   164  	return &opts
   165  }
   166  
   167  func (o *options) EncodersPerBlockLimit() int {
   168  	return o.encodersPerBlockLimit
   169  }
   170  
   171  func (o *options) SetTickSeriesBatchSize(value int) Options {
   172  	opts := *o
   173  	opts.tickSeriesBatchSize = value
   174  	return &opts
   175  }
   176  
   177  func (o *options) TickSeriesBatchSize() int {
   178  	return o.tickSeriesBatchSize
   179  }
   180  
   181  func (o *options) SetTickPerSeriesSleepDuration(value time.Duration) Options {
   182  	opts := *o
   183  	opts.tickPerSeriesSleepDuration = value
   184  	return &opts
   185  }
   186  
   187  func (o *options) TickPerSeriesSleepDuration() time.Duration {
   188  	return o.tickPerSeriesSleepDuration
   189  }
   190  
   191  func (o *options) SetTickMinimumInterval(value time.Duration) Options {
   192  	opts := *o
   193  	opts.tickMinimumInterval = value
   194  	return &opts
   195  }
   196  
   197  func (o *options) TickMinimumInterval() time.Duration {
   198  	return o.tickMinimumInterval
   199  }
   200  
   201  func (o *options) SetMaxWiredBlocks(value uint) Options {
   202  	opts := *o
   203  	opts.maxWiredBlocks = value
   204  	return &opts
   205  }
   206  
   207  func (o *options) MaxWiredBlocks() uint {
   208  	return o.maxWiredBlocks
   209  }
   210  
   211  func (o *options) SetClientBootstrapConsistencyLevel(value topology.ReadConsistencyLevel) Options {
   212  	opts := *o
   213  	opts.clientBootstrapConsistencyLevel = value
   214  	return &opts
   215  }
   216  
   217  func (o *options) ClientBootstrapConsistencyLevel() topology.ReadConsistencyLevel {
   218  	return o.clientBootstrapConsistencyLevel
   219  }
   220  
   221  func (o *options) SetClientReadConsistencyLevel(value topology.ReadConsistencyLevel) Options {
   222  	opts := *o
   223  	opts.clientReadConsistencyLevel = value
   224  	return &opts
   225  }
   226  
   227  func (o *options) ClientReadConsistencyLevel() topology.ReadConsistencyLevel {
   228  	return o.clientReadConsistencyLevel
   229  }
   230  
   231  func (o *options) SetClientWriteConsistencyLevel(value topology.ConsistencyLevel) Options {
   232  	opts := *o
   233  	opts.clientWriteConsistencyLevel = value
   234  	return &opts
   235  }
   236  
   237  func (o *options) ClientWriteConsistencyLevel() topology.ConsistencyLevel {
   238  	return o.clientWriteConsistencyLevel
   239  }
   240  
   241  func (o *options) SetTickCancellationCheckInterval(value time.Duration) Options {
   242  	opts := *o
   243  	opts.tickCancellationCheckInterval = value
   244  	return &opts
   245  }
   246  
   247  func (o *options) TickCancellationCheckInterval() time.Duration {
   248  	return o.tickCancellationCheckInterval
   249  }