group verb: throttle retries on reads, group help, clearer refusals
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
11 files changed,
+298
−23
internal/grouproll/box.go+33 −0internal/grouproll/box_test.go+55 −2internal/grouproll/plan.go+3 −1internal/grouproll/plan_test.go+3 −3internal/grouproll/text.go+2 −1internal/grouproll/text_test.go+4 −3internal/grouprollaws/group.go+62 −9internal/grouprollaws/objects.go+4 −3internal/grouprollaws/throttle_test.go+98 −0system_group.go+33 −0system_roles.go+1 −1
diff --git a/internal/grouproll/box.go b/internal/grouproll/box.go| index 679f1315f..84daef2ae 100644 |
| --- a/internal/grouproll/box.go |
| +++ b/internal/grouproll/box.go |
| @@ -21,6 +21,7 @@ import ( |
| "sort" |
| "strconv" |
| "strings" |
| + "unicode" |
| "unicode/utf8" |
| ) |
| @@ -521,6 +522,12 @@ func ParseFramed(text, nonce, verb string) (BoxResult, error) { |
| return BoxResult{Verb: verb, Value: map[string]any{"exit": int64(1), "detail": d}, Exit: 1, Detail: d}, nil |
| } |
| if code != "0" { |
| + // The verb's own refusal (prepare's "pointer artifact does not |
| + // match requested digest", say) is the operator's best clue: carry |
| + // its last line, printable characters only. |
| + if d := remoteDetail(body); d != "" { |
| + return BoxResult{}, Refusal{Key: "remote_said", Args: map[string]string{"detail": d}} |
| + } |
| return BoxResult{}, Refusal{Key: "remote"} |
| } |
| switch verb { |
| @@ -539,6 +546,32 @@ func ParseFramed(text, nonce, verb string) (BoxResult, error) { |
| return BoxResult{Verb: verb, Value: map[string]any{}}, nil |
| } |
| +// remoteDetail is the tail of a failed verb's output for the operator: its |
| +// last non-empty line, with every non-printable character replaced by a |
| +// space and cut to its last cancelDetail characters. Empty when the |
| +// output has no printable text. |
| +func remoteDetail(body string) string { |
| + lines := pythonSplitLines(body) |
| + for i := len(lines) - 1; i >= 0; i-- { |
| + clean := strings.Map(func(r rune) rune { |
| + if unicode.IsPrint(r) { |
| + return r |
| + } |
| + return ' ' |
| + }, lines[i]) |
| + clean = strings.Join(strings.Fields(clean), " ") |
| + if clean == "" { |
| + continue |
| + } |
| + detail := []rune(clean) |
| + if len(detail) > cancelDetail { |
| + detail = detail[len(detail)-cancelDetail:] |
| + } |
| + return string(detail) |
| + } |
| + return "" |
| +} |
| + |
| // drainOutput is AWS.drain_output. The verb prints its report as one JSON |
| // line and exits 0 only when the report's result is ok; a refusal prints |
| // no report. 124 is the remote timeout's: the verb was killed, the agent's |
diff --git a/internal/grouproll/box_test.go b/internal/grouproll/box_test.go| index 735ca714a..95ca06eeb 100644 |
| --- a/internal/grouproll/box_test.go |
| +++ b/internal/grouproll/box_test.go |
| @@ -298,9 +298,21 @@ func TestParseFramed(t *testing.T) { |
| t.Fatal("an uncertain outcome is also a refusal") |
| } |
| case c.Refusal != "": |
| + // V15: where the script said "remote" for a verb that exited |
| + // non-zero (drain keeps its own report parsing), the verb's last |
| + // printable line is carried |
| + // (remote_said), when there is one. |
| + want, detail := c.Refusal, "" |
| + if want == "remote" && c.Verb != "drain" { |
| + if m := framedBody(c.Text, c.Nonce); m != nil && m[2] != "0" { |
| + if detail = remoteDetail(m[1]); detail != "" { |
| + want = "remote_said" |
| + } |
| + } |
| + } |
| var r Refusal |
| - if !errors.As(err, &r) || r.Key != c.Refusal { |
| - t.Fatalf("got %+v, %v; want refusal %s", got, err, c.Refusal) |
| + if !errors.As(err, &r) || r.Key != want || (want == "remote_said" && r.Args["detail"] != detail) { |
| + t.Fatalf("got %+v, %v; want refusal %s %q", got, err, want, detail) |
| } |
| default: |
| if err != nil { |
| @@ -508,3 +520,44 @@ func TestRemoteCommandDrainPeers(t *testing.T) { |
| t.Errorf("%d peers: %v", MaxDrainPeers, err) |
| } |
| } |
| + |
| +// framedBody is ParseFramed's single frame match (body, exit code), or nil. |
| +func framedBody(text, nonce string) []string { |
| + frame := regexp.MustCompile(`(?s)` + regexp.QuoteMeta(nonce) + `:BEGIN\r?\n(.*?)\r?\n` + |
| + regexp.QuoteMeta(nonce) + `:END:([0-9]+)(?:\r?\n|$)`) |
| + m := frame.FindAllStringSubmatch(text, -1) |
| + if len(m) != 1 { |
| + return nil |
| + } |
| + return m[0] |
| +} |
| + |
| +// The member's own refusal reaches the operator: the last non-empty line, |
| +// non-printable characters (an escape sequence, a bell) made |
| +// spaces, cut to cancelDetail characters; nothing printable keeps "remote". |
| +func TestRemoteDetail(t *testing.T) { |
| + nonce := strings.Repeat("a", 32) |
| + frame := func(body, code string) string { |
| + return "noise\n" + nonce + ":BEGIN\n" + body + "\n" + nonce + ":END:" + code + "\n" |
| + } |
| + _, err := ParseFramed(frame("staging\nprepare: pointer artifact does not match requested digest", "1"), nonce, "prepare") |
| + var r Refusal |
| + if !errors.As(err, &r) || r.Key != "remote_said" || r.Args["detail"] != "prepare: pointer artifact does not match requested digest" { |
| + t.Fatalf("prepare refusal: %v", err) |
| + } |
| + if got := r.Error(); got != "The command on the member failed: prepare: pointer artifact does not match requested digest" { |
| + t.Fatalf("text %q", got) |
| + } |
| + _, err = ParseFramed(frame("\x1b[31mred\x1b[0m\x07 bad", "1"), nonce, "apply") |
| + if !errors.As(err, &r) || r.Args["detail"] != "[31mred [0m bad" { |
| + t.Fatalf("escapes: %q", r.Args["detail"]) |
| + } |
| + _, err = ParseFramed(frame(strings.Repeat("x", cancelDetail+10)+"END", "1"), nonce, "apply") |
| + if !errors.As(err, &r) || len([]rune(r.Args["detail"])) != cancelDetail || !strings.HasSuffix(r.Args["detail"], "END") { |
| + t.Fatalf("long: %d", len(r.Args["detail"])) |
| + } |
| + _, err = ParseFramed(frame(" \t\x00", "1"), nonce, "apply") |
| + if !errors.As(err, &r) || r.Key != "remote" { |
| + t.Fatalf("blank: %v", err) |
| + } |
| +} |
diff --git a/internal/grouproll/plan.go b/internal/grouproll/plan.go| index a9ffe7881..fc30c23e7 100644 |
| --- a/internal/grouproll/plan.go |
| +++ b/internal/grouproll/plan.go |
| @@ -111,9 +111,11 @@ func (e *Engine) Plan(ctx context.Context, iid, sha string) (ok bool, err error) |
| if snapped { |
| check(require(shapeOK(snap.View, iid), "shape")) |
| check(func() error { |
| + // A missing or unreadable observation is the observation's |
| + // refusal, as preflight says it (the script said "record"). |
| obs, err := e.s.Read(e.s.ObservationPath()) |
| if err != nil { |
| - return err |
| + return refuse("observation") |
| } |
| return require(e.observationOK(obs, snap.View.Template), "observation")() |
| }) |
diff --git a/internal/grouproll/plan_test.go b/internal/grouproll/plan_test.go| index 6c9051e46..37c3832d8 100644 |
| --- a/internal/grouproll/plan_test.go |
| +++ b/internal/grouproll/plan_test.go |
| @@ -150,13 +150,13 @@ func TestPlanRefusals(t *testing.T) { |
| } |
| }) |
| }, []string{"blocked"}}, |
| - // The script's plan reads the observation without catching a |
| - // missing file: FileNotFoundError reaches check() as record. |
| + // The script's plan reported a missing observation as record; the |
| + // verb says observation, as its preflight does. |
| {"no observation", fakeIID, newSHA, func(h *harness) { |
| if err := os.Remove(h.state.ObservationPath()); err != nil { |
| h.t.Fatal(err) |
| } |
| - }, []string{"record"}}, |
| + }, []string{"observation"}}, |
| {"other observation", fakeIID, newSHA, func(h *harness) { |
| h.withLock(func() { |
| if err := h.state.Write(h.state.ObservationPath(), map[string]any{"v": 1, "group_arn": h.c.GroupARN, |
diff --git a/internal/grouproll/text.go b/internal/grouproll/text.go| index 73f35af1e..0164a7842 100644 |
| --- a/internal/grouproll/text.go |
| +++ b/internal/grouproll/text.go |
| @@ -72,6 +72,7 @@ var text = map[string]string{ |
| "unknown_phase": "The member's return phase is unknown. Probe failed.", |
| "hook": "The immutable launch hook must have a 900-second ABANDON default", |
| "remote": "The command on the member returned no complete result", |
| + "remote_said": "The command on the member failed: {detail}", |
| "api": "An AWS response is unavailable or invalid", |
| "bucket_region": "The AWS region of bucket {bucket} is unknown or has changed", |
| "rollback_refused": "Rollback requires this transaction's consumed apply in Standby", |
| @@ -127,7 +128,7 @@ var text = map[string]string{ |
| "edge_running": "carlos-edge.service` is still running after the stop. The member is held in Standby.", |
| "h4": "Only the member's EnterStandby with decrement may lower desired capacity.", |
| "desired_changed": "Desired capacity or membership changed when the member was terminated", |
| - "remove_minimum": "remove` must leave at least 2 members.", |
| + "remove_minimum": "A remove must leave at least 2 members.", |
| "remove_pools": "The survivors would leave pool {pool} with no server", |
| "remove_pools_unknown": "The member's served_pools are unknown", |
| "watch_timeout": "The replacement or the departure did not finish in time", |
diff --git a/internal/grouproll/text_test.go b/internal/grouproll/text_test.go| index 389429b8d..5990d811a 100644 |
| --- a/internal/grouproll/text_test.go |
| +++ b/internal/grouproll/text_test.go |
| @@ -15,9 +15,10 @@ func TestTextRendersEveryKey(t *testing.T) { |
| // 139 approved, plus floor_fixed_missing, floor_fixed_boxes, |
| // floor_fixed_both, drain_peers_many, floor_legacy and floor_population |
| // (held floor capability, 2026-11-10), and bucket_region (a bucket in |
| - // another region than the group, 2026-11-10), not yet copy-reviewed. |
| - if len(text) != 146 { |
| - t.Fatalf("text has %d keys, want the 139 approved plus 7", len(text)) |
| + // another region than the group, 2026-11-10), and remote_said (the |
| + // member's own refusal, 2026-11-11), not yet copy-reviewed. |
| + if len(text) != 147 { |
| + t.Fatalf("text has %d keys, want the 139 approved plus 8", len(text)) |
| } |
| for key, raw := range text { |
| var args []string |
diff --git a/internal/grouprollaws/group.go b/internal/grouprollaws/group.go| index 78c6791b5..159425c02 100644 |
| --- a/internal/grouprollaws/group.go |
| +++ b/internal/grouprollaws/group.go |
| @@ -8,6 +8,7 @@ import ( |
| "errors" |
| "io" |
| "math" |
| + "math/rand/v2" |
| "net" |
| "regexp" |
| "strconv" |
| @@ -17,6 +18,7 @@ import ( |
| "unicode/utf8" |
| "github.com/aws/aws-sdk-go-v2/aws" |
| + "github.com/aws/aws-sdk-go-v2/aws/retry" |
| "github.com/aws/aws-sdk-go-v2/service/autoscaling" |
| astypes "github.com/aws/aws-sdk-go-v2/service/autoscaling/types" |
| "github.com/aws/aws-sdk-go-v2/service/ec2" |
| @@ -176,6 +178,57 @@ func call[T any](parent context.Context, fn func(context.Context) (T, error)) (T |
| return zero, grouproll.Uncertain{Key: "api"} |
| } |
| +// readCall is call for a read, retried while AWS throttles it: up to |
| +// throttleAttempts calls, each under call's own bound, with a jittered |
| +// backoff of about 1, 2 and 4 seconds between them (capped by the caller's |
| +// context). Only a throttling answer is retried; any other failure, and a |
| +// throttle on the last attempt, is call's answer as before, so the verb |
| +// still fails closed. Writes and box sessions never come through here: a |
| +// write's outcome after a failure is unknown, and retrying it is the |
| +// engine's decision, not the adapter's. |
| +func readCall[T any](parent context.Context, fn func(context.Context) (T, error)) (T, error) { |
| + for attempt := 1; ; attempt++ { |
| + var raw error |
| + out, err := call(parent, func(ctx context.Context) (T, error) { |
| + o, e := fn(ctx) |
| + raw = e |
| + return o, e |
| + }) |
| + if err == nil || attempt >= throttleAttempts || parent.Err() != nil || !throttled(raw) { |
| + return out, err |
| + } |
| + if werr := throttleWait(parent, attempt); werr != nil { |
| + var zero T |
| + return zero, werr |
| + } |
| + } |
| +} |
| + |
| +// throttleAttempts bounds readCall: the first call and three retries. |
| +const throttleAttempts = 4 |
| + |
| +// throttled is the SDK's own throttle classification (error codes such as |
| +// Throttling, ThrottlingException, RequestLimitExceeded, SlowDown, and an |
| +// HTTP 429), the errors its standard retryer backs off on. |
| +func throttled(err error) bool { |
| + return err != nil && retry.IsErrorThrottles(retry.DefaultThrottles).IsErrorThrottle(err) == aws.TrueTernary |
| +} |
| + |
| +// throttleWait sleeps before retry number attempt (1-based): 2^(attempt-1) |
| +// seconds, jittered to between half and all of it. A test replaces it. |
| +var throttleWait = func(ctx context.Context, attempt int) error { |
| + base := time.Second << (attempt - 1) |
| + d := base/2 + time.Duration(rand.Int64N(int64(base/2)+1)) |
| + t := time.NewTimer(d) |
| + defer t.Stop() |
| + select { |
| + case <-ctx.Done(): |
| + return ctx.Err() |
| + case <-t.C: |
| + return nil |
| + } |
| +} |
| + |
| // malformed is a response missing a field the script read with [] (a |
| // KeyError there): the call's answer is unusable, as a failed call is. |
| func malformed() error { return grouproll.Uncertain{Key: "api"} } |
| @@ -214,7 +267,7 @@ func intOf(p *int32) (int, bool) { |
| // Describe is the script's "group": DescribeAutoScalingGroups for the |
| // configured group, exactly one group or scope. |
| func (a *Adapter) Describe(ctx context.Context) (grouproll.GroupView, error) { |
| - out, err := call(ctx, func(ctx context.Context) (*autoscaling.DescribeAutoScalingGroupsOutput, error) { |
| + out, err := readCall(ctx, func(ctx context.Context) (*autoscaling.DescribeAutoScalingGroupsOutput, error) { |
| return a.autoscaling.DescribeAutoScalingGroups(ctx, &autoscaling.DescribeAutoScalingGroupsInput{ |
| AutoScalingGroupNames: []string{a.c.Group}, |
| }) |
| @@ -269,7 +322,7 @@ func (a *Adapter) Describe(ctx context.Context) (grouproll.GroupView, error) { |
| // registered twice (another port) with different states is "unknown", so a |
| // duplicate registration cannot hide draining. |
| func (a *Adapter) Targets(ctx context.Context, arn string) (map[string]string, error) { |
| - out, err := call(ctx, func(ctx context.Context) (*elbv2.DescribeTargetHealthOutput, error) { |
| + out, err := readCall(ctx, func(ctx context.Context) (*elbv2.DescribeTargetHealthOutput, error) { |
| return a.elb.DescribeTargetHealth(ctx, &elbv2.DescribeTargetHealthInput{TargetGroupArn: aws.String(arn)}) |
| }) |
| if err != nil { |
| @@ -294,7 +347,7 @@ func (a *Adapter) Targets(ctx context.Context, arn string) (map[string]string, e |
| // Hook is the script's "hooks": exactly one configured hook, else hook. |
| func (a *Adapter) Hook(ctx context.Context) (grouproll.Hook, error) { |
| - out, err := call(ctx, func(ctx context.Context) (*autoscaling.DescribeLifecycleHooksOutput, error) { |
| + out, err := readCall(ctx, func(ctx context.Context) (*autoscaling.DescribeLifecycleHooksOutput, error) { |
| return a.autoscaling.DescribeLifecycleHooks(ctx, &autoscaling.DescribeLifecycleHooksInput{ |
| AutoScalingGroupName: aws.String(a.c.Group), |
| LifecycleHookNames: []string{a.c.Hook}, |
| @@ -321,7 +374,7 @@ func (a *Adapter) Hook(ctx context.Context) (grouproll.Hook, error) { |
| // Online is the script's "online": the member is the one Session Manager |
| // instance listed for its id, and its ping status is Online. |
| func (a *Adapter) Online(ctx context.Context, iid string) (bool, error) { |
| - out, err := call(ctx, func(ctx context.Context) (*ssm.DescribeInstanceInformationOutput, error) { |
| + out, err := readCall(ctx, func(ctx context.Context) (*ssm.DescribeInstanceInformationOutput, error) { |
| return a.ssm.DescribeInstanceInformation(ctx, &ssm.DescribeInstanceInformationInput{ |
| Filters: []ssmtypes.InstanceInformationStringFilter{{Key: aws.String("InstanceIds"), Values: []string{iid}}}, |
| }) |
| @@ -360,7 +413,7 @@ func activityKind(description string) string { |
| // whose description names iid as a whole word, classified enter, exit or |
| // terminate; the rest are ignored. |
| func (a *Adapter) Activities(ctx context.Context, iid string) ([]grouproll.Activity, error) { |
| - out, err := call(ctx, func(ctx context.Context) (*autoscaling.DescribeScalingActivitiesOutput, error) { |
| + out, err := readCall(ctx, func(ctx context.Context) (*autoscaling.DescribeScalingActivitiesOutput, error) { |
| return a.autoscaling.DescribeScalingActivities(ctx, &autoscaling.DescribeScalingActivitiesInput{ |
| AutoScalingGroupName: aws.String(a.c.Group), |
| }) |
| @@ -391,7 +444,7 @@ func (a *Adapter) Activities(ctx context.Context, iid string) ([]grouproll.Activ |
| // Membership is the script's "instance" as the probe reads it: the |
| // member's own DescribeAutoScalingInstances row. More than one row is api. |
| func (a *Adapter) Membership(ctx context.Context, iid string) (grouproll.MemberRow, bool, error) { |
| - out, err := call(ctx, func(ctx context.Context) (*autoscaling.DescribeAutoScalingInstancesOutput, error) { |
| + out, err := readCall(ctx, func(ctx context.Context) (*autoscaling.DescribeAutoScalingInstancesOutput, error) { |
| return a.autoscaling.DescribeAutoScalingInstances(ctx, &autoscaling.DescribeAutoScalingInstancesInput{ |
| InstanceIds: []string{iid}, |
| }) |
| @@ -430,7 +483,7 @@ func (a *Adapter) Instance(ctx context.Context, iid string) (grouproll.InstanceS |
| if err := requireIID(iid, "record"); err != nil { |
| return grouproll.InstanceState{}, err |
| } |
| - out, err := call(ctx, func(ctx context.Context) (*ec2.DescribeInstancesOutput, error) { |
| + out, err := readCall(ctx, func(ctx context.Context) (*ec2.DescribeInstancesOutput, error) { |
| return a.ec2.DescribeInstances(ctx, &ec2.DescribeInstancesInput{ |
| Filters: []ec2types.Filter{{Name: aws.String("instance-id"), Values: []string{iid}}}, |
| }) |
| @@ -492,7 +545,7 @@ func (a *Adapter) TemplateRelease(ctx context.Context, tpl map[string]any) (stri |
| name, _ := tpl["LaunchTemplateName"].(string) |
| in.LaunchTemplateName = aws.String(name) |
| } |
| - out, err := call(ctx, func(ctx context.Context) (*ec2.DescribeLaunchTemplateVersionsOutput, error) { |
| + out, err := readCall(ctx, func(ctx context.Context) (*ec2.DescribeLaunchTemplateVersionsOutput, error) { |
| return a.ec2.DescribeLaunchTemplateVersions(ctx, in) |
| }) |
| if err != nil { |
| @@ -695,7 +748,7 @@ func (a *Adapter) Terminate(ctx context.Context, iid string) (grouproll.WriteRes |
| // CallerIdentity is sts:GetCallerIdentity; the engine journals the UserId. |
| func (a *Adapter) CallerIdentity(ctx context.Context) (userID, arn string, err error) { |
| - out, err := call(ctx, func(ctx context.Context) (*sts.GetCallerIdentityOutput, error) { |
| + out, err := readCall(ctx, func(ctx context.Context) (*sts.GetCallerIdentityOutput, error) { |
| return a.sts.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{}) |
| }) |
| if err != nil { |
diff --git a/internal/grouprollaws/objects.go b/internal/grouprollaws/objects.go| index 80a880e8c..8d017a701 100644 |
| --- a/internal/grouprollaws/objects.go |
| +++ b/internal/grouprollaws/objects.go |
| @@ -60,7 +60,7 @@ func (a *Adapter) bucketClient(ctx context.Context, bucket string) (*s3.Client, |
| defer a.bucketsMu.Unlock() |
| region, ok := a.bucketRegion[bucket] |
| if !ok { |
| - got, err := call(ctx, func(ctx context.Context) (string, error) { return headBucketRegion(ctx, a.s3, bucket) }) |
| + got, err := readCall(ctx, func(ctx context.Context) (string, error) { return headBucketRegion(ctx, a.s3, bucket) }) |
| if err != nil { |
| var u grouproll.Uncertain |
| if ctx.Err() != nil || (errors.As(err, &u) && (u.Timeout || u.OutputTooLarge)) { |
| @@ -140,7 +140,8 @@ type object struct { |
| etag string |
| } |
| -// read runs fn on bucket's regional store, as one bounded provider call. |
| +// read runs fn on bucket's regional store, as one bounded provider read |
| +// (retried while S3 throttles it, readCall). |
| func read[T any](ctx context.Context, r readStore, fn func(context.Context, *release.S3Store) (T, error)) (T, error) { |
| var zero T |
| c, err := r.a.bucketClient(ctx, r.bucket) |
| @@ -148,7 +149,7 @@ func read[T any](ctx context.Context, r readStore, fn func(context.Context, *rel |
| return zero, err |
| } |
| store := &release.S3Store{Client: c, Bucket: r.bucket} |
| - return call(ctx, func(ctx context.Context) (T, error) { |
| + return readCall(ctx, func(ctx context.Context) (T, error) { |
| out, err := fn(ctx, store) |
| // A 301 whose body stalled or overran the cap keeps call's |
| // timeout or cap answer: only a whole 301 is a wrong region. |
diff --git a/internal/grouprollaws/throttle_test.go b/internal/grouprollaws/throttle_test.go| new file mode 100644 |
| index 000000000..b2029f056 |
| --- /dev/null |
| +++ b/internal/grouprollaws/throttle_test.go |
| @@ -0,0 +1,98 @@ |
| +package grouprollaws |
| + |
| +import ( |
| + "context" |
| + "reflect" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +const throttleXML = `<ErrorResponse><Error><Type>Sender</Type><Code>Throttling</Code><Message>Rate exceeded</Message></Error><RequestId>r</RequestId></ErrorResponse>` |
| + |
| +// stubThrottleWait records readCall's backoffs instead of sleeping. |
| +func stubThrottleWait(t *testing.T) *[]int { |
| + t.Helper() |
| + var waits []int |
| + prev := throttleWait |
| + throttleWait = func(ctx context.Context, attempt int) error { |
| + waits = append(waits, attempt) |
| + return ctx.Err() |
| + } |
| + t.Cleanup(func() { throttleWait = prev }) |
| + return &waits |
| +} |
| + |
| +// A throttled read is retried with backoff and succeeds when AWS answers |
| +// (the step-3 W4 refusal at the group describe); the budget is four calls, |
| +// after which the verb fails closed with the api answer it gave before. |
| +func TestReadsRetryThrottling(t *testing.T) { |
| + ok := groupXML(2, 2, 3, [][2]string{{testIID, "InService"}}) |
| + waits := stubThrottleWait(t) |
| + rec := newRecorder().onStatus("DescribeAutoScalingGroups", 400, throttleXML). |
| + onStatus("DescribeAutoScalingGroups", 400, throttleXML).on("DescribeAutoScalingGroups", ok) |
| + if _, err := testAdapter(t, rec).Describe(ctx); err != nil { |
| + t.Fatalf("throttled twice, then answered: %v", err) |
| + } |
| + if n := len(rec.requests()); n != 3 || !reflect.DeepEqual(*waits, []int{1, 2}) { |
| + t.Fatalf("requests %d, waits %v", n, *waits) |
| + } |
| + |
| + waits = stubThrottleWait(t) |
| + rec = newRecorder().onStatus("DescribeAutoScalingGroups", 400, throttleXML) |
| + _, err := testAdapter(t, rec).Describe(ctx) |
| + if u := uncertainKey(t, err); u.Key != "api" || u.Timeout { |
| + t.Fatalf("throttled throughout: %+v", u) |
| + } |
| + if n := len(rec.requests()); n != throttleAttempts || !reflect.DeepEqual(*waits, []int{1, 2, 3}) { |
| + t.Fatalf("requests %d, waits %v", n, *waits) |
| + } |
| + |
| + // Any other failure is not retried. |
| + waits = stubThrottleWait(t) |
| + rec = newRecorder() |
| + if _, err := testAdapter(t, rec).Describe(ctx); uncertainKey(t, err).Key != "api" || len(rec.requests()) != 1 || len(*waits) != 0 { |
| + t.Fatalf("500: %v, %d requests, waits %v", err, len(rec.requests()), *waits) |
| + } |
| + |
| + // A write is never retried, throttled or not. |
| + waits = stubThrottleWait(t) |
| + rec = newRecorder().onStatus("UpdateAutoScalingGroup", 400, throttleXML) |
| + if _, err := testAdapter(t, rec).SetMinimum(ctx, 1); err == nil || len(rec.requests()) != 1 || len(*waits) != 0 { |
| + t.Fatalf("throttled write: %v, %d requests, waits %v", err, len(rec.requests()), *waits) |
| + } |
| + |
| + // Ctrl-C during a backoff ends the read with the context's error. |
| + prev := throttleWait |
| + cctx, cancel := context.WithCancel(ctx) |
| + throttleWait = func(context.Context, int) error { cancel(); return context.Canceled } |
| + t.Cleanup(func() { throttleWait = prev }) |
| + rec = newRecorder().onStatus("DescribeAutoScalingGroups", 400, throttleXML) |
| + if _, err := testAdapter(t, rec).Describe(cctx); err != context.Canceled || len(rec.requests()) != 1 { |
| + t.Fatalf("cancelled backoff: %v, %d requests", err, len(rec.requests())) |
| + } |
| +} |
| + |
| +// S3's SlowDown is a throttle too: an edges or lock read is retried. |
| +func TestBucketReadsRetrySlowDown(t *testing.T) { |
| + waits := stubThrottleWait(t) |
| + rec := &s3Recorder{status: 503, body: `<Error><Code>SlowDown</Code><Message>x</Message></Error>`} |
| + _, _, err := testAdapterWith(t, rec).Bucket("carlos-dev-store").Get(ctx, "control/edges/g-1.json") |
| + if uncertainKey(t, err).Key != "api" || len(rec.reqs) != throttleAttempts || len(*waits) != throttleAttempts-1 { |
| + t.Fatalf("SlowDown: %v, %d GETs, waits %v", err, len(rec.reqs), *waits) |
| + } |
| + for _, q := range rec.reqs { |
| + if !strings.HasPrefix(q.URL.Host, "carlos-dev-store.s3.eu-west-1.amazonaws.com") { |
| + t.Fatalf("read went to %s", q.URL.Host) |
| + } |
| + } |
| +} |
| + |
| +// The real backoff waits about 2^(n-1) seconds, jittered to half or more, |
| +// and ends at once when the context does. |
| +func TestThrottleWaitBounds(t *testing.T) { |
| + cctx, cancel := context.WithCancel(ctx) |
| + cancel() |
| + if err := throttleWait(cctx, 3); err != context.Canceled { |
| + t.Fatalf("cancelled wait = %v", err) |
| + } |
| +} |
diff --git a/system_group.go b/system_group.go| index 99ee4f914..0e21d641c 100644 |
| --- a/system_group.go |
| +++ b/system_group.go |
| @@ -110,6 +110,36 @@ func groupProbeHelp(args []string) int { |
| return 2 |
| } |
| +// systemGroupFallback is bare `carlos system group`, its -h, and a verb it |
| +// does not have: the group's own help, after the same root and box |
| +// refusals as every verb. |
| +func systemGroupFallback(args, _ []string) { os.Exit(runSystemGroup("", args)) } |
| + |
| +// groupVerbsHelp is the verb list groupHelp prints, in the order an |
| +// operator meets them. |
| +var groupVerbsHelp = []struct{ verb, key string }{ |
| + {"plan", "plan"}, {"roll", "roll"}, {"replace", "replace"}, {"remove", "remove"}, |
| + {"rollback", "rollback"}, {"resume", "resume"}, {"return", "return"}, {"status", "status"}, |
| + {"abandon", "abandon"}, {"close-lost", "close-lost"}, {"cancel-drain", "cancel_drain"}, |
| + {"probe", "probe_help"}, |
| +} |
| + |
| +// groupHelp is the group's help: on stdout for -h, else on stderr with the |
| +// usage status, as groupProbeHelp. |
| +func groupHelp(args []string) int { |
| + var b strings.Builder |
| + b.WriteString(grouproll.Text("help_group") + "\n\n") |
| + for _, v := range groupVerbsHelp { |
| + fmt.Fprintf(&b, " %-13s %s\n", v.verb, grouproll.Text(v.key)) |
| + } |
| + if len(args) == 1 && (args[0] == "-h" || args[0] == "-help" || args[0] == "--help") { |
| + fmt.Fprint(groupEnv.stdout, b.String()) |
| + return 0 |
| + } |
| + fmt.Fprint(groupEnv.stderr, b.String()) |
| + return 2 |
| +} |
| + |
| // groupFlags are one verb's flags, as the script's argparse declares them. |
| type groupFlags struct { |
| config, attendedBy, member, sha, transaction string |
| @@ -293,6 +323,9 @@ func runSystemGroup(verb string, args []string) int { |
| if verb == "probe" { |
| return groupProbeHelp(args) |
| } |
| + if verb == "" { |
| + return groupHelp(args) |
| + } |
| f, rc, ok := parseGroupFlags(verb, args) |
| if !ok { |
| return rc |
diff --git a/system_roles.go b/system_roles.go| index 7a2c9128c..e98b70831 100644 |
| --- a/system_roles.go |
| +++ b/system_roles.go |
| @@ -145,7 +145,7 @@ func systemGroupCommand() *clireg.Node { |
| noHint(skipGolden(raw("cancel-drain", clireg.Operator, runSystemGroupCancelDrain), groupVerbGolden)), |
| systemGroupProbeCommand(), |
| }, |
| - Fallback: systemRolesFallback, |
| + Fallback: systemGroupFallback, |
| } |
| } |