Skip to content

Commit

Permalink
CBG-3352 remove invalidation, just remove data from cache
Browse files Browse the repository at this point in the history
  • Loading branch information
torcolvin committed Sep 5, 2023
1 parent 2689cf8 commit 8152960
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 29 deletions.
1 change: 0 additions & 1 deletion db/revision_cache_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ type DocumentRevision struct {
Delta *RevisionDelta
Deleted bool
Removed bool // True if the revision is a removal.
Invalid bool

_shallowCopyBody Body // an unmarshalled body that can produce shallow copies
}
Expand Down
24 changes: 4 additions & 20 deletions db/revision_cache_lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ type revCacheValue struct {
lock sync.RWMutex // Synchronizes access to this struct
body Body // unmarshalled body (if available)
removed bool // True if revision is a removal
invalid bool // Marks a revision as invalid meaning it won't be used
}

// Creates a revision cache with the given capacity and an optional loader function.
Expand Down Expand Up @@ -147,10 +146,6 @@ func (rc *LRURevisionCache) getFromCache(ctx context.Context, docID, revID strin
return DocumentRevision{}, nil
}

if value.invalid {
return rc.LoadInvalidRevFromBackingStore(ctx, value.key, nil, includeBody, includeDelta)
}

docRev, statEvent, err := value.load(ctx, rc.backingStore, includeBody, includeDelta)
rc.statsRecorderFunc(statEvent)

Expand All @@ -167,8 +162,7 @@ func (rc *LRURevisionCache) LoadInvalidRevFromBackingStore(ctx context.Context,
var docRevBody Body

value := revCacheValue{
key: key,
invalid: true,
key: key,
}

// If doc has been passed in use this to grab values. Otherwise run revCacheLoader which will grab the Document
Expand Down Expand Up @@ -216,10 +210,6 @@ func (rc *LRURevisionCache) GetActive(ctx context.Context, docID string, include
// Retrieve from or add to rev cache
value := rc.getValue(docID, bucketDoc.CurrentRev, true)

if value.invalid {
return rc.LoadInvalidRevFromBackingStore(ctx, value.key, bucketDoc, includeBody, false)
}

docRev, statEvent, err := value.loadForDoc(ctx, rc.backingStore, bucketDoc, includeBody)
rc.statsRecorderFunc(statEvent)

Expand Down Expand Up @@ -273,9 +263,10 @@ func (rc *LRURevisionCache) Upsert(ctx context.Context, docRev DocumentRevision)

func (rc *LRURevisionCache) Invalidate(ctx context.Context, docID, revID string) {
value := rc.getValue(docID, revID, false)
if value != nil {
value.setInvalidFlag()
if value == nil {
return
}
rc.removeValue(value)
}

func (rc *LRURevisionCache) getValue(docID, revID string, create bool) (value *revCacheValue) {
Expand Down Expand Up @@ -403,7 +394,6 @@ func (value *revCacheValue) asDocumentRevision(body Body, delta *RevisionDelta)
Attachments: value.attachments.ShallowCopy(), // Avoid caller mutating the stored attachments
Deleted: value.deleted,
Removed: value.removed,
Invalid: value.invalid,
}
if body != nil {
docRev._shallowCopyBody = body.ShallowCopy()
Expand Down Expand Up @@ -484,9 +474,3 @@ func (value *revCacheValue) updateDelta(toDelta RevisionDelta) {
value.delta = &toDelta
value.lock.Unlock()
}

func (value *revCacheValue) setInvalidFlag() {
value.lock.Lock()
value.invalid = true
value.lock.Unlock()
}
11 changes: 3 additions & 8 deletions db/revision_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,34 +457,29 @@ func TestInvalidate(t *testing.T) {
docRev, err := collection.revisionCache.Get(base.TestCtx(t), "doc", rev1id, true, true)
assert.NoError(t, err)
assert.Equal(t, rev1id, docRev.RevID)
assert.False(t, docRev.Invalid)
assert.Equal(t, int64(0), db.DbStats.Cache().RevisionCacheMisses.Value())

collection.revisionCache.Invalidate(base.TestCtx(t), "doc", rev1id)

docRev, err = collection.revisionCache.Get(base.TestCtx(t), "doc", rev1id, true, true)
assert.NoError(t, err)
assert.Equal(t, rev1id, docRev.RevID)
assert.True(t, docRev.Invalid)
assert.Equal(t, int64(1), db.DbStats.Cache().RevisionCacheMisses.Value())

docRev, err = collection.revisionCache.GetActive(base.TestCtx(t), "doc", true)
assert.NoError(t, err)
assert.Equal(t, rev1id, docRev.RevID)
assert.True(t, docRev.Invalid)
assert.Equal(t, int64(2), db.DbStats.Cache().RevisionCacheMisses.Value())
assert.Equal(t, int64(1), db.DbStats.Cache().RevisionCacheMisses.Value())

docRev, err = collection.GetRev(ctx, "doc", docRev.RevID, true, nil)
assert.NoError(t, err)
assert.Equal(t, rev1id, docRev.RevID)
assert.True(t, docRev.Invalid)
assert.Equal(t, int64(3), db.DbStats.Cache().RevisionCacheMisses.Value())
assert.Equal(t, int64(1), db.DbStats.Cache().RevisionCacheMisses.Value())

docRev, err = collection.GetRev(ctx, "doc", "", true, nil)
assert.NoError(t, err)
assert.Equal(t, rev1id, docRev.RevID)
assert.True(t, docRev.Invalid)
assert.Equal(t, int64(4), db.DbStats.Cache().RevisionCacheMisses.Value())
assert.Equal(t, int64(1), db.DbStats.Cache().RevisionCacheMisses.Value())
}

func BenchmarkRevisionCacheRead(b *testing.B) {
Expand Down

0 comments on commit 8152960

Please sign in to comment.