@@ -12,14 +12,18 @@ const dbName = "test";
12
12
const collName = "prepare_conflict" ;
13
13
const testDB = db . getSiblingDB ( dbName ) ;
14
14
const testColl = testDB . getCollection ( collName ) ;
15
-
16
- // Logs must be cleared each run.
17
- assert . commandWorked ( testDB . adminCommand ( { clearLog : "global" } ) ) ;
15
+ const prepareConflictDurationLogMsg = "prepareConflictDuration" ;
18
16
19
17
testColl . drop ( { writeConcern : { w : "majority" } } ) ;
20
18
assert . commandWorked ( testDB . runCommand ( { create : collName , writeConcern : { w : "majority" } } ) ) ;
21
19
22
- function assertPrepareConflict ( filter , clusterTime , count ) {
20
+ /**
21
+ * Asserts that a prepare read conflict occurs, and is recorded through the profiler and logs
22
+ * accordingly, by running a find command that uses the provided filter and clusterTime.
23
+ */
24
+ const assertPrepareConflict = function assertPrepareReadConflict ( filter , clusterTime ) {
25
+ assert . commandWorked ( testDB . adminCommand ( { clearLog : "global" } ) ) ;
26
+
23
27
// Uses a 5 second timeout so that there is enough time for the prepared transaction to
24
28
// release its locks and for the command to obtain those locks.
25
29
assert . commandFailedWithCode (
@@ -32,8 +36,7 @@ function assertPrepareConflict(filter, clusterTime, count) {
32
36
} ) ,
33
37
ErrorCodes . MaxTimeMSExpired ) ;
34
38
35
- // Ensures prepareConflictDuration is logged each time a prepare conflict is encountered.
36
- checkLog . containsWithCount ( testDB , "prepareConflictDuration" , count ) ;
39
+ checkLog . contains ( testDB , prepareConflictDurationLogMsg ) ;
37
40
38
41
let prepareConflicted = false ;
39
42
const cur =
@@ -46,15 +49,15 @@ function assertPrepareConflict(filter, clusterTime, count) {
46
49
}
47
50
}
48
51
assert ( prepareConflicted ) ;
49
- }
52
+ } ;
50
53
51
- // Inserts a document modified by the transaction.
54
+ // Insert a document modified by the transaction.
52
55
const txnDoc = {
53
56
_id : 1 ,
54
57
x : 1
55
58
} ;
56
59
assert . commandWorked ( testColl . insert ( txnDoc ) ) ;
57
- // Inserts a document unmodified by the transaction.
60
+ // Insert a document unmodified by the transaction.
58
61
const otherDoc = {
59
62
_id : 2 ,
60
63
y : 2
@@ -77,24 +80,21 @@ assert.commandWorked(sessionDB.runCommand({
77
80
} ) ) ;
78
81
const prepareTimestamp = PrepareHelpers . prepareTransaction ( session ) ;
79
82
80
- // Stores the number of times prepareConflictDuration should appear in the log.
81
- let conflictCount = 1 ;
82
-
83
83
// Conflict on _id of prepared document.
84
- assertPrepareConflict ( { _id : txnDoc . _id } , prepareTimestamp , conflictCount ) ;
84
+ assertPrepareConflict ( { _id : txnDoc . _id } , prepareTimestamp ) ;
85
85
86
86
// Conflict on field that could be added to a prepared document.
87
- assertPrepareConflict ( { randomField : "random" } , prepareTimestamp , ++ conflictCount ) ;
87
+ assertPrepareConflict ( { randomField : "random" } , prepareTimestamp ) ;
88
88
89
89
// No conflict on _id of a non-prepared document.
90
+ assert . commandWorked ( testDB . adminCommand ( { clearLog : "global" } ) ) ;
90
91
assert . commandWorked ( testDB . runCommand ( { find : collName , filter : { _id : otherDoc . _id } } ) ) ;
92
+ assert . eq ( false , checkLog . checkContainsOnce ( testDB , prepareConflictDurationLogMsg ) ) ;
91
93
92
94
// No conflict on indexed field of a non-prepared document.
95
+ assert . commandWorked ( testDB . adminCommand ( { clearLog : "global" } ) ) ;
93
96
assert . commandWorked ( testDB . runCommand ( { find : collName , filter : { y : otherDoc . y } } ) ) ;
94
-
95
- // Ensures that prepareConflictDuration is only logged when a prepare conflict is encountered. We
96
- // expect conflictCount to be 2.
97
- checkLog . containsWithCount ( testDB , "prepareConflictDuration" , conflictCount ) ;
97
+ assert . eq ( false , checkLog . checkContainsOnce ( testDB , prepareConflictDurationLogMsg ) ) ;
98
98
99
99
// At this point, we can guarantee all subsequent reads will conflict. Do a read in a parallel
100
100
// shell, abort the transaction, then ensure the read succeeded with the old document.
@@ -105,7 +105,6 @@ const findAwait = startParallelShell(function() {
105
105
const it = db . getSiblingDB ( TestData . dbName )
106
106
. runCommand ( { find : TestData . collName , filter : { _id : TestData . txnDoc . _id } } ) ;
107
107
} , db . getMongo ( ) . port ) ;
108
-
109
108
assert . commandWorked ( session . abortTransaction_forTesting ( ) ) ;
110
109
111
110
// The find command should be successful.
0 commit comments