github.com/m3db/m3@v1.5.0/src/dbnode/topology/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 topology
    22  
    23  import (
    24  	"errors"
    25  	"fmt"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/cluster/client"
    29  	"github.com/m3db/m3/src/cluster/services"
    30  	"github.com/m3db/m3/src/dbnode/sharding"
    31  	"github.com/m3db/m3/src/x/instrument"
    32  )
    33  
    34  const (
    35  	defaultServiceName = "m3db"
    36  	defaultInitTimeout = 0 // Wait indefinitely by default for topology
    37  	defaultReplicas    = 3
    38  )
    39  
    40  var (
    41  	errNoConfigServiceClient = errors.New("no config service client")
    42  	errNoHashGen             = errors.New("no hash gen function defined")
    43  	errInvalidReplicas       = errors.New("replicas must be equal to or greater than 1")
    44  )
    45  
    46  type staticOptions struct {
    47  	shardSet      sharding.ShardSet
    48  	replicas      int
    49  	hostShardSets []HostShardSet
    50  }
    51  
    52  // NewStaticOptions creates a new set of static topology options
    53  func NewStaticOptions() StaticOptions {
    54  	return &staticOptions{
    55  		replicas: defaultReplicas,
    56  	}
    57  }
    58  
    59  func (o *staticOptions) Validate() error {
    60  	if o.replicas < 1 {
    61  		return errInvalidReplicas
    62  	}
    63  
    64  	// Make a mapping of each shard to a set of hosts and check each
    65  	// shard has at least the required replicas mapped to
    66  	// NB(r): We allow greater than the required replicas in case
    67  	// node is streaming in and needs to take writes
    68  	totalShards := len(o.shardSet.AllIDs())
    69  	hostAddressesByShard := make([]map[string]struct{}, totalShards)
    70  	for i := range hostAddressesByShard {
    71  		hostAddressesByShard[i] = make(map[string]struct{}, o.replicas)
    72  	}
    73  	for _, hostShardSet := range o.hostShardSets {
    74  		hostAddress := hostShardSet.Host().Address()
    75  		for _, shard := range hostShardSet.ShardSet().AllIDs() {
    76  			hostAddressesByShard[shard][hostAddress] = struct{}{}
    77  		}
    78  	}
    79  	for shard, hosts := range hostAddressesByShard {
    80  		if len(hosts) < o.replicas {
    81  			errorFmt := "shard %d has %d replicas, less than the required %d replicas"
    82  			return fmt.Errorf(errorFmt, shard, len(hosts), o.replicas)
    83  		}
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  func (o *staticOptions) SetShardSet(value sharding.ShardSet) StaticOptions {
    90  	opts := *o
    91  	opts.shardSet = value
    92  	return &opts
    93  }
    94  
    95  func (o *staticOptions) ShardSet() sharding.ShardSet {
    96  	return o.shardSet
    97  }
    98  
    99  func (o *staticOptions) SetReplicas(value int) StaticOptions {
   100  	opts := *o
   101  	opts.replicas = value
   102  	return &opts
   103  }
   104  
   105  func (o *staticOptions) Replicas() int {
   106  	return o.replicas
   107  }
   108  
   109  func (o *staticOptions) SetHostShardSets(value []HostShardSet) StaticOptions {
   110  	opts := *o
   111  	opts.hostShardSets = value
   112  	return &opts
   113  }
   114  
   115  func (o *staticOptions) HostShardSets() []HostShardSet {
   116  	return o.hostShardSets
   117  }
   118  
   119  type dynamicOptions struct {
   120  	configServiceClient     client.Client
   121  	serviceID               services.ServiceID
   122  	servicesOverrideOptions services.OverrideOptions
   123  	queryOptions            services.QueryOptions
   124  	instrumentOptions       instrument.Options
   125  	initTimeout             time.Duration
   126  	hashGen                 sharding.HashGen
   127  }
   128  
   129  // NewDynamicOptions creates a new set of dynamic topology options
   130  func NewDynamicOptions() DynamicOptions {
   131  	return &dynamicOptions{
   132  		serviceID:               services.NewServiceID().SetName(defaultServiceName),
   133  		servicesOverrideOptions: services.NewOverrideOptions(),
   134  		queryOptions:            services.NewQueryOptions(),
   135  		instrumentOptions:       instrument.NewOptions(),
   136  		initTimeout:             defaultInitTimeout,
   137  		hashGen:                 sharding.DefaultHashFn,
   138  	}
   139  }
   140  
   141  func (o *dynamicOptions) Validate() error {
   142  	if o.ConfigServiceClient() == nil {
   143  		return errNoConfigServiceClient
   144  	}
   145  
   146  	if o.HashGen() == nil {
   147  		return errNoHashGen
   148  	}
   149  	return nil
   150  }
   151  
   152  func (o *dynamicOptions) SetConfigServiceClient(c client.Client) DynamicOptions {
   153  	o.configServiceClient = c
   154  	return o
   155  }
   156  
   157  func (o *dynamicOptions) ConfigServiceClient() client.Client {
   158  	return o.configServiceClient
   159  }
   160  
   161  func (o *dynamicOptions) SetServiceID(s services.ServiceID) DynamicOptions {
   162  	o.serviceID = s
   163  	return o
   164  }
   165  
   166  func (o *dynamicOptions) ServiceID() services.ServiceID {
   167  	return o.serviceID
   168  }
   169  
   170  func (o *dynamicOptions) SetServicesOverrideOptions(opts services.OverrideOptions) DynamicOptions {
   171  	o.servicesOverrideOptions = opts
   172  	return o
   173  }
   174  
   175  func (o *dynamicOptions) ServicesOverrideOptions() services.OverrideOptions {
   176  	return o.servicesOverrideOptions
   177  }
   178  
   179  func (o *dynamicOptions) SetQueryOptions(qo services.QueryOptions) DynamicOptions {
   180  	o.queryOptions = qo
   181  	return o
   182  }
   183  
   184  func (o *dynamicOptions) QueryOptions() services.QueryOptions {
   185  	return o.queryOptions
   186  }
   187  
   188  func (o *dynamicOptions) SetInstrumentOptions(io instrument.Options) DynamicOptions {
   189  	o.instrumentOptions = io
   190  	return o
   191  }
   192  
   193  func (o *dynamicOptions) InstrumentOptions() instrument.Options {
   194  	return o.instrumentOptions
   195  }
   196  
   197  func (o *dynamicOptions) SetHashGen(h sharding.HashGen) DynamicOptions {
   198  	o.hashGen = h
   199  	return o
   200  }
   201  
   202  func (o *dynamicOptions) HashGen() sharding.HashGen {
   203  	return o.hashGen
   204  }