0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 17:10:48 +01:00
mongodb/jstests/noPassthrough/indexbg2.js

102 lines
3.3 KiB
JavaScript
Raw Normal View History

// Test background index creation w/ constraints
(function() {
"use strict";
load("jstests/noPassthrough/libs/index_build.js");
const conn = MongoRunner.runMongod({smallfiles: "", nojournal: ""});
assert.neq(null, conn, "mongod failed to start.");
var db = conn.getDB("test");
var baseName = "jstests_index12";
var parallel = function() {
return db[baseName + "_parallelStatus"];
};
var resetParallel = function() {
parallel().drop();
};
// Return the PID to call `waitpid` on for clean shutdown.
var doParallel = function(work) {
resetParallel();
return startMongoProgramNoConnect(
"mongo",
"--eval",
work + "; db." + baseName + "_parallelStatus.save( {done:1} );",
db.getMongo().host);
};
var doneParallel = function() {
return !!parallel().findOne();
};
var waitParallel = function() {
assert.soon(function() {
return doneParallel();
}, "parallel did not finish in time", 300000, 1000);
};
2010-02-12 14:46:53 +01:00
var doTest = function() {
"use strict";
var size = 10000;
var bgIndexBuildPid;
while (1) { // if indexing finishes before we can run checks, try indexing w/ more data
print("size: " + size);
var fullName = "db." + baseName;
var t = db[baseName];
t.drop();
2010-02-12 14:46:53 +01:00
for (var i = 0; i < size; ++i) {
db.jstests_index12.save({i: i});
}
assert.eq(size, t.count());
2010-02-12 14:46:53 +01:00
bgIndexBuildPid =
doParallel(fullName + ".ensureIndex( {i:1}, {background:true, unique:true} )");
try {
// wait for indexing to start
assert.soon(function() {
return getIndexBuildOpId(db) != -1;
}, "no index created", 30000, 50);
assert.writeError(t.save({i: 0, n: true})); // duplicate key violation
assert.writeOK(t.save({i: size - 1, n: true}));
} catch (e) {
// only a failure if we're still indexing
// wait for parallel status to update to reflect indexing status
sleep(1000);
if (!doneParallel()) {
waitProgram(bgIndexBuildPid);
throw e;
}
}
2010-02-12 14:46:53 +01:00
if (!doneParallel()) {
// Ensure the shell has exited cleanly. Otherwise the test harness may send a
// SIGTERM which can lead to a false test failure.
waitProgram(bgIndexBuildPid);
break;
2010-02-12 14:46:53 +01:00
}
print("indexing finished too soon, retrying...");
// Although the index build finished, ensure the shell has exited.
waitProgram(bgIndexBuildPid);
size *= 2;
assert(size < 5000000, "unable to run checks in parallel with index creation");
2010-02-12 14:46:53 +01:00
}
waitParallel();
2010-02-12 14:46:53 +01:00
/* it could be that there is more than size now but the index failed
to build - which is valid. we check index isn't there.
*/
if (t.count() != size) {
assert.eq(1, t.getIndexes().length, "change in # of elems yet index is there");
}
2010-02-12 14:46:53 +01:00
};
doTest();
MongoRunner.stopMongod(conn);
})();