2012-07-30 21:39:21 +02:00
|
|
|
/**
|
2012-08-06 18:03:20 +02:00
|
|
|
* Part 1: Simple test of TTL. Create a new collection with 24 docs, with timestamps at one hour
|
|
|
|
* intervals, from now-minus-23 hours ago until now. Also add some docs with non-date
|
2013-10-15 19:52:56 +02:00
|
|
|
* values. Then create a TTL index that expires all docs older than a string. Wait 70
|
|
|
|
* seconds (TTL monitor runs every 60) and check that no documents were deleted.
|
|
|
|
* Part 2: Add a second TTL index that expires all docs older than ~5.5 hours (20000
|
|
|
|
* seconds). Wait 70 seconds and check that 18 docs deleted.
|
|
|
|
* Part 3: Add a third TTL index on an identical field. The second index expires docs older than
|
2012-08-06 18:03:20 +02:00
|
|
|
* ~2.8 hours (10000 seconds). Wait 70 seconds and check that 3 more docs deleted.
|
2012-07-30 21:39:21 +02:00
|
|
|
*/
|
2012-05-10 23:33:30 +02:00
|
|
|
|
2013-10-15 19:52:56 +02:00
|
|
|
assertEntryMatches = function(array, regex) {
|
|
|
|
var found = false;
|
|
|
|
for (i=0; i<array.length; i++) {
|
|
|
|
if (regex.test(array[i])) {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(found,
|
|
|
|
"The regex: " + regex + " did not match any entries in the array: " + array.join('\n'));
|
|
|
|
}
|
2012-08-06 18:03:20 +02:00
|
|
|
// Part 1
|
|
|
|
var t = db.ttl1;
|
2012-05-10 23:33:30 +02:00
|
|
|
t.drop();
|
2014-04-04 18:51:32 +02:00
|
|
|
t.runCommand( "create", { flags : 0 } );
|
2012-08-06 18:03:20 +02:00
|
|
|
var now = (new Date()).getTime();
|
|
|
|
|
2013-10-15 19:52:56 +02:00
|
|
|
for (i=0; i<24; i++) {
|
|
|
|
var past = new Date(now - (3600 * 1000 * i));
|
|
|
|
t.insert({x: past, y: past, z: past});
|
2012-08-06 18:03:20 +02:00
|
|
|
}
|
|
|
|
t.insert( { a : 1 } ) //no x value
|
|
|
|
t.insert( { x: null } ) //non-date value
|
|
|
|
t.insert( { x : true } ) //non-date value
|
|
|
|
t.insert( { x : "yo" } ) //non-date value
|
|
|
|
t.insert( { x : 3 } ) //non-date value
|
|
|
|
t.insert( { x : /foo/ } ) //non-date value
|
2012-05-10 23:33:30 +02:00
|
|
|
|
2012-08-06 18:03:20 +02:00
|
|
|
assert.eq( 30 , t.count() );
|
2012-05-10 23:33:30 +02:00
|
|
|
|
2013-10-15 19:52:56 +02:00
|
|
|
t.ensureIndex( { z : 1 } , { expireAfterSeconds : "20000" } );
|
|
|
|
|
|
|
|
sleep(70 * 1000);
|
|
|
|
|
|
|
|
assert.eq(t.count(), 30);
|
|
|
|
|
|
|
|
var loggedWarning = false;
|
|
|
|
var log = db.adminCommand({getLog: "global"}).log;
|
|
|
|
var msg = RegExp("ttl indexes require the expireAfterSeconds" +
|
|
|
|
" field to be numeric but received a type of:");
|
|
|
|
assertEntryMatches(log, msg);
|
2014-04-04 18:51:32 +02:00
|
|
|
|
2013-10-15 19:52:56 +02:00
|
|
|
// Part 2
|
2014-04-04 18:51:32 +02:00
|
|
|
assert.eq( 0, t.stats().userFlags );
|
2012-05-10 23:33:30 +02:00
|
|
|
t.ensureIndex( { x : 1 } , { expireAfterSeconds : 20000 } );
|
2014-04-04 18:51:32 +02:00
|
|
|
assert.eq( 1, t.stats().userFlags );
|
2012-05-10 23:33:30 +02:00
|
|
|
|
2014-04-04 18:51:32 +02:00
|
|
|
assert.soon(
|
2012-05-10 23:33:30 +02:00
|
|
|
function() {
|
2012-08-06 18:03:20 +02:00
|
|
|
return t.count() < 30;
|
|
|
|
}, "TTL index on x didn't delete" , 70 * 1000
|
2012-05-10 23:33:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.eq( 0 , t.find( { x : { $lt : new Date( now - 20000000 ) } } ).count() );
|
2012-08-06 18:03:20 +02:00
|
|
|
assert.eq( 12 , t.count() );
|
2012-07-26 22:11:17 +02:00
|
|
|
|
2012-12-20 04:47:25 +01:00
|
|
|
assert.lte( 18, db.serverStatus().metrics.ttl.deletedDocuments );
|
2012-12-24 15:09:49 +01:00
|
|
|
assert.lte( 1, db.serverStatus().metrics.ttl.passes );
|
2012-12-20 04:47:25 +01:00
|
|
|
|
2013-10-15 19:52:56 +02:00
|
|
|
// Part 3
|
2012-08-06 18:03:20 +02:00
|
|
|
t.ensureIndex( { y : 1 } , { expireAfterSeconds : 10000 } );
|
2012-07-26 22:11:17 +02:00
|
|
|
|
2012-08-06 18:03:20 +02:00
|
|
|
assert.soon(
|
|
|
|
function() {
|
|
|
|
return t.count() < 12;
|
|
|
|
}, "TTL index on y didn't delete" , 70 * 1000
|
|
|
|
);
|
2012-07-26 22:11:17 +02:00
|
|
|
|
2012-08-06 18:03:20 +02:00
|
|
|
assert.eq( 0 , t.find( { y : { $lt : new Date( now - 10000000 ) } } ).count() );
|
2012-12-20 04:47:25 +01:00
|
|
|
assert.eq( 9 , t.count() );
|