github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3em/node/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 node
    22  
    23  import (
    24  	"fmt"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/x/instrument"
    28  	xretry "github.com/m3db/m3/src/x/retry"
    29  )
    30  
    31  var (
    32  	defaultNodeOptTimeout     = 90 * time.Second
    33  	defaultTransferBufferSize = 1024 * 1024       /* 1 MB */
    34  	defaultMaxPullSize        = int64(1024 * 100) /* 100 KB*/
    35  	fourMegaBytes             = 4 * 1024 * 1024
    36  )
    37  
    38  type nodeOpts struct {
    39  	iopts              instrument.Options
    40  	operationTimeout   time.Duration
    41  	transferBufferSize int
    42  	maxPullSize        int64
    43  	retrier            xretry.Retrier
    44  	hOpts              HeartbeatOptions
    45  	opClientFn         OperatorClientFn
    46  }
    47  
    48  // NewOptions returns a new Options construct.
    49  func NewOptions(
    50  	opts instrument.Options,
    51  ) Options {
    52  	if opts == nil {
    53  		opts = instrument.NewOptions()
    54  	}
    55  	return &nodeOpts{
    56  		iopts:              opts,
    57  		operationTimeout:   defaultNodeOptTimeout,
    58  		transferBufferSize: defaultTransferBufferSize,
    59  		maxPullSize:        defaultMaxPullSize,
    60  		retrier:            xretry.NewRetrier(xretry.NewOptions()),
    61  		hOpts:              NewHeartbeatOptions(),
    62  	}
    63  }
    64  
    65  func (o *nodeOpts) Validate() error {
    66  	// grpc max message size (by default, 4MB). see also: https://github.com/grpc/grpc-go/issues/746
    67  	if o.transferBufferSize >= fourMegaBytes {
    68  		return fmt.Errorf("TransferBufferSize must be < 4MB")
    69  	}
    70  
    71  	if o.maxPullSize < 0 {
    72  		return fmt.Errorf("MaxPullSize must be >= 0")
    73  	}
    74  
    75  	if o.opClientFn == nil {
    76  		return fmt.Errorf("OperatorClientFn is not set")
    77  	}
    78  
    79  	return o.hOpts.Validate()
    80  }
    81  
    82  func (o *nodeOpts) SetInstrumentOptions(io instrument.Options) Options {
    83  	o.iopts = io
    84  	return o
    85  }
    86  
    87  func (o *nodeOpts) InstrumentOptions() instrument.Options {
    88  	return o.iopts
    89  }
    90  
    91  func (o *nodeOpts) SetRetrier(retrier xretry.Retrier) Options {
    92  	o.retrier = retrier
    93  	return o
    94  }
    95  
    96  func (o *nodeOpts) Retrier() xretry.Retrier {
    97  	return o.retrier
    98  }
    99  
   100  func (o *nodeOpts) SetOperationTimeout(d time.Duration) Options {
   101  	o.operationTimeout = d
   102  	return o
   103  }
   104  
   105  func (o *nodeOpts) OperationTimeout() time.Duration {
   106  	return o.operationTimeout
   107  }
   108  
   109  func (o *nodeOpts) SetTransferBufferSize(sz int) Options {
   110  	o.transferBufferSize = sz
   111  	return o
   112  }
   113  
   114  func (o *nodeOpts) TransferBufferSize() int {
   115  	return o.transferBufferSize
   116  }
   117  
   118  func (o *nodeOpts) SetMaxPullSize(sz int64) Options {
   119  	o.maxPullSize = sz
   120  	return o
   121  }
   122  
   123  func (o *nodeOpts) MaxPullSize() int64 {
   124  	return o.maxPullSize
   125  }
   126  
   127  func (o *nodeOpts) SetHeartbeatOptions(ho HeartbeatOptions) Options {
   128  	o.hOpts = ho
   129  	return o
   130  }
   131  
   132  func (o *nodeOpts) HeartbeatOptions() HeartbeatOptions {
   133  	return o.hOpts
   134  }
   135  
   136  func (o *nodeOpts) SetOperatorClientFn(fn OperatorClientFn) Options {
   137  	o.opClientFn = fn
   138  	return o
   139  }
   140  
   141  func (o *nodeOpts) OperatorClientFn() OperatorClientFn {
   142  	return o.opClientFn
   143  }