github.com/m3db/m3@v1.5.0/src/metrics/matcher/cache/options.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 cache 22 23 import ( 24 "time" 25 26 "github.com/m3db/m3/src/metrics/matcher/namespace" 27 "github.com/m3db/m3/src/x/clock" 28 "github.com/m3db/m3/src/x/instrument" 29 ) 30 31 // InvalidationMode is the invalidation mode. 32 type InvalidationMode int 33 34 const ( 35 // InvalidateOne only invalidates a single invalid entry as needed. 36 InvalidateOne InvalidationMode = iota 37 38 // InvalidateAll invalidates all entries as long as one entry is invalid. 39 InvalidateAll 40 ) 41 42 const ( 43 defaultCapacity = 200000 44 defaultFreshDuration = 5 * time.Minute 45 defaultStutterDuration = time.Minute 46 defaultEvictionBatchSize = 1024 47 defaultDeletionBatchSize = 1024 48 defaultInvalidationMode = InvalidateAll 49 ) 50 51 // Options provide a set of cache options. 52 type Options interface { 53 // SetClockOptions sets the clock options. 54 SetClockOptions(value clock.Options) Options 55 56 // ClockOptions returns the clock options. 57 ClockOptions() clock.Options 58 59 // SetInstrumentOptions sets the instrument options. 60 SetInstrumentOptions(value instrument.Options) Options 61 62 // InstrumentOptions returns the instrument options. 63 InstrumentOptions() instrument.Options 64 65 // SetCapacity sets the cache capacity. 66 SetCapacity(value int) Options 67 68 // Capacity returns the cache capacity. 69 Capacity() int 70 71 // SetFreshDuration sets the entry fresh duration. 72 SetFreshDuration(value time.Duration) Options 73 74 // FreshDuration returns the fresh duration. 75 FreshDuration() time.Duration 76 77 // SetStutterDuration sets the entry stutter duration. 78 SetStutterDuration(value time.Duration) Options 79 80 // StutterDuration returns the entry stutter duration. 81 StutterDuration() time.Duration 82 83 // SetEvictionBatchSize sets the eviction batch size. 84 SetEvictionBatchSize(value int) Options 85 86 // EvictionBatchSize returns the eviction batch size. 87 EvictionBatchSize() int 88 89 // SetDeletionBatchSize sets the deletion batch size. 90 SetDeletionBatchSize(value int) Options 91 92 // DeletionBatchSize returns the deletion batch size. 93 DeletionBatchSize() int 94 95 // SetInvalidationMode sets the invalidation mode. 96 SetInvalidationMode(value InvalidationMode) Options 97 98 // InvalidationMode returns the invalidation mode. 99 InvalidationMode() InvalidationMode 100 101 // NamespaceResolver returns the namespace Resolver. 102 NamespaceResolver() namespace.Resolver 103 104 // SetNamespaceResolver set the NamespaceResolver. 105 SetNamespaceResolver(value namespace.Resolver) Options 106 } 107 108 type options struct { 109 clockOpts clock.Options 110 instrumentOpts instrument.Options 111 capacity int 112 freshDuration time.Duration 113 stutterDuration time.Duration 114 evictionBatchSize int 115 deletionBatchSize int 116 invalidationMode InvalidationMode 117 nsResolver namespace.Resolver 118 } 119 120 // NewOptions creates a new set of options. 121 func NewOptions() Options { 122 return &options{ 123 clockOpts: clock.NewOptions(), 124 instrumentOpts: instrument.NewOptions(), 125 capacity: defaultCapacity, 126 freshDuration: defaultFreshDuration, 127 stutterDuration: defaultStutterDuration, 128 evictionBatchSize: defaultEvictionBatchSize, 129 deletionBatchSize: defaultDeletionBatchSize, 130 invalidationMode: defaultInvalidationMode, 131 nsResolver: namespace.Default, 132 } 133 } 134 135 func (o *options) SetClockOptions(value clock.Options) Options { 136 opts := *o 137 opts.clockOpts = value 138 return &opts 139 } 140 141 func (o *options) ClockOptions() clock.Options { 142 return o.clockOpts 143 } 144 145 func (o *options) SetInstrumentOptions(value instrument.Options) Options { 146 opts := *o 147 opts.instrumentOpts = value 148 return &opts 149 } 150 151 func (o *options) InstrumentOptions() instrument.Options { 152 return o.instrumentOpts 153 } 154 155 func (o *options) SetCapacity(value int) Options { 156 opts := *o 157 opts.capacity = value 158 return &opts 159 } 160 161 func (o *options) Capacity() int { 162 return o.capacity 163 } 164 165 func (o *options) SetFreshDuration(value time.Duration) Options { 166 opts := *o 167 opts.freshDuration = value 168 return &opts 169 } 170 171 func (o *options) FreshDuration() time.Duration { 172 return o.freshDuration 173 } 174 175 func (o *options) SetStutterDuration(value time.Duration) Options { 176 opts := *o 177 opts.stutterDuration = value 178 return &opts 179 } 180 181 func (o *options) StutterDuration() time.Duration { 182 return o.stutterDuration 183 } 184 185 func (o *options) SetEvictionBatchSize(value int) Options { 186 opts := *o 187 opts.evictionBatchSize = value 188 return &opts 189 } 190 191 func (o *options) EvictionBatchSize() int { 192 return o.evictionBatchSize 193 } 194 195 func (o *options) SetDeletionBatchSize(value int) Options { 196 opts := *o 197 opts.deletionBatchSize = value 198 return &opts 199 } 200 201 func (o *options) DeletionBatchSize() int { 202 return o.deletionBatchSize 203 } 204 205 func (o *options) SetInvalidationMode(value InvalidationMode) Options { 206 opts := *o 207 opts.invalidationMode = value 208 return &opts 209 } 210 211 func (o *options) InvalidationMode() InvalidationMode { 212 return o.invalidationMode 213 } 214 215 func (o *options) NamespaceResolver() namespace.Resolver { 216 return o.nsResolver 217 } 218 219 func (o *options) SetNamespaceResolver(value namespace.Resolver) Options { 220 opts := *o 221 opts.nsResolver = value 222 return &opts 223 }