Skip to content

Commit a4a99ec

Browse files
committed
feat: add account search cache to prevent account list throttling
1 parent 51ec091 commit a4a99ec

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

pkg/blob/blob.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package blob
1919
import (
2020
"fmt"
2121
"strings"
22+
"time"
2223

2324
"golang.org/x/net/context"
2425

@@ -36,6 +37,7 @@ import (
3637

3738
csicommon "sigs.k8s.io/blob-csi-driver/pkg/csi-common"
3839
"sigs.k8s.io/blob-csi-driver/pkg/util"
40+
azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache"
3941
azure "sigs.k8s.io/cloud-provider-azure/pkg/provider"
4042
)
4143

@@ -137,6 +139,8 @@ type Driver struct {
137139
volumeLocks *volumeLocks
138140
// only for nfs feature
139141
subnetLockMap *util.LockMap
142+
// a timed cache storing acount search history (solve account list throttling issue)
143+
accountSearchCache *azcache.TimedCache
140144
}
141145

142146
// NewDriver Creates a NewCSIDriver object. Assumes vendor version is equal to driver version &
@@ -158,6 +162,12 @@ func NewDriver(options *DriverOptions) *Driver {
158162
d.Name = options.DriverName
159163
d.Version = driverVersion
160164
d.NodeID = options.NodeID
165+
166+
var err error
167+
getter := func(key string) (interface{}, error) { return nil, nil }
168+
if d.accountSearchCache, err = azcache.NewTimedcache(time.Minute, getter); err != nil {
169+
klog.Fatalf("%v", err)
170+
}
161171
return &d
162172
}
163173

pkg/blob/blob_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func TestNewDriver(t *testing.T) {
8787
fakedriver := NewFakeDriver()
8888
fakedriver.Name = DefaultDriverName
8989
fakedriver.Version = driverVersion
90+
fakedriver.accountSearchCache = driver.accountSearchCache
9091
assert.Equal(t, driver, fakedriver)
9192
}
9293

pkg/blob/controllerserver.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"k8s.io/klog/v2"
3434

3535
"sigs.k8s.io/blob-csi-driver/pkg/util"
36+
azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache"
3637
"sigs.k8s.io/cloud-provider-azure/pkg/metrics"
3738
azure "sigs.k8s.io/cloud-provider-azure/pkg/provider"
3839
)
@@ -189,20 +190,30 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
189190
var accountKey string
190191
accountName := account
191192
if len(req.GetSecrets()) == 0 && accountName == "" {
192-
lockKey := storageAccountType + accountKind + resourceGroup + location
193-
d.volLockMap.LockEntry(lockKey)
194-
err = wait.ExponentialBackoff(d.cloud.RequestBackoff(), func() (bool, error) {
195-
var retErr error
196-
accountName, accountKey, retErr = d.cloud.EnsureStorageAccount(ctx, accountOptions, protocol)
197-
if isRetriableError(retErr) {
198-
klog.Warningf("EnsureStorageAccount(%s) failed with error(%v), waiting for retrying", account, retErr)
199-
return false, nil
200-
}
201-
return true, retErr
202-
})
203-
d.volLockMap.UnlockEntry(lockKey)
193+
lockKey := fmt.Sprintf("%s%s%s%s%s", storageAccountType, accountKind, resourceGroup, location, protocol)
194+
// search in cache first
195+
cache, err := d.accountSearchCache.Get(lockKey, azcache.CacheReadTypeDefault)
204196
if err != nil {
205-
return nil, status.Errorf(codes.Internal, "failed to ensure storage account: %v", err)
197+
return nil, err
198+
}
199+
if cache != nil {
200+
accountName = cache.(string)
201+
} else {
202+
d.volLockMap.LockEntry(lockKey)
203+
err = wait.ExponentialBackoff(d.cloud.RequestBackoff(), func() (bool, error) {
204+
var retErr error
205+
accountName, accountKey, retErr = d.cloud.EnsureStorageAccount(ctx, accountOptions, protocol)
206+
if isRetriableError(retErr) {
207+
klog.Warningf("EnsureStorageAccount(%s) failed with error(%v), waiting for retrying", account, retErr)
208+
return false, nil
209+
}
210+
return true, retErr
211+
})
212+
d.volLockMap.UnlockEntry(lockKey)
213+
if err != nil {
214+
return nil, status.Errorf(codes.Internal, "failed to ensure storage account: %v", err)
215+
}
216+
d.accountSearchCache.Set(lockKey, accountName)
206217
}
207218
}
208219
accountOptions.Name = accountName

0 commit comments

Comments
 (0)