github.com/m3db/m3@v1.5.0/src/dbnode/storage/block/options.go (about) 1 // Copyright (c) 2016 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 block 22 23 import ( 24 "github.com/m3db/m3/src/dbnode/encoding" 25 "github.com/m3db/m3/src/dbnode/encoding/m3tsz" 26 "github.com/m3db/m3/src/dbnode/namespace" 27 "github.com/m3db/m3/src/dbnode/ts" 28 "github.com/m3db/m3/src/dbnode/x/xio" 29 "github.com/m3db/m3/src/x/clock" 30 "github.com/m3db/m3/src/x/context" 31 "github.com/m3db/m3/src/x/instrument" 32 "github.com/m3db/m3/src/x/pool" 33 xsync "github.com/m3db/m3/src/x/sync" 34 ) 35 36 const ( 37 // defaultDatabaseBlockAllocSize is the size to allocate for values for each 38 // database block, this should match the size of expected encoded values per 39 // block size. 40 defaultDatabaseBlockAllocSize = 1024 41 42 // defaultCloseContextConcurrency is the default concurrency for closing 43 // the context the block depends on 44 defaultCloseContextConcurrency = 4096 45 ) 46 47 type options struct { 48 clockOpts clock.Options 49 databaseBlockAllocSize int 50 closeContextWorkers xsync.WorkerPool 51 databaseBlockPool DatabaseBlockPool 52 contextPool context.Pool 53 encoderPool encoding.EncoderPool 54 segmentReaderPool xio.SegmentReaderPool 55 bytesPool pool.CheckedBytesPool 56 readerIteratorPool encoding.ReaderIteratorPool 57 multiReaderIteratorPool encoding.MultiReaderIteratorPool 58 wiredList *WiredList 59 } 60 61 // NewOptions creates new database block options 62 func NewOptions() Options { 63 iOpts := instrument.NewOptions() 64 bytesPool := pool.NewCheckedBytesPool(nil, nil, func(s []pool.Bucket) pool.BytesPool { 65 return pool.NewBytesPool(s, nil) 66 }) 67 encoderPool := encoding.NewEncoderPool(nil) 68 readerIteratorPool := encoding.NewReaderIteratorPool(nil) 69 segmentReaderPool := xio.NewSegmentReaderPool(nil) 70 o := &options{ 71 clockOpts: clock.NewOptions(), 72 databaseBlockAllocSize: defaultDatabaseBlockAllocSize, 73 closeContextWorkers: xsync.NewWorkerPool(defaultCloseContextConcurrency), 74 databaseBlockPool: NewDatabaseBlockPool(nil), 75 contextPool: context.NewPool(context.NewOptions()), 76 encoderPool: encoderPool, 77 readerIteratorPool: readerIteratorPool, 78 multiReaderIteratorPool: encoding.NewMultiReaderIteratorPool(nil), 79 segmentReaderPool: segmentReaderPool, 80 bytesPool: bytesPool, 81 } 82 o.closeContextWorkers.Init() 83 o.databaseBlockPool.Init(func() DatabaseBlock { 84 return NewDatabaseBlock(timeZero, 0, ts.Segment{}, o, namespace.Context{}) 85 }) 86 87 encodingOpts := encoding.NewOptions(). 88 SetBytesPool(bytesPool). 89 SetEncoderPool(encoderPool). 90 SetReaderIteratorPool(readerIteratorPool). 91 SetSegmentReaderPool(segmentReaderPool). 92 SetMetrics(encoding.NewMetrics(iOpts.MetricsScope())) 93 94 o.encoderPool.Init(func() encoding.Encoder { 95 return m3tsz.NewEncoder(timeZero, nil, m3tsz.DefaultIntOptimizationEnabled, encodingOpts) 96 }) 97 o.readerIteratorPool.Init(m3tsz.DefaultReaderIteratorAllocFn(encodingOpts)) 98 o.multiReaderIteratorPool.Init( 99 func(r xio.Reader64, descr namespace.SchemaDescr) encoding.ReaderIterator { 100 it := o.readerIteratorPool.Get() 101 it.Reset(r, descr) 102 return it 103 }) 104 o.segmentReaderPool.Init() 105 o.bytesPool.Init() 106 return o 107 } 108 109 func (o *options) SetClockOptions(value clock.Options) Options { 110 opts := *o 111 opts.clockOpts = value 112 return &opts 113 } 114 115 func (o *options) ClockOptions() clock.Options { 116 return o.clockOpts 117 } 118 119 func (o *options) SetDatabaseBlockAllocSize(value int) Options { 120 opts := *o 121 opts.databaseBlockAllocSize = value 122 return &opts 123 } 124 125 func (o *options) DatabaseBlockAllocSize() int { 126 return o.databaseBlockAllocSize 127 } 128 129 func (o *options) SetCloseContextWorkers(value xsync.WorkerPool) Options { 130 opts := *o 131 opts.closeContextWorkers = value 132 return &opts 133 } 134 135 func (o *options) CloseContextWorkers() xsync.WorkerPool { 136 return o.closeContextWorkers 137 } 138 139 func (o *options) SetDatabaseBlockPool(value DatabaseBlockPool) Options { 140 opts := *o 141 opts.databaseBlockPool = value 142 return &opts 143 } 144 145 func (o *options) DatabaseBlockPool() DatabaseBlockPool { 146 return o.databaseBlockPool 147 } 148 149 func (o *options) SetContextPool(value context.Pool) Options { 150 opts := *o 151 opts.contextPool = value 152 return &opts 153 } 154 155 func (o *options) ContextPool() context.Pool { 156 return o.contextPool 157 } 158 159 func (o *options) SetEncoderPool(value encoding.EncoderPool) Options { 160 opts := *o 161 opts.encoderPool = value 162 return &opts 163 } 164 165 func (o *options) EncoderPool() encoding.EncoderPool { 166 return o.encoderPool 167 } 168 169 func (o *options) SetReaderIteratorPool(value encoding.ReaderIteratorPool) Options { 170 opts := *o 171 opts.readerIteratorPool = value 172 return &opts 173 } 174 175 func (o *options) ReaderIteratorPool() encoding.ReaderIteratorPool { 176 return o.readerIteratorPool 177 } 178 179 func (o *options) SetMultiReaderIteratorPool(value encoding.MultiReaderIteratorPool) Options { 180 opts := *o 181 opts.multiReaderIteratorPool = value 182 return &opts 183 } 184 185 func (o *options) MultiReaderIteratorPool() encoding.MultiReaderIteratorPool { 186 return o.multiReaderIteratorPool 187 } 188 189 func (o *options) SetSegmentReaderPool(value xio.SegmentReaderPool) Options { 190 opts := *o 191 opts.segmentReaderPool = value 192 return &opts 193 } 194 195 func (o *options) SegmentReaderPool() xio.SegmentReaderPool { 196 return o.segmentReaderPool 197 } 198 199 func (o *options) SetBytesPool(value pool.CheckedBytesPool) Options { 200 opts := *o 201 opts.bytesPool = value 202 return &opts 203 } 204 205 func (o *options) BytesPool() pool.CheckedBytesPool { 206 return o.bytesPool 207 } 208 209 func (o *options) SetWiredList(value *WiredList) Options { 210 opts := *o 211 opts.wiredList = value 212 return &opts 213 } 214 215 func (o *options) WiredList() *WiredList { 216 return o.wiredList 217 }