/
causal_consistency_helpers.js
80 lines (67 loc) · 2.56 KB
/
causal_consistency_helpers.js
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
/**
* Helper functions for testing causal consistency.
*/
load('jstests/replsets/rslib.js'); // For startSetIfSupportsReadMajority.
function assertAfterClusterTimeReadFails(db, collName) {
assert.commandFailed(db.runCommand(
{find: collName, readConcern: {level: "majority", afterClusterTime: Timestamp(1, 1)}}));
}
function assertAfterClusterTimeReadSucceeds(db, collName) {
assert.commandWorked(db.runCommand(
{find: collName, readConcern: {level: "majority", afterClusterTime: Timestamp(1, 1)}}));
}
function assertDoesNotContainLogicalOrOperationTime(res) {
assertDoesNotContainLogicalTime(res);
assertDoesNotContainOperationTime(res);
}
function assertDoesNotContainLogicalTime(res) {
assert.eq(res.$clusterTime, undefined);
}
function assertDoesNotContainOperationTime(res) {
assert.eq(res.operationTime, undefined);
}
function assertContainsLogicalAndOperationTime(res, opts) {
assertContainsLogicalTime(res, opts);
assertContainsOperationTime(res, opts);
}
function assertContainsLogicalTime(res, opts) {
assert.hasFields(res, ["$clusterTime"]);
assert.hasFields(res.$clusterTime, ["clusterTime", "signature"]);
assert.hasFields(res.$clusterTime.signature, ["hash", "keyId"]);
if (opts.signed !== undefined) {
// Signed cluster times have a keyId greater than 0.
if (opts.signed) {
assert(res.$clusterTime.signature.keyId > NumberLong(0));
} else {
assert.eq(res.$clusterTime.signature.keyId, NumberLong(0));
}
}
if (opts.initialized !== undefined) {
// Initialized operation times are greater than a null timestamp.
if (opts.initialized) {
assert.eq(bsonWoCompare(res.$clusterTime.clusterTime, Timestamp(0, 0)), 1);
} else {
assert.eq(bsonWoCompare(res.$clusterTime.clusterTime, Timestamp(0, 0)), 0);
}
}
}
function assertContainsOperationTime(res, opts) {
assert.hasFields(res, ["operationTime"]);
if (opts.initialized !== undefined) {
// Initialized operation times are greater than a null timestamp.
if (opts.initialized) {
assert.eq(bsonWoCompare(res.operationTime, Timestamp(0, 0)), 1);
} else {
assert.eq(bsonWoCompare(res.operationTime, Timestamp(0, 0)), 0);
}
}
}
function supportsMajorityReadConcern() {
const rst = new ReplSetTest({nodes: 1, nodeOptions: {enableMajorityReadConcern: ""}});
if (!startSetIfSupportsReadMajority(rst)) {
rst.stopSet();
return false;
}
rst.stopSet();
return true;
}