Skip to content

Commit ecdd077

Browse files
committed
feat(submitqueue): Name the in-flight request a duplicate repeats in its failed hook event
1 parent c57ec81 commit ecdd077

7 files changed

Lines changed: 108 additions & 60 deletions

File tree

‎doc/rfc/hook-framework.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Delivery promise:
5252
- Partition key = subject id; per-subject order only.
5353
- Replay finding the target state already written → republish (idempotent). Beyond it → superseded; the event may be lost. Named, accepted gap.
5454
- RPC-caused transitions are at-most-once; needing the guarantee means publishing from the first queue-driven stage.
55-
- SubmitQueue request events follow the gateway's request summary, the status users see, rather than pipeline transitions, so a hook agrees with the UI even for a request whose start failed before the orchestrator saved it. Each status announces one event per change, so a hook retrying one change's failure never repeats another's. A finish is announced on every delivery of a log that restates it, which keeps the replay rule above: a failed publish fails the delivery and its redelivery republishes. `request.received` is published by the update that takes the request out of `accepting`, normally inside `Land`, which makes it RPC-caused and at-most-once: a failed publish is only counted. A later status announces it only when `Land` failed to record `accepted` at all. The events cross from the gateway to the orchestrator's hook stage, so their payload carries the change's URI, which never changes, and `request.failed` adds the failure's category and reason, cut to 512 characters. The summary keeps the details a finish was first recorded with, so a restated finish republishes the same payload. The rest of the summary's metadata, which can hold internal diagnostics, stays out of the payload.
55+
- SubmitQueue request events follow the gateway's request summary, the status users see, rather than pipeline transitions, so a hook agrees with the UI even for a request whose start failed before the orchestrator saved it. Each status announces one event per change, so a hook retrying one change's failure never repeats another's. A finish is announced on every delivery of a log that restates it, which keeps the replay rule above: a failed publish fails the delivery and its redelivery republishes. `request.received` is published by the update that takes the request out of `accepting`, normally inside `Land`, which makes it RPC-caused and at-most-once: a failed publish is only counted. A later status announces it only when `Land` failed to record `accepted` at all. The events cross from the gateway to the orchestrator's hook stage, so their payload carries the change's URI, which never changes, and `request.failed` adds the failure's category and reason, cut to 512 characters, and a duplicate's in-flight request id. The summary keeps the details a finish was first recorded with, so a restated finish republishes the same payload. The rest of the summary's metadata, which can hold internal diagnostics, stays out of the payload.
5656
- Opt-in per host via topic-key registration; a registered host never skips, so off and loss are distinguishable.
5757

5858
### Contract

‎submitqueue/core/hookevent/hookevent.go‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
// Each status announces one event per change of the request, so a hook that
2727
// retries one change's failed side effect never repeats the others'. Payloads
2828
// carry the queue, the request id, and that change's URI. A request.failed
29-
// payload also carries the failure's category and reason, when the request has
29+
// payload also carries the failure's category, its reason, and the details a
30+
// category has (such as the request a duplicate repeats), when the request has
3031
// them, so a hook can say why without asking for the summary. The gateway
3132
// publishes these events and the orchestrator's hook stage consumes them, and a
3233
// hook there cannot read the gateway's store, so the payload carries what a
@@ -84,8 +85,8 @@ const (
8485
TypeRequestCancelled Type = "request.cancelled"
8586
)
8687

87-
// Payload keys. PayloadKeyFailureCategory and PayloadKeyFailureReason appear
88-
// only on request.failed, and only when the request has a value for them.
88+
// Payload keys. The failure keys appear only on request.failed, and only when
89+
// the request has a value for them.
8990
const (
9091
// PayloadKeyQueue holds the request's queue.
9192
PayloadKeyQueue = "queue"
@@ -97,8 +98,18 @@ const (
9798
PayloadKeyFailureCategory = "failure_category"
9899
// PayloadKeyFailureReason holds the failure's reason, cut to maxFailureReasonRunes.
99100
PayloadKeyFailureReason = "failure_reason"
101+
// PayloadKeyDuplicateOf holds the id of the in-flight request a duplicate repeats.
102+
PayloadKeyDuplicateOf = "duplicate_of"
100103
)
101104

105+
// failureMetadataPayloadKeys maps the summary metadata a failed payload may carry
106+
// to its payload key. Anything else in the metadata, such as dead-letter
107+
// diagnostics, stays out of hook events.
108+
var failureMetadataPayloadKeys = map[string]string{
109+
entity.RequestLogMetadataKeyFailureCategory: PayloadKeyFailureCategory,
110+
entity.RequestLogMetadataKeyDuplicateOf: PayloadKeyDuplicateOf,
111+
}
112+
102113
// maxFailureReasonRunes bounds a failure reason in a payload; a reason can be
103114
// long free text from a validator or a merge backend.
104115
const maxFailureReasonRunes = 512
@@ -152,11 +163,13 @@ func newRequestEvents(eventType Type, summary entity.RequestSummary, version int
152163
return events
153164
}
154165

155-
// addFailureFields copies only the classified category and the reason, never the
156-
// rest of the summary's metadata, which can hold internal diagnostics.
166+
// addFailureFields copies the reason and the allowlisted failure metadata, never
167+
// the rest of the summary's metadata, which can hold internal diagnostics.
157168
func addFailureFields(fields map[string]*structpb.Value, summary entity.RequestSummary) {
158-
if category := summary.Metadata[entity.RequestLogMetadataKeyFailureCategory]; category != "" {
159-
fields[PayloadKeyFailureCategory] = structpb.NewStringValue(category)
169+
for metadataKey, payloadKey := range failureMetadataPayloadKeys {
170+
if value := summary.Metadata[metadataKey]; value != "" {
171+
fields[payloadKey] = structpb.NewStringValue(value)
172+
}
160173
}
161174
if summary.LastError != "" {
162175
fields[PayloadKeyFailureReason] = structpb.NewStringValue(truncateRunes(summary.LastError, maxFailureReasonRunes))

‎submitqueue/core/hookevent/hookevent_test.go‎

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -149,32 +149,44 @@ func eventIDs(events []*basehook.HookEvent) []string {
149149
func TestNewRequestFinished_FailureFields(t *testing.T) {
150150
longReason := strings.Repeat("é", maxFailureReasonRunes+10)
151151
tests := []struct {
152-
name string
153-
status entity.RequestStatus
154-
lastError string
155-
metadata map[string]string
156-
wantCategory string
157-
wantReason string
152+
name string
153+
status entity.RequestStatus
154+
lastError string
155+
metadata map[string]string
156+
wantFields map[string]string
158157
}{
159158
{
160-
name: "failed with a category and a reason",
161-
status: entity.RequestStatusError,
162-
lastError: "custom validation failed: missing approval",
163-
metadata: map[string]string{entity.RequestLogMetadataKeyFailureCategory: "validation_rejected", "dlq.original_topic": "validate"},
164-
wantCategory: "validation_rejected",
165-
wantReason: "custom validation failed: missing approval",
159+
name: "failed with a category and a reason",
160+
status: entity.RequestStatusError,
161+
lastError: "custom validation failed: missing approval",
162+
metadata: map[string]string{entity.RequestLogMetadataKeyFailureCategory: "validation_rejected", "dlq.original_topic": "validate"},
163+
wantFields: map[string]string{
164+
PayloadKeyFailureCategory: "validation_rejected",
165+
PayloadKeyFailureReason: "custom validation failed: missing approval",
166+
},
167+
},
168+
{
169+
name: "a duplicate names the request it repeats",
170+
status: entity.RequestStatusError,
171+
lastError: "request 94 is a duplicate of in-flight request 93",
172+
metadata: map[string]string{entity.RequestLogMetadataKeyFailureCategory: "duplicate", entity.RequestLogMetadataKeyDuplicateOf: "93"},
173+
wantFields: map[string]string{
174+
PayloadKeyFailureCategory: "duplicate",
175+
PayloadKeyFailureReason: "request 94 is a duplicate of in-flight request 93",
176+
PayloadKeyDuplicateOf: "93",
177+
},
166178
},
167179
{
168180
name: "failed without a category",
169181
status: entity.RequestStatusError,
170182
lastError: "land failed",
171-
wantReason: "land failed",
183+
wantFields: map[string]string{PayloadKeyFailureReason: "land failed"},
172184
},
173185
{
174186
name: "a long reason is cut",
175187
status: entity.RequestStatusError,
176188
lastError: longReason,
177-
wantReason: strings.Repeat("é", maxFailureReasonRunes-1) + "…",
189+
wantFields: map[string]string{PayloadKeyFailureReason: strings.Repeat("é", maxFailureReasonRunes-1) + "…"},
178190
},
179191
{
180192
name: "failed with no details",
@@ -185,7 +197,7 @@ func TestNewRequestFinished_FailureFields(t *testing.T) {
185197
name: "landed carries no failure fields",
186198
status: entity.RequestStatusLanded,
187199
lastError: "ignored",
188-
metadata: map[string]string{entity.RequestLogMetadataKeyFailureCategory: "build_failed"},
200+
metadata: map[string]string{entity.RequestLogMetadataKeyFailureCategory: "build_failed", entity.RequestLogMetadataKeyDuplicateOf: "93"},
189201
},
190202
}
191203
for _, tt := range tests {
@@ -195,21 +207,15 @@ func TestNewRequestFinished_FailureFields(t *testing.T) {
195207
events := NewRequestFinished(summary)
196208

197209
require.Len(t, events, 1)
198-
fields := events[0].GetPayload().GetFields()
199-
assert.Equal(t, tt.wantCategory, fields[PayloadKeyFailureCategory].GetStringValue())
200-
assert.Equal(t, tt.wantReason, fields[PayloadKeyFailureReason].GetStringValue())
201-
wantKeys := []string{PayloadKeyQueue, PayloadKeyRequestID, PayloadKeyChangeURI}
202-
if tt.wantCategory != "" {
203-
wantKeys = append(wantKeys, PayloadKeyFailureCategory)
204-
}
205-
if tt.wantReason != "" {
206-
wantKeys = append(wantKeys, PayloadKeyFailureReason)
210+
got := map[string]string{}
211+
for key, value := range events[0].GetPayload().GetFields() {
212+
got[key] = value.GetStringValue()
207213
}
208-
keys := make([]string, 0, len(fields))
209-
for key := range fields {
210-
keys = append(keys, key)
214+
want := map[string]string{PayloadKeyQueue: "q", PayloadKeyRequestID: "1", PayloadKeyChangeURI: "uri/1"}
215+
for key, value := range tt.wantFields {
216+
want[key] = value
211217
}
212-
assert.ElementsMatch(t, wantKeys, keys)
218+
assert.Equal(t, want, got)
213219
})
214220
}
215221
}

‎submitqueue/entity/failure_category.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ const (
3838
// error status's FailureCategory. It is absent when the failure was not classified.
3939
const RequestLogMetadataKeyFailureCategory = "failure_category"
4040

41+
// RequestLogMetadataKeyDuplicateOf is the request log metadata key holding the
42+
// ID of the in-flight request that a FailureCategoryDuplicate request duplicates.
43+
const RequestLogMetadataKeyDuplicateOf = "duplicate_of"
44+
4145
// FailureCategoryMetadata returns metadata with category recorded under
4246
// RequestLogMetadataKeyFailureCategory, leaving metadata itself unchanged. An
4347
// unknown category records nothing.

‎submitqueue/gateway/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ A finished status keeps the reason and metadata it was first recorded with. When
2929

3030
## Request hook events
3131

32-
The materializer publishes SubmitQueue's request hook events (`submitqueue/core/hookevent`) to the `submitqueue-hook` topic, which the orchestrator pipeline's hook stage consumes, one event per change of the request. The update that takes the summary out of `accepting` announces `request.received`, once. That is normally `Land`, and a later log only when `Land` failed to record `accepted`; a failed receipt publish is only counted on `request_hook.publish_errors` and never repeated. Every delivery of a log that restates a `landed`, `error` or `cancelled` summary announces `request.landed`, `request.failed` or `request.cancelled`, so a failed publish is returned and the log's redelivery repairs it; a copy that arrives after the queue has forgotten the event id reaches hooks twice. Events therefore follow the summary users see, including a request whose start failed before the orchestrator saved it, and a stale or unfinished log publishes nothing. A `request.failed` payload also carries the summary's `failure_category` and its reason as `failure_reason`, when it has them.
32+
The materializer publishes SubmitQueue's request hook events (`submitqueue/core/hookevent`) to the `submitqueue-hook` topic, which the orchestrator pipeline's hook stage consumes, one event per change of the request. The update that takes the summary out of `accepting` announces `request.received`, once. That is normally `Land`, and a later log only when `Land` failed to record `accepted`; a failed receipt publish is only counted on `request_hook.publish_errors` and never repeated. Every delivery of a log that restates a `landed`, `error` or `cancelled` summary announces `request.landed`, `request.failed` or `request.cancelled`, so a failed publish is returned and the log's redelivery repairs it; a copy that arrives after the queue has forgotten the event id reaches hooks twice. Events therefore follow the summary users see, including a request whose start failed before the orchestrator saved it, and a stale or unfinished log publishes nothing. A `request.failed` payload also carries the summary's `failure_category`, its reason as `failure_reason`, and for a duplicate the in-flight request as `duplicate_of`, when it has them.
3333

3434
## Final log recovery
3535

‎submitqueue/orchestrator/controller/validate/validate.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ func (c *Controller) Process(ctx context.Context, delivery consumer.Delivery) er
175175
"duplicate_id", dupID,
176176
)
177177
coremetrics.NamedCounter(c.metricsScope, "process", "duplicate_requests", 1, coremetrics.TagsFromContext(ctx)...)
178-
return c.reject(ctx, store, request.ID, fmt.Sprintf("request %s is a duplicate of in-flight request %s", request.ID, dupID), entity.FailureCategoryDuplicate)
178+
return c.reject(ctx, store, request.ID, fmt.Sprintf("request %s is a duplicate of in-flight request %s", request.ID, dupID), entity.FailureCategoryDuplicate,
179+
map[string]string{entity.RequestLogMetadataKeyDuplicateOf: dupID})
179180
}
180181

181182
// Fetch change metadata
@@ -221,7 +222,7 @@ func (c *Controller) Process(ctx context.Context, delivery consumer.Delivery) er
221222
"queue", request.Queue,
222223
"error", err.Error(),
223224
)
224-
return c.reject(ctx, store, request.ID, fmt.Sprintf("custom validation failed: %v", err), entity.FailureCategoryValidationRejected)
225+
return c.reject(ctx, store, request.ID, fmt.Sprintf("custom validation failed: %v", err), entity.FailureCategoryValidationRejected, nil)
225226
}
226227
}
227228
}
@@ -270,8 +271,8 @@ func (c *Controller) Process(ctx context.Context, delivery consumer.Delivery) er
270271
// Only an infra failure while terminating (storage/publish) is returned as an
271272
// error so the delivery is retried; the request itself is never re-queued for
272273
// validation once rejected.
273-
func (c *Controller) reject(ctx context.Context, store orchstorage.Storage, requestID, reason string, category entity.FailureCategory) error {
274-
if _, err := corerequest.TerminateRequest(ctx, store, c.registry, requestID, entity.RequestStateError, reason, entity.FailureCategoryMetadata(nil, category)); err != nil {
274+
func (c *Controller) reject(ctx context.Context, store orchstorage.Storage, requestID, reason string, category entity.FailureCategory, metadata map[string]string) error {
275+
if _, err := corerequest.TerminateRequest(ctx, store, c.registry, requestID, entity.RequestStateError, reason, entity.FailureCategoryMetadata(metadata, category)); err != nil {
275276
coremetrics.NamedCounter(c.metricsScope, "process", "terminate_errors", 1, coremetrics.TagsFromContext(ctx)...)
276277
return fmt.Errorf("failed to terminate rejected request %s: %w", requestID, err)
277278
}

0 commit comments

Comments
 (0)