Skip to content

Commit 36ac1d0

Browse files
authored
CBG-4883: decrement stats when removing stale value from cache list in add to lookup map functions (#7779)
1 parent fefbd63 commit 36ac1d0

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

db/revision_cache_lru.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (rc *LRURevisionCache) getFromCacheByRev(ctx context.Context, docID, revID
222222
// given err is nil if we get to this code we can safely assign error returned from addToHLVMapPostLoad to err
223223
// and in the event we do error adding to the HLV map post load we will remove the value from the rev cache below
224224
// and return the error to the caller
225-
err = rc.addToHLVMapPostLoad(docID, docRev.RevID, docRev.CV, collectionID)
225+
err = rc.addToHLVMapPostLoad(ctx, docID, docRev.RevID, docRev.CV, collectionID)
226226
if err != nil {
227227
base.WarnfCtx(ctx, "Error adding to HLV map post load in getFromCacheByRev: %v", err)
228228
}
@@ -253,7 +253,7 @@ func (rc *LRURevisionCache) getFromCacheByCV(ctx context.Context, docID string,
253253
rc.incrRevCacheMemoryUsage(ctx, value.getItemBytes())
254254
// check for memory based eviction
255255
rc.revCacheMemoryBasedEviction(ctx)
256-
rc.addToRevMapPostLoad(docID, docRev.RevID, docRev.CV, collectionID)
256+
rc.addToRevMapPostLoad(ctx, docID, docRev.RevID, docRev.CV, collectionID)
257257
}
258258

259259
return docRev, err
@@ -291,7 +291,7 @@ func (rc *LRURevisionCache) GetActive(ctx context.Context, docID string, collect
291291
// given err is nil if we get to this code we can safely assign error returned from addToHLVMapPostLoad to err
292292
// and in the event we do error adding to the HLV map post load we will remove the value from the rev cache below
293293
// and return the error to the caller
294-
err = rc.addToHLVMapPostLoad(docID, docRev.RevID, docRev.CV, collectionID)
294+
err = rc.addToHLVMapPostLoad(ctx, docID, docRev.RevID, docRev.CV, collectionID)
295295
if err != nil {
296296
base.WarnfCtx(ctx, "Error adding to HLV map post load in GetActive: %v", err)
297297
}
@@ -329,7 +329,7 @@ func (rc *LRURevisionCache) Put(ctx context.Context, docRev DocumentRevision, co
329329
value.store(docRev)
330330

331331
// add new doc version to the rev id lookup map
332-
rc.addToRevMapPostLoad(docRev.DocID, docRev.RevID, docRev.CV, collectionID)
332+
rc.addToRevMapPostLoad(ctx, docRev.DocID, docRev.RevID, docRev.CV, collectionID)
333333

334334
// check for rev cache memory based eviction
335335
rc.revCacheMemoryBasedEviction(ctx)
@@ -457,7 +457,7 @@ func (rc *LRURevisionCache) getValueByCV(ctx context.Context, docID string, cv *
457457
}
458458

459459
// addToRevMapPostLoad will generate and entry in the Rev lookup map for a new document entering the cache
460-
func (rc *LRURevisionCache) addToRevMapPostLoad(docID, revID string, cv *Version, collectionID uint32) {
460+
func (rc *LRURevisionCache) addToRevMapPostLoad(ctx context.Context, docID, revID string, cv *Version, collectionID uint32) {
461461
legacyKey := IDAndRev{DocID: docID, RevID: revID, CollectionID: collectionID}
462462
key := IDandCV{DocID: docID, Source: cv.SourceID, Version: cv.Value, CollectionID: collectionID}
463463

@@ -479,6 +479,8 @@ func (rc *LRURevisionCache) addToRevMapPostLoad(docID, revID string, cv *Version
479479
}
480480
// if CV map and rev map are targeting different list elements, update to have both use the cv map element
481481
rc.cache[legacyKey] = cvElem
482+
rc._decrRevCacheMemoryUsage(ctx, -revElem.Value.(*revCacheValue).getItemBytes())
483+
rc.cacheNumItems.Add(-1)
482484
rc.lruList.Remove(revElem)
483485
} else {
484486
// if not found we need to add the element to the rev lookup (for PUT code path)
@@ -487,7 +489,7 @@ func (rc *LRURevisionCache) addToRevMapPostLoad(docID, revID string, cv *Version
487489
}
488490

489491
// addToHLVMapPostLoad will generate and entry in the CV lookup map for a new document entering the cache
490-
func (rc *LRURevisionCache) addToHLVMapPostLoad(docID, revID string, cv *Version, collectionID uint32) error {
492+
func (rc *LRURevisionCache) addToHLVMapPostLoad(ctx context.Context, docID, revID string, cv *Version, collectionID uint32) error {
491493
legacyKey := IDAndRev{DocID: docID, RevID: revID, CollectionID: collectionID}
492494

493495
if cv == nil {
@@ -519,6 +521,8 @@ func (rc *LRURevisionCache) addToHLVMapPostLoad(docID, revID string, cv *Version
519521
}
520522
// if CV map and rev map are targeting different list elements, update to have both use the cv map element
521523
rc.cache[legacyKey] = cvElem
524+
rc._decrRevCacheMemoryUsage(ctx, -revElem.Value.(*revCacheValue).getItemBytes())
525+
rc.cacheNumItems.Add(-1)
522526
rc.lruList.Remove(revElem)
523527
} else {
524528
// if not found we need to add the element to the hlv lookup

db/revision_cache_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,6 +2290,33 @@ func TestRemoveFromRevLookup(t *testing.T) {
22902290
assert.Equal(t, 10, len(cache.hlvCache))
22912291
}
22922292

2293+
func TestIncorrectStatsWhenAddingToLookupMapKeyThatExists(t *testing.T) {
2294+
cacheHitCounter, cacheMissCounter, cacheNumItems, memoryBytesCounted := base.SgwIntStat{}, base.SgwIntStat{}, base.SgwIntStat{}, base.SgwIntStat{}
2295+
backingStoreMap := CreateTestSingleBackingStoreMap(&noopBackingStore{}, testCollectionID)
2296+
cacheOptions := &RevisionCacheOptions{
2297+
MaxItemCount: 20,
2298+
MaxBytes: 0,
2299+
}
2300+
cache := NewLRURevisionCache(cacheOptions, backingStoreMap, &cacheHitCounter, &cacheMissCounter, &cacheNumItems, &memoryBytesCounted)
2301+
2302+
// NOTE: the below should never happen really in normal processing at the cache but test is forcing a scenario to
2303+
// force a code path
2304+
ctx := base.TestCtx(t)
2305+
// add entry, remove from cv lookup then add same entry again (but diff body)
2306+
cache.Put(ctx, DocumentRevision{BodyBytes: []byte(`{"some":"data"}`), DocID: "doc1", RevID: "1-abc", CV: &Version{Value: 123, SourceID: "test"}, History: Revisions{"start": 1}}, testCollectionID)
2307+
cache.RemoveCVOnly(ctx, "doc1", &Version{Value: 123, SourceID: "test"}, testCollectionID)
2308+
// adding with different body but same revID, docID and CV will cause the addToRevMapPostLoad function to pick up
2309+
// that the revID entry for this key points to a different element and thus it will remove the old element from the cache
2310+
cache.Put(ctx, DocumentRevision{BodyBytes: []byte(`{"some":"data1000"}`), DocID: "doc1", RevID: "1-abc", CV: &Version{Value: 123, SourceID: "test"}, History: Revisions{"start": 1}}, testCollectionID)
2311+
2312+
// assert that the stats are correct
2313+
assert.Equal(t, 1, cache.lruList.Len())
2314+
assert.Equal(t, 1, len(cache.cache))
2315+
assert.Equal(t, 1, len(cache.hlvCache))
2316+
assert.Equal(t, int64(1), cacheNumItems.Value())
2317+
assert.Equal(t, int64(19), memoryBytesCounted.Value())
2318+
}
2319+
22932320
func TestLoadFromBucketLegacyRevsThatAreBackedUpPreUpgrade(t *testing.T) {
22942321
db, ctx := SetupTestDBWithOptions(t, DatabaseContextOptions{
22952322
OldRevExpirySeconds: base.DefaultOldRevExpirySeconds,

0 commit comments

Comments
 (0)