Skip to content

Commit

Permalink
fix: reduce mount lock to avoid volumeID collision issue
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhangx committed Dec 27, 2023
1 parent 8a5fe81 commit 0b81726
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions pkg/smb/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ func (d *Driver) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequ
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("%s field is missing, current context: %v", sourceField, context))
}

if acquired := d.volumeLocks.TryAcquire(volumeID); !acquired {
lockKey := fmt.Sprintf("%s-%s", volumeID, targetPath)
if acquired := d.volumeLocks.TryAcquire(lockKey); !acquired {
return nil, status.Errorf(codes.Aborted, volumeOperationAlreadyExistsFmt, volumeID)
}
defer d.volumeLocks.Release(volumeID)
defer d.volumeLocks.Release(lockKey)

var username, password, domain string
for k, v := range secrets {
Expand Down Expand Up @@ -254,10 +255,11 @@ func (d *Driver) NodeUnstageVolume(_ context.Context, req *csi.NodeUnstageVolume
return nil, status.Error(codes.InvalidArgument, "Staging target not provided")
}

if acquired := d.volumeLocks.TryAcquire(volumeID); !acquired {
lockKey := fmt.Sprintf("%s-%s", volumeID, stagingTargetPath)
if acquired := d.volumeLocks.TryAcquire(lockKey); !acquired {
return nil, status.Errorf(codes.Aborted, volumeOperationAlreadyExistsFmt, volumeID)
}
defer d.volumeLocks.Release(volumeID)
defer d.volumeLocks.Release(lockKey)

klog.V(2).Infof("NodeUnstageVolume: CleanupMountPoint on %s with volume %s", stagingTargetPath, volumeID)
if err := CleanupSMBMountPoint(d.mounter, stagingTargetPath, true /*extensiveMountPointCheck*/, volumeID); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/smb/nodeserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestNodeStageVolume(t *testing.T) {
{
desc: "[Error] Volume operation in progress",
setup: func(d *Driver) {
d.volumeLocks.TryAcquire("vol_1")
d.volumeLocks.TryAcquire(fmt.Sprintf("%s-%s", "vol_1", sourceTest))
},
req: csi.NodeStageVolumeRequest{VolumeId: "vol_1", StagingTargetPath: sourceTest,
VolumeCapability: &stdVolCap,
Expand All @@ -146,7 +146,7 @@ func TestNodeStageVolume(t *testing.T) {
DefaultError: status.Error(codes.Aborted, fmt.Sprintf(volumeOperationAlreadyExistsFmt, "vol_1")),
},
cleanup: func(d *Driver) {
d.volumeLocks.Release("vol_1")
d.volumeLocks.Release(fmt.Sprintf("%s-%s", "vol_1", sourceTest))
},
},
{
Expand Down Expand Up @@ -493,14 +493,14 @@ func TestNodeUnstageVolume(t *testing.T) {
{
desc: "[Error] Volume operation in progress",
setup: func(d *Driver) {
d.volumeLocks.TryAcquire("vol_1")
d.volumeLocks.TryAcquire(fmt.Sprintf("%s-%s", "vol_1", targetFile))
},
req: csi.NodeUnstageVolumeRequest{StagingTargetPath: targetFile, VolumeId: "vol_1"},
expectedErr: testutil.TestError{
DefaultError: status.Error(codes.Aborted, fmt.Sprintf(volumeOperationAlreadyExistsFmt, "vol_1")),
},
cleanup: func(d *Driver) {
d.volumeLocks.Release("vol_1")
d.volumeLocks.Release(fmt.Sprintf("%s-%s", "vol_1", targetFile))
},
},
{
Expand Down

0 comments on commit 0b81726

Please sign in to comment.