Files
mongo/s/request.cpp

256 lines
7.2 KiB
C++
Raw Normal View History

2008-11-09 17:49:37 -05:00
/* dbgrid/request.cpp
Top level handling of requests (operations such as query, insert, ...)
*/
2008-09-15 09:14:42 -04:00
/**
* Copyright (C) 2008 10gen Inc.
2008-12-28 20:28:49 -05:00
*
2008-09-15 09:14:42 -04:00
* 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.
2008-12-28 20:28:49 -05:00
*
2008-09-15 09:14:42 -04:00
* 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.
2008-12-28 20:28:49 -05:00
*
2008-09-15 09:14:42 -04:00
* 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/>.
*/
2010-04-27 15:27:52 -04:00
#include "pch.h"
2009-02-05 11:50:27 -05:00
#include "server.h"
2008-12-05 16:03:35 -05:00
#include "../db/commands.h"
2008-09-29 18:00:53 -04:00
#include "../db/dbmessage.h"
#include "../db/stats/counters.h"
2008-10-31 19:17:54 -05:00
#include "../client/connpool.h"
2008-10-08 18:20:02 -04:00
#include "request.h"
2009-02-12 21:03:46 -05:00
#include "config.h"
#include "chunk.h"
#include "stats.h"
2010-05-28 14:23:37 -04:00
#include "cursors.h"
#include "grid.h"
2009-02-05 16:45:58 -05:00
2009-01-14 17:09:51 -05:00
namespace mongo {
2009-09-14 11:33:42 -04:00
Request::Request( Message& m, AbstractMessagingPort* p ) :
_m(m) , _d( m ) , _p(p) , _didInit(false){
2009-09-14 11:33:42 -04:00
2009-02-06 15:44:21 -05:00
assert( _d.getns() );
2010-05-12 15:26:00 -07:00
_id = _m.header()->id;
2009-04-07 15:19:27 -04:00
2010-05-28 17:07:18 -04:00
_clientId = p ? p->getClientId() : 0;
2009-09-14 11:33:42 -04:00
_clientInfo = ClientInfo::get( _clientId );
2010-04-27 16:49:22 -04:00
_clientInfo->newRequest( p );
2009-09-14 11:33:42 -04:00
}
void Request::init(){
if ( _didInit )
return;
_didInit = true;
2009-04-07 15:19:27 -04:00
reset();
}
2009-04-07 15:19:27 -04:00
void Request::reset( bool reload ){
2010-05-28 14:23:37 -04:00
if ( _m.operation() == dbKillCursors ){
return;
}
2009-02-06 15:44:21 -05:00
_config = grid.getDBConfig( getns() );
if ( reload )
uassert( 10192 , "db config reload failed!" , _config->reload() );
2009-09-01 12:17:41 -04:00
if ( _config->isSharded( getns() ) ){
_chunkManager = _config->getChunkManager( getns() , reload );
uassert( 10193 , (string)"no shard info for: " + getns() , _chunkManager );
}
else {
_chunkManager.reset();
2009-04-07 15:19:27 -04:00
}
2009-02-06 15:44:21 -05:00
2010-05-12 15:26:00 -07:00
_m.header()->id = _id;
}
Shard Request::primaryShard() const {
assert( _didInit );
if ( _chunkManager ){
if ( _chunkManager->numChunks() > 1 )
throw UserException( 8060 , "can't call primaryShard on a sharded collection" );
return _chunkManager->findChunk( _chunkManager->getShardKey().globalMin() )->getShard();
}
Shard s = _config->getShard( getns() );
uassert( 10194 , "can't call primaryShard on a sharded collection!" , s.ok() );
return s;
}
2009-04-07 15:19:27 -04:00
void Request::process( int attempt ){
init();
2010-05-12 15:26:00 -07:00
int op = _m.operation();
assert( op > dbMsg );
2009-02-05 16:45:58 -05:00
2010-05-28 14:23:37 -04:00
if ( op == dbKillCursors ){
cursorCache.gotKillCursors( _m );
return;
}
log(3) << "Request::process ns: " << getns() << " msg id:" << (int)(_m.header()->id) << " attempt: " << attempt << endl;
2009-02-18 13:54:22 -05:00
Strategy * s = SINGLE;
2010-05-05 18:41:59 -04:00
_counter = &opsNonSharded;
2009-02-18 13:54:22 -05:00
_d.markSet();
if ( _chunkManager ){
s = SHARDED;
2010-05-05 18:41:59 -04:00
_counter = &opsSharded;
2009-02-19 17:32:19 -05:00
}
bool iscmd = false;
if ( op == dbQuery ) {
iscmd = isCommand();
2009-04-07 15:19:27 -04:00
try {
s->queryOp( *this );
}
catch ( StaleConfigException& staleConfig ){
log() << staleConfig.what() << " attempt: " << attempt << endl;
uassert( 10195 , "too many attempts to update config, failing" , attempt < 5 );
ShardConnection::checkMyConnectionVersions( getns() );
2010-08-04 13:00:41 -04:00
if (!staleConfig.justConnection() )
sleepsecs( attempt );
reset( ! staleConfig.justConnection() );
_d.markReset();
2009-04-07 15:19:27 -04:00
process( attempt + 1 );
return;
}
}
else if ( op == dbGetMore ) {
2009-02-19 12:55:01 -05:00
s->getMore( *this );
}
else {
2009-02-19 12:55:01 -05:00
s->writeOp( op, *this );
}
globalOpCounters.gotOp( op , iscmd );
2010-05-05 18:41:59 -04:00
_counter->gotOp( op , iscmd );
}
2009-02-05 16:45:58 -05:00
bool Request::isCommand() const {
int x = _d.getQueryNToReturn();
return ( x == 1 || x == -1 ) && strstr( getns() , ".$cmd" );
}
2010-05-05 18:41:59 -04:00
void Request::gotInsert(){
globalOpCounters.gotInsert();
_counter->gotInsert();
}
2010-05-28 14:23:37 -04:00
void Request::reply( Message & response , const string& fromServer ){
assert( _didInit );
2010-05-28 14:23:37 -04:00
long long cursor =response.header()->getCursor();
if ( cursor ){
cursorCache.storeRef( fromServer , cursor );
}
_p->reply( _m , response , _id );
}
2009-09-14 11:33:42 -04:00
ClientInfo::ClientInfo( int clientId ) : _id( clientId ){
_cur = &_a;
_prev = &_b;
newRequest();
}
ClientInfo::~ClientInfo(){
2010-05-28 17:07:18 -04:00
if ( _lastAccess ){
scoped_lock lk( _clientsLock );
ClientCache::iterator i = _clients.find( _id );
if ( i != _clients.end() ){
_clients.erase( i );
}
2009-09-14 11:33:42 -04:00
}
}
void ClientInfo::addShard( const string& shard ){
_cur->insert( shard );
_sinceLastGetError.insert( shard );
2009-09-14 11:33:42 -04:00
}
2010-04-27 16:49:22 -04:00
void ClientInfo::newRequest( AbstractMessagingPort* p ){
if ( p ){
string r = p->remote().toString();
if ( _remote == "" )
_remote = r;
else if ( _remote != r ){
stringstream ss;
ss << "remotes don't match old [" << _remote << "] new [" << r << "]";
throw UserException( 13134 , ss.str() );
}
}
2009-09-24 14:21:40 -04:00
_lastAccess = (int) time(0);
2009-09-14 11:33:42 -04:00
set<string> * temp = _cur;
_cur = _prev;
_prev = temp;
_cur->clear();
}
void ClientInfo::disconnect(){
_lastAccess = 0;
}
ClientInfo * ClientInfo::get( int clientId , bool create ){
if ( ! clientId )
clientId = getClientId();
if ( ! clientId ){
ClientInfo * info = _tlInfo.get();
if ( ! info ){
info = new ClientInfo( 0 );
_tlInfo.reset( info );
}
info->newRequest();
return info;
}
scoped_lock lk( _clientsLock );
2009-09-14 11:33:42 -04:00
ClientCache::iterator i = _clients.find( clientId );
if ( i != _clients.end() )
return i->second;
if ( ! create )
return 0;
ClientInfo * info = new ClientInfo( clientId );
_clients[clientId] = info;
return info;
}
2010-05-28 17:07:18 -04:00
void ClientInfo::disconnect( int clientId ){
if ( ! clientId )
return;
scoped_lock lk( _clientsLock );
ClientCache::iterator i = _clients.find( clientId );
if ( i == _clients.end() )
return;
ClientInfo* ci = i->second;
ci->disconnect();
delete ci;
_clients.erase( i );
}
2010-05-26 21:47:02 -04:00
ClientCache& ClientInfo::_clients = *(new ClientCache());
2010-05-26 00:46:49 -04:00
mongo::mutex ClientInfo::_clientsLock("_clientsLock");
boost::thread_specific_ptr<ClientInfo> ClientInfo::_tlInfo;
2009-09-14 11:33:42 -04:00
2009-01-14 17:09:51 -05:00
} // namespace mongo