MongoDB2.2 的 Time To Live (TTL) 集合一些注意事项

来源:互联网 发布:dota2 娜迦海妖 知乎 编辑:程序博客网 时间:2024/06/05 06:24


ttl是2.2版本引入的一个新特性,但是在今天的测试过程中,发现秒级别的删除没有正确删除。执行如下:

> use test1switched 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;                }



原创粉丝点击