@@ -18,7 +18,6 @@ import (
18
18
"fmt"
19
19
"os"
20
20
"strings"
21
- "sync"
22
21
23
22
"context"
24
23
csi "github.com/container-storage-interface/spec/lib/go/csi"
@@ -39,7 +38,7 @@ type GCENodeServer struct {
39
38
40
39
// A map storing all volumes with ongoing operations so that additional operations
41
40
// for that same volume (as defined by VolumeID) return an Aborted error
42
- volumes sync. Map
41
+ volumes * common. VolumeLocks
43
42
}
44
43
45
44
var _ csi.NodeServer = & GCENodeServer {}
@@ -74,10 +73,10 @@ func (ns *GCENodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePub
74
73
return nil , status .Error (codes .InvalidArgument , "NodePublishVolume Volume Capability must be provided" )
75
74
}
76
75
77
- if _ , alreadyExists := ns .volumes .LoadOrStore (volumeID , true ); alreadyExists {
76
+ if acquired := ns .volumes .TryAcquire (volumeID ); ! acquired {
78
77
return nil , status .Error (codes .Aborted , fmt .Sprintf ("An operation with the given Volume ID %s already exists" , volumeID ))
79
78
}
80
- defer ns .volumes .Delete (volumeID )
79
+ defer ns .volumes .Release (volumeID )
81
80
82
81
if err := validateVolumeCapability (volumeCapability ); err != nil {
83
82
return nil , status .Error (codes .InvalidArgument , fmt .Sprintf ("VolumeCapability is invalid: %v" , err ))
@@ -189,18 +188,18 @@ func (ns *GCENodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeU
189
188
190
189
// Validate Arguments
191
190
targetPath := req .GetTargetPath ()
192
- volID := req .GetVolumeId ()
193
- if len (volID ) == 0 {
191
+ volumeID := req .GetVolumeId ()
192
+ if len (volumeID ) == 0 {
194
193
return nil , status .Error (codes .InvalidArgument , "NodeUnpublishVolume Volume ID must be provided" )
195
194
}
196
195
if len (targetPath ) == 0 {
197
196
return nil , status .Error (codes .InvalidArgument , "NodeUnpublishVolume Target Path must be provided" )
198
197
}
199
198
200
- if _ , alreadyExists := ns .volumes .LoadOrStore ( volID , true ); alreadyExists {
201
- return nil , status .Error (codes .Aborted , fmt .Sprintf ("An operation with the given Volume ID %s already exists" , volID ))
199
+ if acquired := ns .volumes .TryAcquire ( volumeID ); ! acquired {
200
+ return nil , status .Error (codes .Aborted , fmt .Sprintf ("An operation with the given Volume ID %s already exists" , volumeID ))
202
201
}
203
- defer ns .volumes .Delete ( volID )
202
+ defer ns .volumes .Release ( volumeID )
204
203
205
204
err := mount .CleanupMountPoint (targetPath , ns .Mounter .Interface , false /* bind mount */ )
206
205
if err != nil {
@@ -227,10 +226,10 @@ func (ns *GCENodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStage
227
226
return nil , status .Error (codes .InvalidArgument , "NodeStageVolume Volume Capability must be provided" )
228
227
}
229
228
230
- if _ , alreadyExists := ns .volumes .LoadOrStore (volumeID , true ); alreadyExists {
229
+ if acquired := ns .volumes .TryAcquire (volumeID ); ! acquired {
231
230
return nil , status .Error (codes .Aborted , fmt .Sprintf ("An operation with the given Volume ID %s already exists" , volumeID ))
232
231
}
233
- defer ns .volumes .Delete (volumeID )
232
+ defer ns .volumes .Release (volumeID )
234
233
235
234
if err := validateVolumeCapability (volumeCapability ); err != nil {
236
235
return nil , status .Error (codes .InvalidArgument , fmt .Sprintf ("VolumeCapability is invalid: %v" , err ))
@@ -321,10 +320,10 @@ func (ns *GCENodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUns
321
320
return nil , status .Error (codes .InvalidArgument , "NodeUnstageVolume Staging Target Path must be provided" )
322
321
}
323
322
324
- if _ , alreadyExists := ns .volumes .LoadOrStore (volumeID , true ); alreadyExists {
323
+ if acquired := ns .volumes .TryAcquire (volumeID ); ! acquired {
325
324
return nil , status .Error (codes .Aborted , fmt .Sprintf ("An operation with the given Volume ID %s already exists" , volumeID ))
326
325
}
327
- defer ns .volumes .Delete (volumeID )
326
+ defer ns .volumes .Release (volumeID )
328
327
329
328
err := mount .CleanupMountPoint (stagingTargetPath , ns .Mounter .Interface , false /* bind mount */ )
330
329
if err != nil {
0 commit comments