2017-03-20 10:42:49 -04:00
|
|
|
/**
|
|
|
|
|
* Tests that reads and writes in a replica set return the correct operationTime for their
|
|
|
|
|
* read/write concern level. Majority reads and writes return the last committed optime's timestamp
|
|
|
|
|
* and local reads and writes return the last applied optime's timestamp.
|
2018-09-26 11:26:36 -04:00
|
|
|
* @tags: [requires_majority_read_concern]
|
2017-03-20 10:42:49 -04:00
|
|
|
*/
|
2024-08-20 17:54:15 -04:00
|
|
|
import {ReplSetTest} from "jstests/libs/replsettest.js";
|
|
|
|
|
|
2017-03-20 10:42:49 -04:00
|
|
|
function assertCorrectOperationTime(operationTime, expectedTimestamp, opTimeType) {
|
2025-08-21 10:17:44 -07:00
|
|
|
assert.eq(
|
|
|
|
|
0,
|
|
|
|
|
timestampCmp(operationTime, expectedTimestamp),
|
|
|
|
|
"operationTime in command response, " +
|
|
|
|
|
tojson(operationTime) +
|
|
|
|
|
", does not equal the last " +
|
|
|
|
|
opTimeType +
|
|
|
|
|
" timestamp, " +
|
|
|
|
|
tojson(expectedTimestamp),
|
|
|
|
|
);
|
2019-07-26 18:20:35 -04:00
|
|
|
}
|
|
|
|
|
|
2025-08-28 15:11:44 -04:00
|
|
|
let name = "command_response_operation_time";
|
2019-07-26 18:20:35 -04:00
|
|
|
|
2025-08-28 15:11:44 -04:00
|
|
|
let replTest = new ReplSetTest({name: name, nodes: 3, waitForKeys: true});
|
2020-08-12 11:49:19 -04:00
|
|
|
replTest.startSet();
|
2017-03-20 10:42:49 -04:00
|
|
|
replTest.initiate();
|
2019-07-26 18:20:35 -04:00
|
|
|
|
2025-08-28 15:11:44 -04:00
|
|
|
let res, statusRes;
|
|
|
|
|
let testDB = replTest.getPrimary().getDB(name);
|
2019-07-26 18:20:35 -04:00
|
|
|
|
2017-03-20 10:42:49 -04:00
|
|
|
jsTestLog("Executing majority write.");
|
2025-08-21 10:17:44 -07:00
|
|
|
res = assert.commandWorked(testDB.runCommand({insert: "foo", documents: [{x: 1}], writeConcern: {w: "majority"}}));
|
2017-03-20 10:42:49 -04:00
|
|
|
statusRes = assert.commandWorked(testDB.adminCommand({replSetGetStatus: 1}));
|
2025-08-21 10:17:44 -07:00
|
|
|
assertCorrectOperationTime(res.operationTime, statusRes.optimes.lastCommittedOpTime.ts, "committed");
|
2019-07-26 18:20:35 -04:00
|
|
|
|
2017-03-20 10:42:49 -04:00
|
|
|
jsTestLog("Executing local write.");
|
|
|
|
|
res = assert.commandWorked(testDB.runCommand({insert: "foo", documents: [{x: 2}]}));
|
|
|
|
|
statusRes = assert.commandWorked(testDB.adminCommand({replSetGetStatus: 1}));
|
|
|
|
|
assertCorrectOperationTime(res.operationTime, statusRes.optimes.appliedOpTime.ts, "applied");
|
2019-07-26 18:20:35 -04:00
|
|
|
|
2017-03-20 10:42:49 -04:00
|
|
|
replTest.awaitLastOpCommitted();
|
2019-07-26 18:20:35 -04:00
|
|
|
|
2017-03-20 10:42:49 -04:00
|
|
|
jsTestLog("Executing majority read.");
|
2025-08-21 10:17:44 -07:00
|
|
|
res = assert.commandWorked(testDB.runCommand({find: "foo", filter: {x: 1}, readConcern: {level: "majority"}}));
|
2017-03-20 10:42:49 -04:00
|
|
|
statusRes = assert.commandWorked(testDB.adminCommand({replSetGetStatus: 1}));
|
2025-08-21 10:17:44 -07:00
|
|
|
assertCorrectOperationTime(res.operationTime, statusRes.optimes.lastCommittedOpTime.ts, "committed");
|
2019-07-26 18:20:35 -04:00
|
|
|
|
2017-03-20 10:42:49 -04:00
|
|
|
jsTestLog("Executing local read.");
|
|
|
|
|
res = assert.commandWorked(testDB.runCommand({find: "foo", filter: {x: 1}}));
|
|
|
|
|
statusRes = assert.commandWorked(testDB.adminCommand({replSetGetStatus: 1}));
|
|
|
|
|
assertCorrectOperationTime(res.operationTime, statusRes.optimes.appliedOpTime.ts, "applied");
|
2019-07-26 18:20:35 -04:00
|
|
|
|
2024-06-03 14:47:17 -07:00
|
|
|
replTest.stopSet();
|