Skip to content

Commit

Permalink
Minor changes for stat cache for HNS (#2229)
Browse files Browse the repository at this point in the history
* refactor if condition in lookup folder for stat cache

* adds folder in stat cache when get folder is success

* adds comment
  • Loading branch information
ankitaluthra1 authored Jul 29, 2024
1 parent be79578 commit ec54a1a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
16 changes: 8 additions & 8 deletions internal/cache/metadata/stat_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,16 @@ func (sc *statCacheBucketView) LookUpFolder(
// Look up in the LRU cache.
hit, entry := sc.sharedCacheLookup(folderName, now)

if hit {
// Adds scenario to check folder as well even if object with same name is already present
if entry.f == nil {
return false, nil
}

return true, entry.f
if !hit {
return false, nil
}
// Adds scenario to check folder as well even if object with same name is already present
// TODO: should be removed once integration for lookup is completed
if entry.f == nil {
return false, nil
}

return false, nil
return true, entry.f
}

func (sc *statCacheBucketView) sharedCacheLookup(key string, now time.Time) (bool, *entry) {
Expand Down
10 changes: 9 additions & 1 deletion internal/storage/caching/fast_stat_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,15 @@ func (b *fastStatBucket) GetFolder(
}

// Fetch the Folder
return b.wrapped.GetFolder(ctx, prefix)
folder, error := b.wrapped.GetFolder(ctx, prefix)

if error != nil {
return nil, error
}

// Record the new folder.
b.cache.InsertFolder(folder, b.clock.Now().Add(b.ttl))
return folder, nil
}

func (b *fastStatBucket) CreateFolder(ctx context.Context, folderName string) (f *gcs.Folder, err error) {
Expand Down
17 changes: 17 additions & 0 deletions internal/storage/caching/fast_stat_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,8 @@ func (t *StatObjectTest) TestShouldCallGetFolderWhenEntryIsNotPresent() {

ExpectCall(t.cache, "LookUpFolder")(name, Any()).
WillOnce(Return(false, nil))
ExpectCall(t.cache, "InsertFolder")(folder, Any()).
WillOnce(Return())
ExpectCall(t.wrapped, "GetFolder")(Any(), name).
WillOnce(Return(folder, nil))

Expand All @@ -770,6 +772,21 @@ func (t *StatObjectTest) TestShouldCallGetFolderWhenEntryIsNotPresent() {
ExpectThat(result, Pointee(DeepEquals(*folder)))
}

func (t *StatObjectTest) TestShouldReturnNilWhenErrorIsReturnedFromGetFolder() {
const name = "some-name"
error := errors.New("connection error")

ExpectCall(t.cache, "LookUpFolder")(name, Any()).
WillOnce(Return(false, nil))
ExpectCall(t.wrapped, "GetFolder")(Any(), name).
WillOnce(Return(nil, error))

folder, result := t.bucket.GetFolder(context.TODO(), name)

AssertEq(nil, folder)
AssertEq(error, result)
}

func (t *StatObjectTest) TestRenameFolder() {
const name = "some-name"
const newName = "new-name"
Expand Down

0 comments on commit ec54a1a

Please sign in to comment.