Skip to content

Commit 265830c

Browse files
authored
fix(submitqueue): Resend a repaired terminal log with the details its caller knows and keep the resent marker out of summaries
1 parent 7d4931e commit 265830c

12 files changed

Lines changed: 201 additions & 45 deletions

File tree

‎submitqueue/entity/request_log.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import (
1818
"time"
1919
)
2020

21+
// RequestLogMetadataKeyResent marks a terminal request log that repeats a status
22+
// already written. It stays on the log in History but never reaches a summary.
23+
const RequestLogMetadataKeyResent = "resent"
24+
2125
// RequestStatus is where a request is in the pipeline. It is customer-friendly and can be displayed to the user.
2226
// It is different from the request state, which is internal and used to implement a state machine. Request statuses can be
2327
// generally added freely by the system without breaking the state machine.

‎submitqueue/gateway/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The gateway owns the request log read model and is the only service that reads i
2525

2626
The materializer appends every audit event, selects the current authoritative winner, and repairs the queue projection. The normal orchestrator pipeline does not read or write the request-log store directly.
2727

28-
A finished status keeps the reason and metadata it was first recorded with. The orchestrator resends a terminal log without them (marked `resent`) when it repairs a request already in that state, so a log restating the summary's finished status and request version only fills details the summary lacks, whichever copy arrives first.
28+
A finished status keeps the reason and metadata it was first recorded with. When the orchestrator repairs a request already in that state it resends the terminal log marked `resent`, with whatever details the repairing path knows, which may differ from the original's or be none. So a log restating the summary's finished status and request version only fills details the summary lacks, whichever copy arrives first, and the `resent` marker stays in History, never in the summary.
2929

3030
## Request hook events
3131

‎submitqueue/gateway/core/request/materializer.go‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func projectLog(log entity.RequestLog, summary entity.RequestSummary) (entity.Re
212212
updated.Status = log.Status
213213
updated.RequestVersion = log.RequestVersion
214214
updated.StatusTimestampMs = log.TimestampMs
215-
updated.LastError, updated.Metadata = log.LastError, cloneMetadata(log.Metadata)
215+
updated.LastError, updated.Metadata = log.LastError, summaryMetadata(log.Metadata)
216216
if restatesFinish {
217217
updated.LastError, updated.Metadata = fillFinishedDetails(log, summary)
218218
}
@@ -231,15 +231,23 @@ func fillFinishedDetails(log entity.RequestLog, summary entity.RequestSummary) (
231231
if lastError == "" {
232232
lastError = log.LastError
233233
}
234-
metadata := cloneMetadata(summary.Metadata)
235-
for key, value := range log.Metadata {
234+
metadata := summaryMetadata(summary.Metadata)
235+
for key, value := range summaryMetadata(log.Metadata) {
236236
if _, ok := metadata[key]; !ok {
237237
metadata[key] = value
238238
}
239239
}
240240
return lastError, metadata
241241
}
242242

243+
// summaryMetadata copies a log's metadata for a summary, without the resent
244+
// marker: it describes one log, not the request.
245+
func summaryMetadata(metadata map[string]string) map[string]string {
246+
summary := cloneMetadata(metadata)
247+
delete(summary, entity.RequestLogMetadataKeyResent)
248+
return summary
249+
}
250+
243251
func isTerminalStatus(status entity.RequestStatus) bool {
244252
return entity.IsRequestStateTerminal(entity.RequestState(status))
245253
}

‎submitqueue/gateway/core/request/materializer_test.go‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,13 @@ func TestMaterializer_KeepsFinishedDetails(t *testing.T) {
171171
failed := testRequestSummary()
172172
failed.Status, failed.RequestVersion, failed.StatusTimestampMs, failed.Version = entity.RequestStatusError, 2, 20, 4
173173
failed.LastError, failed.Metadata = "build failed", map[string]string{"batch_id": "b1"}
174+
started := testRequestSummary()
175+
started.Status, started.RequestVersion, started.StatusTimestampMs, started.Version = entity.RequestStatusStarted, 1, 15, 3
174176
failedByResend := failed
175-
failedByResend.StatusTimestampMs, failedByResend.LastError, failedByResend.Metadata = 30, "", map[string]string{"resent": "true"}
176-
resend := entity.RequestLog{Queue: "q", RequestID: "1", Type: entity.RequestLogTypeStatus, Status: entity.RequestStatusError, RequestVersion: 2, TimestampMs: 30, Metadata: map[string]string{"resent": "true"}}
177+
failedByResend.StatusTimestampMs, failedByResend.LastError, failedByResend.Metadata = 30, "", map[string]string{}
178+
resend := entity.RequestLog{Queue: "q", RequestID: "1", Type: entity.RequestLogTypeStatus, Status: entity.RequestStatusError, RequestVersion: 2, TimestampMs: 30, LastError: "dead lettered", Metadata: map[string]string{"batch_id": "b9", "dlq.failure_count": "3", "resent": "true"}}
179+
bareResend := resend
180+
bareResend.LastError, bareResend.Metadata = "", map[string]string{"resent": "true"}
177181
original := entity.RequestLog{Queue: "q", RequestID: "1", Type: entity.RequestLogTypeStatus, Status: entity.RequestStatusError, RequestVersion: 2, TimestampMs: 20, LastError: "build failed", Metadata: map[string]string{"batch_id": "b1"}}
178182
laterVersion := original
179183
laterVersion.RequestVersion, laterVersion.TimestampMs, laterVersion.LastError, laterVersion.Metadata = 3, 40, "land failed", map[string]string{"batch_id": "b2"}
@@ -187,20 +191,36 @@ func TestMaterializer_KeepsFinishedDetails(t *testing.T) {
187191
wantMetadata map[string]string
188192
}{
189193
{
190-
name: "a resend after the original keeps its reason",
194+
name: "a resend after the original keeps its details and only fills what they lack",
191195
before: failed,
192196
log: resend,
193197
wantUpdate: true,
194198
wantLastError: "build failed",
195-
wantMetadata: map[string]string{"batch_id": "b1", "resent": "true"},
199+
wantMetadata: map[string]string{"batch_id": "b1", "dlq.failure_count": "3"},
200+
},
201+
{
202+
name: "a resend without details after the original keeps them",
203+
before: failed,
204+
log: bareResend,
205+
wantUpdate: true,
206+
wantLastError: "build failed",
207+
wantMetadata: map[string]string{"batch_id": "b1"},
208+
},
209+
{
210+
name: "a resend whose original never arrived records its details without the resent marker",
211+
before: started,
212+
log: resend,
213+
wantUpdate: true,
214+
wantLastError: "dead lettered",
215+
wantMetadata: map[string]string{"batch_id": "b9", "dlq.failure_count": "3"},
196216
},
197217
{
198-
name: "the original after a resend fills the missing reason despite its older timestamp",
218+
name: "the original after a bare resend fills the missing reason despite its older timestamp",
199219
before: failedByResend,
200220
log: original,
201221
wantUpdate: true,
202222
wantLastError: "build failed",
203-
wantMetadata: map[string]string{"batch_id": "b1", "resent": "true"},
223+
wantMetadata: map[string]string{"batch_id": "b1"},
204224
},
205225
{
206226
name: "a redelivered original changes nothing",

‎submitqueue/orchestrator/controller/cancel/cancel.go‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (c *Controller) Process(ctx context.Context, delivery consumer.Delivery) er
149149

150150
// The cancellation may be terminal even though its final log was never published.
151151
if request.State == entity.RequestStateCancelled {
152-
return corerequest.ResendTerminalRequestLog(ctx, c.registry, request)
152+
return corerequest.ResendTerminalRequestLog(ctx, c.registry, request, "", cancelMetadata(cancelReq.Reason))
153153
}
154154

155155
// Other terminal outcomes belong to their winning writer and are left untouched.
@@ -264,11 +264,7 @@ func (c *Controller) findBatches(ctx context.Context, store orchstorage.Storage,
264264
// reports TerminationDiverged and we simply ack — the other writer owns the
265265
// terminal log for the state it wrote.
266266
func (c *Controller) cancelRequest(ctx context.Context, store orchstorage.Storage, request entity.Request, reason string) error {
267-
metadata := map[string]string{}
268-
if reason != "" {
269-
metadata["reason"] = reason
270-
}
271-
res, err := corerequest.TerminateRequest(ctx, store, c.registry, request.ID, entity.RequestStateCancelled, "", metadata)
267+
res, err := corerequest.TerminateRequest(ctx, store, c.registry, request.ID, entity.RequestStateCancelled, "", cancelMetadata(reason))
272268
if err != nil {
273269
metrics.NamedCounter(c.metricsScope, opName, "request_terminate_errors", 1, metrics.TagsFromContext(ctx)...)
274270
return fmt.Errorf("failed to cancel request %s: %w", request.ID, err)
@@ -385,3 +381,12 @@ func (c *Controller) TopicKey() consumer.TopicKey {
385381
func (c *Controller) ConsumerGroup() string {
386382
return c.consumerGroup
387383
}
384+
385+
// cancelMetadata is the cancelled request log's metadata: the caller's reason, when given.
386+
func cancelMetadata(reason string) map[string]string {
387+
metadata := map[string]string{}
388+
if reason != "" {
389+
metadata["reason"] = reason
390+
}
391+
return metadata
392+
}

‎submitqueue/orchestrator/controller/cancel/cancel_test.go‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,48 @@ func TestProcess_AlreadyTerminal_NoOp(t *testing.T) {
166166
require.NoError(t, err)
167167
}
168168

169+
func TestProcess_AlreadyCancelled_ResendsTerminalLog(t *testing.T) {
170+
tests := []struct {
171+
name string
172+
reason string
173+
wantMetadata map[string]string
174+
}{
175+
{name: "with the cancel reason", reason: "user asked", wantMetadata: map[string]string{"reason": "user asked", entity.RequestLogMetadataKeyResent: "true"}},
176+
{name: "without a reason", wantMetadata: map[string]string{entity.RequestLogMetadataKeyResent: "true"}},
177+
}
178+
for _, tt := range tests {
179+
t.Run(tt.name, func(t *testing.T) {
180+
ctrl := gomock.NewController(t)
181+
registry, pub := newRegistry(t, ctrl)
182+
var logs []entity.RequestLog
183+
pub.EXPECT().Publish(gomock.Any(), "log", gomock.Any()).DoAndReturn(
184+
func(_ context.Context, _ string, msg entityqueue.Message) error {
185+
logEntry, err := sqmq.UnmarshalRequestLog(msg.Payload)
186+
require.NoError(t, err)
187+
logs = append(logs, logEntry)
188+
return nil
189+
},
190+
)
191+
192+
reqStore := storagemock.NewMockRequestStore(ctrl)
193+
reqStore.EXPECT().Get(gomock.Any(), "1").Return(entity.Request{
194+
ID: "1", Queue: "q", State: entity.RequestStateCancelled, Version: 5,
195+
}, nil)
196+
store := orchstoragemock.NewMockStorage(ctrl)
197+
store.EXPECT().GetQueueBatchStateStore().Return(newQueueBatchStateStore(ctrl)).AnyTimes()
198+
store.EXPECT().GetRequestStore().Return(reqStore).AnyTimes()
199+
200+
controller := newController(t, store, registry)
201+
require.NoError(t, controller.Process(context.Background(), newDelivery(t, ctrl, cancelPayload(t, "1", tt.reason), "1")))
202+
203+
require.Len(t, logs, 1)
204+
assert.Equal(t, entity.RequestStatusCancelled, logs[0].Status)
205+
assert.Equal(t, int32(5), logs[0].RequestVersion)
206+
assert.Equal(t, tt.wantMetadata, logs[0].Metadata)
207+
})
208+
}
209+
}
210+
169211
func TestProcess_RequestNotFound_Retryable(t *testing.T) {
170212
ctrl := gomock.NewController(t)
171213
registry, _ := newRegistry(t, ctrl)

‎submitqueue/orchestrator/controller/landconflictsignal/landconflictsignal.go‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,14 @@ func (c *Controller) Process(ctx context.Context, delivery consumer.Delivery) er
114114
"partition_key", msg.PartitionKey,
115115
)
116116

117-
// The failed conflict check may be terminal even though its final log was never published.
117+
// The failed conflict check may be terminal even though its final log was never
118+
// published. A redelivered failure carries the same result, so the resend can say why.
118119
if request.State == entity.RequestStateError {
119-
return corerequest.ResendTerminalRequestLog(ctx, c.registry, request)
120+
var reason string
121+
if result.Outcome != runwaypb.Outcome_SUCCEEDED {
122+
reason = result.Reason
123+
}
124+
return corerequest.ResendTerminalRequestLog(ctx, c.registry, request, reason, nil)
120125
}
121126

122127
if entity.IsRequestStateHalted(request.State) {

‎submitqueue/orchestrator/controller/landconflictsignal/landconflictsignal_test.go‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,59 @@ func TestProcess_HaltedRequestSkips(t *testing.T) {
244244
msg := entityqueue.NewMessage(testRequestID, resultPayload(t, res), testQueue, nil)
245245
require.NoError(t, controller.Process(context.Background(), newDelivery(ctrl, msg)))
246246
}
247+
248+
func TestProcess_ErrorRequestResendsTerminalLog(t *testing.T) {
249+
tests := []struct {
250+
name string
251+
result runwaymq.MergeResult
252+
wantLastError string
253+
}{
254+
{
255+
name: "a redelivered failure says why",
256+
result: runwaymq.MergeResult{Id: testRequestID, Outcome: runwaypb.Outcome_FAILED, Reason: "conflict in foo.go"},
257+
wantLastError: "conflict in foo.go",
258+
},
259+
{
260+
name: "a success cannot say why the request failed",
261+
result: runwaymq.MergeResult{Id: testRequestID, Outcome: runwaypb.Outcome_SUCCEEDED},
262+
},
263+
}
264+
for _, tt := range tests {
265+
t.Run(tt.name, func(t *testing.T) {
266+
ctrl := gomock.NewController(t)
267+
reqStore := storagemock.NewMockRequestStore(ctrl)
268+
reqStore.EXPECT().Get(gomock.Any(), testRequestID).Return(
269+
entity.Request{ID: testRequestID, Queue: testQueue, State: entity.RequestStateError, Version: 2}, nil)
270+
store := orchstoragemock.NewMockStorage(ctrl)
271+
store.EXPECT().GetRequestStore().Return(reqStore).AnyTimes()
272+
273+
var logs []entity.RequestLog
274+
pub := queuemock.NewMockPublisher(ctrl)
275+
pub.EXPECT().Publish(gomock.Any(), "log", gomock.Any()).DoAndReturn(
276+
func(_ context.Context, _ string, msg entityqueue.Message) error {
277+
logEntry, err := sqmq.UnmarshalRequestLog(msg.Payload)
278+
require.NoError(t, err)
279+
logs = append(logs, logEntry)
280+
return nil
281+
},
282+
)
283+
q := queuemock.NewMockQueue(ctrl)
284+
q.EXPECT().Publisher().Return(pub).AnyTimes()
285+
registry, err := consumer.NewTopicRegistry([]consumer.TopicConfig{{Key: topickey.TopicKeyLog, Name: "log", Queue: q}})
286+
require.NoError(t, err)
287+
storageFactory := orchstoragemock.NewMockFactory(ctrl)
288+
storageFactory.EXPECT().For(gomock.Any()).Return(store, nil).AnyTimes()
289+
controller := NewController(zaptest.NewLogger(t).Sugar(), tally.NoopScope, storageFactory, registry,
290+
runwaymq.TopicKeyMergeConflictCheckSignal, "orchestrator-landconflictsignal")
291+
292+
msg := entityqueue.NewMessage(testRequestID, resultPayload(t, tt.result), testQueue, nil)
293+
require.NoError(t, controller.Process(context.Background(), newDelivery(ctrl, msg)))
294+
295+
require.Len(t, logs, 1)
296+
assert.Equal(t, entity.RequestStatusError, logs[0].Status)
297+
assert.Equal(t, int32(2), logs[0].RequestVersion)
298+
assert.Equal(t, tt.wantLastError, logs[0].LastError)
299+
assert.Equal(t, "true", logs[0].Metadata[entity.RequestLogMetadataKeyResent])
300+
})
301+
}
302+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ func (c *Controller) Process(ctx context.Context, delivery consumer.Delivery) er
134134
)
135135

136136
// The rejection may be terminal even though its final log was never published.
137+
// This delivery cannot tell why the request was rejected, so the resend has no reason.
137138
if request.State == entity.RequestStateError {
138-
return corerequest.ResendTerminalRequestLog(ctx, c.registry, request)
139+
return corerequest.ResendTerminalRequestLog(ctx, c.registry, request, "", nil)
139140
}
140141

141142
// Cancellation intent halts forward progress even before the terminal outcome.

‎submitqueue/orchestrator/core/request/terminal_log.go‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,29 @@ package request
1717
import (
1818
"context"
1919
"fmt"
20+
"maps"
2021

2122
"github.com/uber/submitqueue/platform/consumer"
2223
"github.com/uber/submitqueue/submitqueue/entity"
2324
)
2425

25-
// MetadataKeyResent marks a terminal request log that repeats a status already
26-
// written, without the original's reason or metadata.
27-
const MetadataKeyResent = "resent"
28-
29-
func resentTerminalLogMetadata() map[string]string {
30-
return map[string]string{MetadataKeyResent: "true"}
26+
// resentTerminalLogMetadata copies metadata and marks the copy resent.
27+
func resentTerminalLogMetadata(metadata map[string]string) map[string]string {
28+
resent := make(map[string]string, len(metadata)+1)
29+
maps.Copy(resent, metadata)
30+
resent[entity.RequestLogMetadataKeyResent] = "true"
31+
return resent
3132
}
3233

33-
// ResendTerminalRequestLog resends the request's terminal status without another state write.
34-
// The log carries no reason and is marked resent; the gateway keeps the details it already has.
35-
func ResendTerminalRequestLog(ctx context.Context, registry consumer.TopicRegistry, request entity.Request) error {
34+
// ResendTerminalRequestLog resends the request's terminal status without another state write, marked resent.
35+
// lastError and metadata are what the caller knows of the finish, empty when it knows nothing. The gateway
36+
// keeps the details that reached it first and only fills those it lacks, so a resend never rewrites them.
37+
func ResendTerminalRequestLog(ctx context.Context, registry consumer.TopicRegistry, request entity.Request, lastError string, metadata map[string]string) error {
3638
status, err := terminalStateToStatus(request.State)
3739
if err != nil {
3840
return err
3941
}
40-
logEntry := entity.NewRequestStatusLog(request.Queue, request.ID, status, request.Version, "", resentTerminalLogMetadata())
42+
logEntry := entity.NewRequestStatusLog(request.Queue, request.ID, status, request.Version, lastError, resentTerminalLogMetadata(metadata))
4143
if err := PublishLog(ctx, registry, logEntry, request.ID, ""); err != nil {
4244
return fmt.Errorf("failed to publish request log for %s: %w", request.ID, err)
4345
}

0 commit comments

Comments
 (0)