|
| 1 | +// Copyright (c) 2025 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package request |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/uber/submitqueue/submitqueue/entity" |
| 23 | + "github.com/uber/submitqueue/submitqueue/extension/storage" |
| 24 | +) |
| 25 | + |
| 26 | +const maxSummaryProjectionAttempts = 8 |
| 27 | + |
| 28 | +// CreateContext creates immutable admission data and the initial List projection. |
| 29 | +func CreateContext(ctx context.Context, store storage.Storage, requestContext entity.RequestContext) error { |
| 30 | + if err := store.GetRequestContextStore().Create(ctx, requestContext); err != nil { |
| 31 | + if !errors.Is(err, storage.ErrAlreadyExists) { |
| 32 | + return fmt.Errorf("failed to create request context request_id=%s: %w", requestContext.RequestID, err) |
| 33 | + } |
| 34 | + existing, getErr := store.GetRequestContextStore().Get(ctx, requestContext.RequestID) |
| 35 | + if getErr != nil { |
| 36 | + return fmt.Errorf("failed to verify existing request context request_id=%s: %w", requestContext.RequestID, getErr) |
| 37 | + } |
| 38 | + if !equalRequestContext(existing, requestContext) { |
| 39 | + return fmt.Errorf("request context conflicts with existing immutable context request_id=%s", requestContext.RequestID) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + summary := entity.RequestSummary{ |
| 44 | + RequestID: requestContext.RequestID, |
| 45 | + Queue: requestContext.Queue, |
| 46 | + ChangeURIs: append([]string(nil), requestContext.ChangeURIs...), |
| 47 | + Status: entity.RequestStatusAccepted, |
| 48 | + Metadata: map[string]string{}, |
| 49 | + StartedAtMs: requestContext.AdmittedAtMs, |
| 50 | + UpdatedAtMs: requestContext.AdmittedAtMs, |
| 51 | + StatusTimestampMs: requestContext.AdmittedAtMs, |
| 52 | + Version: 1, |
| 53 | + } |
| 54 | + if err := store.GetRequestSummaryStore().Create(ctx, summary); err != nil && !errors.Is(err, storage.ErrAlreadyExists) { |
| 55 | + return fmt.Errorf("failed to create request summary request_id=%s: %w", requestContext.RequestID, err) |
| 56 | + } |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +// PersistLog appends one immutable status event and updates its summary projection. A projection error is returned so queue delivery retries. |
| 61 | +func PersistLog(ctx context.Context, store storage.Storage, log entity.RequestLog) error { |
| 62 | + if err := store.GetRequestLogStore().Insert(ctx, log); err != nil { |
| 63 | + return fmt.Errorf("failed to insert request log request_id=%s: %w", log.RequestID, err) |
| 64 | + } |
| 65 | + if err := ProjectLog(ctx, store, log); err != nil { |
| 66 | + return fmt.Errorf("failed to project request log request_id=%s: %w", log.RequestID, err) |
| 67 | + } |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +// ProjectLog applies a status event to an existing summary using optimistic concurrency. |
| 72 | +func ProjectLog(ctx context.Context, store storage.Storage, log entity.RequestLog) error { |
| 73 | + requestContext, err := store.GetRequestContextStore().Get(ctx, log.RequestID) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("failed to get request context request_id=%s: %w", log.RequestID, err) |
| 76 | + } |
| 77 | + |
| 78 | + for attempt := 0; attempt < maxSummaryProjectionAttempts; attempt++ { |
| 79 | + existing, err := store.GetRequestSummaryStore().Get(ctx, requestContext.Queue, log.RequestID) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("failed to get request summary request_id=%s: %w", log.RequestID, err) |
| 82 | + } |
| 83 | + next := MergeSummary(existing, log) |
| 84 | + newVersion := existing.Version + 1 |
| 85 | + if err := store.GetRequestSummaryStore().Update(ctx, next, existing.Version, newVersion); err == nil { |
| 86 | + return nil |
| 87 | + } else if !errors.Is(err, storage.ErrVersionMismatch) { |
| 88 | + return fmt.Errorf("failed to update request summary request_id=%s: %w", log.RequestID, err) |
| 89 | + } |
| 90 | + } |
| 91 | + return fmt.Errorf("request summary projection did not converge request_id=%s: %w", log.RequestID, storage.ErrVersionMismatch) |
| 92 | +} |
| 93 | + |
| 94 | +// MergeSummary returns the summary that results when log is reconciled against existing. |
| 95 | +func MergeSummary(existing entity.RequestSummary, log entity.RequestLog) entity.RequestSummary { |
| 96 | + next := existing |
| 97 | + if !shouldReplaceWinner(existing, log) { |
| 98 | + return next |
| 99 | + } |
| 100 | + next.Status = log.Status |
| 101 | + next.LastError = log.LastError |
| 102 | + next.Metadata = cloneMetadata(log.Metadata) |
| 103 | + next.UpdatedAtMs = log.TimestampMs |
| 104 | + next.RequestVersion = log.RequestVersion |
| 105 | + next.StatusTimestampMs = log.TimestampMs |
| 106 | + next.WinnerTerminalVersion = isTerminalVersion(log) |
| 107 | + if entity.IsRequestStateTerminal(entity.RequestState(string(log.Status))) { |
| 108 | + next.CompletedAtMs = log.TimestampMs |
| 109 | + } else { |
| 110 | + next.CompletedAtMs = 0 |
| 111 | + } |
| 112 | + return next |
| 113 | +} |
| 114 | + |
| 115 | +func shouldReplaceWinner(existing entity.RequestSummary, log entity.RequestLog) bool { |
| 116 | + incomingTerminalVersion := isTerminalVersion(log) |
| 117 | + if incomingTerminalVersion { |
| 118 | + if !existing.WinnerTerminalVersion { |
| 119 | + return true |
| 120 | + } |
| 121 | + return log.RequestVersion > existing.RequestVersion || (log.RequestVersion == existing.RequestVersion && log.TimestampMs > existing.StatusTimestampMs) |
| 122 | + } |
| 123 | + if existing.WinnerTerminalVersion { |
| 124 | + return false |
| 125 | + } |
| 126 | + return log.TimestampMs > existing.StatusTimestampMs |
| 127 | +} |
| 128 | + |
| 129 | +func isTerminalVersion(log entity.RequestLog) bool { |
| 130 | + return log.RequestVersion > 0 && entity.IsRequestStateTerminal(entity.RequestState(string(log.Status))) |
| 131 | +} |
| 132 | + |
| 133 | +func cloneMetadata(metadata map[string]string) map[string]string { |
| 134 | + if metadata == nil { |
| 135 | + return map[string]string{} |
| 136 | + } |
| 137 | + clone := make(map[string]string, len(metadata)) |
| 138 | + for key, value := range metadata { |
| 139 | + clone[key] = value |
| 140 | + } |
| 141 | + return clone |
| 142 | +} |
| 143 | + |
| 144 | +func equalRequestContext(left, right entity.RequestContext) bool { |
| 145 | + return left.RequestID == right.RequestID && |
| 146 | + left.Queue == right.Queue && |
| 147 | + left.AdmittedAtMs == right.AdmittedAtMs && |
| 148 | + equalChangeURIs(left.ChangeURIs, right.ChangeURIs) |
| 149 | +} |
| 150 | + |
| 151 | +func equalChangeURIs(left, right []string) bool { |
| 152 | + if len(left) != len(right) { |
| 153 | + return false |
| 154 | + } |
| 155 | + for index := range left { |
| 156 | + if left[index] != right[index] { |
| 157 | + return false |
| 158 | + } |
| 159 | + } |
| 160 | + return true |
| 161 | +} |
0 commit comments