Skip to content

Commit

Permalink
fix: add VolumeStats cache to avoid massive statfs calls
Browse files Browse the repository at this point in the history
chore: change log level

add flag
  • Loading branch information
andyzhangx committed Sep 11, 2023
1 parent e92bfcb commit 558b50e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
10 changes: 10 additions & 0 deletions pkg/azurefile/azurefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ type DriverOptions struct {
AppendClosetimeoOption bool
AppendNoShareSockOption bool
SkipMatchingTagCacheExpireInMinutes int
VolStatsCacheExpireInMinutes int
}

// Driver implements all interfaces of CSI drivers
Expand Down Expand Up @@ -255,6 +256,8 @@ type Driver struct {
skipMatchingTagCache azcache.Resource
// a timed cache when resize file share failed due to account limit exceeded
resizeFileShareFailureCache azcache.Resource
// a timed cache storing volume stats <volumeID, volumeStats>
volStatsCache azcache.Resource
}

// NewDriver Creates a NewCSIDriver object. Assumes vendor version is equal to driver version &
Expand Down Expand Up @@ -315,6 +318,13 @@ func NewDriver(options *DriverOptions) *Driver {
klog.Fatalf("%v", err)
}

if options.VolStatsCacheExpireInMinutes <= 0 {
options.VolStatsCacheExpireInMinutes = 10 // default expire in 10 minutes
}
if driver.volStatsCache, err = azcache.NewTimedCache(time.Duration(options.VolStatsCacheExpireInMinutes)*time.Minute, getter, false); err != nil {
klog.Fatalf("%v", err)
}

return &driver
}

Expand Down
22 changes: 21 additions & 1 deletion pkg/azurefile/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
"google.golang.org/grpc/status"

"golang.org/x/net/context"

azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache"
)

// NodePublishVolume mount the volume from staging to target path
Expand Down Expand Up @@ -424,14 +426,32 @@ func (d *Driver) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeS
return nil, status.Error(codes.InvalidArgument, "NodeGetVolumeStats volume path was empty")
}

// check if the volume stats is cached
cache, err := d.volStatsCache.Get(req.VolumeId, azcache.CacheReadTypeDefault)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
if cache != nil {
resp := cache.(csi.NodeGetVolumeStatsResponse)
klog.V(6).Infof("NodeGetVolumeStats: volume stats for volume %s path %s is cached", req.VolumeId, req.VolumePath)
return &resp, nil
}

if _, err := os.Lstat(req.VolumePath); err != nil {
if os.IsNotExist(err) {
return nil, status.Errorf(codes.NotFound, "path %s does not exist", req.VolumePath)
}
return nil, status.Errorf(codes.Internal, "failed to stat file %s: %v", req.VolumePath, err)
}

return GetVolumeStats(req.VolumePath, d.enableWindowsHostProcess)
klog.V(5).Infof("NodeGetVolumeStats: begin to get VolumeStats on volume %s path %s", req.VolumeId, req.VolumePath)
resp, err := GetVolumeStats(req.VolumePath, d.enableWindowsHostProcess)
if err == nil && resp != nil {
klog.V(6).Infof("NodeGetVolumeStats: volume stats for volume %s path %s is %v", req.VolumeId, req.VolumePath, resp)
// cache the volume stats per volume
d.volStatsCache.Set(req.VolumeId, *resp)
}
return resp, err
}

// NodeExpandVolume node expand volume
Expand Down
2 changes: 2 additions & 0 deletions pkg/azurefileplugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ var (
appendClosetimeoOption = flag.Bool("append-closetimeo-option", false, "Whether appending closetimeo=0 option to smb mount command")
appendNoShareSockOption = flag.Bool("append-nosharesock-option", true, "Whether appending nosharesock option to smb mount command")
skipMatchingTagCacheExpireInMinutes = flag.Int("skip-matching-tag-cache-expire-in-minutes", 30, "The cache expire time in minutes for skipMatchingTagCache")
volStatsCacheExpireInMinutes = flag.Int("vol-stats-cache-expire-in-minutes", 10, "The cache expire time in minutes for volume stats cache")
)

func main() {
Expand Down Expand Up @@ -105,6 +106,7 @@ func handle() {
AppendClosetimeoOption: *appendClosetimeoOption,
AppendNoShareSockOption: *appendNoShareSockOption,
SkipMatchingTagCacheExpireInMinutes: *skipMatchingTagCacheExpireInMinutes,
VolStatsCacheExpireInMinutes: *volStatsCacheExpireInMinutes,
}
driver := azurefile.NewDriver(&driverOptions)
if driver == nil {
Expand Down

0 comments on commit 558b50e

Please sign in to comment.