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"
|
2010-04-20 12:58:04 -04:00
|
|
|
|
2008-12-05 16:03:35 -05:00
|
|
|
#include "../db/commands.h"
|
2008-09-29 18:00:53 -04:00
|
|
|
#include "../db/dbmessage.h"
|
2010-04-20 12:58:04 -04:00
|
|
|
#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
|
|
|
|
2009-02-06 14:11:31 -05:00
|
|
|
#include "request.h"
|
2009-02-12 21:03:46 -05:00
|
|
|
#include "config.h"
|
2009-08-31 16:31:50 -04:00
|
|
|
#include "chunk.h"
|
2010-04-20 15:05:48 -04:00
|
|
|
#include "stats.h"
|
2010-05-28 14:23:37 -04:00
|
|
|
#include "cursors.h"
|
2010-07-28 14:24:55 -04:00
|
|
|
#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
|
|
|
|
2010-07-22 15:39:20 -04:00
|
|
|
}
|
2011-01-04 00:40:41 -05:00
|
|
|
|
|
|
|
|
void Request::init() {
|
2010-07-22 15:39:20 -04:00
|
|
|
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() );
|
2009-11-24 17:28:57 -05:00
|
|
|
if ( reload )
|
2009-12-28 16:43:43 -05:00
|
|
|
uassert( 10192 , "db config reload failed!" , _config->reload() );
|
2009-02-19 13:26:25 -05:00
|
|
|
|
2011-01-04 00:40:41 -05:00
|
|
|
if ( _config->isSharded( getns() ) ) {
|
2009-09-01 16:30:20 -04:00
|
|
|
_chunkManager = _config->getChunkManager( getns() , reload );
|
2009-12-28 16:43:43 -05:00
|
|
|
uassert( 10193 , (string)"no shard info for: " + getns() , _chunkManager );
|
2009-02-19 13:26:25 -05:00
|
|
|
}
|
|
|
|
|
else {
|
2010-05-26 12:40:46 -04:00
|
|
|
_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
|
|
|
|
2009-04-12 22:19:41 -04:00
|
|
|
}
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2010-04-27 12:32:59 -04:00
|
|
|
Shard Request::primaryShard() const {
|
2010-07-22 15:39:20 -04:00
|
|
|
assert( _didInit );
|
2011-01-04 00:40:41 -05:00
|
|
|
|
|
|
|
|
if ( _chunkManager ) {
|
2009-09-01 16:30:20 -04:00
|
|
|
if ( _chunkManager->numChunks() > 1 )
|
2010-04-27 12:32:59 -04:00
|
|
|
throw UserException( 8060 , "can't call primaryShard on a sharded collection" );
|
2010-05-24 17:24:41 -04:00
|
|
|
return _chunkManager->findChunk( _chunkManager->getShardKey().globalMin() )->getShard();
|
2009-02-19 13:26:25 -05:00
|
|
|
}
|
2010-04-27 12:32:59 -04:00
|
|
|
Shard s = _config->getShard( getns() );
|
|
|
|
|
uassert( 10194 , "can't call primaryShard on a sharded collection!" , s.ok() );
|
2009-02-19 13:26:25 -05:00
|
|
|
return s;
|
|
|
|
|
}
|
2011-01-04 00:40:41 -05:00
|
|
|
|
|
|
|
|
void Request::process( int attempt ) {
|
2010-07-22 15:39:20 -04:00
|
|
|
init();
|
2010-05-12 15:26:00 -07:00
|
|
|
int op = _m.operation();
|
2009-01-15 10:17:11 -05:00
|
|
|
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
|
|
|
|
2009-04-12 22:19:41 -04:00
|
|
|
_d.markSet();
|
2011-01-04 00:40:41 -05:00
|
|
|
|
|
|
|
|
if ( _chunkManager ) {
|
2009-09-01 16:30:20 -04:00
|
|
|
s = SHARDED;
|
2010-05-05 18:41:59 -04:00
|
|
|
_counter = &opsSharded;
|
2009-02-19 17:32:19 -05:00
|
|
|
}
|
2010-04-28 15:39:52 -04:00
|
|
|
|
2010-04-20 12:58:04 -04:00
|
|
|
bool iscmd = false;
|
2009-01-15 10:17:11 -05:00
|
|
|
if ( op == dbQuery ) {
|
2010-04-20 12:58:04 -04:00
|
|
|
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 ) {
|
2009-04-12 22:19:41 -04:00
|
|
|
log() << staleConfig.what() << " attempt: " << attempt << endl;
|
2009-12-28 16:43:43 -05:00
|
|
|
uassert( 10195 , "too many attempts to update config, failing" , attempt < 5 );
|
2010-08-03 18:12:04 -04:00
|
|
|
ShardConnection::checkMyConnectionVersions( getns() );
|
2010-08-04 13:00:41 -04:00
|
|
|
if (!staleConfig.justConnection() )
|
|
|
|
|
sleepsecs( attempt );
|
2010-07-22 12:45:58 -04:00
|
|
|
reset( ! staleConfig.justConnection() );
|
2009-04-12 22:19:41 -04:00
|
|
|
_d.markReset();
|
2009-04-07 15:19:27 -04:00
|
|
|
process( attempt + 1 );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2009-01-15 10:17:11 -05:00
|
|
|
}
|
|
|
|
|
else if ( op == dbGetMore ) {
|
2009-02-19 12:55:01 -05:00
|
|
|
s->getMore( *this );
|
2009-01-15 10:17:11 -05:00
|
|
|
}
|
|
|
|
|
else {
|
2009-02-19 12:55:01 -05:00
|
|
|
s->writeOp( op, *this );
|
2009-01-15 10:17:11 -05:00
|
|
|
}
|
2010-04-20 12:58:04 -04:00
|
|
|
|
|
|
|
|
globalOpCounters.gotOp( op , iscmd );
|
2010-05-05 18:41:59 -04:00
|
|
|
_counter->gotOp( op , iscmd );
|
2008-10-13 15:55:38 -04:00
|
|
|
}
|
2011-01-04 00:40:41 -05:00
|
|
|
|
2010-04-20 12:58:04 -04: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 ) {
|
2010-07-22 15:39:20 -04:00
|
|
|
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 ) {
|
2011-02-05 17:52:07 -05:00
|
|
|
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
|