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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "stdafx.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"
|
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"
|
2009-02-05 16:45:58 -05:00
|
|
|
|
2009-01-14 17:09:51 -05:00
|
|
|
namespace mongo {
|
|
|
|
|
|
2009-03-01 23:43:00 -05:00
|
|
|
Request::Request( Message& m, AbstractMessagingPort* p ) : _m(m) , _d( m ) , _p(p){
|
2009-02-06 15:44:21 -05:00
|
|
|
assert( _d.getns() );
|
|
|
|
|
_id = _m.data->id;
|
2009-04-07 15:19:27 -04:00
|
|
|
|
|
|
|
|
reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Request::reset( bool reload ){
|
2009-02-06 15:44:21 -05:00
|
|
|
_config = grid.getDBConfig( getns() );
|
2009-02-19 13:26:25 -05:00
|
|
|
|
2009-09-01 12:17:41 -04:00
|
|
|
if ( _config->isSharded( getns() ) ){
|
2009-09-01 16:30:20 -04:00
|
|
|
_chunkManager = _config->getChunkManager( getns() , reload );
|
|
|
|
|
uassert( (string)"no shard info for: " + getns() , _chunkManager );
|
2009-02-19 13:26:25 -05:00
|
|
|
}
|
|
|
|
|
else {
|
2009-09-01 16:30:20 -04:00
|
|
|
_chunkManager = 0;
|
2009-04-07 15:19:27 -04:00
|
|
|
}
|
2009-02-06 15:44:21 -05:00
|
|
|
|
2009-04-12 22:19:41 -04:00
|
|
|
_m.data->id = _id;
|
2009-04-18 21:55:34 -04:00
|
|
|
|
2009-04-12 22:19:41 -04:00
|
|
|
}
|
|
|
|
|
|
2009-02-19 13:26:25 -05:00
|
|
|
string Request::singleServerName(){
|
2009-09-01 16:30:20 -04:00
|
|
|
if ( _chunkManager ){
|
|
|
|
|
if ( _chunkManager->numChunks() > 1 )
|
2009-02-19 13:26:25 -05:00
|
|
|
throw UserException( "can't call singleServerName on a sharded collection" );
|
2009-09-01 16:30:20 -04:00
|
|
|
return _chunkManager->findChunk( _chunkManager->getShardKey().globalMin() ).getShard();
|
2009-02-19 13:26:25 -05:00
|
|
|
}
|
2009-09-01 11:09:43 -04:00
|
|
|
string s = _config->getShard( getns() );
|
2009-02-19 13:26:25 -05:00
|
|
|
uassert( "can't call singleServerName on a sharded collection!" , s.size() > 0 );
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-07 15:19:27 -04:00
|
|
|
void Request::process( int attempt ){
|
2009-04-12 22:19:41 -04:00
|
|
|
|
|
|
|
|
log(2) << "Request::process ns: " << getns() << " msg id:" << (int)(_m.data->id) << " attempt: " << attempt << endl;
|
|
|
|
|
|
2009-02-19 12:55:01 -05:00
|
|
|
int op = _m.data->operation();
|
2009-01-15 10:17:11 -05:00
|
|
|
assert( op > dbMsg );
|
2009-02-05 16:45:58 -05:00
|
|
|
|
2009-02-18 13:54:22 -05:00
|
|
|
Strategy * s = SINGLE;
|
|
|
|
|
|
2009-04-12 22:19:41 -04:00
|
|
|
_d.markSet();
|
2009-02-19 13:26:25 -05:00
|
|
|
|
2009-09-01 16:30:20 -04:00
|
|
|
if ( _chunkManager ){
|
|
|
|
|
s = SHARDED;
|
2009-02-19 17:32:19 -05:00
|
|
|
}
|
2009-04-12 22:19:41 -04:00
|
|
|
|
2009-01-15 10:17:11 -05:00
|
|
|
if ( op == dbQuery ) {
|
2009-04-07 15:19:27 -04:00
|
|
|
try {
|
|
|
|
|
s->queryOp( *this );
|
|
|
|
|
}
|
|
|
|
|
catch ( StaleConfigException& staleConfig ){
|
2009-04-12 22:19:41 -04:00
|
|
|
log() << staleConfig.what() << " attempt: " << attempt << endl;
|
2009-04-07 15:19:27 -04:00
|
|
|
uassert( "too many attempts to update config, failing" , attempt < 5 );
|
2009-04-12 22:19:41 -04:00
|
|
|
|
2009-04-15 10:29:17 -04:00
|
|
|
sleepsecs( attempt );
|
2009-04-07 15:19:27 -04:00
|
|
|
reset( true );
|
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
|
|
|
}
|
2008-10-13 15:55:38 -04:00
|
|
|
}
|
2009-02-05 16:45:58 -05:00
|
|
|
|
2009-01-14 17:09:51 -05:00
|
|
|
} // namespace mongo
|