carlos / platform Public

console: a failed engine read is not retried plainly (R-41)

engineReads.get returns a failed conditional GET's error, which the source
already treats as unread, rather than reading the object again
unconditionally; a failed ListMod returns its error rather than listing
again. A store without either (errors.ErrUnsupported, a capability) is
still read plainly. The request test now also counts passes with a failing
alert object, a failing heartbeat listing and a failing heartbeat object.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 6669ad0e08d1f402c9798aead7ffe4ed854b8794 parent 1a8ce24
2 files changed, +86 −16
  • internal/console/alertengine_pass_test.go +68 −11
  • internal/console/alertsignals.go +18 −5
diff --git a/internal/console/alertengine_pass_test.go b/internal/console/alertengine_pass_test.go
index 9cd5bdcd4..db7f33bcb 100644
--- a/internal/console/alertengine_pass_test.go
+++ b/internal/console/alertengine_pass_test.go
@@ -3,6 +3,7 @@ package console
import (
"context"
"encoding/json"
+ "errors"
"fmt"
"os"
"path/filepath"
@@ -12,6 +13,7 @@ import (
"time"
"github.com/carlosframework/platform/internal/metrics"
+ "github.com/carlosframework/platform/internal/release"
"github.com/carlosframework/platform/internal/status"
)
@@ -178,16 +180,78 @@ func TestEnginePassFiveCycleRequests(t *testing.T) {
ef.fleetServices(t, map[string]bool{"keymail": true})
ef.passAt(t, 0) // warm-up: the first reads, and the first heartbeat pass
- rec := recordS3(ef.lat, nil)
- ef.passesTo(t, time.Minute, 5*time.Minute)
+ want := map[string]int{"LIST alerts/boxes/": 5, "LIST status/boxes/": 1, "GET status/fleet.json": 1}
+ for i := 1; i <= alerts; i++ {
+ want[fmt.Sprintf("GET alerts/boxes/box-%d.json", i)] = 5
+ }
+ for i := 1; i <= heartbeats; i++ {
+ want[fmt.Sprintf("GET status/boxes/box-%d.json", i)] = 1
+ }
+ ef.fiveCycleRequests(t, "5 idle passes", time.Minute, nil, want)
+
+ // Ruling R-41: a failed conditional read is not read again plainly, and
+ // a failed listing with times is not listed again plainly. An alert
+ // object and the status/boxes/ listing fail for five passes: the same
+ // requests, the failed ones once each.
+ ef.fiveCycleRequests(t, "5 passes with an alert object and the heartbeat listing failing", 6*time.Minute,
+ func(q s3Req) bool {
+ return q.Key == "alerts/boxes/box-1.json" || (q.Key == "" && q.Prefix == alertStatusBoxesPrefix)
+ },
+ map[string]int{"LIST alerts/boxes/": 5, "LIST status/boxes/": 1, "GET status/fleet.json": 1,
+ "GET alerts/boxes/box-1.json": 5, "GET alerts/boxes/box-2.json": 5, "GET alerts/boxes/box-3.json": 5})
+
+ // A heartbeat object failing costs its one read a heartbeat pass.
+ want["GET alerts/boxes/box-1.json"] = 5
+ ef.fiveCycleRequests(t, "5 passes with a heartbeat object failing", 11*time.Minute,
+ func(q s3Req) bool { return q.Key == "status/boxes/box-1.json" }, want)
+}
+
+// unsupportedReads is a store whose conditional read and timed listing
+// answer errors.ErrUnsupported, as a wrapper over a store without them
+// does: a missing capability, not a failure.
+type unsupportedReads struct{ *release.DirStore }
+
+func (unsupportedReads) GetIfChanged(context.Context, string, string) ([]byte, string, bool, error) {
+ return nil, "", false, errors.ErrUnsupported
+}
+
+func (unsupportedReads) ListMod(context.Context, string) ([]release.KeyMod, error) {
+ return nil, errors.ErrUnsupported
+}
+// Ruling R-41: a store without the conditional read or the timed listing
+// is still read, plainly; only a failure is not retried.
+func TestEngineReadsUnsupportedFallsBackToPlain(t *testing.T) {
+ ctx := context.Background()
+ store := unsupportedReads{release.NewDirStore(t.TempDir())}
+ if _, err := store.Put(ctx, "status/boxes/box-1.json", []byte(`{}`), ""); err != nil {
+ t.Fatal(err)
+ }
+ var r engineReads
+ body, changed, err := r.get(ctx, store, "status/boxes/box-1.json")
+ if err != nil || !changed || string(body) != `{}` {
+ t.Fatalf("get = %q, %v, %v; want the object read plainly", body, changed, err)
+ }
+ keys, err := engineBoxKeyList(ctx, store, alertT0)
+ if err != nil || len(keys) != 1 {
+ t.Fatalf("listing = %v, %v; want the object listed plainly", keys, err)
+ }
+}
+
+// fiveCycleRequests runs passes a minute apart from +from to +from+4m with
+// requests matching fail refused, and checks every bucket request they made
+// against want: each GET conditional, LISTs by prefix.
+func (ef *engineFixture) fiveCycleRequests(t *testing.T, what string, from time.Duration, fail func(s3Req) bool, want map[string]int) {
+ t.Helper()
+ rec := recordS3(ef.lat, fail)
+ ef.passesTo(t, from, from+4*time.Minute)
got := map[string]int{}
for _, q := range rec.all() {
switch {
case q.Key == "" && q.Method == "GET":
got["LIST "+q.Prefix]++
case q.Method == "GET" && q.IfNoneMatch == "":
- t.Errorf("unconditional GET %s", q.Key)
+ t.Errorf("%s: unconditional GET %s", what, q.Key)
got["GET "+q.Key]++
case q.Method == "GET":
got["GET "+q.Key]++
@@ -195,15 +259,8 @@ func TestEnginePassFiveCycleRequests(t *testing.T) {
got[q.Method+" "+q.Key]++
}
}
- want := map[string]int{"LIST alerts/boxes/": 5, "LIST status/boxes/": 1, "GET status/fleet.json": 1}
- for i := 1; i <= alerts; i++ {
- want[fmt.Sprintf("GET alerts/boxes/box-%d.json", i)] = 5
- }
- for i := 1; i <= heartbeats; i++ {
- want[fmt.Sprintf("GET status/boxes/box-%d.json", i)] = 1
- }
if fmt.Sprint(got) != fmt.Sprint(want) {
- t.Fatalf("requests over 5 idle passes:\n got %v\nwant %v", got, want)
+ t.Fatalf("requests over %s:\n got %v\nwant %v", what, got, want)
}
}
diff --git a/internal/console/alertsignals.go b/internal/console/alertsignals.go
index 829a751f3..e91b817aa 100644
--- a/internal/console/alertsignals.go
+++ b/internal/console/alertsignals.go
@@ -69,9 +69,13 @@ type engineReads struct {
// get reads key, conditionally once it has been read before: an unchanged
// object answers from the cache with changed false. A store without the
-// conditional read, or one whose conditional read fails other than with
-// not-found, is read plainly, as condGet does (provision/recordview.go). A
-// gone object leaves the cache and returns an os.ErrNotExist error.
+// conditional read is read plainly, as is one that answers
+// errors.ErrUnsupported (a wrapper over a store without it): that is a
+// capability, not a failure. A conditional read that fails returns
+// its error, which the caller treats as the source unread, and is not
+// tried again plainly (ruling R-41): a failure must not cost a second
+// request every pass. A gone object leaves the cache and returns an
+// os.ErrNotExist error.
func (r *engineReads) get(ctx context.Context, store release.Store, key string) (body []byte, changed bool, err error) {
prev, cached := r.cache[key]
if g, ok := store.(release.ConditionalGetter); ok {
@@ -85,7 +89,11 @@ func (r *engineReads) get(ctx context.Context, store release.Store, key string)
case errors.Is(err, os.ErrNotExist):
delete(r.cache, key)
return nil, false, err
+ case err != nil && !errors.Is(err, errors.ErrUnsupported):
+ return nil, false, err
}
+ // Unsupported, or unchanged with nothing cached to answer from:
+ // read it plainly.
}
b, etag, err := store.Get(ctx, key)
if err != nil {
@@ -169,16 +177,21 @@ func (r *engineReads) boxes(ctx context.Context, store release.Store, now time.T
// engineBoxKeyList is one listing of status/boxes/ without the objects not
// written within the horizon of now (the pass's clock), as status.LoadBoxes
// lists it. A store that cannot say when an object was written is listed
-// whole.
+// whole, as is one whose listing with times answers errors.ErrUnsupported.
+// A listing with times that fails returns its error and is not listed
+// again plainly (ruling R-41).
func engineBoxKeyList(ctx context.Context, store release.Store, now time.Time) ([]string, error) {
ml, ok := store.(release.ModLister)
if !ok {
return store.List(ctx, alertStatusBoxesPrefix)
}
mods, err := ml.ListMod(ctx, alertStatusBoxesPrefix)
- if err != nil {
+ if errors.Is(err, errors.ErrUnsupported) {
return store.List(ctx, alertStatusBoxesPrefix)
}
+ if err != nil {
+ return nil, err
+ }
cut := now.Add(-engineListingHorizon)
keys := make([]string, 0, len(mods))
for _, m := range mods {