0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/s/request.cpp

174 lines
4.8 KiB
C++
Raw Normal View History

2008-11-09 23:49:37 +01:00
/* dbgrid/request.cpp
Top level handling of requests (operations such as query, insert, ...)
*/
2008-09-15 15:14:42 +02:00
/**
* Copyright (C) 2008 10gen Inc.
2008-12-29 02:28:49 +01:00
*
2008-09-15 15:14:42 +02: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-29 02:28:49 +01:00
*
2008-09-15 15:14:42 +02: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-29 02:28:49 +01:00
*
2008-09-15 15:14:42 +02: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/>.
*/
#include "stdafx.h"
2009-02-05 17:50:27 +01:00
#include "server.h"
2008-12-05 22:03:35 +01:00
#include "../db/commands.h"
2008-09-30 00:00:53 +02:00
#include "../db/dbmessage.h"
2008-11-01 01:17:54 +01:00
#include "../client/connpool.h"
2008-10-09 00:20:02 +02:00
#include "request.h"
2009-02-13 03:03:46 +01:00
#include "config.h"
#include "chunk.h"
2009-02-05 22:45:58 +01:00
2009-01-14 23:09:51 +01:00
namespace mongo {
2009-09-14 17:33:42 +02:00
Request::Request( Message& m, AbstractMessagingPort* p ) :
_m(m) , _d( m ) , _p(p){
2009-02-06 21:44:21 +01:00
assert( _d.getns() );
_id = _m.data->id;
2009-04-07 21:19:27 +02:00
_clientId = p ? p->remotePort() << 16 : 0;
2009-09-14 17:33:42 +02:00
_clientInfo = ClientInfo::get( _clientId );
_clientInfo->newRequest();
2009-04-07 21:19:27 +02:00
reset();
}
void Request::reset( bool reload ){
2009-02-06 21:44:21 +01:00
_config = grid.getDBConfig( getns() );
2009-09-01 18:17:41 +02:00
if ( _config->isSharded( getns() ) ){
_chunkManager = _config->getChunkManager( getns() , reload );
uassert( (string)"no shard info for: " + getns() , _chunkManager );
}
else {
_chunkManager = 0;
2009-04-07 21:19:27 +02:00
}
2009-02-06 21:44:21 +01:00
_m.data->id = _id;
}
string Request::singleServerName(){
if ( _chunkManager ){
if ( _chunkManager->numChunks() > 1 )
throw UserException( "can't call singleServerName on a sharded collection" );
return _chunkManager->findChunk( _chunkManager->getShardKey().globalMin() ).getShard();
}
string s = _config->getShard( getns() );
uassert( "can't call singleServerName on a sharded collection!" , s.size() > 0 );
return s;
}
2009-04-07 21:19:27 +02:00
void Request::process( int attempt ){
log(2) << "Request::process ns: " << getns() << " msg id:" << (int)(_m.data->id) << " attempt: " << attempt << endl;
2009-02-19 18:55:01 +01:00
int op = _m.data->operation();
assert( op > dbMsg );
2009-02-05 22:45:58 +01:00
2009-02-18 19:54:22 +01:00
Strategy * s = SINGLE;
_d.markSet();
if ( _chunkManager ){
s = SHARDED;
2009-02-19 23:32:19 +01:00
}
if ( op == dbQuery ) {
2009-04-07 21:19:27 +02:00
try {
s->queryOp( *this );
}
catch ( StaleConfigException& staleConfig ){
log() << staleConfig.what() << " attempt: " << attempt << endl;
2009-04-07 21:19:27 +02:00
uassert( "too many attempts to update config, failing" , attempt < 5 );
2009-04-15 16:29:17 +02:00
sleepsecs( attempt );
2009-04-07 21:19:27 +02:00
reset( true );
_d.markReset();
2009-04-07 21:19:27 +02:00
process( attempt + 1 );
return;
}
}
else if ( op == dbGetMore ) {
2009-02-19 18:55:01 +01:00
s->getMore( *this );
}
else {
2009-02-19 18:55:01 +01:00
s->writeOp( op, *this );
}
}
2009-02-05 22:45:58 +01:00
2009-09-14 17:33:42 +02:00
ClientInfo::ClientInfo( int clientId ) : _id( clientId ){
_cur = &_a;
_prev = &_b;
newRequest();
}
ClientInfo::~ClientInfo(){
boostlock lk( _clientsLock );
ClientCache::iterator i = _clients.find( _id );
if ( i != _clients.end() ){
_clients.erase( i );
}
}
void ClientInfo::addShard( const string& shard ){
_cur->insert( shard );
}
void ClientInfo::newRequest(){
2009-09-24 20:21:40 +02:00
_lastAccess = (int) time(0);
2009-09-14 17:33:42 +02: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;
}
boostlock lk( _clientsLock );
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;
}
map<int,ClientInfo*> ClientInfo::_clients;
boost::mutex ClientInfo::_clientsLock;
boost::thread_specific_ptr<ClientInfo> ClientInfo::_tlInfo;
2009-09-14 17:33:42 +02:00
2009-01-14 23:09:51 +01:00
} // namespace mongo