Skip to content

Commit 0b5639e

Browse files
committed
kvclient: add tests for CPut evaluation with buffered writes
ConditionFailedErrors returned by CPuts are special, in that they're non-terminal. That means that a transaction is allowed to commit even if it gets one of these. This patch reverts changes to tests from da7a4eb, which assumed we won't try to commit transactions that encountered ConditionFailedErrors. While here, we also extend TestTxnContinueAfterCputError to run with buffered writes and validate the transaction's effects upon commit. All these tests fail as of this commit. Epic: none Release note: None
1 parent 74c7286 commit 0b5639e

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

pkg/kv/kvclient/kvcoord/txn_interceptor_write_buffer_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,11 +1118,11 @@ func TestTxnWriteBufferDecomposesConditionalPuts(t *testing.T) {
11181118
require.IsType(t, &kvpb.ConditionFailedError{}, pErr.GoError())
11191119
}
11201120

1121-
// Lastly, commit or rollback the transaction. A put should only be
1122-
// flushed if the condition evaluated successfully.
1121+
// Lastly, commit the transaction. A put should only be flushed if the
1122+
// condition evaluated successfully.
11231123
ba = &kvpb.BatchRequest{}
11241124
ba.Header = kvpb.Header{Txn: &txn}
1125-
ba.Add(&kvpb.EndTxnRequest{Commit: condEvalSuccessful})
1125+
ba.Add(&kvpb.EndTxnRequest{Commit: true})
11261126

11271127
mockSender.MockSend(func(ba *kvpb.BatchRequest) (*kvpb.BatchResponse, *kvpb.Error) {
11281128
if condEvalSuccessful {

pkg/kv/kvclient/kvcoord/txn_test.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,19 +1037,38 @@ func TestTxnCommitTimestampAdvancedByRefresh(t *testing.T) {
10371037
func TestTxnContinueAfterCputError(t *testing.T) {
10381038
defer leaktest.AfterTest(t)()
10391039
defer log.Scope(t).Close(t)
1040-
ctx := context.Background()
1041-
s := createTestDB(t)
1042-
defer s.Stop()
10431040

1044-
txn := s.DB.NewTxn(ctx, "test txn")
1045-
// Note: Since we're expecting the CPut to fail, the massaging done by
1046-
// StrToCPutExistingValue() is not actually necessary.
1047-
expVal := kvclientutils.StrToCPutExistingValue("dummy")
1048-
err := txn.CPut(ctx, "a", "val", expVal)
1049-
require.IsType(t, &kvpb.ConditionFailedError{}, err)
1041+
testutils.RunTrueAndFalse(t, "bufferedWritesEnabled", func(t *testing.T, bufferedWritesEnabled bool) {
1042+
ctx := context.Background()
1043+
s := createTestDB(t)
1044+
defer s.Stop()
10501045

1051-
require.NoError(t, txn.Put(ctx, "a", "b'"))
1052-
require.NoError(t, txn.Commit(ctx))
1046+
txn := s.DB.NewTxn(ctx, "test txn")
1047+
1048+
if bufferedWritesEnabled {
1049+
txn.SetBufferedWritesEnabled(true)
1050+
}
1051+
1052+
// Note: Since we're expecting the CPut to fail, the massaging done by
1053+
// StrToCPutExistingValue() is not actually necessary.
1054+
expVal := kvclientutils.StrToCPutExistingValue("dummy")
1055+
err := txn.CPut(ctx, "a", "val", expVal)
1056+
require.IsType(t, &kvpb.ConditionFailedError{}, err)
1057+
1058+
// Write to a different key.
1059+
require.NoError(t, txn.Put(ctx, "b", "b'"))
1060+
require.NoError(t, txn.Commit(ctx))
1061+
1062+
// We've successfully commited the transaction. The write to key "b"
1063+
// should be visible whereas the write to keyA shouldn't.
1064+
val, err := s.DB.Get(ctx, "a")
1065+
require.NoError(t, err)
1066+
require.False(t, val.Exists())
1067+
1068+
val, err = s.DB.Get(ctx, "b")
1069+
require.NoError(t, err)
1070+
require.Equal(t, "b'", string(val.ValueBytes()))
1071+
})
10531072
}
10541073

10551074
// Test that a transaction can be used after a locking request returns a
@@ -2013,7 +2032,6 @@ func TestTxnBufferedWritesConditionalPuts(t *testing.T) {
20132032
if expErr {
20142033
require.Error(t, err)
20152034
require.IsType(t, &kvpb.ConditionFailedError{}, err)
2016-
return err
20172035
} else {
20182036
require.NoError(t, err)
20192037
}
@@ -2025,10 +2043,11 @@ func TestTxnBufferedWritesConditionalPuts(t *testing.T) {
20252043
}
20262044
})
20272045

2028-
if commit && !expErr {
2046+
if commit {
20292047
require.NoError(t, err)
20302048
} else {
20312049
require.Error(t, err)
2050+
testutils.IsError(err, "abort")
20322051
}
20332052

20342053
// Verify the values are visible only if the transaction commited

0 commit comments

Comments
 (0)