Skip to content

Commit

Permalink
feat: use a timed account cache
Browse files Browse the repository at this point in the history
fix golint
  • Loading branch information
andyzhangx committed Oct 9, 2021
1 parent eea17ea commit 26856c0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 34 deletions.
42 changes: 27 additions & 15 deletions pkg/azurefile/azurefile.go
Expand Up @@ -179,8 +179,8 @@ type Driver struct {
volumeLocks *volumeLocks
// a map storing all volumes created by this driver <volumeName, accountName>
volMap sync.Map
// a map storing all account name and keys retrieved by this driver <accountName, accountkey>
accountMap sync.Map
// a timed cache storing all account name and keys retrieved by this driver <accountName, accountkey>
accountCacheMap *azcache.TimedCache
// a map storing all secret names created by this driver <secretCacheKey, "">
secretCacheMap sync.Map
// a map storing all volumes using data plane API <volumeID, "">, <accountName, "">
Expand Down Expand Up @@ -208,16 +208,20 @@ func NewDriver(options *DriverOptions) *Driver {
getter := func(key string) (interface{}, error) {
return nil, nil
}
cache, err := azcache.NewTimedcache(time.Minute, getter)
if err != nil {

var err error
if driver.accountSearchCache, err = azcache.NewTimedcache(time.Minute, getter); err != nil {
klog.Fatalf("%v", err)
}
driver.accountSearchCache = cache
cache, err = azcache.NewTimedcache(time.Minute, getter)
if err != nil {

if driver.removeTagCache, err = azcache.NewTimedcache(time.Minute, getter); err != nil {
klog.Fatalf("%v", err)
}

if driver.accountCacheMap, err = azcache.NewTimedcache(5*time.Minute, getter); err != nil {
klog.Fatalf("%v", err)
}
driver.removeTagCache = cache

return &driver
}

Expand Down Expand Up @@ -556,8 +560,12 @@ func (d *Driver) GetAccountInfo(ctx context.Context, volumeID string, secrets, r

if len(secrets) == 0 {
// read account key from cache first
if v, ok := d.accountMap.Load(accountName); ok {
accountKey = v.(string)
cache, errCache := d.accountCacheMap.Get(accountName, azcache.CacheReadTypeDefault)
if errCache != nil {
return rgName, accountName, accountKey, fileShareName, diskName, errCache
}
if cache != nil {
accountKey = cache.(string)
} else {
if secretName == "" && accountName != "" {
secretName = fmt.Sprintf(secretNameTemplate, accountName)
Expand All @@ -583,7 +591,7 @@ func (d *Driver) GetAccountInfo(ctx context.Context, volumeID string, secrets, r
}

if err == nil && accountKey != "" {
d.accountMap.Store(accountName, accountKey)
d.accountCacheMap.Set(accountName, accountKey)
}
return rgName, accountName, accountKey, fileShareName, diskName, err
}
Expand Down Expand Up @@ -730,9 +738,13 @@ func (d *Driver) GetStorageAccesskey(ctx context.Context, accountOptions *azure.
}

accountName := accountOptions.Name
// read from cache first
if v, ok := d.accountMap.Load(accountName); ok {
return v.(string), nil
// read account key from cache first
cache, err := d.accountCacheMap.Get(accountName, azcache.CacheReadTypeDefault)
if err != nil {
return "", err
}
if cache != nil {
return cache.(string), nil
}

// read from k8s secret first
Expand All @@ -746,7 +758,7 @@ func (d *Driver) GetStorageAccesskey(ctx context.Context, accountOptions *azure.
}

if err == nil && accountKey != "" {
d.accountMap.Store(accountName, accountKey)
d.accountCacheMap.Set(accountName, accountKey)
}
return accountKey, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/azurefile/controllerserver.go
Expand Up @@ -309,7 +309,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
d.accountSearchCache.Set(lockKey, accountName)
d.volMap.Store(volName, accountName)
if accountKey != "" {
d.accountMap.Store(accountName, accountKey)
d.accountCacheMap.Set(accountName, accountKey)
}
}
}
Expand Down
18 changes: 0 additions & 18 deletions pkg/azurefile/controllerserver_test.go
Expand Up @@ -1396,15 +1396,6 @@ func TestControllerPublishVolume(t *testing.T) {
},
expectedErr: nil,
},
{
desc: "Get account info returns error",
req: &csi.ControllerPublishVolumeRequest{
VolumeId: "vol_2#f5713de20cde511e8ba4900#fileshare#diskname",
VolumeCapability: &stdVolCap,
NodeId: "vm3",
},
expectedErr: status.Error(codes.InvalidArgument, "GetAccountInfo(vol_2#f5713de20cde511e8ba4900#fileshare#diskname) failed with error: Retriable: false, RetryAfter: 0s, HTTPStatusCode: 502, RawError: instance not found"),
},
{
desc: "Unsupported access mode",
req: &csi.ControllerPublishVolumeRequest{
Expand Down Expand Up @@ -1485,15 +1476,6 @@ func TestControllerUnpublishVolume(t *testing.T) {
},
expectedErr: nil,
},
{
desc: "Get account info returns error",
req: &csi.ControllerUnpublishVolumeRequest{
VolumeId: "vol_2#f5713de20cde511e8ba4901#fileshare#diskname#",
NodeId: fakeNodeID,
Secrets: map[string]string{},
},
expectedErr: status.Error(codes.InvalidArgument, "GetAccountInfo(vol_2#f5713de20cde511e8ba4901#fileshare#diskname#) failed with error: Retriable: false, RetryAfter: 0s, HTTPStatusCode: 502, RawError: instance not found"),
},
}

for _, test := range tests {
Expand Down

0 comments on commit 26856c0

Please sign in to comment.