Files
mongo/s/request.cpp

170 lines
4.8 KiB
C++
Raw Normal View History

2010-12-27 16:27:32 -05:00
// s/request.cpp
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"
2010-12-27 16:27:32 -05:00
#include "client.h"
2009-02-05 16:45:58 -05:00
2009-01-14 17:09:51 -05:00
namespace mongo {
2011-01-04 00:40:41 -05:00
Request::Request( Message& m, AbstractMessagingPort* p ) :
_m(m) , _d( m ) , _p(p) , _didInit(false) {
2009-02-06 15:44:21 -05:00
assert( _d.getns() );
2010-05-12 15:26:00 -07:00
_id = _m.header()->id;
2011-01-04 00:40:41 -05: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 );
2011-01-04 00:40:41 -05:00
}
2011-01-04 00:40:41 -05:00
void Request::init() {
if ( _didInit )
return;
_didInit = true;
2009-04-07 15:19:27 -04:00
reset();
}
2011-01-04 00:40:41 -05:00
void Request::reset( bool reload ) {
if ( _m.operation() == dbKillCursors ) {
2010-05-28 14:23:37 -04:00
return;
}
2011-01-04 00:40:41 -05:00
2009-02-06 15:44:21 -05:00
_config = grid.getDBConfig( getns() );
if ( reload )
uassert( 10192 , "db config reload failed!" , _config->reload() );
2011-01-04 00:40:41 -05:00
if ( _config->isSharded( getns() ) ) {
_chunkManager = _config->getChunkManager( getns() , reload );
uassert( 10193 , (string)"no shard info for: " + getns() , _chunkManager );
}
else {
_chunkManager.reset();
2011-01-04 00:40:41 -05:00
}
2009-02-06 15:44:21 -05:00
2010-05-12 15:26:00 -07:00
_m.header()->id = _id;
2011-01-04 00:40:41 -05:00
}
2011-01-04 00:40:41 -05:00
Shard Request::primaryShard() const {
assert( _didInit );
2011-01-04 00:40:41 -05:00
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;
}
2011-01-04 00:40:41 -05:00
void Request::process( int attempt ) {
init();
2010-05-12 15:26:00 -07:00
int op = _m.operation();
assert( op > dbMsg );
2011-01-04 00:40:41 -05:00
if ( op == dbKillCursors ) {
2010-05-28 14:23:37 -04:00
cursorCache.gotKillCursors( _m );
return;
}
2011-01-04 00:40:41 -05:00
2010-05-28 14:23:37 -04:00
log(3) << "Request::process ns: " << getns() << " msg id:" << (int)(_m.header()->id) << " attempt: " << attempt << endl;
2011-01-04 00:40:41 -05:00
2009-02-18 13:54:22 -05:00
Strategy * s = SINGLE;
2010-05-05 18:41:59 -04:00
_counter = &opsNonSharded;
2011-01-04 00:40:41 -05:00
_d.markSet();
2011-01-04 00:40:41 -05:00
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 );
}
2011-01-04 00:40:41 -05:00
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 );
}
2011-01-04 00:40:41 -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
2011-01-04 00:40:41 -05:00
void Request::gotInsert() {
2010-05-05 18:41:59 -04:00
globalOpCounters.gotInsert();
_counter->gotInsert();
}
2010-05-28 14:23:37 -04:00
2011-01-04 00:40:41 -05: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();
2011-01-04 00:40:41 -05:00
if ( cursor ) {
if ( fromServer.size() ) {
cursorCache.storeRef( fromServer , cursor );
}
else {
// probably a getMore
// make sure we have a ref for this
assert( cursorCache.getRef( cursor ).size() );
}
2010-05-28 14:23:37 -04:00
}
_p->reply( _m , response , _id );
}
2011-01-04 00:40:41 -05:00
2009-01-14 17:09:51 -05:00
} // namespace mongo