github.com/m3db/m3@v1.5.0/src/cluster/placement/config.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 placement
    22  
    23  import (
    24  	"time"
    25  
    26  	"gopkg.in/yaml.v2"
    27  
    28  	"github.com/m3db/m3/src/cluster/generated/proto/placementpb"
    29  	"github.com/m3db/m3/src/cluster/kv"
    30  	"github.com/m3db/m3/src/x/instrument"
    31  )
    32  
    33  // Configuration is configuration for placement options.
    34  type Configuration struct {
    35  	AllowPartialReplace *bool           `yaml:"allowPartialReplace"`
    36  	AllowAllZones       *bool           `yaml:"allowAllZones"`
    37  	AddAllCandidates    *bool           `yaml:"addAllCandidates"`
    38  	IsSharded           *bool           `yaml:"isSharded"`
    39  	ShardStateMode      *ShardStateMode `yaml:"shardStateMode"`
    40  	IsMirrored          *bool           `yaml:"isMirrored"`
    41  	SkipPortMirroring   *bool           `yaml:"skipPortMirroring"`
    42  	IsStaged            *bool           `yaml:"isStaged"`
    43  	ValidZone           *string         `yaml:"validZone"`
    44  }
    45  
    46  // NewOptions creates a placement options.
    47  func (c *Configuration) NewOptions() Options {
    48  	opts := NewOptions()
    49  	if value := c.AllowPartialReplace; value != nil {
    50  		opts = opts.SetAllowPartialReplace(*value)
    51  	}
    52  	if value := c.AllowAllZones; value != nil {
    53  		opts = opts.SetAllowAllZones(*value)
    54  	}
    55  	if value := c.AddAllCandidates; value != nil {
    56  		opts = opts.SetAddAllCandidates(*value)
    57  	}
    58  	if value := c.IsSharded; value != nil {
    59  		opts = opts.SetIsSharded(*value)
    60  	}
    61  	if value := c.ShardStateMode; value != nil {
    62  		opts = opts.SetShardStateMode(*value)
    63  	}
    64  	if value := c.IsMirrored; value != nil {
    65  		opts = opts.SetIsMirrored(*value)
    66  	}
    67  	if value := c.SkipPortMirroring; value != nil {
    68  		opts = opts.SetSkipPortMirroring(*value)
    69  	}
    70  	if value := c.IsStaged; value != nil {
    71  		opts = opts.SetIsStaged(*value)
    72  	}
    73  	if value := c.ValidZone; value != nil {
    74  		opts = opts.SetValidZone(*value)
    75  	}
    76  	return opts
    77  }
    78  
    79  // DeepCopy makes a deep copy of the configuration.
    80  func (c Configuration) DeepCopy() (Configuration, error) {
    81  	b, err := yaml.Marshal(c)
    82  	if err != nil {
    83  		return Configuration{}, err
    84  	}
    85  	var res Configuration
    86  	if err := yaml.Unmarshal(b, &res); err != nil {
    87  		return Configuration{}, err
    88  	}
    89  	return res, nil
    90  }
    91  
    92  // ApplyOverride applys the override values.
    93  func (c Configuration) ApplyOverride(opts *placementpb.Options) Configuration {
    94  	if opts == nil {
    95  		return c
    96  	}
    97  	if opts.IsSharded != nil {
    98  		isShardedValueCopy := opts.IsSharded.Value
    99  		c.IsSharded = &isShardedValueCopy
   100  	}
   101  	if opts.SkipPortMirroring != nil {
   102  		skipPortMirroringCopy := opts.SkipPortMirroring.Value
   103  		c.SkipPortMirroring = &skipPortMirroringCopy
   104  	}
   105  	return c
   106  }
   107  
   108  // WatcherConfiguration contains placement watcher configuration.
   109  type WatcherConfiguration struct {
   110  	// Placement key.
   111  	Key string `yaml:"key" validate:"nonzero"`
   112  
   113  	// Initial watch timeout.
   114  	InitWatchTimeout time.Duration `yaml:"initWatchTimeout"`
   115  }
   116  
   117  // NewOptions creates a placement watcher option.
   118  func (c *WatcherConfiguration) NewOptions(
   119  	store kv.Store,
   120  	instrumentOpts instrument.Options,
   121  ) WatcherOptions {
   122  	opts := NewWatcherOptions().
   123  		SetInstrumentOptions(instrumentOpts).
   124  		SetStagedPlacementKey(c.Key).
   125  		SetStagedPlacementStore(store)
   126  	if c.InitWatchTimeout != 0 {
   127  		opts = opts.SetInitWatchTimeout(c.InitWatchTimeout)
   128  	}
   129  	return opts
   130  }