mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
fix(api): bound live release reads in application watches
A stalled live release read holds the whole application watch event loop.
End the watch when a refresh runs out of time so clients can reconnect
without relying on stale cached release data.
Fixes: 0ccb3fead7 ("fix(api): retry the live release read behind a monitor event")
Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
This commit is contained in:
2 files changed
+163
-6
No files matched your search
@@ -777,11 +777,11 @@ func newerResourceVersion(a, b string) string {
|
||||
return a
|
||||
}
|
||||
|
||||
// Bounded so that an apiserver outage drops the monitor event instead of
|
||||
// holding the watch goroutine.
|
||||
// Watches bypass the API server's default one-minute request timeout.
|
||||
const (
|
||||
liveReleaseReadAttempts = 3
|
||||
liveReleaseReadRetryDelay = 100 * time.Millisecond
|
||||
liveReleaseReadTimeout = time.Minute
|
||||
)
|
||||
|
||||
// Watch sets up a watch on HelmReleases, filters them based on application labels, and converts events to Applications
|
||||
@@ -1189,14 +1189,22 @@ func (r *REST) Watch(ctx context.Context, options *metainternalversion.ListOptio
|
||||
// Nothing replays a monitor event this watch drops, so a
|
||||
// transient failure of the live read is retried briefly.
|
||||
// A missing release is final.
|
||||
liveErr := r.w.Get(readCtx, hrKey, hr)
|
||||
liveCtx, cancelLiveRead := context.WithTimeout(readCtx, liveReleaseReadTimeout)
|
||||
liveErr := r.w.Get(liveCtx, hrKey, hr)
|
||||
for attempt := 1; liveErr != nil && !apierrors.IsNotFound(liveErr) && attempt < liveReleaseReadAttempts; attempt++ {
|
||||
select {
|
||||
case <-readCtx.Done():
|
||||
return
|
||||
case <-liveCtx.Done():
|
||||
case <-time.After(liveReleaseReadRetryDelay):
|
||||
}
|
||||
liveErr = r.w.Get(readCtx, hrKey, hr)
|
||||
if liveCtx.Err() != nil {
|
||||
break
|
||||
}
|
||||
liveErr = r.w.Get(liveCtx, hrKey, hr)
|
||||
}
|
||||
readEnded := liveCtx.Err() != nil
|
||||
cancelLiveRead()
|
||||
if readEnded {
|
||||
return
|
||||
}
|
||||
if liveErr != nil {
|
||||
klog.V(4).Infof("Cannot find HelmRelease %s/%s for WorkloadMonitor event: %v", hrNS, hrName, liveErr)
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
|
||||
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/apiserver/pkg/endpoints/request"
|
||||
)
|
||||
|
||||
func expectLiveReadWatchClosed(t *testing.T, w watch.Interface) {
|
||||
t.Helper()
|
||||
select {
|
||||
case event, open := <-w.ResultChan():
|
||||
if open {
|
||||
t.Fatalf("unexpected %s event instead of watch closure", event.Type)
|
||||
}
|
||||
default:
|
||||
t.Fatal("watch remains open after the live-read deadline")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWatchHungLiveReadDeadlineClosesAndReconnects(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
f := newRVWatchFixture(t)
|
||||
parent, w := startRVWatch(t, f)
|
||||
cached := f.liveRelease(t)
|
||||
f.hrW.Modify(cached.DeepCopy())
|
||||
resume := nextApplication(t, w).ResourceVersion
|
||||
live := f.advanceLive(t, "before-timeout")
|
||||
readsBefore := f.liveReleaseGets
|
||||
entered := make(chan context.Context, 1)
|
||||
f.liveReleaseErrs = []error{errors.New("live read failed")}
|
||||
f.onLiveReleaseError = func(ctx context.Context) {
|
||||
entered <- ctx
|
||||
<-ctx.Done()
|
||||
}
|
||||
|
||||
readStarted := time.Now()
|
||||
f.monitorEvent(live.ResourceVersion)
|
||||
synctest.Wait()
|
||||
var liveCtx context.Context
|
||||
select {
|
||||
case liveCtx = <-entered:
|
||||
default:
|
||||
t.Fatal("live read did not start")
|
||||
}
|
||||
time.Sleep(time.Minute)
|
||||
synctest.Wait()
|
||||
expectLiveReadWatchClosed(t, w)
|
||||
if !errors.Is(liveCtx.Err(), context.DeadlineExceeded) {
|
||||
t.Fatalf("live read ended with %v, want its own deadline", liveCtx.Err())
|
||||
}
|
||||
deadline, bounded := liveCtx.Deadline()
|
||||
if !bounded || deadline.Sub(readStarted) != time.Minute {
|
||||
t.Fatalf("live read budget %v, bounded=%t, want exactly one minute", deadline.Sub(readStarted), bounded)
|
||||
}
|
||||
if parent.Err() != nil {
|
||||
t.Fatalf("watch parent unexpectedly ended: %v", parent.Err())
|
||||
}
|
||||
if got := f.liveReleaseGets - readsBefore; got != 1 {
|
||||
t.Fatalf("live release reads %d, want one bounded hung read", got)
|
||||
}
|
||||
|
||||
f.hrW, f.wmW = watch.NewFake(), watch.NewFake()
|
||||
fresh := f.advanceLive(t, "after-reconnect")
|
||||
reconnected := startRVWatchFrom(t, f, resume)
|
||||
f.monitorEvent(fresh.ResourceVersion)
|
||||
app := nextApplication(t, reconnected)
|
||||
if got := specMarker(t, app); got != "after-reconnect" {
|
||||
t.Fatalf("reconnected spec marker %q, want fresh live state", got)
|
||||
}
|
||||
if app.ResourceVersion != fresh.ResourceVersion {
|
||||
t.Fatalf("reconnected version %q, want %q", app.ResourceVersion, fresh.ResourceVersion)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestWatchLiveReadDeadlineIncludesRetries(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
f := newRVWatchFixture(t)
|
||||
_, w := startRVWatch(t, f)
|
||||
live := f.advanceLive(t, "live")
|
||||
readsBefore := f.liveReleaseGets
|
||||
f.liveReleaseErrs = []error{errors.New("temporary live read failure")}
|
||||
f.onLiveReleaseError = func(context.Context) {
|
||||
time.Sleep(45 * time.Second)
|
||||
f.liveReleaseHang = true
|
||||
}
|
||||
|
||||
f.monitorEvent(live.ResourceVersion)
|
||||
synctest.Wait()
|
||||
time.Sleep(time.Minute)
|
||||
synctest.Wait()
|
||||
expectLiveReadWatchClosed(t, w)
|
||||
if got := f.liveReleaseGets - readsBefore; got != 2 {
|
||||
t.Fatalf("live release reads %d, want one slow failure and one hung retry", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestWatchCancellationEndsHungLiveReadBeforeDeadline(t *testing.T) {
|
||||
for _, mode := range []string{"stop", "context"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
f := newRVWatchFixture(t)
|
||||
parent, cancel := context.WithCancel(request.WithNamespace(context.Background(), "default"))
|
||||
t.Cleanup(cancel)
|
||||
w, err := f.rest.Watch(parent, &metainternalversion.ListOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(w.Stop)
|
||||
live := f.advanceLive(t, "live")
|
||||
readsBefore := f.liveReleaseGets
|
||||
entered := make(chan context.Context, 1)
|
||||
f.liveReleaseErrs = []error{errors.New("live read failed")}
|
||||
f.onLiveReleaseError = func(ctx context.Context) {
|
||||
entered <- ctx
|
||||
<-ctx.Done()
|
||||
}
|
||||
f.monitorEvent(live.ResourceVersion)
|
||||
synctest.Wait()
|
||||
liveCtx := <-entered
|
||||
before := time.Now()
|
||||
if mode == "stop" {
|
||||
w.Stop()
|
||||
} else {
|
||||
cancel()
|
||||
}
|
||||
synctest.Wait()
|
||||
expectLiveReadWatchClosed(t, w)
|
||||
if !errors.Is(liveCtx.Err(), context.Canceled) {
|
||||
t.Fatalf("live read ended with %v, want cancellation", liveCtx.Err())
|
||||
}
|
||||
if time.Since(before) != 0 {
|
||||
t.Fatal("cancellation waited for a timer")
|
||||
}
|
||||
if got := f.liveReleaseGets - readsBefore; got != 1 {
|
||||
t.Fatalf("live release reads %d, want exactly one", got)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user