-
Notifications
You must be signed in to change notification settings - Fork 451
/
namespace_readers.go
512 lines (435 loc) · 14.3 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// 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"
"github.com/m3db/m3/src/dbnode/namespace"
"github.com/m3db/m3/src/dbnode/persist/fs"
"github.com/m3db/m3/src/dbnode/sharding"
"github.com/m3db/m3/src/dbnode/storage/block"
"github.com/m3db/m3/src/x/ident"
"github.com/m3db/m3/src/x/pool"
xtime "github.com/m3db/m3/src/x/time"
"github.com/uber-go/tally"
"go.uber.org/zap"
)
// 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 xtime.UnixNano,
) (bool, error)
get(
shard uint32,
blockStart xtime.UnixNano,
position readerPosition,
) (fs.DataFileSetReader, error)
put(reader fs.DataFileSetReader) error
latestVolume(shard uint32, blockStart xtime.UnixNano) (int, error)
assignShardSet(shardSet sharding.ShardSet)
tick()
close()
}
type fsFileSetExistsFn func(
prefix string,
namespace ident.ID,
shard uint32,
blockStart xtime.UnixNano,
volume int,
) (bool, error)
type fsNewReaderFn func(
bytesPool pool.CheckedBytesPool,
opts fs.Options,
) (fs.DataFileSetReader, error)
type namespaceReaderManager struct {
sync.Mutex
filesetExistsFn fsFileSetExistsFn
newReaderFn fsNewReaderFn
namespace namespace.Metadata
fsOpts fs.Options
blockLeaseManager block.LeaseManager
bytesPool pool.CheckedBytesPool
logger *zap.Logger
closedReaders []cachedReader
openReaders map[cachedOpenReaderKey]cachedReader
shardSet sharding.ShardSet
metrics namespaceReaderManagerMetrics
}
type cachedOpenReaderKey struct {
shard uint32
blockStart xtime.UnixNano
position readerPosition
}
type readerPosition struct {
volume int
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 {
blm := opts.BlockLeaseManager()
mgr := &namespaceReaderManager{
filesetExistsFn: fs.DataFileSetExists,
newReaderFn: fs.NewReader,
namespace: namespace,
fsOpts: opts.CommitLogOptions().FilesystemOptions(),
blockLeaseManager: blm,
bytesPool: opts.BytesPool(),
logger: opts.InstrumentOptions().Logger(),
openReaders: make(map[cachedOpenReaderKey]cachedReader),
shardSet: sharding.NewEmptyShardSet(sharding.DefaultHashFn(1)),
metrics: newNamespaceReaderManagerMetrics(namespaceScope),
}
blm.RegisterLeaser(mgr)
return mgr
}
func (m *namespaceReaderManager) latestVolume(
shard uint32,
blockStart xtime.UnixNano,
) (int, error) {
state, err := m.blockLeaseManager.OpenLatestLease(m, block.LeaseDescriptor{
Namespace: m.namespace.ID(),
Shard: shard,
BlockStart: blockStart,
})
if err != nil {
return -1, err
}
return state.Volume, nil
}
func (m *namespaceReaderManager) filesetExistsAt(
shard uint32,
blockStart xtime.UnixNano,
) (bool, error) {
latestVolume, err := m.latestVolume(shard, blockStart)
if err != nil {
return false, err
}
return m.filesetExistsFn(m.fsOpts.FilePathPrefix(),
m.namespace.ID(), shard, blockStart, latestVolume)
}
func (m *namespaceReaderManager) assignShardSet(shardSet sharding.ShardSet) {
m.Lock()
defer m.Unlock()
m.shardSet = shardSet
}
func (m *namespaceReaderManager) shardExistsWithLock(shard uint32) bool {
_, err := m.shardSet.LookupStateByID(shard)
// NB(bodu): LookupStateByID returns ErrInvalidShardID when shard
// does not exist in the shard map which means the shard is not available.
return err == nil
}
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 xtime.UnixNano,
position readerPosition,
) (fs.DataFileSetReader, error) {
latestVolume, err := m.latestVolume(shard, blockStart)
if err != nil {
return nil, err
}
// If requesting an outdated volume, we need to start reading again from
// the beginning of the latest volume. The caller knows how to handle
// duplicate metadata, so doing this is okay.
//
// The previously cached reader for the outdated volume will eventually be
// cleaned up either during the ticking process or the next time
// UpdateOpenLease gets called, so we don't need to worry about closing it
// here.
if position.volume < latestVolume {
position.volume = latestVolume
position.dataIdx = 0
position.metadataIdx = 0
}
key := cachedOpenReaderKey{
shard: shard,
blockStart: 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,
VolumeIndex: latestVolume,
},
}
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) closeAndPushReaderWithLock(reader fs.DataFileSetReader) error {
if err := reader.Close(); err != nil {
return err
}
m.pushClosedReaderWithLock(reader)
return nil
}
func (m *namespaceReaderManager) put(reader fs.DataFileSetReader) error {
status := reader.Status()
m.Lock()
defer m.Unlock()
if !status.Open {
m.pushClosedReaderWithLock(reader)
return nil
}
shard := status.Shard
latestVolume, err := m.latestVolume(shard, status.BlockStart)
if err != nil {
return err
}
// If the supplied reader is for a stale volume, then it will never be
// reused in its current state. Instead, put it in the closed reader pool
// so that it can be reconfigured to be reopened later.
if latestVolume > status.Volume {
if err := m.closeAndPushReaderWithLock(reader); err != nil {
// Best effort on closing the reader and caching it. If it fails,
// we can always allocate a new reader.
m.logger.Error("error closing reader on put from reader cache", zap.Error(err))
}
return nil
}
key := cachedOpenReaderKey{
shard: shard,
blockStart: status.BlockStart,
position: readerPosition{
volume: status.Volume,
dataIdx: reader.EntriesRead(),
metadataIdx: reader.MetadataRead(),
},
}
if _, ok := m.openReaders[key]; ok {
// There is already an open reader cached for this key. We don't need
// a duplicate one, so close the reader and push to slice of closed
// readers.
if err := m.closeAndPushReaderWithLock(reader); err != nil {
// Best effort on closing the reader and caching it. If it fails,
// we can always allocate a new reader.
m.logger.Error("error closing reader on put from reader cache", zap.Error(err))
}
return nil
}
m.openReaders[key] = cachedReader{reader: reader}
return nil
}
func (m *namespaceReaderManager) tick() {
m.tickWithThreshold(expireCachedReadersAfterNumTicks)
}
func (m *namespaceReaderManager) close() {
m.blockLeaseManager.UnregisterLeaser(m)
// 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 ||
// Also check to see if shard is still available and remove cached readers for
// shards that are no longer available. This ensures cached readers are eventually
// consistent with shard state.
!m.shardExistsWithLock(key.shard) {
// Close before removing ref
if err := elem.reader.Close(); err != nil {
m.logger.Error("error closing reader from reader cache", zap.Error(err))
}
delete(m.openReaders, key)
continue
}
// Save the mutated copy back to the map
m.openReaders[key] = elem
}
}
// UpdateOpenLease() implements block.Leaser.
func (m *namespaceReaderManager) UpdateOpenLease(
descriptor block.LeaseDescriptor,
state block.LeaseState,
) (block.UpdateOpenLeaseResult, error) {
if !m.namespace.ID().Equal(descriptor.Namespace) {
return block.NoOpenLease, nil
}
m.Lock()
defer m.Unlock()
// Close and remove open readers with matching key but lower volume.
for readerKey, cachedReader := range m.openReaders {
if readerKey.shard == descriptor.Shard &&
readerKey.blockStart == descriptor.BlockStart &&
readerKey.position.volume < state.Volume {
delete(m.openReaders, readerKey)
if err := m.closeAndPushReaderWithLock(cachedReader.reader); err != nil {
// Best effort on closing the reader and caching it. If it
// fails, we can always allocate a new reader.
m.logger.Error("error closing reader on put from reader cache", zap.Error(err))
}
}
}
return block.UpdateOpenLease, nil
}