github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/namespace/namespace_runtime_options.go (about) 1 // Copyright (c) 2020 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 "sync" 25 26 xresource "github.com/m3db/m3/src/x/resource" 27 "github.com/m3db/m3/src/x/watch" 28 ) 29 30 const ( 31 defaultWriteIndexingPerCPUConcurrency = 0.75 32 defaultFlushIndexingPerCPUConcurrency = 0.25 33 ) 34 35 // RuntimeOptions is a set of runtime options that can 36 // be set per namespace. 37 type RuntimeOptions interface { 38 // IsDefault returns whether the runtime options are purely defaults 39 // with no values explicitly set. 40 IsDefault() bool 41 42 // Equal will return whether it's equal to another runtime options. 43 Equal(other RuntimeOptions) bool 44 45 // SetWriteIndexingPerCPUConcurrency sets the write 46 // indexing per CPU concurrency. 47 SetWriteIndexingPerCPUConcurrency(value *float64) RuntimeOptions 48 49 // WriteIndexingPerCPUConcurrency returns the write 50 // indexing per CPU concurrency. 51 WriteIndexingPerCPUConcurrency() *float64 52 53 // WriteIndexingPerCPUConcurrencyOrDefault returns the write 54 // indexing per CPU concurrency or default. 55 WriteIndexingPerCPUConcurrencyOrDefault() float64 56 57 // SetFlushIndexingPerCPUConcurrency sets the flush 58 // indexing per CPU concurrency. 59 SetFlushIndexingPerCPUConcurrency(value *float64) RuntimeOptions 60 61 // FlushIndexingPerCPUConcurrency returns the flush 62 // indexing per CPU concurrency. 63 FlushIndexingPerCPUConcurrency() *float64 64 65 // FlushIndexingPerCPUConcurrencyOrDefault returns the flush 66 // indexing per CPU concurrency. 67 FlushIndexingPerCPUConcurrencyOrDefault() float64 68 } 69 70 // RuntimeOptionsManagerRegistry is a registry of runtime options managers. 71 type RuntimeOptionsManagerRegistry interface { 72 // RuntimeOptionsManager returns a namespace runtime options manager 73 // for the given namespace. 74 RuntimeOptionsManager(namespace string) RuntimeOptionsManager 75 76 // Close closes the watcher and all descendent watches. 77 Close() 78 } 79 80 // RuntimeOptionsManager is a runtime options manager. 81 type RuntimeOptionsManager interface { 82 // Update updates the current runtime options. 83 Update(value RuntimeOptions) error 84 85 // Get returns the current values. 86 Get() RuntimeOptions 87 88 // RegisterListener registers a listener for updates to runtime options, 89 // it will synchronously call back the listener when this method is called 90 // to deliver the current set of runtime options. 91 RegisterListener(l RuntimeOptionsListener) xresource.SimpleCloser 92 93 // Close closes the watcher and all descendent watches. 94 Close() 95 } 96 97 // RuntimeOptionsListener listens for updates to runtime options. 98 type RuntimeOptionsListener interface { 99 // SetNamespaceRuntimeOptions is called when the listener is registered 100 // and when any updates occurred passing the new runtime options. 101 SetNamespaceRuntimeOptions(value RuntimeOptions) 102 } 103 104 // runtimeOptions should always use pointer value types for it's options 105 // and provide a "ValueOrDefault()" method so that we can be sure whether 106 // the options are all defaults or not with the "IsDefault" method. 107 type runtimeOptions struct { 108 writeIndexingPerCPUConcurrency *float64 109 flushIndexingPerCPUConcurrency *float64 110 } 111 112 // NewRuntimeOptions returns a new namespace runtime options. 113 func NewRuntimeOptions() RuntimeOptions { 114 return newRuntimeOptions() 115 } 116 117 func newRuntimeOptions() *runtimeOptions { 118 return &runtimeOptions{} 119 } 120 121 func (o *runtimeOptions) IsDefault() bool { 122 defaults := newRuntimeOptions() 123 return *o == *defaults 124 } 125 126 func (o *runtimeOptions) Equal(other RuntimeOptions) bool { 127 return o.writeIndexingPerCPUConcurrency == other.WriteIndexingPerCPUConcurrency() && 128 o.flushIndexingPerCPUConcurrency == other.FlushIndexingPerCPUConcurrency() 129 } 130 131 func (o *runtimeOptions) SetWriteIndexingPerCPUConcurrency(value *float64) RuntimeOptions { 132 opts := *o 133 opts.writeIndexingPerCPUConcurrency = value 134 return &opts 135 } 136 137 func (o *runtimeOptions) WriteIndexingPerCPUConcurrency() *float64 { 138 return o.writeIndexingPerCPUConcurrency 139 } 140 141 func (o *runtimeOptions) WriteIndexingPerCPUConcurrencyOrDefault() float64 { 142 value := o.writeIndexingPerCPUConcurrency 143 if value == nil { 144 return defaultWriteIndexingPerCPUConcurrency 145 } 146 return *value 147 } 148 149 func (o *runtimeOptions) SetFlushIndexingPerCPUConcurrency(value *float64) RuntimeOptions { 150 opts := *o 151 opts.flushIndexingPerCPUConcurrency = value 152 return &opts 153 } 154 155 func (o *runtimeOptions) FlushIndexingPerCPUConcurrency() *float64 { 156 return o.flushIndexingPerCPUConcurrency 157 } 158 159 func (o *runtimeOptions) FlushIndexingPerCPUConcurrencyOrDefault() float64 { 160 value := o.flushIndexingPerCPUConcurrency 161 if value == nil { 162 return defaultFlushIndexingPerCPUConcurrency 163 } 164 return *value 165 } 166 167 type runtimeOptionsManagerRegistry struct { 168 sync.RWMutex 169 managers map[string]RuntimeOptionsManager 170 } 171 172 // NewRuntimeOptionsManagerRegistry returns a new runtime options 173 // manager registry. 174 func NewRuntimeOptionsManagerRegistry() RuntimeOptionsManagerRegistry { 175 return &runtimeOptionsManagerRegistry{ 176 managers: make(map[string]RuntimeOptionsManager), 177 } 178 } 179 180 func (r *runtimeOptionsManagerRegistry) RuntimeOptionsManager( 181 namespace string, 182 ) RuntimeOptionsManager { 183 r.Lock() 184 defer r.Unlock() 185 186 manager, ok := r.managers[namespace] 187 if !ok { 188 manager = NewRuntimeOptionsManager(namespace) 189 r.managers[namespace] = manager 190 } 191 192 return manager 193 } 194 195 func (r *runtimeOptionsManagerRegistry) Close() { 196 r.Lock() 197 defer r.Unlock() 198 199 for k, v := range r.managers { 200 v.Close() 201 delete(r.managers, k) 202 } 203 } 204 205 type runtimeOptionsManager struct { 206 namespace string 207 watchable watch.Watchable 208 } 209 210 // NewRuntimeOptionsManager returns a new runtime options manager. 211 func NewRuntimeOptionsManager(namespace string) RuntimeOptionsManager { 212 watchable := watch.NewWatchable() 213 watchable.Update(NewRuntimeOptions()) 214 return &runtimeOptionsManager{ 215 namespace: namespace, 216 watchable: watchable, 217 } 218 } 219 220 func (w *runtimeOptionsManager) Update(value RuntimeOptions) error { 221 w.watchable.Update(value) 222 return nil 223 } 224 225 func (w *runtimeOptionsManager) Get() RuntimeOptions { 226 return w.watchable.Get().(RuntimeOptions) 227 } 228 229 func (w *runtimeOptionsManager) RegisterListener( 230 listener RuntimeOptionsListener, 231 ) xresource.SimpleCloser { 232 _, watch, _ := w.watchable.Watch() 233 234 // We always initialize the watchable so always read 235 // the first notification value 236 <-watch.C() 237 238 // Deliver the current runtime options 239 listener.SetNamespaceRuntimeOptions(watch.Get().(RuntimeOptions)) 240 241 // Spawn a new goroutine that will terminate when the 242 // watchable terminates on the close of the runtime options manager 243 go func() { 244 for range watch.C() { 245 listener.SetNamespaceRuntimeOptions(watch.Get().(RuntimeOptions)) 246 } 247 }() 248 249 return watch 250 } 251 252 func (w *runtimeOptionsManager) Close() { 253 w.watchable.Close() 254 }