github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/cmd/services/m3coordinator/downsample/async_downsampler.go (about) 1 // Copyright (c) 2018 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 downsample 22 23 import ( 24 "errors" 25 "fmt" 26 "sync" 27 28 "github.com/m3db/m3/src/metrics/rules/view" 29 ) 30 31 const ( 32 errNewDownsamplerFailFmt = "downsampler failed to initialize: %v" 33 ) 34 35 var ( 36 errDownsamplerUninitialized = errors.New("downsampler is not yet initialized") 37 ) 38 39 // asyncDownsampler is an asynchronous downsampler that can be lazily 40 // initialized, it will return errors on calls to its methods until it is 41 // initialized. 42 // This is useful when a component needs to be built with a downsampler that 43 // might not be able to wait for to it be constructed (i.e. HTTP handlers 44 // need to be registered immediately but can respond with errors until 45 // the downsampler downstream services have been discovered/connected to). 46 type asyncDownsampler struct { 47 sync.RWMutex 48 downsampler Downsampler 49 done chan<- struct{} 50 err error 51 } 52 53 // NewDownsamplerFn creates a downsampler. 54 type NewDownsamplerFn func() (Downsampler, error) 55 56 // NewAsyncDownsampler is a downsampler that is lazily initialized. 57 func NewAsyncDownsampler( 58 fn NewDownsamplerFn, 59 done chan<- struct{}, 60 ) Downsampler { 61 asyncDownsampler := &asyncDownsampler{ 62 done: done, 63 err: errDownsamplerUninitialized, 64 } 65 66 go func() { 67 asyncDownsampler.Lock() 68 defer asyncDownsampler.Unlock() 69 70 if asyncDownsampler.done != nil { 71 defer func() { 72 asyncDownsampler.done <- struct{}{} 73 }() 74 } 75 downsampler, err := fn() 76 77 if err != nil { 78 asyncDownsampler.err = fmt.Errorf(errNewDownsamplerFailFmt, err) 79 return 80 } 81 82 asyncDownsampler.downsampler = downsampler 83 asyncDownsampler.err = nil 84 }() 85 86 return asyncDownsampler 87 } 88 89 func (d *asyncDownsampler) LatestRollupRules(namespace []byte, timeNanos int64) ([]view.RollupRule, error) { 90 d.RLock() 91 defer d.RUnlock() 92 return d.downsampler.LatestRollupRules(namespace, timeNanos) 93 } 94 95 func (d *asyncDownsampler) NewMetricsAppender() (MetricsAppender, error) { 96 d.RLock() 97 defer d.RUnlock() 98 if d.err != nil { 99 return nil, d.err 100 } 101 return d.downsampler.NewMetricsAppender() 102 } 103 104 func (d *asyncDownsampler) Enabled() bool { 105 d.RLock() 106 defer d.RUnlock() 107 if d.err != nil { 108 return false 109 } 110 return d.downsampler.Enabled() 111 }