-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathnamespace_readers.go
374 lines (320 loc) · 10.1 KB
/
namespace_readers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package storage
import (
"sync"
"time"
"github.com/m3db/m3/src/dbnode/persist/fs"
"github.com/m3db/m3/src/dbnode/storage/namespace"
"github.com/m3db/m3x/ident"
xlog "github.com/m3db/m3x/log"
"github.com/m3db/m3x/pool"
xtime "github.com/m3db/m3x/time"
"github.com/uber-go/tally"
)
// namespaceReaderManager maintains a pool of closed readers which can be
// re-used (to prevent additional allocations), as well as a cache of recently
// used open readers based on their position. The cache of recently used open
// readers is useful during peer bootstrapping because a pageToken (which
// contains an offset into the reader for both the data and metadata portions
// of the fileset) is used to communicate the clients current position to the
// server.
// In the general case, the client will miss on its first request for a given
// shard/block start, and then experience a cache hit on every subsequent
// request because the current client implementation does not perform any
// parallel requests for a single shard.
// The closedReaders pool is modeled as a stack (implemented via slice
// operations) and the open readers cache is implemented as a map where the
// key is of type cachedOpenReaderKey.
// The namespaceReaderManager also implements a tick() method which should
// be called regularly in order to shrunk the closedReaders stack after bursts
// of usage, as well as to expire cached open readers which have not been used
// for a configurable number of ticks.
const (
expireCachedReadersAfterNumTicks = 2
)
type databaseNamespaceReaderManager interface {
filesetExistsAt(
shard uint32,
blockStart time.Time,
) (bool, error)
get(
shard uint32,
blockStart time.Time,
position readerPosition,
) (fs.DataFileSetReader, error)
put(reader fs.DataFileSetReader)
tick()
close()
}
type fsFileSetExistsAtFn func(
prefix string,
namespace ident.ID,
shard uint32,
blockStart time.Time,
) (bool, error)
type fsNewReaderFn func(
bytesPool pool.CheckedBytesPool,
opts fs.Options,
) (fs.DataFileSetReader, error)
type namespaceReaderManager struct {
sync.Mutex
filesetExistsAtFn fsFileSetExistsAtFn
newReaderFn fsNewReaderFn
namespace namespace.Metadata
fsOpts fs.Options
bytesPool pool.CheckedBytesPool
logger xlog.Logger
closedReaders []cachedReader
openReaders map[cachedOpenReaderKey]cachedReader
metrics namespaceReaderManagerMetrics
}
type cachedOpenReaderKey struct {
shard uint32
blockStart xtime.UnixNano
position readerPosition
}
type readerPosition struct {
dataIdx int
metadataIdx int
}
type cachedReader struct {
reader fs.DataFileSetReader
ticksSinceUsed int
}
type namespaceReaderManagerMetrics struct {
cacheHit tally.Counter
cacheMissAllocReader tally.Counter
cacheMissReusedReader tally.Counter
}
func newNamespaceReaderManagerMetrics(
scope tally.Scope,
) namespaceReaderManagerMetrics {
subScope := scope.SubScope("reader-cache")
return namespaceReaderManagerMetrics{
cacheHit: subScope.Counter("hit"),
cacheMissAllocReader: subScope.Tagged(map[string]string{
"miss_type": "alloc_reader",
}).Counter("miss"),
cacheMissReusedReader: subScope.Tagged(map[string]string{
"miss_type": "reuse_reader",
}).Counter("miss"),
}
}
func newNamespaceReaderManager(
namespace namespace.Metadata,
namespaceScope tally.Scope,
opts Options,
) databaseNamespaceReaderManager {
return &namespaceReaderManager{
filesetExistsAtFn: fs.DataFileSetExistsAt,
newReaderFn: fs.NewReader,
namespace: namespace,
fsOpts: opts.CommitLogOptions().FilesystemOptions(),
bytesPool: opts.BytesPool(),
logger: opts.InstrumentOptions().Logger(),
openReaders: make(map[cachedOpenReaderKey]cachedReader),
metrics: newNamespaceReaderManagerMetrics(namespaceScope),
}
}
func (m *namespaceReaderManager) filesetExistsAt(
shard uint32,
blockStart time.Time,
) (bool, error) {
return m.filesetExistsAtFn(m.fsOpts.FilePathPrefix(),
m.namespace.ID(), shard, blockStart)
}
type cachedReaderForKeyResult struct {
openReader fs.DataFileSetReader
closedReader fs.DataFileSetReader
}
func (m *namespaceReaderManager) pushClosedReaderWithLock(
reader fs.DataFileSetReader,
) {
m.closedReaders = append(m.closedReaders, cachedReader{
reader: reader,
})
}
func (m *namespaceReaderManager) popClosedReaderWithLock() fs.DataFileSetReader {
idx := len(m.closedReaders) - 1
reader := m.closedReaders[idx].reader
// Zero refs from element in slice and shrink slice
m.closedReaders[idx] = cachedReader{}
m.closedReaders = m.closedReaders[:idx]
return reader
}
func (m *namespaceReaderManager) cachedReaderForKey(
key cachedOpenReaderKey,
) (cachedReaderForKeyResult, error) {
m.Lock()
defer m.Unlock()
openReader, ok := m.openReaders[key]
if ok {
// Cache hit, take this open reader
delete(m.openReaders, key)
m.metrics.cacheHit.Inc(1)
return cachedReaderForKeyResult{
openReader: openReader.reader,
}, nil
}
// Cache miss, need to return a reused reader or open a new reader
if len(m.closedReaders) > 0 {
reader := m.popClosedReaderWithLock()
m.metrics.cacheMissReusedReader.Inc(1)
return cachedReaderForKeyResult{
closedReader: reader,
}, nil
}
reader, err := m.newReaderFn(m.bytesPool, m.fsOpts)
if err != nil {
return cachedReaderForKeyResult{}, err
}
m.metrics.cacheMissAllocReader.Inc(1)
return cachedReaderForKeyResult{
closedReader: reader,
}, nil
}
func (m *namespaceReaderManager) get(
shard uint32,
blockStart time.Time,
position readerPosition,
) (fs.DataFileSetReader, error) {
key := cachedOpenReaderKey{
shard: shard,
blockStart: xtime.ToUnixNano(blockStart),
position: position,
}
lookup, err := m.cachedReaderForKey(key)
if err != nil {
return nil, err
}
if reader := lookup.openReader; reader != nil {
return reader, nil // Found an open reader for the position
}
// We have a closed reader from the cache (either a cached closed
// reader or newly allocated, either way need to prepare it)
reader := lookup.closedReader
openOpts := fs.DataReaderOpenOptions{
Identifier: fs.FileSetFileIdentifier{
Namespace: m.namespace.ID(),
Shard: shard,
BlockStart: blockStart,
},
}
if err := reader.Open(openOpts); err != nil {
return nil, err
}
// We can validate metadata immediately since its read when opened
if err := reader.ValidateMetadata(); err != nil {
return nil, err
}
// Fast fwd through if in the middle of a volume
for i := 0; i < position.dataIdx; i++ {
id, tags, data, _, err := reader.Read()
if err != nil {
return nil, err
}
id.Finalize()
tags.Close()
data.Finalize()
}
for i := 0; i < position.metadataIdx; i++ {
id, tags, _, _, err := reader.ReadMetadata()
if err != nil {
return nil, err
}
id.Finalize()
tags.Close()
}
return reader, nil
}
func (m *namespaceReaderManager) put(reader fs.DataFileSetReader) {
status := reader.Status()
m.Lock()
defer m.Unlock()
if !status.Open {
m.pushClosedReaderWithLock(reader)
return
}
key := cachedOpenReaderKey{
shard: status.Shard,
blockStart: xtime.ToUnixNano(status.BlockStart),
position: readerPosition{
dataIdx: reader.EntriesRead(),
metadataIdx: reader.MetadataRead(),
},
}
if _, ok := m.openReaders[key]; ok {
// Unlikely, however if so just close the reader we were trying to put
// and put into the closed readers
if err := reader.Close(); err != nil {
m.logger.Errorf("error closing reader on put from reader cache: %v", err)
return
}
m.pushClosedReaderWithLock(reader)
return
}
m.openReaders[key] = cachedReader{reader: reader}
}
func (m *namespaceReaderManager) tick() {
m.tickWithThreshold(expireCachedReadersAfterNumTicks)
}
func (m *namespaceReaderManager) close() {
// Perform a tick but make the threshold zero so all readers must be expired
m.tickWithThreshold(0)
}
func (m *namespaceReaderManager) tickWithThreshold(threshold int) {
m.Lock()
defer m.Unlock()
// First increment ticks since used for closed readers
expiredClosedReaders := 0
for i := range m.closedReaders {
m.closedReaders[i].ticksSinceUsed++
if m.closedReaders[i].ticksSinceUsed >= threshold {
expiredClosedReaders++
}
}
// Expire any closed readers, alloc a new slice to avoid spikes
// of use creating slices that are never released
if expired := expiredClosedReaders; expired > 0 {
newClosedReaders := make([]cachedReader, 0, len(m.closedReaders)-expired)
for _, elem := range m.closedReaders {
if elem.ticksSinceUsed < threshold {
newClosedReaders = append(newClosedReaders, elem)
}
}
m.closedReaders = newClosedReaders
}
// For open readers calculate and expire from map directly
for key, elem := range m.openReaders {
// Mutate the for-loop copy in place before checking the threshold
elem.ticksSinceUsed++
if elem.ticksSinceUsed >= threshold {
// Close before removing ref
if err := elem.reader.Close(); err != nil {
m.logger.Errorf("error closing reader from reader cache: %v", err)
}
delete(m.openReaders, key)
continue
}
// Save the mutated copy back to the map
m.openReaders[key] = elem
}
}