github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/cmd/services/m3dbnode/config/cache.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 config
    22  
    23  import "github.com/m3db/m3/src/dbnode/storage/series"
    24  
    25  var (
    26  	defaultPostingsListCacheSize   = 2 << 15 // ~65k
    27  	defaultPostingsListCacheRegexp = true
    28  	defaultPostingsListCacheTerms  = true
    29  	defaultPostingsListCacheSearch = true
    30  	defaultRegexpCacheSize         = 1024
    31  )
    32  
    33  // CacheConfigurations is the cache configurations.
    34  type CacheConfigurations struct {
    35  	// Series cache policy.
    36  	Series *SeriesCacheConfiguration `yaml:"series"`
    37  
    38  	// PostingsList cache policy.
    39  	PostingsList *PostingsListCacheConfiguration `yaml:"postingsList"`
    40  
    41  	// Regexp cache policy.
    42  	Regexp *RegexpCacheConfiguration `yaml:"regexp"`
    43  }
    44  
    45  // SeriesConfiguration returns the series cache configuration or default
    46  // if none is specified.
    47  func (c CacheConfigurations) SeriesConfiguration() SeriesCacheConfiguration {
    48  	if c.Series == nil {
    49  		// Return default cache configuration
    50  		return SeriesCacheConfiguration{Policy: series.DefaultCachePolicy}
    51  	}
    52  	return *c.Series
    53  }
    54  
    55  // PostingsListConfiguration returns the postings list cache configuration
    56  // or default if none is specified.
    57  func (c CacheConfigurations) PostingsListConfiguration() PostingsListCacheConfiguration {
    58  	if c.PostingsList == nil {
    59  		return PostingsListCacheConfiguration{}
    60  	}
    61  	return *c.PostingsList
    62  }
    63  
    64  // RegexpConfiguration returns the regexp cache configuration or default
    65  // if none is specified.
    66  func (c CacheConfigurations) RegexpConfiguration() RegexpCacheConfiguration {
    67  	if c.Regexp == nil {
    68  		return RegexpCacheConfiguration{}
    69  	}
    70  	return *c.Regexp
    71  }
    72  
    73  // SeriesCacheConfiguration is the series cache configuration.
    74  type SeriesCacheConfiguration struct {
    75  	Policy series.CachePolicy                 `yaml:"policy"`
    76  	LRU    *LRUSeriesCachePolicyConfiguration `yaml:"lru"`
    77  }
    78  
    79  // LRUSeriesCachePolicyConfiguration contains configuration for the LRU
    80  // series caching policy.
    81  type LRUSeriesCachePolicyConfiguration struct {
    82  	MaxBlocks         uint `yaml:"maxBlocks" validate:"nonzero"`
    83  	EventsChannelSize uint `yaml:"eventsChannelSize" validate:"nonzero"`
    84  }
    85  
    86  // PostingsListCacheConfiguration is the postings list cache configuration.
    87  type PostingsListCacheConfiguration struct {
    88  	Size        *int  `yaml:"size"`
    89  	CacheRegexp *bool `yaml:"cacheRegexp"`
    90  	CacheTerms  *bool `yaml:"cacheTerms"`
    91  	CacheSearch *bool `yaml:"cacheSearch"`
    92  }
    93  
    94  // SizeOrDefault returns the provided size or the default value is none is
    95  // provided.
    96  func (p PostingsListCacheConfiguration) SizeOrDefault() int {
    97  	if p.Size == nil {
    98  		return defaultPostingsListCacheSize
    99  	}
   100  
   101  	return *p.Size
   102  }
   103  
   104  // CacheRegexpOrDefault returns the provided cache regexp configuration value
   105  // or the default value is none is provided.
   106  func (p PostingsListCacheConfiguration) CacheRegexpOrDefault() bool {
   107  	if p.CacheRegexp == nil {
   108  		return defaultPostingsListCacheRegexp
   109  	}
   110  
   111  	return *p.CacheRegexp
   112  }
   113  
   114  // CacheTermsOrDefault returns the provided cache terms configuration value
   115  // or the default value is none is provided.
   116  func (p PostingsListCacheConfiguration) CacheTermsOrDefault() bool {
   117  	if p.CacheTerms == nil {
   118  		return defaultPostingsListCacheTerms
   119  	}
   120  
   121  	return *p.CacheTerms
   122  }
   123  
   124  // CacheSearchOrDefault returns the provided cache search configuration value
   125  // or the default value is none is provided.
   126  func (p PostingsListCacheConfiguration) CacheSearchOrDefault() bool {
   127  	if p.CacheSearch == nil {
   128  		return defaultPostingsListCacheSearch
   129  	}
   130  
   131  	return *p.CacheSearch
   132  }
   133  
   134  // RegexpCacheConfiguration is a compiled regexp cache for query regexps.
   135  type RegexpCacheConfiguration struct {
   136  	Size *int `yaml:"size"`
   137  }
   138  
   139  // SizeOrDefault returns the provided size or the default value is none is
   140  // provided.
   141  func (c RegexpCacheConfiguration) SizeOrDefault() int {
   142  	if c.Size == nil {
   143  		return defaultRegexpCacheSize
   144  	}
   145  
   146  	return *c.Size
   147  }