Skip to content

Commit

Permalink
updates mock stat cache with new implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitaluthra1 committed Jul 5, 2024
1 parent dfa53c5 commit 114331d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
24 changes: 22 additions & 2 deletions internal/cache/metadata/stat_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ type StatCache interface {
// entry. Return hit == false when there is neither a positive nor a negative
// entry, or the entry has expired according to the supplied current time.
LookUpFolder(folderName string, now time.Time) (bool, *controlpb.Folder)

// Set up a negative entry for the given folder name, indicating that the name
// doesn't exist. Overwrite any existing entry for the name, positive or
// negative.
AddNegativeEntryForFolder(folderName string, expiration time.Time)
}

// Create a new bucket-view to the passed shared-cache object.
Expand Down Expand Up @@ -108,10 +113,10 @@ type entry struct {
// benchmark runs) to heap-size per positive stat-cache entry
// to calculate a size closer to the actual memory utilization.
func (e entry) Size() (size uint64) {
// First, calculate size on heap.
// First, calculate size on heap (including folder size also in case of hns buckets, in case of non-hns buckets 0 will be added as e.f will be Nil ).
// Additional 2*util.UnsafeSizeOf(&e.key) is to account for the copies of string
// struct stored in the cache map and in the cache linked-list.
size = uint64(util.UnsafeSizeOf(&e) + len(e.key) + 2*util.UnsafeSizeOf(&e.key) + util.NestedSizeOfGcsMinObject(e.m))
size = uint64(util.UnsafeSizeOf(&e) + len(e.key) + 2*util.UnsafeSizeOf(&e.key) + util.NestedSizeOfGcsMinObject(e.m) + util.UnsafeSizeOf(&e.f))
if e.m != nil {
size += 515
}
Expand Down Expand Up @@ -192,6 +197,21 @@ func (sc *statCacheBucketView) AddNegativeEntry(objectName string, expiration ti
}
}

func (sc *statCacheBucketView) AddNegativeEntryForFolder(folderName string, expiration time.Time) {
name := sc.key(folderName)

// Insert a negative entry.
e := entry{
f: nil,
expiration: expiration,
key: name,
}

if _, err := sc.sharedCache.Insert(name, e); err != nil {
panic(err)
}
}

func (sc *statCacheBucketView) Erase(objectName string) {
name := sc.key(objectName)
sc.sharedCache.Erase(name)
Expand Down
15 changes: 15 additions & 0 deletions internal/cache/metadata/stat_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,18 @@ func (t *StatCacheTest) Test_Should_Not_Override_Entry_If_Metageneration_Is_Old(
assert.Equal(t.T(), "key1", entry.Name)
assert.Equal(t.T(), int64(2), entry.Metageneration)
}

func (t *StatCacheTest) Test_Should_Add_Negative_Entry_For_Folder() {
const name = "key1"
existingEntry := &controlpb.Folder{
Name: name,
Metageneration: 2,
}
t.statCache.InsertFolder(existingEntry, expiration)

t.statCache.AddNegativeEntryForFolder(name, expiration)

hit, entry := t.statCache.LookUpFolder(name, someTime)
assert.True(t.T(), hit)
assert.Nil(t.T(), entry)
}
17 changes: 17 additions & 0 deletions internal/storage/caching/mock_gcscaching/mock_stat_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ type mockStatCache struct {
description string
}

func (m *mockStatCache) AddNegativeEntryForFolder(p0 string, p1 time.Time) {
// Get a folder name and line number for the caller.
_, name, line, _ := runtime.Caller(1)

// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"AddNegativeEntryForFolder",
name,
line,
[]interface{}{p0, p1})

if len(retVals) != 0 {
panic(fmt.Sprintf("mockStatCache.AddNegativeEntryforFolder: invalid return values: %v", retVals))
}
}

func NewMockStatCache(
c oglemock.Controller,
desc string) MockStatCache {
Expand Down

0 comments on commit 114331d

Please sign in to comment.