@@ -19,7 +19,6 @@ import (
19
19
"math/rand"
20
20
"sort"
21
21
"strings"
22
- "sync"
23
22
"time"
24
23
25
24
"github.com/golang/protobuf/ptypes"
@@ -46,7 +45,7 @@ type GCEControllerServer struct {
46
45
47
46
// A map storing all volumes with ongoing operations so that additional operations
48
47
// for that same volume (as defined by Volume Key) return an Aborted error
49
- volumes sync. Map
48
+ volumes * common. VolumeLocks
50
49
}
51
50
52
51
var _ csi.ControllerServer = & GCEControllerServer {}
@@ -144,10 +143,10 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
144
143
return nil , status .Error (codes .InvalidArgument , fmt .Sprintf ("CreateVolume replication type '%s' is not supported" , replicationType ))
145
144
}
146
145
147
- if _ , alreadyExists := gceCS .volumes .LoadOrStore (volKey .String (), true ); alreadyExists {
146
+ if acquired := gceCS .volumes .TryAcquire (volKey .String ()); ! acquired {
148
147
return nil , status .Error (codes .Aborted , fmt .Sprintf ("An operation with the given Volume Key %s already exists" , volKey .String ()))
149
148
}
150
- defer gceCS .volumes .Delete (volKey .String ())
149
+ defer gceCS .volumes .Release (volKey .String ())
151
150
152
151
// Validate if disk already exists
153
152
existingDisk , err := gceCS .CloudProvider .GetDisk (ctx , volKey )
@@ -232,10 +231,10 @@ func (gceCS *GCEControllerServer) DeleteVolume(ctx context.Context, req *csi.Del
232
231
return nil , status .Error (codes .NotFound , fmt .Sprintf ("Could not find volume with ID %v: %v" , volumeID , err ))
233
232
}
234
233
235
- if _ , alreadyExists := gceCS .volumes .LoadOrStore (volKey .String (), true ); alreadyExists {
234
+ if acquired := gceCS .volumes .TryAcquire (volKey .String ()); ! acquired {
236
235
return nil , status .Error (codes .Aborted , fmt .Sprintf ("An operation with the given Volume Key %s already exists" , volKey .String ()))
237
236
}
238
- defer gceCS .volumes .Delete (volKey .String ())
237
+ defer gceCS .volumes .Release (volKey .String ())
239
238
240
239
err = gceCS .CloudProvider .DeleteDisk (ctx , volKey )
241
240
if err != nil {
@@ -273,10 +272,11 @@ func (gceCS *GCEControllerServer) ControllerPublishVolume(ctx context.Context, r
273
272
return nil , status .Error (codes .NotFound , fmt .Sprintf ("Could not find volume with ID %v: %v" , volumeID , err ))
274
273
}
275
274
276
- if _ , alreadyExists := gceCS .volumes .LoadOrStore (volKey .String (), true ); alreadyExists {
277
- return nil , status .Error (codes .Aborted , fmt .Sprintf ("An operation with the given Volume Key %s already exists" , volKey .String ()))
275
+ volKeyStr := volKey .String ()
276
+ if acquired := gceCS .volumes .TryAcquire (nodeID + "/" + volKeyStr ); ! acquired {
277
+ return nil , status .Error (codes .Aborted , fmt .Sprintf ("An operation with the given Volume Key %s or Node ID %s already exists" , volKeyStr , nodeID ))
278
278
}
279
- defer gceCS .volumes .Delete ( volKey . String () )
279
+ defer gceCS .volumes .Release ( nodeID + "/" + volKeyStr )
280
280
281
281
// TODO(#253): Check volume capability matches for ALREADY_EXISTS
282
282
if err = validateVolumeCapability (volumeCapability ); err != nil {
@@ -363,10 +363,11 @@ func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context,
363
363
return nil , err
364
364
}
365
365
366
- if _ , alreadyExists := gceCS .volumes .LoadOrStore (volKey .String (), true ); alreadyExists {
367
- return nil , status .Error (codes .Aborted , fmt .Sprintf ("An operation with the given Volume Key %s already exists" , volKey .String ()))
366
+ volKeyStr := volKey .String ()
367
+ if acquired := gceCS .volumes .TryAcquire (nodeID + "/" + volKeyStr ); ! acquired {
368
+ return nil , status .Error (codes .Aborted , fmt .Sprintf ("An operation with the given Volume Key %s or Node ID %s already exists" , volKeyStr , nodeID ))
368
369
}
369
- defer gceCS .volumes .Delete ( volKey . String () )
370
+ defer gceCS .volumes .Release ( nodeID + "/" + volKeyStr )
370
371
371
372
instanceZone , instanceName , err := common .NodeIDToZoneAndName (nodeID )
372
373
if err != nil {
@@ -415,10 +416,10 @@ func (gceCS *GCEControllerServer) ValidateVolumeCapabilities(ctx context.Context
415
416
return nil , status .Error (codes .NotFound , fmt .Sprintf ("Volume ID is of improper format, got %v" , volumeID ))
416
417
}
417
418
418
- if _ , alreadyExists := gceCS .volumes .LoadOrStore (volKey .String (), true ); alreadyExists {
419
+ if acquired := gceCS .volumes .TryAcquire (volKey .String ()); ! acquired {
419
420
return nil , status .Error (codes .Aborted , fmt .Sprintf ("An operation with the given Volume Key %s already exists" , volKey .String ()))
420
421
}
421
- defer gceCS .volumes .Delete (volKey .String ())
422
+ defer gceCS .volumes .Release (volKey .String ())
422
423
423
424
_ , err = gceCS .CloudProvider .GetDisk (ctx , volKey )
424
425
if err != nil {
@@ -527,10 +528,10 @@ func (gceCS *GCEControllerServer) CreateSnapshot(ctx context.Context, req *csi.C
527
528
return nil , status .Error (codes .NotFound , fmt .Sprintf ("Could not find volume with ID %v: %v" , volumeID , err ))
528
529
}
529
530
530
- if _ , alreadyExists := gceCS .volumes .LoadOrStore (volKey .String (), true ); alreadyExists {
531
+ if acquired := gceCS .volumes .TryAcquire (volKey .String ()); ! acquired {
531
532
return nil , status .Error (codes .Aborted , fmt .Sprintf ("An operation with the given Volume Key %s already exists" , volKey .String ()))
532
533
}
533
- defer gceCS .volumes .Delete (volKey .String ())
534
+ defer gceCS .volumes .Release (volKey .String ())
534
535
535
536
// Check if snapshot already exists
536
537
var snapshot * compute.Snapshot
0 commit comments