diff options
author | Mathias Stearn <mathias@10gen.com> | 2011-05-31 17:31:24 -0400 |
---|---|---|
committer | Mathias Stearn <mathias@10gen.com> | 2011-05-31 17:49:19 -0400 |
commit | c4dff971d6ba6bf69b40637168286aaeaaa227f1 (patch) | |
tree | 0b5ed89ee86783bd218f7e221269de3a5f2a3cac | |
parent | 8a2bdb812ce335cdefb6d1688b68ff8e7968502f (diff) | |
download | mongo-c4dff971d6ba6bf69b40637168286aaeaaa227f1.tar.gz |
Don't use separate threads for Future::spawnCommand SERVER-1790
-rw-r--r-- | client/parallel.cpp | 68 | ||||
-rw-r--r-- | client/parallel.h | 3 |
2 files changed, 33 insertions, 38 deletions
diff --git a/client/parallel.cpp b/client/parallel.cpp index 5e0860df263..604ff228607 100644 --- a/client/parallel.cpp +++ b/client/parallel.cpp @@ -550,58 +550,54 @@ namespace mongo { // ---- Future ----- // ----------------- - Future::CommandResult::CommandResult( const string& server , const string& db , const BSONObj& cmd , DBClientBase * conn ) { - _server = server; - _db = db; - _cmd = cmd; - _conn = conn; - _done = false; + Future::CommandResult::CommandResult( const string& server , const string& db , const BSONObj& cmd , DBClientBase * conn ) + :_server(server) ,_db(db) ,_cmd(cmd) ,_conn(conn) ,_done(false) + { + try { + if ( ! _conn ){ + _connHolder.reset( new ScopedDbConnection( _server ) ); + _conn = _connHolder->get(); + } + + _cursor.reset( new DBClientCursor(_conn, _db + ".$cmd", _cmd, -1/*limit*/, 0, NULL, 0, 0)); + _cursor->initLazy(); + } + catch ( std::exception& e ) { + error() << "Future::spawnComand (part 1) exception: " << e.what() << endl; + _ok = false; + _done = true; + } } bool Future::CommandResult::join() { - _thr->join(); - assert( _done ); - return _ok; - } - - void Future::commandThread(shared_ptr<CommandResult> res) { - setThreadName( "future" ); + if (_done) + return _ok; try { - DBClientBase * conn = res->_conn; - - scoped_ptr<ScopedDbConnection> myconn; - if ( ! conn ){ - myconn.reset( new ScopedDbConnection( res->_server ) ); - conn = myconn->get(); - } - - res->_cursor.reset( new DBClientCursor(conn, res->_db + ".$cmd", res->_cmd, -1/*limit*/, 0, NULL, 0, 0)); - res->_cursor->initLazy(); - //TODO: return here and resume later + bool finished = _cursor->initLazyFinish(); - bool finished = res->_cursor->initLazyFinish(); - uassert(14812, str::stream() << "Error running command on server: " << res->_server, finished); - massert(14813, "Command returned nothing", res->_cursor->more()); + // Shouldn't need to communicate with server any more + if ( _connHolder ) + _connHolder->done(); - res->_res = res->_cursor->nextSafe(); - res->_ok = res->_res["ok"].trueValue(); + uassert(14812, str::stream() << "Error running command on server: " << _server, finished); + massert(14813, "Command returned nothing", _cursor->more()); - if ( myconn ) - myconn->done(); + _res = _cursor->nextSafe(); + _ok = _res["ok"].trueValue(); } catch ( std::exception& e ) { - error() << "Future::commandThread exception: " << e.what() << endl; - res->_ok = false; + error() << "Future::spawnComand (part 2) exception: " << e.what() << endl; + _ok = false; } - res->_done = true; + + _done = true; + return _ok; } shared_ptr<Future::CommandResult> Future::spawnCommand( const string& server , const string& db , const BSONObj& cmd , DBClientBase * conn ) { shared_ptr<Future::CommandResult> res (new Future::CommandResult( server , db , cmd , conn )); - res->_thr.reset( new boost::thread( boost::bind(Future::commandThread, res) ) ); - return res; } diff --git a/client/parallel.h b/client/parallel.h index 4a6aa2a6283..332840edea1 100644 --- a/client/parallel.h +++ b/client/parallel.h @@ -286,8 +286,8 @@ namespace mongo { string _db; BSONObj _cmd; DBClientBase * _conn; + scoped_ptr<ScopedDbConnection> _connHolder; // used if not provided a connection - scoped_ptr<boost::thread> _thr; scoped_ptr<DBClientCursor> _cursor; BSONObj _res; @@ -297,7 +297,6 @@ namespace mongo { friend class Future; }; - static void commandThread(shared_ptr<CommandResult> res); /** * @param server server name |