|
| 1 | +// Copyright 2023 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0, included in the file |
| 9 | +// licenses/APL.txt. |
| 10 | + |
| 11 | +package kvpb |
| 12 | + |
| 13 | +import ( |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/isolation" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/roachpb" |
| 18 | + "github.com/cockroachdb/cockroach/pkg/storage/enginepb" |
| 19 | + "github.com/cockroachdb/cockroach/pkg/util/hlc" |
| 20 | + "github.com/cockroachdb/cockroach/pkg/util/timeutil" |
| 21 | + "github.com/cockroachdb/cockroach/pkg/util/uuid" |
| 22 | + "github.com/cockroachdb/errors" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +func TestPrepareTransactionForRetry(t *testing.T) { |
| 27 | + ts1 := hlc.Timestamp{WallTime: 1} |
| 28 | + ts2 := hlc.Timestamp{WallTime: 2} |
| 29 | + tsClock := hlc.Timestamp{WallTime: 3} |
| 30 | + txn := roachpb.MakeTransaction("test", nil, isolation.Serializable, -1, ts1, 0, 99) |
| 31 | + txn2ID := uuid.MakeV4() // used if txn is aborted |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + err *Error |
| 35 | + expTxn roachpb.Transaction |
| 36 | + expErr bool |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "no error", |
| 40 | + err: nil, |
| 41 | + expErr: true, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "no txn", |
| 45 | + err: NewError(errors.New("random")), |
| 46 | + expErr: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "random error", |
| 50 | + err: NewErrorWithTxn(errors.New("random"), &txn), |
| 51 | + expErr: true, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "txn aborted error", |
| 55 | + err: NewErrorWithTxn(&TransactionAbortedError{}, &txn), |
| 56 | + expTxn: func() roachpb.Transaction { |
| 57 | + nextTxn := txn |
| 58 | + nextTxn.ID = txn2ID |
| 59 | + nextTxn.ReadTimestamp = tsClock |
| 60 | + nextTxn.WriteTimestamp = tsClock |
| 61 | + nextTxn.MinTimestamp = tsClock |
| 62 | + nextTxn.LastHeartbeat = tsClock |
| 63 | + nextTxn.GlobalUncertaintyLimit = tsClock |
| 64 | + return nextTxn |
| 65 | + }(), |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "read within uncertainty error", |
| 69 | + err: NewErrorWithTxn(&ReadWithinUncertaintyIntervalError{ValueTimestamp: ts2}, &txn), |
| 70 | + expTxn: func() roachpb.Transaction { |
| 71 | + nextTxn := txn |
| 72 | + nextTxn.Epoch++ |
| 73 | + nextTxn.ReadTimestamp = ts2.Next() |
| 74 | + nextTxn.WriteTimestamp = ts2.Next() |
| 75 | + return nextTxn |
| 76 | + }(), |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "txn push error", |
| 80 | + err: NewErrorWithTxn(&TransactionPushError{ |
| 81 | + PusheeTxn: roachpb.Transaction{TxnMeta: enginepb.TxnMeta{WriteTimestamp: ts2, Priority: 3}}, |
| 82 | + }, &txn), |
| 83 | + expTxn: func() roachpb.Transaction { |
| 84 | + nextTxn := txn |
| 85 | + nextTxn.Epoch++ |
| 86 | + nextTxn.ReadTimestamp = ts2 |
| 87 | + nextTxn.WriteTimestamp = ts2 |
| 88 | + nextTxn.Priority = 2 |
| 89 | + return nextTxn |
| 90 | + }(), |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "txn retry error (reason: write too old)", |
| 94 | + err: NewErrorWithTxn(&TransactionRetryError{Reason: RETRY_WRITE_TOO_OLD}, &txn), |
| 95 | + expTxn: func() roachpb.Transaction { |
| 96 | + nextTxn := txn |
| 97 | + nextTxn.Epoch++ |
| 98 | + return nextTxn |
| 99 | + }(), |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "txn retry error (reason: serializable)", |
| 103 | + err: NewErrorWithTxn(&TransactionRetryError{Reason: RETRY_SERIALIZABLE}, &txn), |
| 104 | + expTxn: func() roachpb.Transaction { |
| 105 | + nextTxn := txn |
| 106 | + nextTxn.Epoch++ |
| 107 | + nextTxn.ReadTimestamp = tsClock |
| 108 | + nextTxn.WriteTimestamp = tsClock |
| 109 | + return nextTxn |
| 110 | + }(), |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "write too old error", |
| 114 | + err: NewErrorWithTxn(&WriteTooOldError{ActualTimestamp: ts2}, &txn), |
| 115 | + expTxn: func() roachpb.Transaction { |
| 116 | + nextTxn := txn |
| 117 | + nextTxn.Epoch++ |
| 118 | + nextTxn.ReadTimestamp = ts2 |
| 119 | + nextTxn.WriteTimestamp = ts2 |
| 120 | + return nextTxn |
| 121 | + }(), |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "intent missing error", |
| 125 | + err: NewErrorWithTxn(&IntentMissingError{}, &txn), |
| 126 | + expErr: true, |
| 127 | + }, |
| 128 | + } |
| 129 | + for _, tt := range tests { |
| 130 | + t.Run(tt.name, func(t *testing.T) { |
| 131 | + clock := hlc.NewClockForTesting(timeutil.NewManualTime(timeutil.Unix(0, tsClock.WallTime))) |
| 132 | + nextTxn, err := PrepareTransactionForRetry(tt.err, -1 /* pri */, clock) |
| 133 | + if tt.expErr { |
| 134 | + require.Error(t, err) |
| 135 | + require.True(t, errors.IsAssertionFailure(err)) |
| 136 | + require.Zero(t, nextTxn) |
| 137 | + } else { |
| 138 | + require.NoError(t, err) |
| 139 | + if nextTxn.ID != txn.ID { |
| 140 | + // Eliminate randomness from ID generation. |
| 141 | + nextTxn.ID = txn2ID |
| 142 | + } |
| 143 | + require.Equal(t, tt.expTxn, nextTxn) |
| 144 | + } |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestTransactionRefreshTimestamp(t *testing.T) { |
| 150 | + ts1 := hlc.Timestamp{WallTime: 1} |
| 151 | + ts2 := hlc.Timestamp{WallTime: 2} |
| 152 | + txn := roachpb.MakeTransaction("test", nil, isolation.Serializable, 1, ts1, 0, 99) |
| 153 | + tests := []struct { |
| 154 | + name string |
| 155 | + err *Error |
| 156 | + expOk bool |
| 157 | + expTs hlc.Timestamp |
| 158 | + }{ |
| 159 | + { |
| 160 | + name: "no error", |
| 161 | + err: nil, |
| 162 | + expOk: false, |
| 163 | + expTs: hlc.Timestamp{}, |
| 164 | + }, |
| 165 | + { |
| 166 | + name: "no txn", |
| 167 | + err: NewError(errors.New("random")), |
| 168 | + expOk: false, |
| 169 | + expTs: hlc.Timestamp{}, |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "random error", |
| 173 | + err: NewErrorWithTxn(errors.New("random"), &txn), |
| 174 | + expOk: false, |
| 175 | + expTs: hlc.Timestamp{}, |
| 176 | + }, |
| 177 | + { |
| 178 | + name: "txn aborted error", |
| 179 | + err: NewErrorWithTxn(&TransactionAbortedError{}, &txn), |
| 180 | + expOk: false, |
| 181 | + expTs: hlc.Timestamp{}, |
| 182 | + }, |
| 183 | + { |
| 184 | + name: "txn retry error (reason: unknown)", |
| 185 | + err: NewErrorWithTxn(&TransactionRetryError{Reason: RETRY_REASON_UNKNOWN}, &txn), |
| 186 | + expOk: false, |
| 187 | + expTs: hlc.Timestamp{}, |
| 188 | + }, |
| 189 | + { |
| 190 | + name: "txn retry error (reason: write too old)", |
| 191 | + err: NewErrorWithTxn(&TransactionRetryError{Reason: RETRY_WRITE_TOO_OLD}, &txn), |
| 192 | + expOk: true, |
| 193 | + expTs: ts1, |
| 194 | + }, |
| 195 | + { |
| 196 | + name: "txn retry error (reason: serializable)", |
| 197 | + err: NewErrorWithTxn(&TransactionRetryError{Reason: RETRY_SERIALIZABLE}, &txn), |
| 198 | + expOk: true, |
| 199 | + expTs: ts1, |
| 200 | + }, |
| 201 | + { |
| 202 | + name: "txn retry error (reason: async write failure)", |
| 203 | + err: NewErrorWithTxn(&TransactionRetryError{Reason: RETRY_ASYNC_WRITE_FAILURE}, &txn), |
| 204 | + expOk: false, |
| 205 | + expTs: hlc.Timestamp{}, |
| 206 | + }, |
| 207 | + { |
| 208 | + name: "txn retry error (reason: commit deadline exceeded)", |
| 209 | + err: NewErrorWithTxn(&TransactionRetryError{Reason: RETRY_COMMIT_DEADLINE_EXCEEDED}, &txn), |
| 210 | + expOk: false, |
| 211 | + expTs: hlc.Timestamp{}, |
| 212 | + }, |
| 213 | + { |
| 214 | + name: "write too old error", |
| 215 | + err: NewErrorWithTxn(&WriteTooOldError{ActualTimestamp: ts2}, &txn), |
| 216 | + expOk: true, |
| 217 | + expTs: ts2, |
| 218 | + }, |
| 219 | + { |
| 220 | + name: "read within uncertainty error", |
| 221 | + err: NewErrorWithTxn(&ReadWithinUncertaintyIntervalError{ValueTimestamp: ts2}, &txn), |
| 222 | + expOk: true, |
| 223 | + expTs: ts2.Next(), |
| 224 | + }, |
| 225 | + } |
| 226 | + for _, tt := range tests { |
| 227 | + t.Run(tt.name, func(t *testing.T) { |
| 228 | + ok, ts := TransactionRefreshTimestamp(tt.err) |
| 229 | + require.Equal(t, tt.expOk, ok) |
| 230 | + require.Equal(t, tt.expTs, ts) |
| 231 | + }) |
| 232 | + } |
| 233 | +} |
0 commit comments