Mongo因插入太多而陷入困境

时间:2013-04-03 09:10:11

标签: mongodb nosql

我尝试使用mongodb来运行多代理模拟。

我在运行模拟程序的同一台服务器上有一个mongo实例,但是当我有太多的代理程序(在10个模拟步骤中约为100.000)时,mongodb会在几秒钟内停止运行。

mongo中插入数据的代码类似于:

if( mongo_client( &m_conn , m_dbhost.c_str(), m_dbport ) != MONGO_OK ) {
    cout << "failed to connect '" << m_dbhost << ":" << m_dbport << "'\n";
    cout << "  mongo error: " << m_conn.err << endl;
    return;
}
bson_init( &b );
bson_append_new_oid( &b, "_id" ) != BSON_OK );
bson_append_double( &b, "time", time );
bson_append_double( &b, "x", posx );
bson_append_double( &b, "y", posy );
bson_finish( &b );

if( mongo_insert( &m_conn , ns.c_str() , &b, NULL ) != MONGO_OK ){
    cout << "failed to insert in mongo\n";
}
bson_destroy( &b );
mongo_disconnect( &m_conn );

此外,在模拟过程中,如果我尝试使用mongo-shell访问,我也会收到错误:

$ mongo
MongoDB shell version: 2.4.1
connecting to: test
Wed Apr  3 10:10:24.870 JavaScript execution failed: Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112
exception: connect failed

模拟结束后,mongo shell再次响应,我可以检查数据库中是否有数据,但它已经停止。在该示例中,代理程序m0n999仅保存了10个步骤中的6个步骤:

> show dbs
dB0B7F527F0FA45518712C8CB27611BD7   5.951171875GB
local   0.078125GB
> db.ins.m0n999.find()
{ "_id" : ObjectId("515bdf564c60ec1e000003e7"), "time" : 1, "x" : 1.1, "y" : 8.1 }
{ "_id" : ObjectId("515be0214c60ec1e0001075f"), "time" : 2, "x" : 1.2000000000000002, "y" : 8.2 }
{ "_id" : ObjectId("515be1c04c60ec1e0002da3a"), "time" : 4, "x" : 1.4000000000000004, "y" : 8.399999999999999 }
{ "_id" : ObjectId("515be2934c60ec1e0003b82c"), "time" : 5, "x" : 1.5000000000000004, "y" : 8.499999999999998 }
{ "_id" : ObjectId("515be3664c60ec1e000497cf"), "time" : 6, "x" : 1.6000000000000005, "y" : 8.599999999999998 }
{ "_id" : ObjectId("515be6cc4c60ec1e000824b2"), "time" : 10, "x" : 2.000000000000001, "y" : 8.999999999999996 }
> 

怎样才能解决这个问题?如何避免连接丢失并从mongo摊位中恢复?

更新 我遇到了全局日志错误,如:

    "Wed Apr  3 11:53:00.379 [conn1378573] error: hashtable namespace index max chain reached:1335",
    "Wed Apr  3 11:53:00.379 [conn1378573] error: hashtable namespace index max chain reached:1335",
    "Wed Apr  3 11:53:00.379 [conn1378573] error: hashtable namespace index max chain reached:1335",
    "Wed Apr  3 11:53:00.379 [conn1378573] error: hashtable namespace index max chain reached:1335",
    "Wed Apr  3 11:53:00.379 [conn1378573] end connection 127.0.0.1:40748 (1 connection now open)",

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,它有两个错误:

  • 我创造了太多的收藏品。我将每个代理的一个集合更改为每个模拟过程的集合。

  • 我创建了太多连接。我从每个代理迭代的一个连接更改为每个模拟步骤只有一个连接。