Skip to content

Commit 81ac623

Browse files
committed
cherry-pick #38855 - Fix variable shadowing in exponential backoff
Also fix pv_controller unit tests to behave more accurately in light of exponential backoffs
1 parent a80e219 commit 81ac623

4 files changed

Lines changed: 72 additions & 4 deletions

File tree

‎pkg/controller/volume/persistentvolume/framework_test.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ func (r *volumeReactor) getChangeCount() int {
461461
// waitForIdle waits until all tests, controllers and other goroutines do their
462462
// job and no new actions are registered for 10 milliseconds.
463463
func (r *volumeReactor) waitForIdle() {
464-
r.ctrl.runningOperations.Wait()
464+
r.ctrl.runningOperations.WaitForCompletion()
465465
// Check every 10ms if the controller does something and stop if it's
466466
// idle.
467467
oldChanges := -1
@@ -489,7 +489,7 @@ func (r *volumeReactor) waitTest(test controllerTest) error {
489489
}
490490
err := wait.ExponentialBackoff(backoff, func() (done bool, err error) {
491491
// Finish all operations that are in progress
492-
r.ctrl.runningOperations.Wait()
492+
r.ctrl.runningOperations.WaitForCompletion()
493493

494494
// Return 'true' if the reactor reached the expected state
495495
err1 := r.checkClaims(test.expectedClaims)
@@ -1038,6 +1038,8 @@ func runMultisyncTests(t *testing.T, tests []controllerTest, storageClasses []*s
10381038
break
10391039
}
10401040
}
1041+
// waiting here cools down exponential backoff
1042+
time.Sleep(600 * time.Millisecond)
10411043

10421044
// There were some changes, process them
10431045
switch obj.(type) {

‎pkg/controller/volume/persistentvolume/pv_controller.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ func (ctrl *PersistentVolumeController) deleteVolumeOperation(arg interface{}) e
10791079
} else {
10801080
// The plugin failed, mark the volume as Failed and send Warning
10811081
// event
1082-
if _, err = ctrl.updateVolumePhaseWithEvent(volume, api.VolumeFailed, api.EventTypeWarning, "VolumeFailedDelete", err.Error()); err != nil {
1082+
if _, err := ctrl.updateVolumePhaseWithEvent(volume, api.VolumeFailed, api.EventTypeWarning, "VolumeFailedDelete", err.Error()); err != nil {
10831083
glog.V(4).Infof("deleteVolumeOperation [%s]: failed to mark volume as failed: %v", volume.Name, err)
10841084
// Save failed, retry on the next deletion attempt
10851085
return err

‎pkg/util/goroutinemap/goroutinemap.go‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,16 @@ type GoRoutineMap interface {
5757
// a new operation to be started with the same operation name without error.
5858
Run(operationName string, operationFunc func() error) error
5959

60-
// Wait blocks until all operations are completed. This is typically
60+
// Wait blocks until operations map is empty. This is typically
6161
// necessary during tests - the test should wait until all operations finish
6262
// and evaluate results after that.
6363
Wait()
6464

65+
// WaitForCompletion blocks until either all operations have successfully completed
66+
// or have failed but are not pending. The test should wait until operations are either
67+
// complete or have failed.
68+
WaitForCompletion()
69+
6570
// IsOperationPending returns true if the operation is pending (currently
6671
// running), otherwise returns false.
6772
IsOperationPending(operationName string) bool
@@ -179,6 +184,32 @@ func (grm *goRoutineMap) Wait() {
179184
}
180185
}
181186

187+
func (grm *goRoutineMap) WaitForCompletion() {
188+
grm.lock.Lock()
189+
defer grm.lock.Unlock()
190+
191+
for {
192+
if len(grm.operations) == 0 || grm.nothingPending() {
193+
break
194+
} else {
195+
grm.cond.Wait()
196+
}
197+
}
198+
}
199+
200+
// Check if any operation is pending. Already assumes caller has the
201+
// necessary locks
202+
func (grm *goRoutineMap) nothingPending() bool {
203+
nothingIsPending := true
204+
for _, operation := range grm.operations {
205+
if operation.operationPending {
206+
nothingIsPending = false
207+
break
208+
}
209+
}
210+
return nothingIsPending
211+
}
212+
182213
// NewAlreadyExistsError returns a new instance of AlreadyExists error.
183214
func NewAlreadyExistsError(operationName string) error {
184215
return alreadyExistsError{operationName}

‎pkg/util/goroutinemap/goroutinemap_test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,13 +448,48 @@ func Test_NewGoRoutineMap_Positive_WaitWithExpBackoff(t *testing.T) {
448448
}
449449
}
450450

451+
func Test_NewGoRoutineMap_WaitForCompletionWithExpBackoff(t *testing.T) {
452+
grm := NewGoRoutineMap(true /* exponentialBackOffOnError */)
453+
operationName := "operation-err"
454+
455+
operation1DoneCh := make(chan interface{}, 0 /* bufferSize */)
456+
operation1 := generateErrorFunc(operation1DoneCh)
457+
err := grm.Run(operationName, operation1)
458+
if err != nil {
459+
t.Fatalf("NewGoRoutine failed. Expected: <no error> Actual: <%v>", err)
460+
}
461+
462+
// Act
463+
waitDoneCh := make(chan interface{}, 1)
464+
go func() {
465+
grm.WaitForCompletion()
466+
waitDoneCh <- true
467+
}()
468+
469+
// Finish the operation
470+
operation1DoneCh <- true
471+
472+
// Assert that WaitForCompletion returns even if scheduled op had error
473+
err = waitChannelWithTimeout(waitDoneCh, testTimeout)
474+
if err != nil {
475+
t.Fatalf("Error waiting for GoRoutineMap.Wait: %v", err)
476+
}
477+
}
478+
451479
func generateCallbackFunc(done chan<- interface{}) func() error {
452480
return func() error {
453481
done <- true
454482
return nil
455483
}
456484
}
457485

486+
func generateErrorFunc(done <-chan interface{}) func() error {
487+
return func() error {
488+
<-done
489+
return fmt.Errorf("Generic error")
490+
}
491+
}
492+
458493
func generateWaitFunc(done <-chan interface{}) func() error {
459494
return func() error {
460495
<-done

0 commit comments

Comments
 (0)