Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback for the EOS UID cache to retry user fetch for failed keys on expiry #2266

Merged
merged 2 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/unreleased/eos-uid-cache-expire-cb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Enhancement: Callback for the EOS UID cache to retry fetch for failed keys

https://github.com/cs3org/reva/pull/2266
24 changes: 17 additions & 7 deletions pkg/storage/utils/eosfs/eosfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"strings"
"time"

"github.com/ReneKroon/ttlcache/v2"
"github.com/bluele/gcache"
grouppb "github.com/cs3org/go-cs3apis/cs3/identity/group/v1beta1"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
Expand Down Expand Up @@ -140,7 +141,7 @@ type eosfs struct {
conf *Config
chunkHandler *chunking.ChunkHandler
singleUserAuth eosclient.Authorization
userIDCache gcache.Cache
userIDCache *ttlcache.Cache
tokenCache gcache.Cache
}

Expand Down Expand Up @@ -205,10 +206,19 @@ func NewEOSFS(c *Config) (storage.FS, error) {
c: eosClient,
conf: c,
chunkHandler: chunking.NewChunkHandler(c.CacheDirectory),
userIDCache: gcache.New(c.UserIDCacheSize).LFU().Build(),
userIDCache: ttlcache.NewCache(),
tokenCache: gcache.New(c.UserIDCacheSize).LFU().Build(),
}

eosfs.userIDCache.SetCacheSizeLimit(c.UserIDCacheSize)
eosfs.userIDCache.SetExpirationReasonCallback(func(key string, reason ttlcache.EvictionReason, value interface{}) {
// We only set those keys with TTL which we weren't able to retrieve the last time
// For those keys, try to contact the userprovider service again when they expire
if reason == ttlcache.Expired {
_, _ = eosfs.getUserIDGateway(context.Background(), key)
}
})

go eosfs.userIDcacheWarmup()

return eosfs, nil
Expand Down Expand Up @@ -1751,6 +1761,7 @@ func (fs *eosfs) convert(ctx context.Context, eosFileInfo *eosclient.FileInfo) (
Size: size,
PermissionSet: fs.permissionSet(ctx, eosFileInfo, owner),
Checksum: &xs,
Type: getResourceType(eosFileInfo.IsDir),
Mtime: &types.Timestamp{
Seconds: eosFileInfo.MTimeSec,
Nanos: eosFileInfo.MTimeNanos,
Expand All @@ -1775,7 +1786,6 @@ func (fs *eosfs) convert(ctx context.Context, eosFileInfo *eosclient.FileInfo) (
}
}

info.Type = getResourceType(eosFileInfo.IsDir)
return info, nil
}

Expand Down Expand Up @@ -1811,11 +1821,11 @@ func (fs *eosfs) getUIDGateway(ctx context.Context, u *userpb.UserId) (eosclient
UserId: u,
})
if err != nil {
_ = fs.userIDCache.SetWithExpire(u.OpaqueId, &userpb.User{}, 12*time.Hour)
_ = fs.userIDCache.SetWithTTL(u.OpaqueId, &userpb.User{}, 12*time.Hour)
return eosclient.Authorization{}, errors.Wrap(err, "eosfs: error getting user")
}
if getUserResp.Status.Code != rpc.Code_CODE_OK {
_ = fs.userIDCache.SetWithExpire(u.OpaqueId, &userpb.User{}, 12*time.Hour)
_ = fs.userIDCache.SetWithTTL(u.OpaqueId, &userpb.User{}, 12*time.Hour)
return eosclient.Authorization{}, status.NewErrorFromCode(getUserResp.Status.Code, "eosfs")
}

Expand Down Expand Up @@ -1847,13 +1857,13 @@ func (fs *eosfs) getUserIDGateway(ctx context.Context, uid string) (*userpb.User
if err != nil {
// Insert an empty object in the cache so that we don't make another call
// for a specific amount of time
_ = fs.userIDCache.SetWithExpire(uid, &userpb.UserId{}, 12*time.Hour)
_ = fs.userIDCache.SetWithTTL(uid, &userpb.UserId{}, 12*time.Hour)
return nil, errors.Wrap(err, "eosfs: error getting user")
}
if getUserResp.Status.Code != rpc.Code_CODE_OK {
// Insert an empty object in the cache so that we don't make another call
// for a specific amount of time
_ = fs.userIDCache.SetWithExpire(uid, &userpb.UserId{}, 12*time.Hour)
_ = fs.userIDCache.SetWithTTL(uid, &userpb.UserId{}, 12*time.Hour)
return nil, status.NewErrorFromCode(getUserResp.Status.Code, "eosfs")
}

Expand Down