diff options
Diffstat (limited to 'jstests/noPassthrough/indexbg2.js')
-rw-r--r-- | jstests/noPassthrough/indexbg2.js | 260 |
1 files changed, 130 insertions, 130 deletions
diff --git a/jstests/noPassthrough/indexbg2.js b/jstests/noPassthrough/indexbg2.js index a037dc97dd3..c7a119048ec 100644 --- a/jstests/noPassthrough/indexbg2.js +++ b/jstests/noPassthrough/indexbg2.js @@ -2,156 +2,156 @@ // @tags: [SERVER-40561, requires_document_locking] (function() { - "use strict"; +"use strict"; + +load("jstests/libs/check_log.js"); + +const conn = MongoRunner.runMongod({nojournal: ""}); +assert.neq(null, conn, "mongod failed to start."); + +let db = conn.getDB("test"); +let baseName = "jstests_index12"; + +let parallel = function() { + return db[baseName + "_parallelStatus"]; +}; + +let resetParallel = function() { + parallel().drop(); +}; + +// Return the PID to call `waitpid` on for clean shutdown. +let doParallel = function(work) { + resetParallel(); + return startMongoProgramNoConnect( + "mongo", + "--eval", + work + "; db." + baseName + "_parallelStatus.save( {done:1} );", + db.getMongo().host); +}; + +let indexBuild = function() { + let fullName = "db." + baseName; + return doParallel(fullName + ".ensureIndex( {i:1}, {background:true, unique:true} )"); +}; + +let doneParallel = function() { + return !!parallel().findOne(); +}; + +let waitParallel = function() { + assert.soon(function() { + return doneParallel(); + }, "parallel did not finish in time", 300000, 1000); +}; + +let turnFailPointOn = function(failPointName, i) { + assert.commandWorked( + conn.adminCommand({configureFailPoint: failPointName, mode: "alwaysOn", data: {"i": i}})); +}; + +let turnFailPointOff = function(failPointName) { + assert.commandWorked(conn.adminCommand({configureFailPoint: failPointName, mode: "off"})); +}; + +// Unique background index build fails when there exists duplicate indexed values +// for the duration of the build. +let failOnExistingDuplicateValue = function(coll) { + let duplicateKey = 0; + assert.writeOK(coll.save({i: duplicateKey})); + + let bgIndexBuildPid = indexBuild(); + waitProgram(bgIndexBuildPid); + assert.eq(1, coll.getIndexes().length, "Index should fail. There exist duplicate values."); + + // Revert to unique key set + coll.deleteOne({i: duplicateKey}); +}; + +// Unique background index build fails when started with a unique key set, +// but a document with a duplicate key is inserted prior to that key being indexed. +let failOnInsertedDuplicateValue = function(coll) { + let duplicateKey = 7; + + turnFailPointOn("hangBeforeIndexBuildOf", duplicateKey); + + let bgIndexBuildPid; + try { + bgIndexBuildPid = indexBuild(); + jsTestLog("Waiting to hang before index build of i=" + duplicateKey); + checkLog.contains(conn, "Hanging before index build of i=" + duplicateKey); - load("jstests/libs/check_log.js"); - - const conn = MongoRunner.runMongod({nojournal: ""}); - assert.neq(null, conn, "mongod failed to start."); - - let db = conn.getDB("test"); - let baseName = "jstests_index12"; - - let parallel = function() { - return db[baseName + "_parallelStatus"]; - }; - - let resetParallel = function() { - parallel().drop(); - }; - - // Return the PID to call `waitpid` on for clean shutdown. - let doParallel = function(work) { - resetParallel(); - return startMongoProgramNoConnect( - "mongo", - "--eval", - work + "; db." + baseName + "_parallelStatus.save( {done:1} );", - db.getMongo().host); - }; - - let indexBuild = function() { - let fullName = "db." + baseName; - return doParallel(fullName + ".ensureIndex( {i:1}, {background:true, unique:true} )"); - }; - - let doneParallel = function() { - return !!parallel().findOne(); - }; - - let waitParallel = function() { - assert.soon(function() { - return doneParallel(); - }, "parallel did not finish in time", 300000, 1000); - }; - - let turnFailPointOn = function(failPointName, i) { - assert.commandWorked(conn.adminCommand( - {configureFailPoint: failPointName, mode: "alwaysOn", data: {"i": i}})); - }; - - let turnFailPointOff = function(failPointName) { - assert.commandWorked(conn.adminCommand({configureFailPoint: failPointName, mode: "off"})); - }; - - // Unique background index build fails when there exists duplicate indexed values - // for the duration of the build. - let failOnExistingDuplicateValue = function(coll) { - let duplicateKey = 0; assert.writeOK(coll.save({i: duplicateKey})); + } finally { + turnFailPointOff("hangBeforeIndexBuildOf"); + } - let bgIndexBuildPid = indexBuild(); - waitProgram(bgIndexBuildPid); - assert.eq(1, coll.getIndexes().length, "Index should fail. There exist duplicate values."); - - // Revert to unique key set - coll.deleteOne({i: duplicateKey}); - }; - - // Unique background index build fails when started with a unique key set, - // but a document with a duplicate key is inserted prior to that key being indexed. - let failOnInsertedDuplicateValue = function(coll) { - let duplicateKey = 7; - - turnFailPointOn("hangBeforeIndexBuildOf", duplicateKey); + waitProgram(bgIndexBuildPid); + assert.eq(1, + coll.getIndexes().length, + "Index should fail. Duplicate key is inserted prior to that key being indexed."); - let bgIndexBuildPid; - try { - bgIndexBuildPid = indexBuild(); - jsTestLog("Waiting to hang before index build of i=" + duplicateKey); - checkLog.contains(conn, "Hanging before index build of i=" + duplicateKey); + // Revert to unique key set + coll.deleteOne({i: duplicateKey}); +}; - assert.writeOK(coll.save({i: duplicateKey})); - } finally { - turnFailPointOff("hangBeforeIndexBuildOf"); - } +// Unique background index build succeeds: +// 1) when a document is inserted and removed with a key that has already been indexed +// 2) when a document with a key not present in the initial set is inserted and removed +let succeedWithoutWriteErrors = function(coll, newKey) { + let duplicateKey = 3; - waitProgram(bgIndexBuildPid); - assert.eq(1, - coll.getIndexes().length, - "Index should fail. Duplicate key is inserted prior to that key being indexed."); + turnFailPointOn("hangAfterIndexBuildOf", duplicateKey); - // Revert to unique key set - coll.deleteOne({i: duplicateKey}); - }; + let bgIndexBuildPid; + try { + bgIndexBuildPid = indexBuild(); - // Unique background index build succeeds: - // 1) when a document is inserted and removed with a key that has already been indexed - // 2) when a document with a key not present in the initial set is inserted and removed - let succeedWithoutWriteErrors = function(coll, newKey) { - let duplicateKey = 3; + jsTestLog("Waiting to hang after index build of i=" + duplicateKey); + checkLog.contains(conn, "Hanging after index build of i=" + duplicateKey); - turnFailPointOn("hangAfterIndexBuildOf", duplicateKey); + assert.commandWorked(coll.insert({i: duplicateKey, n: true})); - let bgIndexBuildPid; - try { - bgIndexBuildPid = indexBuild(); + // First insert on key not present in initial set. + assert.commandWorked(coll.insert({i: newKey, n: true})); - jsTestLog("Waiting to hang after index build of i=" + duplicateKey); - checkLog.contains(conn, "Hanging after index build of i=" + duplicateKey); + // Remove duplicates before completing the index build. + assert.commandWorked(coll.deleteOne({i: duplicateKey, n: true})); + assert.commandWorked(coll.deleteOne({i: newKey, n: true})); - assert.commandWorked(coll.insert({i: duplicateKey, n: true})); + } finally { + turnFailPointOff("hangAfterIndexBuildOf"); + } - // First insert on key not present in initial set. - assert.commandWorked(coll.insert({i: newKey, n: true})); + waitProgram(bgIndexBuildPid); + assert.eq(2, coll.getIndexes().length, "Index build should succeed"); +}; - // Remove duplicates before completing the index build. - assert.commandWorked(coll.deleteOne({i: duplicateKey, n: true})); - assert.commandWorked(coll.deleteOne({i: newKey, n: true})); - - } finally { - turnFailPointOff("hangAfterIndexBuildOf"); - } - - waitProgram(bgIndexBuildPid); - assert.eq(2, coll.getIndexes().length, "Index build should succeed"); - }; - - let doTest = function() { - "use strict"; - const size = 10; +let doTest = function() { + "use strict"; + const size = 10; - let coll = db[baseName]; - coll.drop(); + let coll = db[baseName]; + coll.drop(); - for (let i = 0; i < size; ++i) { - assert.writeOK(coll.save({i: i})); - } - assert.eq(size, coll.count()); - assert.eq(1, coll.getIndexes().length, "_id index should already exist"); + for (let i = 0; i < size; ++i) { + assert.writeOK(coll.save({i: i})); + } + assert.eq(size, coll.count()); + assert.eq(1, coll.getIndexes().length, "_id index should already exist"); - failOnExistingDuplicateValue(coll); - assert.eq(size, coll.count()); + failOnExistingDuplicateValue(coll); + assert.eq(size, coll.count()); - failOnInsertedDuplicateValue(coll); - assert.eq(size, coll.count()); + failOnInsertedDuplicateValue(coll); + assert.eq(size, coll.count()); - succeedWithoutWriteErrors(coll, size); + succeedWithoutWriteErrors(coll, size); - waitParallel(); - }; + waitParallel(); +}; - doTest(); +doTest(); - MongoRunner.stopMongod(conn); +MongoRunner.stopMongod(conn); })(); |