From be458d8fd227590a14527359aaaf08678c95f7d3 Mon Sep 17 00:00:00 2001 From: Ishank Arora Date: Thu, 26 Nov 2020 17:21:54 +0530 Subject: [PATCH] Add cache to store UID to UserID mapping in EOS --- changelog/unreleased/eos-user-cache.md | 7 +++++++ pkg/storage/utils/eosfs/eosfs.go | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 changelog/unreleased/eos-user-cache.md diff --git a/changelog/unreleased/eos-user-cache.md b/changelog/unreleased/eos-user-cache.md new file mode 100644 index 00000000000..38bcb65a540 --- /dev/null +++ b/changelog/unreleased/eos-user-cache.md @@ -0,0 +1,7 @@ +Enhancement: Add cache to store UID to UserID mapping in EOS + +Previously, we used to send an RPC to the user provider service for every lookup +of user IDs from the UID stored in EOS. This PR adds an in-memory lock-protected +cache to store this mapping. + +https://github.com/cs3org/reva/pull/1340 diff --git a/pkg/storage/utils/eosfs/eosfs.go b/pkg/storage/utils/eosfs/eosfs.go index 087f1e696f8..945d6b614c1 100644 --- a/pkg/storage/utils/eosfs/eosfs.go +++ b/pkg/storage/utils/eosfs/eosfs.go @@ -29,6 +29,7 @@ import ( "regexp" "strconv" "strings" + "sync" userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" @@ -179,6 +180,12 @@ type eosfs struct { chunkHandler *chunking.ChunkHandler singleUserUID string singleUserGID string + userIDCache *userCache +} + +type userCache struct { + *sync.RWMutex + cache map[string]*userpb.UserId } // NewEOSFS returns a storage.FS interface implementation that connects to an @@ -213,6 +220,10 @@ func NewEOSFS(c *Config) (storage.FS, error) { c: eosClient, conf: c, chunkHandler: chunking.NewChunkHandler(c.CacheDirectory), + userIDCache: &userCache{ + &sync.RWMutex{}, + make(map[string]*userpb.UserId), + }, } return eosfs, nil @@ -1455,6 +1466,12 @@ func (fs *eosfs) getUIDGateway(ctx context.Context, u *userpb.UserId) (string, s } func (fs *eosfs) getUserIDGateway(ctx context.Context, uid string) (*userpb.UserId, error) { + fs.userIDCache.RLock() + if userID, ok := fs.userIDCache.cache[uid]; ok { + fs.userIDCache.RUnlock() + return userID, nil + } + fs.userIDCache.RUnlock() client, err := pool.GetGatewayServiceClient(fs.conf.GatewaySvc) if err != nil { return nil, errors.Wrap(err, "eos: error getting gateway grpc client") @@ -1469,6 +1486,9 @@ func (fs *eosfs) getUserIDGateway(ctx context.Context, uid string) (*userpb.User if getUserResp.Status.Code != rpc.Code_CODE_OK { return nil, errors.Wrap(err, "eos: grpc get user failed") } + fs.userIDCache.Lock() + fs.userIDCache.cache[uid] = getUserResp.User.Id + fs.userIDCache.Unlock() return getUserResp.User.Id, nil }