github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/storage/series/policy.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 series 22 23 import ( 24 "errors" 25 "fmt" 26 ) 27 28 var ( 29 errCachePolicyUnspecified = errors.New("series cache policy unspecified") 30 ) 31 32 // CachePolicy is the series cache policy. 33 type CachePolicy uint 34 35 const ( 36 // CacheNone specifies that no series will be cached by default. 37 CacheNone CachePolicy = iota 38 // CacheAll specifies that all series must be cached at all times 39 // which requires loading all into cache on bootstrap and never 40 // expiring series from memory until expired from retention. 41 CacheAll 42 // CacheRecentlyRead specifies that series that are recently read 43 // must be cached, configurable by the namespace block expiry after 44 // not accessed period. 45 CacheRecentlyRead 46 // CacheLRU specifies that series that are read will be cached 47 // using an LRU of fixed capacity. Series that are least recently 48 // used will be evicted first. 49 CacheLRU 50 51 // DefaultCachePolicy is the default cache policy. 52 DefaultCachePolicy = CacheLRU 53 ) 54 55 // ValidCachePolicies returns the valid series cache policies. 56 func ValidCachePolicies() []CachePolicy { 57 return []CachePolicy{CacheNone, CacheAll, CacheRecentlyRead, CacheLRU} 58 } 59 60 func (p CachePolicy) String() string { 61 switch p { 62 case CacheNone: 63 return "none" 64 case CacheAll: 65 return "all" 66 case CacheRecentlyRead: 67 return "recently_read" 68 case CacheLRU: 69 return "lru" 70 } 71 return "unknown" 72 } 73 74 // ValidateCachePolicy validates a cache policy. 75 func ValidateCachePolicy(v CachePolicy) error { 76 validSeriesCachePolicy := false 77 for _, valid := range ValidCachePolicies() { 78 if valid == v { 79 validSeriesCachePolicy = true 80 break 81 } 82 } 83 if !validSeriesCachePolicy { 84 return fmt.Errorf("invalid series CachePolicy '%d' valid types are: %v", 85 uint(v), ValidCachePolicies()) 86 } 87 return nil 88 } 89 90 // ParseCachePolicy parses a CachePolicy from a string. 91 func ParseCachePolicy(str string) (CachePolicy, error) { 92 var r CachePolicy 93 if str == "" { 94 return r, errCachePolicyUnspecified 95 } 96 for _, valid := range ValidCachePolicies() { 97 if str == valid.String() { 98 r = valid 99 return r, nil 100 } 101 } 102 return r, fmt.Errorf("invalid series CachePolicy '%s' valid types are: %v", 103 str, ValidCachePolicies()) 104 } 105 106 // MarshalYAML marshals a CachePolicy. 107 func (p CachePolicy) MarshalYAML() (interface{}, error) { 108 return p.String(), nil 109 } 110 111 // UnmarshalYAML unmarshals an CachePolicy into a valid type from string. 112 func (p *CachePolicy) UnmarshalYAML(unmarshal func(interface{}) error) error { 113 var str string 114 if err := unmarshal(&str); err != nil { 115 return err 116 } 117 r, err := ParseCachePolicy(str) 118 if err != nil { 119 return err 120 } 121 *p = r 122 return nil 123 }