Skip to content

Commit

Permalink
fix(serving): Add an error window for ReadyCondition
Browse files Browse the repository at this point in the history
A default error window of 2 seconds (not sure if this should be made configurable
as it is hard to explain/document) cause return an error during
sync operations only, if a ReadyCondition == false stays for that
long in false. This is needed to compensate situation where the
condition is false when a race between revision and route creation
occurse during a service creation.

This should fix the flakes we have in our E2E tests as described in knative#544.
  • Loading branch information
rhuss committed Feb 7, 2020
1 parent c3f77df commit be0267c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
29 changes: 25 additions & 4 deletions pkg/wait/wait_for_ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
"knative.dev/pkg/apis"
)

// Window for how long to wait until a ReadyCondition == false has to stay
// for being considered as an error
var ErrorWindow = 2 * time.Second

// Callbacks and configuration used while waiting
type waitForReadyConfig struct {
watchMaker WatchMaker
Expand Down Expand Up @@ -87,7 +91,7 @@ func (w *waitForReadyConfig) Wait(name string, timeout time.Duration, msgCallbac
floatingTimeout := timeout
for {
start := time.Now()
retry, timeoutReached, err := w.waitForReadyCondition(start, name, floatingTimeout, msgCallback)
retry, timeoutReached, err := w.waitForReadyCondition(start, name, floatingTimeout, ErrorWindow, msgCallback)
if err != nil {
return err, time.Since(start)
}
Expand All @@ -104,18 +108,30 @@ func (w *waitForReadyConfig) Wait(name string, timeout time.Duration, msgCallbac
}
}

func (w *waitForReadyConfig) waitForReadyCondition(start time.Time, name string, timeout time.Duration, msgCallback MessageCallback) (retry bool, timeoutReached bool, err error) {
func (w *waitForReadyConfig) waitForReadyCondition(start time.Time, name string, timeout time.Duration, errorWindow time.Duration, msgCallback MessageCallback) (retry bool, timeoutReached bool, err error) {

watcher, err := w.watchMaker(name, timeout)
if err != nil {
return false, false, err
}

defer watcher.Stop()

// channel used to transport the error
errChan := make(chan error)
var errorTimer *time.Timer
// Stop any error time if any has started
defer (func() {
if errorTimer != nil {
errorTimer.Stop()
}
})()

for {
select {
case <-time.After(timeout):
return false, true, nil
case err = <-errChan:
return false, false, err
case event, ok := <-watcher.ResultChan():
if !ok || event.Object == nil {
return true, false, nil
Expand All @@ -140,7 +156,12 @@ func (w *waitForReadyConfig) waitForReadyCondition(start time.Time, name string,
case corev1.ConditionTrue:
return false, false, nil
case corev1.ConditionFalse:
return false, false, fmt.Errorf("%s: %s", cond.Reason, cond.Message)
// Fire up timer waiting for the duration of the error window for allowing to reconcile
// to a true condition after the condition went to false. If this is not the case within
// this window, then an error is returned.
errorTimer = time.AfterFunc(errorWindow, func() {
errChan <- fmt.Errorf("%s: %s", cond.Reason, cond.Message)
})
}
if cond.Message != "" {
msgCallback(time.Since(start), cond.Message)
Expand Down
9 changes: 9 additions & 0 deletions pkg/wait/wait_for_ready_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func prepareTestCases(name string) []waitForReadyTestCase {
tc(peError, name, true),
tc(peWrongGeneration, name, true),
tc(peTimeout, name, true),
tc(peReadFalseWithinErrorWindow, name, false),
}
}

Expand Down Expand Up @@ -131,3 +132,11 @@ func peWrongGeneration(name string) ([]watch.Event, int) {
{watch.Modified, CreateTestServiceWithConditions(name, corev1.ConditionTrue, corev1.ConditionTrue, "", "", 1, 2)},
}, len(messages)
}

func peReadFalseWithinErrorWindow(name string) ([]watch.Event, int) {
messages := pMessages(1)
return []watch.Event{
{watch.Added, CreateTestServiceWithConditions(name, corev1.ConditionFalse, corev1.ConditionFalse, "Route not ready", messages[0])},
{watch.Modified, CreateTestServiceWithConditions(name, corev1.ConditionTrue, corev1.ConditionTrue, "Route ready", "")},
}, len(messages)
}

0 comments on commit be0267c

Please sign in to comment.