ttl是2.2版本引入的一个新特性,但是在今天的测试过程中,发现秒级别的删除没有正确删除。执行如下:
> use test1
switched to db test1
> db.ttl.ensureIndex({"Date":1},{expireAfterSeconds:10})
> db.ttl.insert({"Date":new Date});
> var startDate = new Date();
> while(true) {
... var count = db.ttl.count();
... print("Docs: "+ count + "("+ (new Date() - startDate) + " ms)");
... if(count == 0)
... break;
... sleep(4000);
... }
Docs: 1(545 ms)
Docs: 1(4545 ms)
Docs: 1(8546 ms)
Docs: 1(12548 ms)
Docs: 1(16549 ms)
Docs: 1(20551 ms)
Docs: 1(24552 ms)
Docs: 1(28553 ms)
Docs: 1(32555 ms)
Docs: 1(36556 ms)
Docs: 1(40557 ms)
Docs: 1(44574 ms)
Docs: 1(48576 ms)
Docs: 1(52577 ms)
Docs: 1(56578 ms)
Docs: 1(60580 ms)
Docs: 0(64591 ms)
源码中找到原因是有60s的sleep。
virtual void run() {
Client::initThread( name().c_str() );
while ( ! inShutdown() ) {
sleepsecs( 60 );
LOG(3) << "TTLMonitor thread awake" << endl;
if ( lockedForWriting() ) {
// note: this is not perfect as you can go into fsync+lock between
// this and actually doing the delete later
LOG(3) << " locked for writing" << endl;
continue;
}