Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

SERVER-211 - simple ttl collections where we can expire based on age

  • Loading branch information...
commit 25bdc679a0e559d64ec7f22b0468cf5b1671c4e7 1 parent 844026a
@erh erh authored
View
22 jstests/slowNightly/ttl1.js
@@ -0,0 +1,22 @@
+
+t = db.ttl1;
+t.drop();
+
+now = (new Date()).getTime();
+
+for ( i=0; i<24; i++ )
+ t.insert( { x : new Date( now - ( 3600 * 1000 * i ) ) } );
+db.getLastError();
+
+assert.eq( 24 , t.count() );
+
+t.ensureIndex( { x : 1 } , { expireAfterSeconds : 20000 } );
+
+assert.soon(
+ function() {
+ return t.count() < 24;
+ }, "never deleted" , 120 * 1000
+);
+
+assert.eq( 0 , t.find( { x : { $lt : new Date( now - 20000000 ) } } ).count() );
+assert.eq( 6 , t.count() );
View
1  src/mongo/SConscript
@@ -196,6 +196,7 @@ serverOnlyFiles = [ "db/curop.cpp",
"db/d_globals.cpp",
"db/pagefault.cpp",
"util/compress.cpp",
+ "db/ttl.cpp",
"db/d_concurrency.cpp",
"db/lockstat.cpp",
"db/lockstate.cpp",
View
4 src/mongo/db/db.cpp
@@ -49,6 +49,7 @@
#include "d_concurrency.h"
#include "../s/d_writeback.h"
#include "d_globals.h"
+#include "ttl.h"
#if defined(_WIN32)
# include "../util/ntservice.h"
@@ -530,7 +531,8 @@ namespace mongo {
snapshotThread.go();
d.clientCursorMonitor.go();
PeriodicTask::theRunner->go();
-
+ startTTLBackgroundJob();
+
#ifndef _WIN32
CmdLine::launchOk();
#endif
View
110 src/mongo/db/ttl.cpp
@@ -0,0 +1,110 @@
+// ttl.cpp
+
+/**
+* Copyright (C) 2008 10gen Inc.
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License, version 3,
+* as published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "pch.h"
+
+#include "mongo/util/background.h"
+#include "mongo/db/instance.h"
+#include "mongo/db/databaseholder.h"
+#include "mongo/db/ops/delete.h"
+
+namespace mongo {
+
+ class TTLMonitor : public BackgroundJob {
+ public:
+ TTLMonitor(){}
+ virtual ~TTLMonitor(){}
+
+ virtual string name() const { return "TTLMonitor"; }
+
+ static string secondsExpireField;
+
+ void doTTLForDB( const string& dbName ) {
+ vector<BSONObj> indexes;
+ {
+ auto_ptr<DBClientCursor> cursor = db.query( dbName + ".system.indexes" ,
+ BSON( secondsExpireField << BSON( "$exists" << true ) ) );
+ if ( cursor.get() ) {
+ while ( cursor->more() ) {
+ indexes.push_back( cursor->next().getOwned() );
+ }
+ }
+ }
+
+ for ( unsigned i=0; i<indexes.size(); i++ ) {
+ BSONObj idx = indexes[i];
+
+
+ BSONObj key = idx["key"].Obj();
+ uassert( 16187 , "key for ttl index can only have 1 field" , key.nFields() == 1 );
+
+ BSONObj query;
+ {
+ BSONObjBuilder b;
+ b.appendDate( "$lt" , curTimeMillis64() - ( 1000 * idx[secondsExpireField].numberLong() ) );
+ query = BSON( key.firstElement().fieldName() << b.obj() );
+ }
+
+ LOG(1) << "TTL: " << key << " \t " << query << endl;
+
+ long long n = 0;
+ {
+ string ns = idx["ns"].String();
+ Client::WriteContext ctx( ns );
+ nsdetails( ns.c_str() )->setUserFlag( NamespaceDetails::Flag_UsePowerOf2Sizes );
+ n = deleteObjects( ns.c_str() , query , false , true );
+ }
+
+ LOG(1) << "\tTTL deleted: " << n << endl;
+ }
+
+
+ }
+
+ virtual void run() {
+ Client::initThread( name().c_str() );
+
+ while ( ! inShutdown() ) {
+ sleepsecs( 60 );
+
+ LOG(3) << "TTLMonitor thread awake" << endl;
+
+ set<string> dbs;
+ {
+ Lock::DBRead lk( "local" );
+ dbHolder().getAllShortNames( dbs );
+ }
+
+ for ( set<string>::const_iterator i=dbs.begin(); i!=dbs.end(); ++i ) {
+ string db = *i;
+ doTTLForDB( db );
+ }
+
+ }
+ }
+
+ DBDirectClient db;
+ };
+
+ void startTTLBackgroundJob() {
+ TTLMonitor* ttl = new TTLMonitor();
+ ttl->go();
+ }
+
+ string TTLMonitor::secondsExpireField = "expireAfterSeconds";
+}
View
23 src/mongo/db/ttl.h
@@ -0,0 +1,23 @@
+// ttl.h
+
+/**
+* Copyright (C) 2008 10gen Inc.
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License, version 3,
+* as published by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#pragma once
+
+namespace mongo {
+ void startTTLBackgroundJob();
+}

7 comments on commit 25bdc67

@sidupadhyay

@erh What do you think of the redis implementation of TTL? http://redis.io/commands/expire

@erh
Owner
@sidupadhyay
@erh
Owner
@sidupadhyay
@erh
Owner
@sidupadhyay
Please sign in to comment.
Something went wrong with that request. Please try again.