Skip to content

Commit bf48872

Browse files
committed
block: hide cache hits/misses from the level checker
We introduce a "reason" for using a `BufferPool`. We use this reason to determine what category to pass to the cache. We introduce a special `CategoryHidden` which doesn't update any metrics; we use this for the level checker. Since the level checker is a testing facility only, it only adds noise to the metrics.
1 parent 0c63ea8 commit bf48872

22 files changed

+95
-61
lines changed

blob_rewrite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func (d *DB) runBlobFileRewriteLocked(
307307
// the compaction finishes new iterators will read the new blob file, so it
308308
// would be unlikely the cached blocks would be reused.
309309
var bufferPool block.BufferPool
310-
bufferPool.Init(4)
310+
bufferPool.Init(4, block.ForBlobFileRewrite)
311311
defer bufferPool.Release()
312312
env := block.ReadEnv{
313313
Stats: &c.internalIteratorStats,

blob_rewrite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ func TestBlobRewriteRandomized(t *testing.T) {
334334
fch := fc.newHandle(ch, objStore, base.NoopLoggerAndTracer{}, sstable.ReaderOptions{}, nil)
335335
defer fch.Close()
336336
var bufferPool block.BufferPool
337-
bufferPool.Init(4)
337+
bufferPool.Init(4, block.ForBlobFileRewrite)
338338
defer bufferPool.Release()
339339
readEnv := block.ReadEnv{BufferPool: &bufferPool}
340340

compaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3379,7 +3379,7 @@ func (d *DB) compactAndWrite(
33793379
// a 18-buffer pool is expected to be within reason, even if all the buffers
33803380
// grow to the typical size of an index block (256 KiB) which would
33813381
// translate to 4.5 MiB per compaction.
3382-
c.iterationState.bufferPool.Init(18 + suggestedCacheReaders*2)
3382+
c.iterationState.bufferPool.Init(18+suggestedCacheReaders*2, block.ForCompaction)
33833383
defer c.iterationState.bufferPool.Release()
33843384
blockReadEnv := block.ReadEnv{
33853385
BufferPool: &c.iterationState.bufferPool,

external_iterator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func NewExternalIterWithContext(
9292
},
9393
seqNum: base.SeqNumMax,
9494
}
95-
dbi.externalIter.bufferPool.Init(2)
95+
dbi.externalIter.bufferPool.Init(2, block.ForExternalIter)
9696

9797
if iterOpts != nil {
9898
dbi.opts = *iterOpts

internal/cache/cache.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,17 @@ func (c *Handle) Cache() *Cache {
249249
// Peek retrieves the cache value for the specified file and offset, returning
250250
// nil if no value is present. Peek does not affect the state of the cache (it
251251
// does not "count" as an access as far as the cache replacement is concerned).
252+
//
253+
// Peek supports the special CategoryHidden category, in which case the hit or
254+
// miss is not recorded in metrics.
252255
func (c *Handle) Peek(fileNum base.DiskFileNum, offset uint64, category Category) *Value {
253256
k := makeKey(c.id, fileNum, offset)
254257
return c.cache.getShard(k).get(k, category, true /* peekOnly */)
255258
}
256259

260+
// CategoryHidden can be used with Peek to avoid recording a cache hit or miss.
261+
const CategoryHidden Category = -1
262+
257263
// Get retrieves the cache value for the specified file and offset, returning
258264
// nil if no value is present.
259265
func (c *Handle) Get(fileNum base.DiskFileNum, offset uint64, category Category) *Value {

internal/cache/clockpro.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,16 @@ func (c *shard) get(k key, category Category, peekOnly bool) *Value {
147147
e.referenced.Store(true)
148148
}
149149
c.mu.RUnlock()
150-
c.counters[category].hits.Add(1)
150+
if category != CategoryHidden {
151+
c.counters[category].hits.Add(1)
152+
}
151153
return value
152154
}
153155
}
154156
c.mu.RUnlock()
155-
c.counters[category].misses.Add(1)
157+
if category != CategoryHidden {
158+
c.counters[category].misses.Add(1)
159+
}
156160
return nil
157161
}
158162

internal/cache/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
// Category is used to maintain granular cache hit/miss statistics.
14-
type Category uint8
14+
type Category int8
1515

1616
const (
1717
// CategoryBackground is used for cache accesses made by compactions or

level_checker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ func (d *DB) CheckLevels(stats *CheckLevelsStats) error {
541541
seqNum := d.mu.versions.visibleSeqNum.Load()
542542

543543
bufferPool := new(block.BufferPool)
544-
bufferPool.Init(10)
544+
bufferPool.Init(10, block.ForLevelChecking)
545545
defer bufferPool.Release()
546546

547547
checkConfig := &checkConfig{

sstable/blob/blob.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ func (r *FileReader) ReadProperties(ctx context.Context) (FileProperties, error)
666666
// We don't want the property block to go into the block cache, so we use a
667667
// buffer pool.
668668
var bufferPool block.BufferPool
669-
bufferPool.Init(1)
669+
bufferPool.Init(1, block.ForBlobFileMetadata)
670670
defer bufferPool.Release()
671671
b, err := r.r.Read(
672672
ctx, block.ReadEnv{BufferPool: &bufferPool}, nil /* readHandle */, r.footer.propertiesHandle,

sstable/block/block.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,6 @@ type ReadEnv struct {
269269

270270
// BufferPool is not-nil if we read blocks into a buffer pool and not into the
271271
// cache. This is used during compactions and by the level checker.
272-
//
273-
// When BufferPool is non-nil, any block cache accesses use the background
274-
// category.
275272
BufferPool *BufferPool
276273

277274
// ReportCorruptionFn is called with ReportCorruptionArg and the error
@@ -382,7 +379,20 @@ func (r *Reader) Read(
382379
// reading a block.
383380
if r.opts.CacheOpts.CacheHandle == nil || env.BufferPool != nil {
384381
if r.opts.CacheOpts.CacheHandle != nil {
385-
if cv := r.opts.CacheOpts.CacheHandle.Peek(r.opts.CacheOpts.FileNum, bh.Offset, cache.CategoryBackground); cv != nil {
382+
var category cache.Category
383+
switch env.BufferPool.Reason() {
384+
case ForLevelChecking:
385+
category = cache.CategoryHidden
386+
case ForCompaction, ForBlobFileRewrite:
387+
// We don't want background work to obscure the hit/miss metrics for
388+
// "foreground" traffic; and having two variants for each category would
389+
// be overkill, so compaction-related metrics all go into one
390+
// "background" category.
391+
category = cache.CategoryBackground
392+
default:
393+
category = kindToCacheCategory[kind]
394+
}
395+
if cv := r.opts.CacheOpts.CacheHandle.Peek(r.opts.CacheOpts.FileNum, bh.Offset, category); cv != nil {
386396
recordCacheHit(ctx, env, readHandle, bh, kind)
387397
return CacheBufferHandle(cv), nil
388398
}

0 commit comments

Comments
 (0)