diff options
Diffstat (limited to 'jstests/replsets/speculative_majority_find.js')
-rw-r--r-- | jstests/replsets/speculative_majority_find.js | 280 |
1 files changed, 140 insertions, 140 deletions
diff --git a/jstests/replsets/speculative_majority_find.js b/jstests/replsets/speculative_majority_find.js index fecfbf5dea1..94463145b7e 100644 --- a/jstests/replsets/speculative_majority_find.js +++ b/jstests/replsets/speculative_majority_find.js @@ -10,148 +10,148 @@ * @tags: [uses_speculative_majority] */ (function() { - "use strict"; - - load("jstests/libs/write_concern_util.js"); // for [stop|restart]ServerReplication. - load("jstests/libs/parallelTester.js"); // for ScopedThread. - - let name = "speculative_majority_find"; - let replTest = new ReplSetTest({ - name: name, - nodes: [{}, {rsConfig: {priority: 0}}], - nodeOptions: {enableMajorityReadConcern: 'false'} - }); - replTest.startSet(); - replTest.initiate(); - - let dbName = name; - let collName = "coll"; - - let primary = replTest.getPrimary(); - let secondary = replTest.getSecondary(); - - let primaryDB = primary.getDB(dbName); - let secondaryDB = secondary.getDB(dbName); - let primaryColl = primaryDB[collName]; - // Create a collection. - assert.commandWorked(primaryColl.insert({}, {writeConcern: {w: "majority"}})); - - // - // Test basic reads with speculative majority. - // - - // Pause replication on the secondary so that writes won't majority commit. - stopServerReplication(secondary); - assert.commandWorked(primaryColl.insert({_id: 1})); - - jsTestLog("Do a speculative majority read that should time out."); - let res = primaryDB.runCommand({ +"use strict"; + +load("jstests/libs/write_concern_util.js"); // for [stop|restart]ServerReplication. +load("jstests/libs/parallelTester.js"); // for ScopedThread. + +let name = "speculative_majority_find"; +let replTest = new ReplSetTest({ + name: name, + nodes: [{}, {rsConfig: {priority: 0}}], + nodeOptions: {enableMajorityReadConcern: 'false'} +}); +replTest.startSet(); +replTest.initiate(); + +let dbName = name; +let collName = "coll"; + +let primary = replTest.getPrimary(); +let secondary = replTest.getSecondary(); + +let primaryDB = primary.getDB(dbName); +let secondaryDB = secondary.getDB(dbName); +let primaryColl = primaryDB[collName]; +// Create a collection. +assert.commandWorked(primaryColl.insert({}, {writeConcern: {w: "majority"}})); + +// +// Test basic reads with speculative majority. +// + +// Pause replication on the secondary so that writes won't majority commit. +stopServerReplication(secondary); +assert.commandWorked(primaryColl.insert({_id: 1})); + +jsTestLog("Do a speculative majority read that should time out."); +let res = primaryDB.runCommand({ + find: collName, + readConcern: {level: "majority"}, + filter: {_id: 1}, + allowSpeculativeMajorityRead: true, + maxTimeMS: 5000 +}); +assert.commandFailedWithCode(res, ErrorCodes.MaxTimeMSExpired); + +restartServerReplication(secondary); +replTest.awaitReplication(); + +jsTestLog("Do a speculative majority read that should succeed."); +res = primaryDB.runCommand({ + find: collName, + readConcern: {level: "majority"}, + filter: {_id: 1}, + allowSpeculativeMajorityRead: true +}); +assert.commandWorked(res); +assert.eq(res.cursor.firstBatch.length, 1); +assert.eq(res.cursor.firstBatch[0], {_id: 1}); + +// +// Test that blocked reads can succeed when a write majority commits. +// + +// Pause replication on the secondary so that writes won't majority commit. +stopServerReplication(secondary); +assert.commandWorked(primaryColl.insert({_id: 2})); + +jsTestLog("Do a speculative majority that should block until write commits."); +let speculativeRead = new ScopedThread(function(host, dbName, collName) { + const nodeDB = new Mongo(host).getDB(dbName); + return nodeDB.runCommand({ find: collName, readConcern: {level: "majority"}, - filter: {_id: 1}, - allowSpeculativeMajorityRead: true, - maxTimeMS: 5000 - }); - assert.commandFailedWithCode(res, ErrorCodes.MaxTimeMSExpired); - - restartServerReplication(secondary); - replTest.awaitReplication(); - - jsTestLog("Do a speculative majority read that should succeed."); - res = primaryDB.runCommand({ - find: collName, - readConcern: {level: "majority"}, - filter: {_id: 1}, - allowSpeculativeMajorityRead: true - }); - assert.commandWorked(res); - assert.eq(res.cursor.firstBatch.length, 1); - assert.eq(res.cursor.firstBatch[0], {_id: 1}); - - // - // Test that blocked reads can succeed when a write majority commits. - // - - // Pause replication on the secondary so that writes won't majority commit. - stopServerReplication(secondary); - assert.commandWorked(primaryColl.insert({_id: 2})); - - jsTestLog("Do a speculative majority that should block until write commits."); - let speculativeRead = new ScopedThread(function(host, dbName, collName) { - const nodeDB = new Mongo(host).getDB(dbName); - return nodeDB.runCommand({ - find: collName, - readConcern: {level: "majority"}, - filter: {_id: 2}, - allowSpeculativeMajorityRead: true - }); - }, primary.host, dbName, collName); - speculativeRead.start(); - - // Wait for the read to start on the server. - assert.soon(() => primaryDB.currentOp({ns: primaryColl.getFullName(), "command.find": collName}) - .inprog.length === 1); - - // Let the previous write commit. - restartServerReplication(secondary); - assert.commandWorked( - primaryColl.insert({_id: "commit_last_write"}, {writeConcern: {w: "majority"}})); - - // Make sure the read finished and returned correct results. - speculativeRead.join(); - res = speculativeRead.returnData(); - assert.commandWorked(res); - assert.eq(res.cursor.firstBatch.length, 1); - assert.eq(res.cursor.firstBatch[0], {_id: 2}); - - // - // Test 'afterClusterTime' reads with speculative majority. - // - stopServerReplication(secondary); - - // Insert a document on the primary and record the response. - let writeRes = primaryDB.runCommand({insert: collName, documents: [{_id: 3}]}); - assert.commandWorked(writeRes); - - jsTestLog( - "Do a speculative majority read on primary with 'afterClusterTime' that should time out."); - res = primaryDB.runCommand({ - find: collName, - readConcern: {level: "majority", afterClusterTime: writeRes.operationTime}, - filter: {_id: 3}, - $clusterTime: writeRes.$clusterTime, - allowSpeculativeMajorityRead: true, - maxTimeMS: 5000 - }); - assert.commandFailedWithCode(res, ErrorCodes.MaxTimeMSExpired); - - jsTestLog( - "Do a speculative majority read on secondary with 'afterClusterTime' that should time out."); - res = secondaryDB.runCommand({ - find: collName, - readConcern: {level: "majority", afterClusterTime: writeRes.operationTime}, - filter: {_id: 3}, - $clusterTime: writeRes.$clusterTime, - allowSpeculativeMajorityRead: true, - maxTimeMS: 5000 - }); - assert.commandFailedWithCode(res, ErrorCodes.MaxTimeMSExpired); - - // Let the previous write majority commit. - restartServerReplication(secondary); - replTest.awaitReplication(); - - jsTestLog("Do a speculative majority read with 'afterClusterTime' that should succeed."); - res = primaryDB.runCommand({ - find: collName, - readConcern: {level: "majority", afterClusterTime: writeRes.operationTime}, - filter: {_id: 3}, - $clusterTime: res.$clusterTime, + filter: {_id: 2}, allowSpeculativeMajorityRead: true }); - assert.commandWorked(res); - assert.eq(res.cursor.firstBatch.length, 1); - assert.eq(res.cursor.firstBatch[0], {_id: 3}); - - replTest.stopSet(); +}, primary.host, dbName, collName); +speculativeRead.start(); + +// Wait for the read to start on the server. +assert.soon(() => primaryDB.currentOp({ns: primaryColl.getFullName(), "command.find": collName}) + .inprog.length === 1); + +// Let the previous write commit. +restartServerReplication(secondary); +assert.commandWorked( + primaryColl.insert({_id: "commit_last_write"}, {writeConcern: {w: "majority"}})); + +// Make sure the read finished and returned correct results. +speculativeRead.join(); +res = speculativeRead.returnData(); +assert.commandWorked(res); +assert.eq(res.cursor.firstBatch.length, 1); +assert.eq(res.cursor.firstBatch[0], {_id: 2}); + +// +// Test 'afterClusterTime' reads with speculative majority. +// +stopServerReplication(secondary); + +// Insert a document on the primary and record the response. +let writeRes = primaryDB.runCommand({insert: collName, documents: [{_id: 3}]}); +assert.commandWorked(writeRes); + +jsTestLog( + "Do a speculative majority read on primary with 'afterClusterTime' that should time out."); +res = primaryDB.runCommand({ + find: collName, + readConcern: {level: "majority", afterClusterTime: writeRes.operationTime}, + filter: {_id: 3}, + $clusterTime: writeRes.$clusterTime, + allowSpeculativeMajorityRead: true, + maxTimeMS: 5000 +}); +assert.commandFailedWithCode(res, ErrorCodes.MaxTimeMSExpired); + +jsTestLog( + "Do a speculative majority read on secondary with 'afterClusterTime' that should time out."); +res = secondaryDB.runCommand({ + find: collName, + readConcern: {level: "majority", afterClusterTime: writeRes.operationTime}, + filter: {_id: 3}, + $clusterTime: writeRes.$clusterTime, + allowSpeculativeMajorityRead: true, + maxTimeMS: 5000 +}); +assert.commandFailedWithCode(res, ErrorCodes.MaxTimeMSExpired); + +// Let the previous write majority commit. +restartServerReplication(secondary); +replTest.awaitReplication(); + +jsTestLog("Do a speculative majority read with 'afterClusterTime' that should succeed."); +res = primaryDB.runCommand({ + find: collName, + readConcern: {level: "majority", afterClusterTime: writeRes.operationTime}, + filter: {_id: 3}, + $clusterTime: res.$clusterTime, + allowSpeculativeMajorityRead: true +}); +assert.commandWorked(res); +assert.eq(res.cursor.firstBatch.length, 1); +assert.eq(res.cursor.firstBatch[0], {_id: 3}); + +replTest.stopSet(); })();
\ No newline at end of file |