github.com/m3db/m3@v1.5.0/src/cluster/services/options.go (about)

     1  // Copyright (c) 2016 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 services
    22  
    23  import (
    24  	"errors"
    25  	"os"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/x/instrument"
    29  )
    30  
    31  const (
    32  	defaultInitTimeout   = 5 * time.Second
    33  	defaultLeaderTimeout = 10 * time.Second
    34  	defaultResignTimeout = 10 * time.Second
    35  )
    36  
    37  var (
    38  	errNoKVGen            = errors.New("no KVGen function set")
    39  	errNoHeartbeatGen     = errors.New("no HeartbeatGen function set")
    40  	errNoLeaderGen        = errors.New("no LeaderGen function set")
    41  	errInvalidInitTimeout = errors.New("negative init timeout for service watch")
    42  )
    43  
    44  type options struct {
    45  	initTimeout time.Duration
    46  	nOpts       NamespaceOptions
    47  	kvGen       KVGen
    48  	hbGen       HeartbeatGen
    49  	ldGen       LeaderGen
    50  	iopts       instrument.Options
    51  }
    52  
    53  // NewOptions creates an Option
    54  func NewOptions() Options {
    55  	return options{
    56  		iopts:       instrument.NewOptions(),
    57  		nOpts:       NewNamespaceOptions(),
    58  		initTimeout: defaultInitTimeout,
    59  	}
    60  }
    61  
    62  func (o options) Validate() error {
    63  	if o.kvGen == nil {
    64  		return errNoKVGen
    65  	}
    66  
    67  	if o.hbGen == nil {
    68  		return errNoHeartbeatGen
    69  	}
    70  
    71  	if o.ldGen == nil {
    72  		return errNoLeaderGen
    73  	}
    74  
    75  	if o.initTimeout < 0 {
    76  		return errInvalidInitTimeout
    77  	}
    78  
    79  	return nil
    80  }
    81  
    82  func (o options) InitTimeout() time.Duration {
    83  	return o.initTimeout
    84  }
    85  
    86  func (o options) SetInitTimeout(t time.Duration) Options {
    87  	o.initTimeout = t
    88  	return o
    89  }
    90  
    91  func (o options) KVGen() KVGen {
    92  	return o.kvGen
    93  }
    94  
    95  func (o options) SetKVGen(gen KVGen) Options {
    96  	o.kvGen = gen
    97  	return o
    98  }
    99  
   100  func (o options) HeartbeatGen() HeartbeatGen {
   101  	return o.hbGen
   102  }
   103  
   104  func (o options) SetHeartbeatGen(gen HeartbeatGen) Options {
   105  	o.hbGen = gen
   106  	return o
   107  }
   108  
   109  func (o options) LeaderGen() LeaderGen {
   110  	return o.ldGen
   111  }
   112  
   113  func (o options) SetLeaderGen(lg LeaderGen) Options {
   114  	o.ldGen = lg
   115  	return o
   116  }
   117  
   118  func (o options) InstrumentsOptions() instrument.Options {
   119  	return o.iopts
   120  }
   121  
   122  func (o options) SetInstrumentsOptions(iopts instrument.Options) Options {
   123  	o.iopts = iopts
   124  	return o
   125  }
   126  
   127  func (o options) NamespaceOptions() NamespaceOptions {
   128  	return o.nOpts
   129  }
   130  
   131  func (o options) SetNamespaceOptions(opts NamespaceOptions) Options {
   132  	o.nOpts = opts
   133  	return o
   134  }
   135  
   136  // NewElectionOptions returns an empty ElectionOptions.
   137  func NewElectionOptions() ElectionOptions {
   138  	eo := electionOpts{
   139  		leaderTimeout: defaultLeaderTimeout,
   140  		resignTimeout: defaultResignTimeout,
   141  	}
   142  
   143  	return eo
   144  }
   145  
   146  type electionOpts struct {
   147  	leaderTimeout time.Duration
   148  	resignTimeout time.Duration
   149  	ttlSecs       int
   150  }
   151  
   152  func (e electionOpts) LeaderTimeout() time.Duration {
   153  	return e.leaderTimeout
   154  }
   155  
   156  func (e electionOpts) SetLeaderTimeout(t time.Duration) ElectionOptions {
   157  	e.leaderTimeout = t
   158  	return e
   159  }
   160  
   161  func (e electionOpts) ResignTimeout() time.Duration {
   162  	return e.resignTimeout
   163  }
   164  
   165  func (e electionOpts) SetResignTimeout(t time.Duration) ElectionOptions {
   166  	e.resignTimeout = t
   167  	return e
   168  }
   169  
   170  func (e electionOpts) TTLSecs() int {
   171  	return e.ttlSecs
   172  }
   173  
   174  func (e electionOpts) SetTTLSecs(ttl int) ElectionOptions {
   175  	e.ttlSecs = ttl
   176  	return e
   177  }
   178  
   179  type campaignOpts struct {
   180  	val string
   181  }
   182  
   183  // NewCampaignOptions returns an empty CampaignOptions.
   184  func NewCampaignOptions() (CampaignOptions, error) {
   185  	h, err := os.Hostname()
   186  	if err != nil {
   187  		return nil, err
   188  	}
   189  
   190  	return campaignOpts{val: h}, nil
   191  }
   192  
   193  func (c campaignOpts) LeaderValue() string {
   194  	return c.val
   195  }
   196  
   197  func (c campaignOpts) SetLeaderValue(v string) CampaignOptions {
   198  	c.val = v
   199  	return c
   200  }
   201  
   202  type overrideOptions struct {
   203  	namespaceOpts NamespaceOptions
   204  }
   205  
   206  // NewOverrideOptions constructs a new OverrideOptions.
   207  func NewOverrideOptions() OverrideOptions {
   208  	return &overrideOptions{
   209  		namespaceOpts: NewNamespaceOptions(),
   210  	}
   211  }
   212  
   213  func (o overrideOptions) NamespaceOptions() NamespaceOptions {
   214  	return o.namespaceOpts
   215  }
   216  
   217  func (o overrideOptions) SetNamespaceOptions(opts NamespaceOptions) OverrideOptions {
   218  	o.namespaceOpts = opts
   219  	return o
   220  }
   221  
   222  type namespaceOpts struct {
   223  	placement string
   224  	metadata  string
   225  }
   226  
   227  // NewNamespaceOptions constructs a new NamespaceOptions.
   228  func NewNamespaceOptions() NamespaceOptions {
   229  	return &namespaceOpts{}
   230  }
   231  
   232  func (opts namespaceOpts) PlacementNamespace() string {
   233  	return opts.placement
   234  }
   235  
   236  func (opts namespaceOpts) SetPlacementNamespace(v string) NamespaceOptions {
   237  	opts.placement = v
   238  	return opts
   239  }
   240  
   241  func (opts namespaceOpts) MetadataNamespace() string {
   242  	return opts.metadata
   243  }
   244  
   245  func (opts namespaceOpts) SetMetadataNamespace(v string) NamespaceOptions {
   246  	opts.metadata = v
   247  	return opts
   248  }
   249  
   250  // NewQueryOptions creates new QueryOptions.
   251  func NewQueryOptions() QueryOptions { return new(queryOptions) }
   252  
   253  type queryOptions struct {
   254  	includeUnhealthy bool
   255  	interruptedCh    <-chan struct{}
   256  }
   257  
   258  func (qo *queryOptions) IncludeUnhealthy() bool                  { return qo.includeUnhealthy }
   259  func (qo *queryOptions) SetIncludeUnhealthy(h bool) QueryOptions { qo.includeUnhealthy = h; return qo }
   260  
   261  func (qo *queryOptions) InterruptedCh() <-chan struct{} {
   262  	return qo.interruptedCh
   263  }
   264  
   265  func (qo *queryOptions) SetInterruptedCh(ch <-chan struct{}) QueryOptions {
   266  	qo.interruptedCh = ch
   267  	return qo
   268  }