2010-02-10 00:23:42 +01:00
|
|
|
// Test background index creation w/ constraints
|
|
|
|
|
2017-06-07 16:03:57 +02:00
|
|
|
(function() {
|
|
|
|
"use strict";
|
2017-11-28 00:19:40 +01:00
|
|
|
|
|
|
|
load("jstests/noPassthrough/libs/index_build.js");
|
|
|
|
|
2017-06-07 16:03:57 +02:00
|
|
|
const conn = MongoRunner.runMongod({smallfiles: "", nojournal: ""});
|
|
|
|
assert.neq(null, conn, "mongod failed to start.");
|
|
|
|
var db = conn.getDB("test");
|
|
|
|
var baseName = "jstests_index12";
|
2012-04-09 05:39:11 +02:00
|
|
|
|
2017-06-07 16:03:57 +02:00
|
|
|
var parallel = function() {
|
|
|
|
return db[baseName + "_parallelStatus"];
|
|
|
|
};
|
2010-02-10 00:23:42 +01:00
|
|
|
|
2017-06-07 16:03:57 +02:00
|
|
|
var resetParallel = function() {
|
|
|
|
parallel().drop();
|
|
|
|
};
|
2010-02-10 00:23:42 +01:00
|
|
|
|
2017-06-07 16:03:57 +02:00
|
|
|
// 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);
|
|
|
|
};
|
2010-02-10 00:23:42 +01:00
|
|
|
|
2017-06-07 16:03:57 +02:00
|
|
|
var doneParallel = function() {
|
|
|
|
return !!parallel().findOne();
|
|
|
|
};
|
2010-02-10 00:23:42 +01:00
|
|
|
|
2017-06-07 16:03:57 +02:00
|
|
|
var waitParallel = function() {
|
|
|
|
assert.soon(function() {
|
|
|
|
return doneParallel();
|
|
|
|
}, "parallel did not finish in time", 300000, 1000);
|
|
|
|
};
|
2010-02-12 14:46:53 +01:00
|
|
|
|
2017-06-07 16:03:57 +02: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
|
|
|
|
2017-06-07 16:03:57 +02: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
|
|
|
|
2017-06-07 16:03:57 +02:00
|
|
|
bgIndexBuildPid =
|
|
|
|
doParallel(fullName + ".ensureIndex( {i:1}, {background:true, unique:true} )");
|
|
|
|
try {
|
|
|
|
// wait for indexing to start
|
|
|
|
assert.soon(function() {
|
2017-11-28 00:19:40 +01:00
|
|
|
return getIndexBuildOpId(db) != -1;
|
2017-06-07 16:03:57 +02:00
|
|
|
}, "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()) {
|
2017-06-07 16:03:57 +02:00
|
|
|
// Ensure the shell has exited cleanly. Otherwise the test harness may send a
|
|
|
|
// SIGTERM which can lead to a false test failure.
|
2016-11-10 16:36:35 +01:00
|
|
|
waitProgram(bgIndexBuildPid);
|
2017-06-07 16:03:57 +02:00
|
|
|
break;
|
2010-02-12 14:46:53 +01:00
|
|
|
}
|
2017-06-07 16:03:57 +02:00
|
|
|
print("indexing finished too soon, retrying...");
|
|
|
|
// Although the index build finished, ensure the shell has exited.
|
2016-11-10 16:36:35 +01:00
|
|
|
waitProgram(bgIndexBuildPid);
|
2017-06-07 16:03:57 +02:00
|
|
|
size *= 2;
|
|
|
|
assert(size < 5000000, "unable to run checks in parallel with index creation");
|
2010-02-12 14:46:53 +01:00
|
|
|
}
|
|
|
|
|
2017-06-07 16:03:57 +02:00
|
|
|
waitParallel();
|
2010-02-12 14:46:53 +01:00
|
|
|
|
2017-06-07 16:03:57 +02: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
|
|
|
|
2017-06-07 16:03:57 +02:00
|
|
|
};
|
2010-02-10 00:23:42 +01:00
|
|
|
|
2017-06-07 16:03:57 +02:00
|
|
|
doTest();
|
2012-04-09 05:39:11 +02:00
|
|
|
|
2017-06-07 16:03:57 +02:00
|
|
|
MongoRunner.stopMongod(conn);
|
|
|
|
})();
|