|
| 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 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + "github.com/uber/submitqueue/submitqueue/entity" |
| 25 | + "github.com/uber/submitqueue/submitqueue/extension/storage" |
| 26 | + storagemock "github.com/uber/submitqueue/submitqueue/extension/storage/mock" |
| 27 | + "go.uber.org/mock/gomock" |
| 28 | +) |
| 29 | + |
| 30 | +func TestMaterializer_PersistLog(t *testing.T) { |
| 31 | + base := testRequestSummary() |
| 32 | + log := entity.RequestLog{RequestID: "q/1", TimestampMs: 20, Status: entity.RequestStatusLanded, RequestVersion: 2, Metadata: map[string]string{}} |
| 33 | + t.Run("winning log updates both projections", func(t *testing.T) { |
| 34 | + ctrl := gomock.NewController(t) |
| 35 | + store, summaryStore, queueStore, logStore := materializerStores(ctrl) |
| 36 | + logStore.EXPECT().Insert(gomock.Any(), log).Return(nil) |
| 37 | + summaryStore.EXPECT().Get(gomock.Any(), "q/1").Return(base, nil) |
| 38 | + summaryStore.EXPECT().Update(gomock.Any(), gomock.Any(), int32(1), int32(2)).DoAndReturn(func(_ context.Context, updated entity.RequestSummary, _, _ int32) error { |
| 39 | + assert.Equal(t, entity.RequestStatusLanded, updated.Status) |
| 40 | + assert.Equal(t, int32(2), updated.RequestVersion) |
| 41 | + return nil |
| 42 | + }) |
| 43 | + queueStore.EXPECT().Get(gomock.Any(), "q", int64(10), "q/1").Return(queueSummaryFromSummary(base), nil) |
| 44 | + queueStore.EXPECT().Update(gomock.Any(), gomock.Any(), int32(1), int32(2)).Return(nil) |
| 45 | + require.NoError(t, NewMaterializer(store).PersistLog(context.Background(), log)) |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("unversioned terminal status does not receive terminal precedence", func(t *testing.T) { |
| 49 | + ctrl := gomock.NewController(t) |
| 50 | + store, summaryStore, queueStore, logStore := materializerStores(ctrl) |
| 51 | + current := base |
| 52 | + current.Status = entity.RequestStatusLanded |
| 53 | + current.RequestVersion = 0 |
| 54 | + incoming := entity.RequestLog{RequestID: "q/1", TimestampMs: 20, Status: entity.RequestStatusProcessing, RequestVersion: 0, Metadata: map[string]string{}} |
| 55 | + logStore.EXPECT().Insert(gomock.Any(), incoming).Return(nil) |
| 56 | + summaryStore.EXPECT().Get(gomock.Any(), "q/1").Return(current, nil) |
| 57 | + summaryStore.EXPECT().Update(gomock.Any(), gomock.Any(), int32(1), int32(2)).DoAndReturn(func(_ context.Context, updated entity.RequestSummary, _, _ int32) error { |
| 58 | + assert.Equal(t, entity.RequestStatusProcessing, updated.Status) |
| 59 | + return nil |
| 60 | + }) |
| 61 | + queueStore.EXPECT().Get(gomock.Any(), "q", int64(10), "q/1").Return(queueSummaryFromSummary(current), nil) |
| 62 | + queueStore.EXPECT().Update(gomock.Any(), gomock.Any(), int32(1), int32(2)).Return(nil) |
| 63 | + require.NoError(t, NewMaterializer(store).PersistLog(context.Background(), incoming)) |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("CAS conflict reloads and repairs winner", func(t *testing.T) { |
| 67 | + ctrl := gomock.NewController(t) |
| 68 | + store, summaryStore, queueStore, logStore := materializerStores(ctrl) |
| 69 | + logStore.EXPECT().Insert(gomock.Any(), log).Return(nil) |
| 70 | + summaryStore.EXPECT().Get(gomock.Any(), "q/1").Return(base, nil) |
| 71 | + summaryStore.EXPECT().Update(gomock.Any(), gomock.Any(), int32(1), int32(2)).Return(storage.ErrVersionMismatch) |
| 72 | + advanced := base |
| 73 | + advanced.Status = entity.RequestStatusLanded |
| 74 | + advanced.RequestVersion = 2 |
| 75 | + advanced.StatusTimestampMs = 20 |
| 76 | + advanced.Version = 2 |
| 77 | + summaryStore.EXPECT().Get(gomock.Any(), "q/1").Return(advanced, nil) |
| 78 | + queueStore.EXPECT().Get(gomock.Any(), "q", int64(10), "q/1").Return(queueSummaryFromSummary(base), nil) |
| 79 | + queueStore.EXPECT().Update(gomock.Any(), gomock.Any(), int32(1), int32(2)).Return(nil) |
| 80 | + require.NoError(t, NewMaterializer(store).PersistLog(context.Background(), log)) |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run("non-winning redelivery repairs stale queue projection", func(t *testing.T) { |
| 84 | + ctrl := gomock.NewController(t) |
| 85 | + store, summaryStore, queueStore, logStore := materializerStores(ctrl) |
| 86 | + logStore.EXPECT().Insert(gomock.Any(), log).Return(nil) |
| 87 | + advanced := base |
| 88 | + advanced.Status = entity.RequestStatusLanded |
| 89 | + advanced.RequestVersion = 2 |
| 90 | + advanced.StatusTimestampMs = 20 |
| 91 | + advanced.Version = 2 |
| 92 | + summaryStore.EXPECT().Get(gomock.Any(), "q/1").Return(advanced, nil) |
| 93 | + queueStore.EXPECT().Get(gomock.Any(), "q", int64(10), "q/1").Return(queueSummaryFromSummary(base), nil) |
| 94 | + queueStore.EXPECT().Update(gomock.Any(), gomock.Any(), int32(1), int32(2)).Return(nil) |
| 95 | + require.NoError(t, NewMaterializer(store).PersistLog(context.Background(), log)) |
| 96 | + }) |
| 97 | + |
| 98 | + t.Run("missing queue projection is recreated", func(t *testing.T) { |
| 99 | + ctrl := gomock.NewController(t) |
| 100 | + store, summaryStore, queueStore, logStore := materializerStores(ctrl) |
| 101 | + logStore.EXPECT().Insert(gomock.Any(), log).Return(nil) |
| 102 | + advanced := base |
| 103 | + advanced.Status = entity.RequestStatusLanded |
| 104 | + advanced.RequestVersion = 2 |
| 105 | + advanced.StatusTimestampMs = 20 |
| 106 | + advanced.Version = 2 |
| 107 | + summaryStore.EXPECT().Get(gomock.Any(), "q/1").Return(advanced, nil) |
| 108 | + queueStore.EXPECT().Get(gomock.Any(), "q", int64(10), "q/1").Return(entity.RequestQueueSummary{}, storage.ErrNotFound) |
| 109 | + queueStore.EXPECT().Create(gomock.Any(), queueSummaryFromSummary(advanced)).Return(nil) |
| 110 | + require.NoError(t, NewMaterializer(store).PersistLog(context.Background(), log)) |
| 111 | + }) |
| 112 | + |
| 113 | + t.Run("retry after projection failure appends another audit row", func(t *testing.T) { |
| 114 | + ctrl := gomock.NewController(t) |
| 115 | + store, summaryStore, queueStore, logStore := materializerStores(ctrl) |
| 116 | + materializer := NewMaterializer(store) |
| 117 | + advanced := base |
| 118 | + advanced.Status = entity.RequestStatusLanded |
| 119 | + advanced.RequestVersion = 2 |
| 120 | + advanced.StatusTimestampMs = 20 |
| 121 | + advanced.Version = 2 |
| 122 | + logStore.EXPECT().Insert(gomock.Any(), log).Return(nil).Times(2) |
| 123 | + summaryStore.EXPECT().Get(gomock.Any(), "q/1").Return(advanced, nil).Times(2) |
| 124 | + queueStore.EXPECT().Get(gomock.Any(), "q", int64(10), "q/1").Return(entity.RequestQueueSummary{}, errors.New("queue store down")) |
| 125 | + queueStore.EXPECT().Get(gomock.Any(), "q", int64(10), "q/1").Return(queueSummaryFromSummary(advanced), nil) |
| 126 | + |
| 127 | + require.Error(t, materializer.PersistLog(context.Background(), log)) |
| 128 | + require.NoError(t, materializer.PersistLog(context.Background(), log)) |
| 129 | + }) |
| 130 | + |
| 131 | + t.Run("missing authoritative summary fails", func(t *testing.T) { |
| 132 | + ctrl := gomock.NewController(t) |
| 133 | + store, summaryStore, _, logStore := materializerStores(ctrl) |
| 134 | + logStore.EXPECT().Insert(gomock.Any(), log).Return(nil) |
| 135 | + summaryStore.EXPECT().Get(gomock.Any(), "q/1").Return(entity.RequestSummary{}, storage.ErrNotFound) |
| 136 | + require.Error(t, NewMaterializer(store).PersistLog(context.Background(), log)) |
| 137 | + }) |
| 138 | + |
| 139 | + t.Run("queue projection ahead fails fast", func(t *testing.T) { |
| 140 | + ctrl := gomock.NewController(t) |
| 141 | + store, summaryStore, queueStore, logStore := materializerStores(ctrl) |
| 142 | + logStore.EXPECT().Insert(gomock.Any(), log).Return(nil) |
| 143 | + advanced := base |
| 144 | + advanced.Status = entity.RequestStatusLanded |
| 145 | + advanced.RequestVersion = 2 |
| 146 | + advanced.StatusTimestampMs = 20 |
| 147 | + advanced.Version = 2 |
| 148 | + summaryStore.EXPECT().Get(gomock.Any(), "q/1").Return(advanced, nil) |
| 149 | + queueAhead := queueSummaryFromSummary(advanced) |
| 150 | + queueAhead.Version = 3 |
| 151 | + queueStore.EXPECT().Get(gomock.Any(), "q", int64(10), "q/1").Return(queueAhead, nil) |
| 152 | + require.Error(t, NewMaterializer(store).PersistLog(context.Background(), log)) |
| 153 | + }) |
| 154 | +} |
| 155 | + |
| 156 | +func TestLogWins(t *testing.T) { |
| 157 | + base := testRequestSummary() |
| 158 | + tests := []struct { |
| 159 | + name string |
| 160 | + current entity.RequestSummary |
| 161 | + incoming entity.RequestLog |
| 162 | + want bool |
| 163 | + }{ |
| 164 | + { |
| 165 | + name: "versioned terminal beats newer unversioned status", |
| 166 | + current: entity.RequestSummary{Status: entity.RequestStatusProcessing, StatusTimestampMs: 200}, |
| 167 | + incoming: entity.RequestLog{ |
| 168 | + Status: entity.RequestStatusLanded, RequestVersion: 1, TimestampMs: 100, |
| 169 | + }, |
| 170 | + want: true, |
| 171 | + }, |
| 172 | + { |
| 173 | + name: "nonterminal cannot replace versioned terminal", |
| 174 | + current: entity.RequestSummary{Status: entity.RequestStatusLanded, RequestVersion: 1, StatusTimestampMs: 100}, |
| 175 | + incoming: entity.RequestLog{ |
| 176 | + Status: entity.RequestStatusProcessing, TimestampMs: 200, |
| 177 | + }, |
| 178 | + }, |
| 179 | + { |
| 180 | + name: "higher terminal request version wins", |
| 181 | + current: entity.RequestSummary{Status: entity.RequestStatusError, RequestVersion: 1, StatusTimestampMs: 200}, |
| 182 | + incoming: entity.RequestLog{ |
| 183 | + Status: entity.RequestStatusLanded, RequestVersion: 2, TimestampMs: 100, |
| 184 | + }, |
| 185 | + want: true, |
| 186 | + }, |
| 187 | + { |
| 188 | + name: "lower terminal request version loses", |
| 189 | + current: entity.RequestSummary{Status: entity.RequestStatusLanded, RequestVersion: 2, StatusTimestampMs: 100}, |
| 190 | + incoming: entity.RequestLog{ |
| 191 | + Status: entity.RequestStatusError, RequestVersion: 1, TimestampMs: 200, |
| 192 | + }, |
| 193 | + }, |
| 194 | + { |
| 195 | + name: "equal terminal version uses later timestamp", |
| 196 | + current: entity.RequestSummary{Status: entity.RequestStatusError, RequestVersion: 2, StatusTimestampMs: 100}, |
| 197 | + incoming: entity.RequestLog{ |
| 198 | + Status: entity.RequestStatusLanded, RequestVersion: 2, TimestampMs: 200, |
| 199 | + }, |
| 200 | + want: true, |
| 201 | + }, |
| 202 | + { |
| 203 | + name: "without terminal winner later timestamp wins", |
| 204 | + current: base, |
| 205 | + incoming: entity.RequestLog{ |
| 206 | + Status: entity.RequestStatusStarted, TimestampMs: base.StatusTimestampMs + 1, |
| 207 | + }, |
| 208 | + want: true, |
| 209 | + }, |
| 210 | + { |
| 211 | + name: "exact version and timestamp tie keeps current winner", |
| 212 | + current: entity.RequestSummary{Status: entity.RequestStatusError, RequestVersion: 2, StatusTimestampMs: 200}, |
| 213 | + incoming: entity.RequestLog{ |
| 214 | + Status: entity.RequestStatusLanded, RequestVersion: 2, TimestampMs: 200, |
| 215 | + }, |
| 216 | + }, |
| 217 | + } |
| 218 | + |
| 219 | + for _, tt := range tests { |
| 220 | + t.Run(tt.name, func(t *testing.T) { |
| 221 | + assert.Equal(t, tt.want, logWins(tt.incoming, tt.current)) |
| 222 | + }) |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +func materializerStores(ctrl *gomock.Controller) (*storagemock.MockStorage, *storagemock.MockRequestSummaryStore, *storagemock.MockRequestQueueSummaryStore, *storagemock.MockRequestLogStore) { |
| 227 | + store := storagemock.NewMockStorage(ctrl) |
| 228 | + summaryStore := storagemock.NewMockRequestSummaryStore(ctrl) |
| 229 | + queueStore := storagemock.NewMockRequestQueueSummaryStore(ctrl) |
| 230 | + logStore := storagemock.NewMockRequestLogStore(ctrl) |
| 231 | + store.EXPECT().GetRequestSummaryStore().Return(summaryStore).AnyTimes() |
| 232 | + store.EXPECT().GetRequestQueueSummaryStore().Return(queueStore).AnyTimes() |
| 233 | + store.EXPECT().GetRequestLogStore().Return(logStore).AnyTimes() |
| 234 | + return store, summaryStore, queueStore, logStore |
| 235 | +} |
0 commit comments