github.com/m3db/m3@v1.5.0/src/dbnode/namespace/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 namespace
    22  
    23  import (
    24  	"errors"
    25  
    26  	"github.com/m3db/m3/src/dbnode/retention"
    27  )
    28  
    29  const (
    30  	// Namespace requires bootstrapping by default.
    31  	defaultBootstrapEnabled = true
    32  
    33  	// Namespace requires flushing by default.
    34  	defaultFlushEnabled = true
    35  
    36  	// Namespace requires snapshotting disabled by default.
    37  	defaultSnapshotEnabled = true
    38  
    39  	// Namespace writes go to commit logs by default.
    40  	defaultWritesToCommitLog = true
    41  
    42  	// Namespace requires fileset/snapshot cleanup by default.
    43  	defaultCleanupEnabled = true
    44  
    45  	// Namespace requires repair disabled by default.
    46  	defaultRepairEnabled = false
    47  
    48  	// Namespace with cold writes disabled by default.
    49  	defaultColdWritesEnabled = false
    50  
    51  	// Namespace does not cache retrieved blocks by default since this is only
    52  	// useful specifically for usage patterns tending towards heavy historical reads.
    53  	defaultCacheBlocksOnRetrieve = false
    54  )
    55  
    56  var (
    57  	errIndexBlockSizePositive                       = errors.New("index block size must positive")
    58  	errIndexBlockSizeTooLarge                       = errors.New("index block size needs to be <= namespace retention period")
    59  	errIndexBlockSizeMustBeAMultipleOfDataBlockSize = errors.New("index block size must be a multiple of data block size")
    60  	errNamespaceRuntimeOptionsNotSet                = errors.New("namespace runtime options is not set")
    61  	errAggregationOptionsNotSet                     = errors.New("aggregation options is not set")
    62  )
    63  
    64  type options struct {
    65  	bootstrapEnabled      bool
    66  	flushEnabled          bool
    67  	snapshotEnabled       bool
    68  	writesToCommitLog     bool
    69  	cleanupEnabled        bool
    70  	repairEnabled         bool
    71  	coldWritesEnabled     bool
    72  	cacheBlocksOnRetrieve bool
    73  	retentionOpts         retention.Options
    74  	indexOpts             IndexOptions
    75  	schemaHis             SchemaHistory
    76  	runtimeOpts           RuntimeOptions
    77  	extendedOpts          ExtendedOptions
    78  	aggregationOpts       AggregationOptions
    79  	stagingState          StagingState
    80  }
    81  
    82  // NewSchemaHistory returns an empty schema history.
    83  func NewSchemaHistory() SchemaHistory {
    84  	return emptySchemaHistory()
    85  }
    86  
    87  // NewOptions creates a new namespace options
    88  func NewOptions() Options {
    89  	return &options{
    90  		bootstrapEnabled:      defaultBootstrapEnabled,
    91  		flushEnabled:          defaultFlushEnabled,
    92  		snapshotEnabled:       defaultSnapshotEnabled,
    93  		writesToCommitLog:     defaultWritesToCommitLog,
    94  		cleanupEnabled:        defaultCleanupEnabled,
    95  		repairEnabled:         defaultRepairEnabled,
    96  		coldWritesEnabled:     defaultColdWritesEnabled,
    97  		cacheBlocksOnRetrieve: defaultCacheBlocksOnRetrieve,
    98  		retentionOpts:         retention.NewOptions(),
    99  		indexOpts:             NewIndexOptions(),
   100  		schemaHis:             NewSchemaHistory(),
   101  		runtimeOpts:           NewRuntimeOptions(),
   102  		aggregationOpts:       NewAggregationOptions(),
   103  	}
   104  }
   105  
   106  func (o *options) Validate() error {
   107  	if err := o.retentionOpts.Validate(); err != nil {
   108  		return err
   109  	}
   110  
   111  	if o.extendedOpts != nil {
   112  		if err := o.extendedOpts.Validate(); err != nil {
   113  			return err
   114  		}
   115  	}
   116  
   117  	if err := o.stagingState.Validate(); err != nil {
   118  		return err
   119  	}
   120  
   121  	if !o.indexOpts.Enabled() {
   122  		return nil
   123  	}
   124  	var (
   125  		retention       = o.retentionOpts.RetentionPeriod()
   126  		futureRetention = o.retentionOpts.FutureRetentionPeriod()
   127  		dataBlockSize   = o.retentionOpts.BlockSize()
   128  		indexBlockSize  = o.indexOpts.BlockSize()
   129  	)
   130  	if indexBlockSize <= 0 {
   131  		return errIndexBlockSizePositive
   132  	}
   133  	if retention < indexBlockSize || (futureRetention != 0 && futureRetention < indexBlockSize) {
   134  		return errIndexBlockSizeTooLarge
   135  	}
   136  	if indexBlockSize%dataBlockSize != 0 {
   137  		return errIndexBlockSizeMustBeAMultipleOfDataBlockSize
   138  	}
   139  	if o.runtimeOpts == nil {
   140  		return errNamespaceRuntimeOptionsNotSet
   141  	}
   142  	if o.aggregationOpts == nil {
   143  		return errAggregationOptionsNotSet
   144  	}
   145  
   146  	return nil
   147  }
   148  
   149  func (o *options) Equal(value Options) bool {
   150  	return o.bootstrapEnabled == value.BootstrapEnabled() &&
   151  		o.flushEnabled == value.FlushEnabled() &&
   152  		o.writesToCommitLog == value.WritesToCommitLog() &&
   153  		o.snapshotEnabled == value.SnapshotEnabled() &&
   154  		o.cleanupEnabled == value.CleanupEnabled() &&
   155  		o.repairEnabled == value.RepairEnabled() &&
   156  		o.coldWritesEnabled == value.ColdWritesEnabled() &&
   157  		o.cacheBlocksOnRetrieve == value.CacheBlocksOnRetrieve() &&
   158  		o.retentionOpts.Equal(value.RetentionOptions()) &&
   159  		o.indexOpts.Equal(value.IndexOptions()) &&
   160  		o.schemaHis.Equal(value.SchemaHistory()) &&
   161  		o.runtimeOpts.Equal(value.RuntimeOptions()) &&
   162  		o.aggregationOpts.Equal(value.AggregationOptions()) &&
   163  		o.stagingState == value.StagingState()
   164  }
   165  
   166  func (o *options) SetBootstrapEnabled(value bool) Options {
   167  	opts := *o
   168  	opts.bootstrapEnabled = value
   169  	return &opts
   170  }
   171  
   172  func (o *options) BootstrapEnabled() bool {
   173  	return o.bootstrapEnabled
   174  }
   175  
   176  func (o *options) SetFlushEnabled(value bool) Options {
   177  	opts := *o
   178  	opts.flushEnabled = value
   179  	return &opts
   180  }
   181  
   182  func (o *options) FlushEnabled() bool {
   183  	return o.flushEnabled
   184  }
   185  
   186  func (o *options) SetSnapshotEnabled(value bool) Options {
   187  	opts := *o
   188  	opts.snapshotEnabled = value
   189  	return &opts
   190  }
   191  
   192  func (o *options) SnapshotEnabled() bool {
   193  	return o.snapshotEnabled
   194  }
   195  
   196  func (o *options) SetWritesToCommitLog(value bool) Options {
   197  	opts := *o
   198  	opts.writesToCommitLog = value
   199  	return &opts
   200  }
   201  
   202  func (o *options) WritesToCommitLog() bool {
   203  	return o.writesToCommitLog
   204  }
   205  
   206  func (o *options) SetCleanupEnabled(value bool) Options {
   207  	opts := *o
   208  	opts.cleanupEnabled = value
   209  	return &opts
   210  }
   211  
   212  func (o *options) CleanupEnabled() bool {
   213  	return o.cleanupEnabled
   214  }
   215  
   216  func (o *options) SetRepairEnabled(value bool) Options {
   217  	opts := *o
   218  	opts.repairEnabled = value
   219  	return &opts
   220  }
   221  
   222  func (o *options) RepairEnabled() bool {
   223  	return o.repairEnabled
   224  }
   225  
   226  func (o *options) SetColdWritesEnabled(value bool) Options {
   227  	opts := *o
   228  	opts.coldWritesEnabled = value
   229  	return &opts
   230  }
   231  
   232  func (o *options) ColdWritesEnabled() bool {
   233  	return o.coldWritesEnabled
   234  }
   235  
   236  func (o *options) SetCacheBlocksOnRetrieve(value bool) Options {
   237  	opts := *o
   238  	opts.cacheBlocksOnRetrieve = value
   239  	return &opts
   240  }
   241  
   242  func (o *options) CacheBlocksOnRetrieve() bool {
   243  	return o.cacheBlocksOnRetrieve
   244  }
   245  
   246  func (o *options) SetRetentionOptions(value retention.Options) Options {
   247  	opts := *o
   248  	opts.retentionOpts = value
   249  	return &opts
   250  }
   251  
   252  func (o *options) RetentionOptions() retention.Options {
   253  	return o.retentionOpts
   254  }
   255  
   256  func (o *options) SetIndexOptions(value IndexOptions) Options {
   257  	opts := *o
   258  	opts.indexOpts = value
   259  	return &opts
   260  }
   261  
   262  func (o *options) IndexOptions() IndexOptions {
   263  	return o.indexOpts
   264  }
   265  
   266  func (o *options) SetSchemaHistory(value SchemaHistory) Options {
   267  	opts := *o
   268  	opts.schemaHis = value
   269  	return &opts
   270  }
   271  
   272  func (o *options) SchemaHistory() SchemaHistory {
   273  	return o.schemaHis
   274  }
   275  
   276  func (o *options) SetRuntimeOptions(value RuntimeOptions) Options {
   277  	opts := *o
   278  	opts.runtimeOpts = value
   279  	return &opts
   280  }
   281  
   282  func (o *options) RuntimeOptions() RuntimeOptions {
   283  	return o.runtimeOpts
   284  }
   285  
   286  func (o *options) SetExtendedOptions(value ExtendedOptions) Options {
   287  	opts := *o
   288  	opts.extendedOpts = value
   289  	return &opts
   290  }
   291  
   292  func (o *options) ExtendedOptions() ExtendedOptions {
   293  	return o.extendedOpts
   294  }
   295  
   296  func (o *options) SetAggregationOptions(value AggregationOptions) Options {
   297  	opts := *o
   298  	opts.aggregationOpts = value
   299  	return &opts
   300  }
   301  
   302  func (o *options) AggregationOptions() AggregationOptions {
   303  	return o.aggregationOpts
   304  }
   305  
   306  func (o *options) SetStagingState(value StagingState) Options {
   307  	opts := *o
   308  	opts.stagingState = value
   309  	return &opts
   310  }
   311  
   312  func (o *options) StagingState() StagingState {
   313  	return o.stagingState
   314  }