carlos / platform Public

drain: bound the waiting block's bytes, not only its names (#1062)

Astra P2: three names per count did not bound the status answer. An
owed key that is not a lock's is named whole, and JSON escapes '<' to
six bytes, so hostile keys pushed group-maintenance status past its
8192 bytes. Each name is now cut at 128 bytes, marked "(cut)", and
names are dropped from the last count first until the waiting block
encodes within 2048 bytes. truncated says so; the counts stay exact.
The log and the refusal use the same projection.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 7f5f642aa878d08472887813f4efd092da378851 parent 1f2c336
2 files changed, +119 −7
  • internal/host/hadrain.go +63 −7
  • internal/host/hadrain_quiet_linux_test.go +56 −0

Intent

diff --git a/internal/host/hadrain.go b/internal/host/hadrain.go
index e6688c3a3..38247d378 100644
--- a/internal/host/hadrain.go
+++ b/internal/host/hadrain.go
@@ -28,6 +28,7 @@ import (
"context"
"crypto/rand"
"encoding/hex"
+ "encoding/json"
"errors"
"fmt"
"log"
@@ -35,6 +36,7 @@ import (
"strings"
"sync"
"time"
+ "unicode/utf8"
"github.com/carlosframework/platform/internal/activator"
"github.com/carlosframework/platform/internal/boxchoice"
@@ -429,17 +431,73 @@ type drainWaiting struct {
// Held is settle's entries still here, by host.
Held []string `json:"held"`
HeldCount int `json:"held_count"`
+ // Truncated is set when a name was cut short or left out to keep the
+ // block within drainWaitingMaxBytes; the counts are always whole.
+ Truncated bool `json:"truncated"`
}
// drainNamesMax is how many hosts the log, the refusal and the status
-// name per count; the counts themselves are whole.
-const drainNamesMax = 3
+// name per count; the counts themselves are whole. drainNameMaxBytes
+// bounds each name (an owed key that is not a lock's is named whole), and
+// drainWaitingMaxBytes the waiting block's JSON, well inside the status
+// verb's 8192-byte answer.
+const (
+ drainNamesMax = 3
+ drainNameMaxBytes = 128
+ drainWaitingMaxBytes = 2048
+ // draft for copy review (D1062-5)
+ drainNameCut = "(cut)"
+)
-func firstNames(names []string) []string {
+// firstNames is the first drainNamesMax names, each cut at
+// drainNameMaxBytes (on a rune boundary, marked drainNameCut).
+func firstNames(names []string, cut *bool) []string {
if len(names) > drainNamesMax {
names = names[:drainNamesMax]
}
- return append([]string{}, names...)
+ out := make([]string, 0, len(names))
+ for _, n := range names {
+ if len(n) > drainNameMaxBytes {
+ i := drainNameMaxBytes
+ for i > 0 && !utf8.RuneStart(n[i]) {
+ i--
+ }
+ n = n[:i] + drainNameCut
+ *cut = true
+ }
+ out = append(out, n)
+ }
+ return out
+}
+
+// newDrainWaiting is the projection the log, the refusal and the status
+// share: at most drainNamesMax names per count, each bounded, and names
+// dropped from the last count first until the encoded block fits
+// drainWaitingMaxBytes. The counts stay whole.
+func newDrainWaiting(step string, since time.Time, q activator.DrainQuiet, names activator.DrainQuietHosts, held []string) *drainWaiting {
+ w := &drainWaiting{Step: step, Since: since, Quiet: q, HeldCount: len(held)}
+ w.Held = firstNames(held, &w.Truncated)
+ w.Hosts = activator.DrainQuietHosts{Waking: firstNames(names.Waking, &w.Truncated), Stopping: firstNames(names.Stopping, &w.Truncated),
+ Recovering: firstNames(names.Recovering, &w.Truncated), Owed: firstNames(names.Owed, &w.Truncated)}
+ lists := []*[]string{&w.Held, &w.Hosts.Waking, &w.Hosts.Stopping, &w.Hosts.Recovering, &w.Hosts.Owed}
+ for {
+ b, err := json.Marshal(w)
+ if err == nil && len(b) <= drainWaitingMaxBytes {
+ return w
+ }
+ dropped := false
+ for i := len(lists) - 1; i >= 0; i-- {
+ if l := lists[i]; len(*l) > 0 {
+ *l = (*l)[:len(*l)-1]
+ dropped = true
+ break
+ }
+ }
+ w.Truncated = true
+ if !dropped {
+ return w
+ }
+ }
}
// describe is the waiting counts that are not zero, each with its hosts:
@@ -813,9 +871,7 @@ func (d *drainDriver) waitQuiet(ctx context.Context, step string, held bool) err
d.note(func() { d.waiting = nil })
return nil
}
- w := &drainWaiting{Step: step, Since: start, Quiet: q, Held: firstNames(h), HeldCount: len(h),
- Hosts: activator.DrainQuietHosts{Waking: firstNames(names.Waking), Stopping: firstNames(names.Stopping),
- Recovering: firstNames(names.Recovering), Owed: firstNames(names.Owed)}}
+ w := newDrainWaiting(step, start, q, names, h)
now := d.now()
waited := now.Sub(start)
if waited >= drainQuietBound {
diff --git a/internal/host/hadrain_quiet_linux_test.go b/internal/host/hadrain_quiet_linux_test.go
index ef76a27a7..973e270d9 100644
--- a/internal/host/hadrain_quiet_linux_test.go
+++ b/internal/host/hadrain_quiet_linux_test.go
@@ -53,3 +53,59 @@ func TestDrainVerbQuiesceGivesUpAndStatusNamesTheHosts(t *testing.T) {
t.Fatalf("status after cancel: %s", out)
}
}
+
+// Astra's P2 on #1062: names are not bounded by their count alone. Owed
+// keys that are not a lock's are named whole, and a 500-byte key of '<'
+// encodes to 3000 bytes; with every count full of them the status verb's
+// whole answer still fits its 8192 bytes, the counts stay exact, the
+// truncation is marked, and the refusal still names the counter.
+func TestDrainVerbStatusBoundsHostileNames(t *testing.T) {
+ hostile := func(c byte) []string {
+ var out []string
+ for i := range 5 {
+ out = append(out, strings.Repeat("<", 500)+string(rune('a'+i))+string(c))
+ }
+ return out
+ }
+ act, ed, marks := healthyDrain()
+ d, clk, _ := quietDriver(act, ed, marks)
+ act.quiet = func() activator.DrainQuiet {
+ clk.poll()
+ return activator.DrainQuiet{Waking: 5, Stopping: 5, Recovering: 5, Owed: 5}
+ }
+ act.named = func() activator.DrainQuietHosts {
+ return activator.DrainQuietHosts{Waking: hostile('w'), Stopping: hostile('s'), Recovering: hostile('r'), Owed: hostile('o')}
+ }
+ cmd, l := verbDrain(t, d)
+ d.fence = l.read
+ rep, err := runDrainVerb(t, cmd, "drain", "t1")
+ if !errors.Is(err, errDrainFailed) || rep == nil || !strings.Contains(strings.Join(rep.Reasons, ";"), "recovering 5") {
+ t.Fatalf("drain: %+v %v", rep, err)
+ }
+ out, err := runMaintenanceVerb(cmd, "status", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(out) > 8192 {
+ t.Fatalf("status is %d bytes", len(out))
+ }
+ var st groupMaintenanceStatus
+ must(t, json.Unmarshal(out, &st))
+ w := st.Drain.Waiting
+ if w == nil || !w.GaveUp || !w.Truncated || w.Quiet != (activator.DrainQuiet{Waking: 5, Stopping: 5, Recovering: 5, Owed: 5}) {
+ t.Fatalf("status: %s", out)
+ }
+ for _, names := range [][]string{w.Hosts.Waking, w.Hosts.Stopping, w.Hosts.Recovering, w.Hosts.Owed} {
+ for _, n := range names {
+ if len(n) > drainNameMaxBytes+len(drainNameCut) || !strings.HasSuffix(n, drainNameCut) {
+ t.Fatalf("name not cut: %d bytes", len(n))
+ }
+ }
+ }
+ if len(w.Hosts.Waking) == 0 {
+ t.Fatal("the budget dropped every name, the first count's too")
+ }
+ if got := w.describe(); !strings.Contains(got, "recovering 5") || !strings.Contains(got, "owed 5") {
+ t.Fatalf("describe %q", got)
+ }
+}