github.com/m3db/m3@v1.5.0/src/dbnode/retention/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 retention
    22  
    23  import (
    24  	"errors"
    25  	"time"
    26  )
    27  
    28  const (
    29  	// defaultRetentionPeriod is how long we keep data in memory by default.
    30  	defaultRetentionPeriod = 2 * 24 * time.Hour
    31  
    32  	// defaultFutureRetentionPeriod is how long we keep data in memory in the
    33  	// future (if cold writes are enabled) by default. If this default is used,
    34  	// M3DB will not accept any future writes outside of the bufferFuture
    35  	// threshold.
    36  	defaultFutureRetentionPeriod = time.Duration(0)
    37  
    38  	// defaultBlockSize is the default block size
    39  	defaultBlockSize = 2 * time.Hour
    40  
    41  	// defaultBufferFuture is the default buffer future limit
    42  	defaultBufferFuture = 2 * time.Minute
    43  
    44  	// defaultBufferPast is the default buffer past limit
    45  	defaultBufferPast = 10 * time.Minute
    46  
    47  	// defaultDataExpiry is the default bool for whether data expiry is on
    48  	defaultDataExpiry = true
    49  
    50  	// defaultDataExpiryAfterNotAccessedPeriod is the default data expiry after not accessed period
    51  	defaultDataExpiryAfterNotAccessedPeriod = 5 * time.Minute
    52  )
    53  
    54  var (
    55  	errBufferFutureNonNegative = errors.New("buffer future must be non-negative")
    56  	errBufferPastNonNegative   = errors.New("buffer past must be non-negative")
    57  	errBlockSizePositive       = errors.New("block size must positive")
    58  	errBufferFutureTooLarge    = errors.New("buffer future must be smaller than block size")
    59  	errBufferPastTooLarge      = errors.New("buffer past must be smaller than block size")
    60  	errRetentionPeriodTooSmall = errors.New("retention period must not be smaller than block size")
    61  )
    62  
    63  type options struct {
    64  	retentionPeriod                  time.Duration
    65  	futureRetentionPeriod            time.Duration
    66  	blockSize                        time.Duration
    67  	bufferFuture                     time.Duration
    68  	bufferPast                       time.Duration
    69  	dataExpiryAfterNotAccessedPeriod time.Duration
    70  	dataExpiry                       bool
    71  }
    72  
    73  // NewOptions creates new retention options
    74  func NewOptions() Options {
    75  	return &options{
    76  		retentionPeriod:                  defaultRetentionPeriod,
    77  		futureRetentionPeriod:            defaultFutureRetentionPeriod,
    78  		blockSize:                        defaultBlockSize,
    79  		bufferFuture:                     defaultBufferFuture,
    80  		bufferPast:                       defaultBufferPast,
    81  		dataExpiry:                       defaultDataExpiry,
    82  		dataExpiryAfterNotAccessedPeriod: defaultDataExpiryAfterNotAccessedPeriod,
    83  	}
    84  }
    85  
    86  func (o *options) Validate() error {
    87  	if o.bufferFuture < 0 {
    88  		return errBufferFutureNonNegative
    89  	}
    90  	if o.bufferPast < 0 {
    91  		return errBufferPastNonNegative
    92  	}
    93  	if o.blockSize <= 0 {
    94  		return errBlockSizePositive
    95  	}
    96  	if o.bufferFuture >= o.blockSize {
    97  		return errBufferFutureTooLarge
    98  	}
    99  	if o.bufferPast >= o.blockSize {
   100  		return errBufferPastTooLarge
   101  	}
   102  	if o.retentionPeriod < o.blockSize {
   103  		return errRetentionPeriodTooSmall
   104  	}
   105  	return nil
   106  }
   107  
   108  func (o *options) Equal(value Options) bool {
   109  	return o.retentionPeriod == value.RetentionPeriod() &&
   110  		o.futureRetentionPeriod == value.FutureRetentionPeriod() &&
   111  		o.blockSize == value.BlockSize() &&
   112  		o.bufferFuture == value.BufferFuture() &&
   113  		o.bufferPast == value.BufferPast() &&
   114  		o.dataExpiry == value.BlockDataExpiry() &&
   115  		o.dataExpiryAfterNotAccessedPeriod == value.BlockDataExpiryAfterNotAccessedPeriod()
   116  }
   117  
   118  func (o *options) SetRetentionPeriod(value time.Duration) Options {
   119  	opts := *o
   120  	opts.retentionPeriod = value
   121  	return &opts
   122  }
   123  
   124  func (o *options) RetentionPeriod() time.Duration {
   125  	return o.retentionPeriod
   126  }
   127  
   128  func (o *options) SetFutureRetentionPeriod(value time.Duration) Options {
   129  	opts := *o
   130  	opts.futureRetentionPeriod = value
   131  	return &opts
   132  }
   133  
   134  func (o *options) FutureRetentionPeriod() time.Duration {
   135  	return o.futureRetentionPeriod
   136  }
   137  
   138  func (o *options) SetBlockSize(value time.Duration) Options {
   139  	opts := *o
   140  	opts.blockSize = value
   141  	return &opts
   142  }
   143  
   144  func (o *options) BlockSize() time.Duration {
   145  	return o.blockSize
   146  }
   147  
   148  func (o *options) SetBufferFuture(value time.Duration) Options {
   149  	opts := *o
   150  	opts.bufferFuture = value
   151  	return &opts
   152  }
   153  
   154  func (o *options) BufferFuture() time.Duration {
   155  	return o.bufferFuture
   156  }
   157  
   158  func (o *options) SetBufferPast(value time.Duration) Options {
   159  	opts := *o
   160  	opts.bufferPast = value
   161  	return &opts
   162  }
   163  
   164  func (o *options) BufferPast() time.Duration {
   165  	return o.bufferPast
   166  }
   167  
   168  func (o *options) SetBlockDataExpiry(value bool) Options {
   169  	opts := *o
   170  	opts.dataExpiry = value
   171  	return &opts
   172  }
   173  
   174  func (o *options) BlockDataExpiry() bool {
   175  	return o.dataExpiry
   176  }
   177  
   178  func (o *options) SetBlockDataExpiryAfterNotAccessedPeriod(value time.Duration) Options {
   179  	opts := *o
   180  	opts.dataExpiryAfterNotAccessedPeriod = value
   181  	return &opts
   182  }
   183  
   184  func (o *options) BlockDataExpiryAfterNotAccessedPeriod() time.Duration {
   185  	return o.dataExpiryAfterNotAccessedPeriod
   186  }