Skip to content

Commit

Permalink
Add cache to store UID to UserID mapping in EOS
Browse files Browse the repository at this point in the history
  • Loading branch information
ishank011 committed Nov 26, 2020
1 parent 27afb32 commit be458d8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions changelog/unreleased/eos-user-cache.md
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions pkg/storage/utils/eosfs/eosfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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
}

Expand Down

0 comments on commit be458d8

Please sign in to comment.