1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
'use strict';
/**
* findAndModify_inc.js
*
* Inserts a single document into a collection. Each thread performs a
* findAndModify command to select the document and increment a particular
* field. Asserts that the field has the correct value based on the number
* of increments performed.
*
* This workload was designed to reproduce SERVER-15892.
*/
// For isMongod and supportsDocumentLevelConcurrency.
load('jstests/concurrency/fsm_workload_helpers/server_types.js');
var $config = (function() {
var states = {
init: function init(db, collName) {
this.fieldName = 't' + this.tid;
this.count = 0;
},
update: function update(db, collName) {
var updateDoc = {
$inc: {}
};
updateDoc.$inc[this.fieldName] = 1;
var res = db.runCommand(
{findAndModify: collName, query: {_id: 'findAndModify_inc'}, update: updateDoc});
assertAlways.commandWorked(res);
// If the document was invalidated during a yield, then we wouldn't have modified it.
// The "findAndModify" command returns a null value in this case. See SERVER-22002 for
// more details.
if (isMongod(db) && supportsDocumentLevelConcurrency(db)) {
// For storage engines that support document-level concurrency, if the document is
// modified by another thread during a yield, then the operation is retried
// internally. We never expect to see a null value returned by the "findAndModify"
// command when it is known that a matching document exists in the collection.
assertWhenOwnColl(res.value !== null, 'query spec should have matched a document');
}
if (res.value !== null) {
++this.count;
}
},
find: function find(db, collName) {
var docs = db[collName].find().toArray();
assertWhenOwnColl.eq(1, docs.length);
assertWhenOwnColl(() => {
var doc = docs[0];
if (doc.hasOwnProperty(this.fieldName)) {
assertWhenOwnColl.eq(this.count, doc[this.fieldName]);
} else {
assertWhenOwnColl.eq(this.count, 0);
}
});
}
};
var transitions = {
init: {update: 1},
update: {find: 1},
find: {update: 1}
};
function setup(db, collName, cluster) {
db[collName].insert({_id: 'findAndModify_inc'});
}
return {
threadCount: 20,
iterations: 20,
states: states,
transitions: transitions,
setup: setup
};
})();
|