github.com/m3db/m3@v1.5.0/src/m3em/cluster/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 cluster
    22  
    23  import (
    24  	"fmt"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/cluster/placement"
    28  	"github.com/m3db/m3/src/m3em/build"
    29  	"github.com/m3db/m3/src/m3em/node"
    30  	"github.com/m3db/m3/src/x/instrument"
    31  	xretry "github.com/m3db/m3/src/x/retry"
    32  )
    33  
    34  const (
    35  	defaultSessionOverride      = false
    36  	defaultReplication          = 3
    37  	defaultConcurrency          = 10
    38  	defaultNumShards            = 1024
    39  	defaultNodeOperationTimeout = 2 * time.Minute
    40  )
    41  
    42  type clusterOpts struct {
    43  	iopts            instrument.Options
    44  	sessionOverride  bool
    45  	token            string
    46  	svcBuild         build.ServiceBuild
    47  	svcConf          build.ServiceConfiguration
    48  	placementSvc     placement.Service
    49  	placementRetrier xretry.Retrier
    50  	replication      int
    51  	numShards        int
    52  	concurrency      int
    53  	nodeOpTimeout    time.Duration
    54  	listener         node.Listener
    55  }
    56  
    57  // NewOptions returns a new Options object
    58  func NewOptions(
    59  	placementSvc placement.Service,
    60  	iopts instrument.Options,
    61  ) Options {
    62  	if iopts == nil {
    63  		iopts = instrument.NewOptions()
    64  	}
    65  	return clusterOpts{
    66  		iopts:            iopts,
    67  		sessionOverride:  defaultSessionOverride,
    68  		replication:      defaultReplication,
    69  		numShards:        defaultNumShards,
    70  		concurrency:      defaultConcurrency,
    71  		nodeOpTimeout:    defaultNodeOperationTimeout,
    72  		placementSvc:     placementSvc,
    73  		placementRetrier: defaultRetrier(),
    74  	}
    75  }
    76  
    77  func defaultRetrier() xretry.Retrier {
    78  	opts := xretry.NewOptions().
    79  		SetForever(true).
    80  		SetMaxBackoff(time.Second).
    81  		SetMaxRetries(10)
    82  	return xretry.NewRetrier(opts)
    83  }
    84  
    85  func (o clusterOpts) Validate() error {
    86  	if o.token == "" {
    87  		return fmt.Errorf("no session token set")
    88  	}
    89  
    90  	if o.svcBuild == nil {
    91  		return fmt.Errorf("ServiceBuild is not set")
    92  	}
    93  
    94  	if o.svcConf == nil {
    95  		return fmt.Errorf("ServiceConf is not set")
    96  	}
    97  
    98  	if o.placementSvc == nil {
    99  		return fmt.Errorf("PlacementService is not set")
   100  	}
   101  
   102  	return nil
   103  }
   104  
   105  func (o clusterOpts) SetInstrumentOptions(iopts instrument.Options) Options {
   106  	o.iopts = iopts
   107  	return o
   108  }
   109  
   110  func (o clusterOpts) InstrumentOptions() instrument.Options {
   111  	return o.iopts
   112  }
   113  
   114  func (o clusterOpts) SetServiceBuild(b build.ServiceBuild) Options {
   115  	o.svcBuild = b
   116  	return o
   117  }
   118  
   119  func (o clusterOpts) ServiceBuild() build.ServiceBuild {
   120  	return o.svcBuild
   121  }
   122  
   123  func (o clusterOpts) SetServiceConfig(c build.ServiceConfiguration) Options {
   124  	o.svcConf = c
   125  	return o
   126  }
   127  
   128  func (o clusterOpts) ServiceConfig() build.ServiceConfiguration {
   129  	return o.svcConf
   130  }
   131  
   132  func (o clusterOpts) SetSessionToken(t string) Options {
   133  	o.token = t
   134  	return o
   135  }
   136  
   137  func (o clusterOpts) SessionToken() string {
   138  	return o.token
   139  }
   140  
   141  func (o clusterOpts) SetSessionOverride(override bool) Options {
   142  	o.sessionOverride = override
   143  	return o
   144  }
   145  
   146  func (o clusterOpts) SessionOverride() bool {
   147  	return o.sessionOverride
   148  }
   149  
   150  func (o clusterOpts) SetReplication(r int) Options {
   151  	o.replication = r
   152  	return o
   153  }
   154  
   155  func (o clusterOpts) Replication() int {
   156  	return o.replication
   157  }
   158  
   159  func (o clusterOpts) SetNumShards(ns int) Options {
   160  	o.numShards = ns
   161  	return o
   162  }
   163  
   164  func (o clusterOpts) NumShards() int {
   165  	return o.numShards
   166  }
   167  
   168  func (o clusterOpts) SetPlacementService(psvc placement.Service) Options {
   169  	o.placementSvc = psvc
   170  	return o
   171  }
   172  
   173  func (o clusterOpts) PlacementService() placement.Service {
   174  	return o.placementSvc
   175  }
   176  
   177  func (o clusterOpts) SetPlacementServiceRetrier(r xretry.Retrier) Options {
   178  	o.placementRetrier = r
   179  	return o
   180  }
   181  
   182  func (o clusterOpts) PlacementServiceRetrier() xretry.Retrier {
   183  	return o.placementRetrier
   184  }
   185  
   186  func (o clusterOpts) SetNodeConcurrency(c int) Options {
   187  	o.concurrency = c
   188  	return o
   189  }
   190  
   191  func (o clusterOpts) NodeConcurrency() int {
   192  	return o.concurrency
   193  }
   194  
   195  func (o clusterOpts) SetNodeOperationTimeout(t time.Duration) Options {
   196  	o.nodeOpTimeout = t
   197  	return o
   198  }
   199  
   200  func (o clusterOpts) NodeOperationTimeout() time.Duration {
   201  	return o.nodeOpTimeout
   202  }
   203  
   204  func (o clusterOpts) SetNodeListener(l node.Listener) Options {
   205  	o.listener = l
   206  	return o
   207  }
   208  
   209  func (o clusterOpts) NodeListener() node.Listener {
   210  	return o.listener
   211  }