mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
fix(api): retry the live release read behind a monitor event
When the cached release is older than the monitor revision, the watch
reads the release live so that an event never pairs a stale spec with
the monitor's version. A transient error on that read dropped the
monitor event. Both streams are plain API watches, so nothing replayed
it, and a client kept the old WorkloadsReady state until the next
change of either object.
Retry the live read a few times before giving the event up. This
covers a failure shorter than the retry window; a longer one still
drops the event, as a cache miss did before. A release that is gone is
not retried: the event is dropped after one read. The live reads run
under a context that Stop cancels, so a stopped watch does not wait on
the API server. The tests also pin the two paths that were untested: a
release the cache has not seen yet is read live and delivered, and a
release that is gone everywhere produces no event.
Fixes: f13362b0ee ("fix(api): make monitor watch versions usable as delete preconditions")
Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
This commit is contained in:
2 files changed
+214
-3
No files matched your search
@@ -777,6 +777,13 @@ func newerResourceVersion(a, b string) string {
|
|||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bounded so that an apiserver outage drops the monitor event instead of
|
||||||
|
// holding the watch goroutine.
|
||||||
|
const (
|
||||||
|
liveReleaseReadAttempts = 3
|
||||||
|
liveReleaseReadRetryDelay = 100 * time.Millisecond
|
||||||
|
)
|
||||||
|
|
||||||
// Watch sets up a watch on HelmReleases, filters them based on application labels, and converts events to Applications
|
// Watch sets up a watch on HelmReleases, filters them based on application labels, and converts events to Applications
|
||||||
func (r *REST) Watch(ctx context.Context, options *metainternalversion.ListOptions) (watch.Interface, error) {
|
func (r *REST) Watch(ctx context.Context, options *metainternalversion.ListOptions) (watch.Interface, error) {
|
||||||
namespace, err := r.getNamespace(ctx)
|
namespace, err := r.getNamespace(ctx)
|
||||||
@@ -916,6 +923,17 @@ func (r *REST) Watch(ctx context.Context, options *metainternalversion.ListOptio
|
|||||||
if wmWatcherForCleanup != nil {
|
if wmWatcherForCleanup != nil {
|
||||||
defer wmWatcherForCleanup.Stop()
|
defer wmWatcherForCleanup.Stop()
|
||||||
}
|
}
|
||||||
|
// Live reads use a context that Stop cancels, so a stopped watch
|
||||||
|
// never waits on the API server.
|
||||||
|
readCtx, cancelReads := context.WithCancel(ctx)
|
||||||
|
defer cancelReads()
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-customW.stopChan:
|
||||||
|
cancelReads()
|
||||||
|
case <-readCtx.Done():
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Buffer of WorkloadMonitor events that arrived before the initial
|
// Buffer of WorkloadMonitor events that arrived before the initial
|
||||||
// snapshot finished. The watch-list contract requires the stream to
|
// snapshot finished. The watch-list contract requires the stream to
|
||||||
@@ -1168,8 +1186,20 @@ func (r *REST) Watch(ctx context.Context, options *metainternalversion.ListOptio
|
|||||||
hr := &helmv2.HelmRelease{}
|
hr := &helmv2.HelmRelease{}
|
||||||
if err := r.c.Get(ctx, hrKey, hr); err != nil || newerResourceVersion(wm.GetResourceVersion(), hr.GetResourceVersion()) != hr.GetResourceVersion() {
|
if err := r.c.Get(ctx, hrKey, hr); err != nil || newerResourceVersion(wm.GetResourceVersion(), hr.GetResourceVersion()) != hr.GetResourceVersion() {
|
||||||
hr = &helmv2.HelmRelease{}
|
hr = &helmv2.HelmRelease{}
|
||||||
if err := r.w.Get(ctx, hrKey, hr); err != nil {
|
// Nothing replays a monitor event this watch drops, so a
|
||||||
klog.V(4).Infof("Cannot find HelmRelease %s/%s for WorkloadMonitor event: %v", hrNS, hrName, err)
|
// transient failure of the live read is retried briefly.
|
||||||
|
// A missing release is final.
|
||||||
|
liveErr := r.w.Get(readCtx, hrKey, hr)
|
||||||
|
for attempt := 1; liveErr != nil && !apierrors.IsNotFound(liveErr) && attempt < liveReleaseReadAttempts; attempt++ {
|
||||||
|
select {
|
||||||
|
case <-readCtx.Done():
|
||||||
|
return
|
||||||
|
case <-time.After(liveReleaseReadRetryDelay):
|
||||||
|
}
|
||||||
|
liveErr = r.w.Get(readCtx, hrKey, hr)
|
||||||
|
}
|
||||||
|
if liveErr != nil {
|
||||||
|
klog.V(4).Infof("Cannot find HelmRelease %s/%s for WorkloadMonitor event: %v", hrNS, hrName, liveErr)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package application
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -37,6 +38,15 @@ type rvWatchFixture struct {
|
|||||||
// watch goroutine and the test both update it; receiving an event from
|
// watch goroutine and the test both update it; receiving an event from
|
||||||
// the watch orders the watch's updates before the test's next access.
|
// the watch orders the watch's updates before the test's next access.
|
||||||
liveReleaseGets int
|
liveReleaseGets int
|
||||||
|
// liveReleaseErrs are returned by the next live HelmRelease reads, one
|
||||||
|
// per read, before reads reach the store again.
|
||||||
|
liveReleaseErrs []error
|
||||||
|
// liveReleaseHang makes live HelmRelease reads block until their context
|
||||||
|
// ends, like a request to an API server that never answers.
|
||||||
|
liveReleaseHang bool
|
||||||
|
// cacheStore is the store behind cache, for tests that leave the cache
|
||||||
|
// without the release.
|
||||||
|
cacheStore client.WithWatch
|
||||||
}
|
}
|
||||||
|
|
||||||
var rvReleaseKey = client.ObjectKey{Namespace: "default", Name: "postgresql-mydb"}
|
var rvReleaseKey = client.ObjectKey{Namespace: "default", Name: "postgresql-mydb"}
|
||||||
@@ -71,6 +81,15 @@ func newRVWatchFixture(t *testing.T) *rvWatchFixture {
|
|||||||
Get: func(ctx context.Context, c client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
|
Get: func(ctx context.Context, c client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
|
||||||
if _, ok := obj.(*helmv2.HelmRelease); ok {
|
if _, ok := obj.(*helmv2.HelmRelease); ok {
|
||||||
f.liveReleaseGets++
|
f.liveReleaseGets++
|
||||||
|
if f.liveReleaseHang {
|
||||||
|
<-ctx.Done()
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
if len(f.liveReleaseErrs) > 0 {
|
||||||
|
err := f.liveReleaseErrs[0]
|
||||||
|
f.liveReleaseErrs = f.liveReleaseErrs[1:]
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return c.Get(ctx, key, obj, opts...)
|
return c.Get(ctx, key, obj, opts...)
|
||||||
},
|
},
|
||||||
@@ -81,7 +100,8 @@ func newRVWatchFixture(t *testing.T) *rvWatchFixture {
|
|||||||
return f.hrW, nil
|
return f.hrW, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
f.cache = interceptor.NewClient(fake.NewClientBuilder().WithScheme(scheme).WithObjects(release()).Build(), interceptor.Funcs{
|
f.cacheStore = fake.NewClientBuilder().WithScheme(scheme).WithObjects(release()).Build()
|
||||||
|
f.cache = interceptor.NewClient(f.cacheStore, interceptor.Funcs{
|
||||||
Delete: func(ctx context.Context, _ client.WithWatch, obj client.Object, opts ...client.DeleteOption) error {
|
Delete: func(ctx context.Context, _ client.WithWatch, obj client.Object, opts ...client.DeleteOption) error {
|
||||||
return liveStore.Delete(ctx, obj, opts...)
|
return liveStore.Delete(ctx, obj, opts...)
|
||||||
},
|
},
|
||||||
@@ -312,6 +332,167 @@ func TestWatchMonitorEventCarriesReleaseAfterFallback(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// expectNoApplication fails if the watch delivers anything within the wait,
|
||||||
|
// then sends a release event and receives it so that the watch goroutine's
|
||||||
|
// earlier bookkeeping is ordered before the caller's next read of it.
|
||||||
|
func expectNoApplication(t *testing.T, f *rvWatchFixture, w watch.Interface, release *helmv2.HelmRelease) {
|
||||||
|
t.Helper()
|
||||||
|
select {
|
||||||
|
case ev := <-w.ResultChan():
|
||||||
|
t.Fatalf("unexpected %s event", ev.Type)
|
||||||
|
case <-time.After((liveReleaseReadAttempts + 2) * liveReleaseReadRetryDelay):
|
||||||
|
}
|
||||||
|
f.hrW.Modify(release)
|
||||||
|
nextApplication(t, w)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A transient failure of the live read must not cost the client the monitor
|
||||||
|
// change: nothing replays it, so the read is retried.
|
||||||
|
func TestWatchMonitorEventRetriesLiveRead(t *testing.T) {
|
||||||
|
f := newRVWatchFixture(t)
|
||||||
|
_, w := startRVWatch(t, f)
|
||||||
|
live := f.advanceLive(t, "live")
|
||||||
|
readsBefore := f.liveReleaseGets
|
||||||
|
timeout := errors.New("etcdserver: request timed out")
|
||||||
|
f.liveReleaseErrs = []error{timeout, timeout}
|
||||||
|
|
||||||
|
f.monitorEvent(live.ResourceVersion)
|
||||||
|
app := nextApplication(t, w)
|
||||||
|
if got := f.liveReleaseGets - readsBefore; got != 3 {
|
||||||
|
t.Fatalf("live release reads %d, want two failed attempts and one success", got)
|
||||||
|
}
|
||||||
|
if got := specMarker(t, app); got != "live" {
|
||||||
|
t.Fatalf("spec marker %q, want the live spec", got)
|
||||||
|
}
|
||||||
|
if app.ResourceVersion != live.ResourceVersion {
|
||||||
|
t.Fatalf("resourceVersion %q, want the live release's %q", app.ResourceVersion, live.ResourceVersion)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A read that keeps failing is given up after the last attempt, so a broken
|
||||||
|
// apiserver cannot hold the watch goroutine.
|
||||||
|
func TestWatchMonitorEventGivesUpAfterRetries(t *testing.T) {
|
||||||
|
f := newRVWatchFixture(t)
|
||||||
|
_, w := startRVWatch(t, f)
|
||||||
|
live := f.advanceLive(t, "live")
|
||||||
|
readsBefore := f.liveReleaseGets
|
||||||
|
timeout := errors.New("etcdserver: request timed out")
|
||||||
|
for range liveReleaseReadAttempts {
|
||||||
|
f.liveReleaseErrs = append(f.liveReleaseErrs, timeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.monitorEvent(live.ResourceVersion)
|
||||||
|
expectNoApplication(t, f, w, live)
|
||||||
|
if got := f.liveReleaseGets - readsBefore; got != liveReleaseReadAttempts {
|
||||||
|
t.Fatalf("live release reads %d, want %d attempts and no more", got, liveReleaseReadAttempts)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// stopDuringLiveReadRetry queues a failure for every attempt, sends a monitor
|
||||||
|
// event and calls stop while the watch waits between attempts. The watch must
|
||||||
|
// close after the one read made before the stop.
|
||||||
|
func stopDuringLiveReadRetry(t *testing.T, f *rvWatchFixture, w watch.Interface, stop func()) {
|
||||||
|
t.Helper()
|
||||||
|
live := f.advanceLive(t, "live")
|
||||||
|
readsBefore := f.liveReleaseGets
|
||||||
|
timeout := errors.New("etcdserver: request timed out")
|
||||||
|
for range liveReleaseReadAttempts {
|
||||||
|
f.liveReleaseErrs = append(f.liveReleaseErrs, timeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.monitorEvent(live.ResourceVersion)
|
||||||
|
stop()
|
||||||
|
select {
|
||||||
|
case ev, ok := <-w.ResultChan():
|
||||||
|
if ok {
|
||||||
|
t.Fatalf("unexpected %s event", ev.Type)
|
||||||
|
}
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
t.Fatal("watch still open")
|
||||||
|
}
|
||||||
|
if got := f.liveReleaseGets - readsBefore; got != 1 {
|
||||||
|
t.Fatalf("live release reads %d, want the one before the stop", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWatchStopEndsLiveReadRetry(t *testing.T) {
|
||||||
|
f := newRVWatchFixture(t)
|
||||||
|
_, w := startRVWatch(t, f)
|
||||||
|
stopDuringLiveReadRetry(t, f, w, w.Stop)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWatchContextCancelEndsLiveReadRetry(t *testing.T) {
|
||||||
|
f := newRVWatchFixture(t)
|
||||||
|
ctx, cancel := context.WithCancel(request.WithNamespace(context.Background(), "default"))
|
||||||
|
t.Cleanup(cancel)
|
||||||
|
w, err := f.rest.Watch(ctx, &metainternalversion.ListOptions{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Cleanup(w.Stop)
|
||||||
|
stopDuringLiveReadRetry(t, f, w, cancel)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A live read that never answers must not outlive the watch: Stop cancels
|
||||||
|
// the read's context, and the watch closes.
|
||||||
|
func TestWatchStopCancelsHungLiveRead(t *testing.T) {
|
||||||
|
f := newRVWatchFixture(t)
|
||||||
|
_, w := startRVWatch(t, f)
|
||||||
|
live := f.advanceLive(t, "live")
|
||||||
|
f.liveReleaseHang = true
|
||||||
|
|
||||||
|
f.monitorEvent(live.ResourceVersion)
|
||||||
|
w.Stop()
|
||||||
|
select {
|
||||||
|
case ev, ok := <-w.ResultChan():
|
||||||
|
if ok {
|
||||||
|
t.Fatalf("unexpected %s event", ev.Type)
|
||||||
|
}
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
t.Fatal("watch still open while a live read hangs")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A release that is gone is final: the event is dropped after one read,
|
||||||
|
// without retries that would delay the next event.
|
||||||
|
func TestWatchMonitorEventDroppedWhenReleaseIsGone(t *testing.T) {
|
||||||
|
f := newRVWatchFixture(t)
|
||||||
|
_, w := startRVWatch(t, f)
|
||||||
|
release := f.liveRelease(t)
|
||||||
|
for _, c := range []client.Client{f.cacheStore, f.live} {
|
||||||
|
if err := c.Delete(context.Background(), release.DeepCopy()); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
readsBefore := f.liveReleaseGets
|
||||||
|
|
||||||
|
f.monitorEvent(strconv.FormatUint(rvNumber(t, release.ResourceVersion)+1, 10))
|
||||||
|
expectNoApplication(t, f, w, release)
|
||||||
|
if got := f.liveReleaseGets - readsBefore; got != 1 {
|
||||||
|
t.Fatalf("live release reads %d, want a single read", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A release the cache has not seen yet is read live and delivered.
|
||||||
|
func TestWatchMonitorEventReadsReleaseMissingFromCache(t *testing.T) {
|
||||||
|
f := newRVWatchFixture(t)
|
||||||
|
_, w := startRVWatch(t, f)
|
||||||
|
live := f.advanceLive(t, "live")
|
||||||
|
if err := f.cacheStore.Delete(context.Background(), live.DeepCopy()); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
readsBefore := f.liveReleaseGets
|
||||||
|
|
||||||
|
f.monitorEvent(live.ResourceVersion)
|
||||||
|
app := nextApplication(t, w)
|
||||||
|
if got := f.liveReleaseGets - readsBefore; got != 1 {
|
||||||
|
t.Fatalf("live release reads %d, want one", got)
|
||||||
|
}
|
||||||
|
if got := specMarker(t, app); got != "live" {
|
||||||
|
t.Fatalf("spec marker %q, want the live spec", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// A release event in between makes the release version the held one again.
|
// A release event in between makes the release version the held one again.
|
||||||
func TestWatchReleaseEventResetsHeldVersion(t *testing.T) {
|
func TestWatchReleaseEventResetsHeldVersion(t *testing.T) {
|
||||||
f := newRVWatchFixture(t)
|
f := newRVWatchFixture(t)
|
||||||
|
|||||||
Reference in new issue
Block a user