2010-02-09 16:48:21 -05:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2010 10gen Inc.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-02-20 10:46:42 -05:00
|
|
|
// stragegy.cpp
|
|
|
|
|
|
2010-04-27 15:27:52 -04:00
|
|
|
#include "pch.h"
|
2009-02-20 10:46:42 -05:00
|
|
|
#include "request.h"
|
2009-04-17 17:11:32 -04:00
|
|
|
#include "../util/background.h"
|
2009-02-20 10:46:42 -05:00
|
|
|
#include "../client/connpool.h"
|
|
|
|
|
#include "../db/commands.h"
|
2009-04-17 17:11:32 -04:00
|
|
|
#include "server.h"
|
2009-02-20 10:46:42 -05:00
|
|
|
|
|
|
|
|
namespace mongo {
|
|
|
|
|
|
2009-02-23 21:47:25 -05:00
|
|
|
// ----- Strategy ------
|
|
|
|
|
|
2010-04-27 12:32:59 -04:00
|
|
|
void Strategy::doWrite( int op , Request& r , const Shard& shard ){
|
|
|
|
|
ShardConnection dbcon( shard );
|
2009-02-20 10:46:42 -05:00
|
|
|
DBClientBase &_c = dbcon.conn();
|
|
|
|
|
|
|
|
|
|
/* TODO FIX - do not case and call DBClientBase::say() */
|
|
|
|
|
DBClientConnection&c = dynamic_cast<DBClientConnection&>(_c);
|
|
|
|
|
c.port().say( r.m() );
|
|
|
|
|
|
|
|
|
|
dbcon.done();
|
|
|
|
|
}
|
2009-02-21 23:39:41 -05:00
|
|
|
|
2010-04-27 12:32:59 -04:00
|
|
|
void Strategy::doQuery( Request& r , const Shard& shard ){
|
2009-02-21 23:39:41 -05:00
|
|
|
try{
|
2010-04-27 12:32:59 -04:00
|
|
|
ShardConnection dbcon( shard );
|
2010-03-22 11:47:37 -04:00
|
|
|
DBClientBase &c = dbcon.conn();
|
2009-02-21 23:39:41 -05:00
|
|
|
|
2010-03-22 11:47:37 -04:00
|
|
|
checkShardVersion( c , r.getns() );
|
2009-04-07 15:19:27 -04:00
|
|
|
|
2009-02-21 23:39:41 -05:00
|
|
|
Message response;
|
2010-03-22 11:47:37 -04:00
|
|
|
bool ok = c.call( r.m(), response);
|
2009-04-07 15:19:27 -04:00
|
|
|
|
|
|
|
|
{
|
2010-05-12 15:26:00 -07:00
|
|
|
QueryResult *qr = (QueryResult *) response.singleData();
|
2009-04-07 15:19:27 -04:00
|
|
|
if ( qr->resultFlags() & QueryResult::ResultFlag_ShardConfigStale ){
|
2009-09-14 14:32:24 -04:00
|
|
|
dbcon.done();
|
2009-04-12 22:19:41 -04:00
|
|
|
throw StaleConfigException( r.getns() , "Strategy::doQuery" );
|
2009-04-07 15:19:27 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-28 16:43:43 -05:00
|
|
|
uassert( 10200 , "mongos: error calling db", ok);
|
2009-02-21 23:39:41 -05:00
|
|
|
r.reply( response );
|
|
|
|
|
dbcon.done();
|
|
|
|
|
}
|
|
|
|
|
catch ( AssertionException& e ) {
|
|
|
|
|
BSONObjBuilder err;
|
|
|
|
|
err.append("$err", string("mongos: ") + (e.msg.empty() ? "assertion during query" : e.msg));
|
2010-04-27 12:32:59 -04:00
|
|
|
err.append("code",e.getCode());
|
2009-02-21 23:39:41 -05:00
|
|
|
BSONObj errObj = err.done();
|
|
|
|
|
replyToQuery(QueryResult::ResultFlag_ErrSet, r.p() , r.m() , errObj);
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-02-20 13:46:57 -05:00
|
|
|
|
2010-04-27 12:32:59 -04:00
|
|
|
void Strategy::insert( const Shard& shard , const char * ns , const BSONObj& obj ){
|
|
|
|
|
ShardConnection dbcon( shard );
|
2009-04-18 21:55:34 -04:00
|
|
|
checkShardVersion( dbcon.conn() , ns );
|
2009-02-20 10:46:42 -05:00
|
|
|
dbcon->insert( ns , obj );
|
2009-02-20 13:46:57 -05:00
|
|
|
dbcon.done();
|
2009-02-20 10:46:42 -05:00
|
|
|
}
|
2009-02-23 21:47:25 -05:00
|
|
|
|
2010-04-28 13:44:31 -04:00
|
|
|
map< pair<DBClientBase*,string> ,unsigned long long> checkShardVersionLastSequence;
|
2009-03-30 10:50:10 -04:00
|
|
|
|
2009-04-17 17:11:32 -04:00
|
|
|
class WriteBackListener : public BackgroundJob {
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
WriteBackListener( const string& addr ) : _addr( addr ){
|
|
|
|
|
cout << "creating WriteBackListener for: " << addr << endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void run(){
|
|
|
|
|
int secsToSleep = 0;
|
|
|
|
|
while ( 1 ){
|
|
|
|
|
try {
|
2010-04-27 12:32:59 -04:00
|
|
|
ScopedDbConnection conn( _addr );
|
2009-04-17 17:11:32 -04:00
|
|
|
|
|
|
|
|
BSONObj result;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
BSONObjBuilder cmd;
|
|
|
|
|
cmd.appendOID( "writebacklisten" , &serverID );
|
|
|
|
|
if ( ! conn->runCommand( "admin" , cmd.obj() , result ) ){
|
|
|
|
|
log() << "writebacklisten command failed! " << result << endl;
|
2009-09-14 14:32:24 -04:00
|
|
|
conn.done();
|
2009-04-17 17:11:32 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-18 21:55:34 -04:00
|
|
|
log(1) << "writebacklisten result: " << result << endl;
|
|
|
|
|
|
|
|
|
|
BSONObj data = result.getObjectField( "data" );
|
|
|
|
|
if ( data.getBoolField( "writeBack" ) ){
|
|
|
|
|
string ns = data["ns"].valuestrsafe();
|
|
|
|
|
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
|
|
Message m( (void*)data["msg"].binData( len ) , false );
|
2010-05-12 15:26:00 -07:00
|
|
|
massert( 10427 , "invalid writeback message" , m.header()->valid() );
|
2009-04-18 21:55:34 -04:00
|
|
|
|
2009-08-31 16:31:50 -04:00
|
|
|
grid.getDBConfig( ns )->getChunkManager( ns , true );
|
2009-04-18 21:55:34 -04:00
|
|
|
|
|
|
|
|
Request r( m , 0 );
|
|
|
|
|
r.process();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
log() << "unknown writeBack result: " << result << endl;
|
|
|
|
|
}
|
2009-04-17 17:11:32 -04:00
|
|
|
|
|
|
|
|
conn.done();
|
|
|
|
|
secsToSleep = 0;
|
|
|
|
|
}
|
|
|
|
|
catch ( std::exception e ){
|
|
|
|
|
log() << "WriteBackListener exception : " << e.what() << endl;
|
|
|
|
|
}
|
|
|
|
|
catch ( ... ){
|
|
|
|
|
log() << "WriteBackListener uncaught exception!" << endl;
|
|
|
|
|
}
|
|
|
|
|
secsToSleep++;
|
2009-04-20 09:16:47 -04:00
|
|
|
sleepsecs(secsToSleep);
|
2009-04-17 17:11:32 -04:00
|
|
|
if ( secsToSleep > 10 )
|
|
|
|
|
secsToSleep = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
string _addr;
|
|
|
|
|
|
2010-05-11 12:06:09 -04:00
|
|
|
static map<string,WriteBackListener*> _cache;
|
|
|
|
|
static mongo::mutex _lock;
|
|
|
|
|
|
2009-04-17 17:11:32 -04:00
|
|
|
public:
|
|
|
|
|
static void init( DBClientBase& conn ){
|
2010-05-11 12:06:09 -04:00
|
|
|
scoped_lock lk( _lock );
|
2009-04-17 17:11:32 -04:00
|
|
|
WriteBackListener*& l = _cache[conn.getServerAddress()];
|
|
|
|
|
if ( l )
|
|
|
|
|
return;
|
|
|
|
|
l = new WriteBackListener( conn.getServerAddress() );
|
|
|
|
|
l->go();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
map<string,WriteBackListener*> WriteBackListener::_cache;
|
2010-05-11 12:06:09 -04:00
|
|
|
mongo::mutex WriteBackListener::_lock;
|
2009-04-17 17:11:32 -04:00
|
|
|
|
2009-03-27 16:55:26 -04:00
|
|
|
void checkShardVersion( DBClientBase& conn , const string& ns , bool authoritative ){
|
2009-03-25 17:35:38 -04:00
|
|
|
// TODO: cache, optimize, etc...
|
|
|
|
|
|
2009-04-17 17:11:32 -04:00
|
|
|
WriteBackListener::init( conn );
|
|
|
|
|
|
2009-03-25 17:35:38 -04:00
|
|
|
DBConfig * conf = grid.getDBConfig( ns );
|
|
|
|
|
if ( ! conf )
|
|
|
|
|
return;
|
|
|
|
|
|
2009-11-24 17:28:57 -05:00
|
|
|
ShardChunkVersion version = 0;
|
|
|
|
|
unsigned long long officialSequenceNumber = 0;
|
|
|
|
|
|
2010-04-28 10:08:28 -04:00
|
|
|
ChunkManager * manager = 0;
|
2009-11-24 17:28:57 -05:00
|
|
|
if ( conf->isSharded( ns ) ){
|
2010-04-28 10:08:28 -04:00
|
|
|
manager = conf->getChunkManager( ns , authoritative );
|
2009-11-24 17:28:57 -05:00
|
|
|
officialSequenceNumber = manager->getSequenceNumber();
|
2010-04-27 12:32:59 -04:00
|
|
|
version = manager->getVersion( Shard::make( conn.getServerAddress() ) );
|
2009-11-24 17:28:57 -05:00
|
|
|
}
|
2009-03-30 10:50:10 -04:00
|
|
|
|
2010-04-28 13:44:31 -04:00
|
|
|
unsigned long long & sequenceNumber = checkShardVersionLastSequence[ make_pair(&conn,ns) ];
|
2010-04-27 17:22:17 -04:00
|
|
|
if ( sequenceNumber == officialSequenceNumber )
|
2009-03-30 10:50:10 -04:00
|
|
|
return;
|
2009-04-10 10:41:35 -04:00
|
|
|
|
2010-04-28 10:08:28 -04:00
|
|
|
log(2) << " have to set shard version for conn: " << &conn << " ns:" << ns
|
|
|
|
|
<< " my last seq: " << sequenceNumber << " current: " << officialSequenceNumber
|
|
|
|
|
<< " version: " << version << " manager: " << manager
|
|
|
|
|
<< endl;
|
2010-04-28 22:13:38 -04:00
|
|
|
|
2009-03-27 16:55:26 -04:00
|
|
|
BSONObj result;
|
|
|
|
|
if ( setShardVersion( conn , ns , version , authoritative , result ) ){
|
2009-03-30 10:50:10 -04:00
|
|
|
// success!
|
2009-11-24 17:28:57 -05:00
|
|
|
log(1) << " setShardVersion success!" << endl;
|
|
|
|
|
sequenceNumber = officialSequenceNumber;
|
2010-04-28 23:59:20 -04:00
|
|
|
dassert( sequenceNumber == checkShardVersionLastSequence[ make_pair(&conn,ns) ] );
|
2009-03-27 16:55:26 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log(1) << " setShardVersion failed!\n" << result << endl;
|
|
|
|
|
|
|
|
|
|
if ( result.getBoolField( "need_authoritative" ) )
|
2009-12-28 16:43:43 -05:00
|
|
|
massert( 10428 , "need_authoritative set but in authoritative mode already" , ! authoritative );
|
2009-03-27 16:55:26 -04:00
|
|
|
|
|
|
|
|
if ( ! authoritative ){
|
|
|
|
|
checkShardVersion( conn , ns , 1 );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-27 15:50:33 -04:00
|
|
|
log() << " setShardVersion failed: " << result << endl;
|
|
|
|
|
massert( 10429 , (string)"setShardVersion failed! " + result.jsonString() , 0 );
|
2009-03-25 17:35:38 -04:00
|
|
|
}
|
2009-03-27 16:55:26 -04:00
|
|
|
|
2009-08-31 16:31:50 -04:00
|
|
|
bool setShardVersion( DBClientBase & conn , const string& ns , ShardChunkVersion version , bool authoritative , BSONObj& result ){
|
2009-03-25 17:35:38 -04:00
|
|
|
|
2009-03-27 16:55:26 -04:00
|
|
|
BSONObjBuilder cmdBuilder;
|
|
|
|
|
cmdBuilder.append( "setShardVersion" , ns.c_str() );
|
|
|
|
|
cmdBuilder.append( "configdb" , configServer.modelServer() );
|
|
|
|
|
cmdBuilder.appendTimestamp( "version" , version );
|
2009-04-17 17:11:32 -04:00
|
|
|
cmdBuilder.appendOID( "serverID" , &serverID );
|
2009-03-27 16:55:26 -04:00
|
|
|
if ( authoritative )
|
|
|
|
|
cmdBuilder.appendBool( "authoritative" , 1 );
|
|
|
|
|
BSONObj cmd = cmdBuilder.obj();
|
|
|
|
|
|
2009-03-30 09:48:15 -04:00
|
|
|
log(1) << " setShardVersion " << conn.getServerAddress() << " " << ns << " " << cmd << " " << &conn << endl;
|
2009-03-27 16:55:26 -04:00
|
|
|
|
|
|
|
|
return conn.runCommand( "admin" , cmd , result );
|
|
|
|
|
}
|
2009-04-03 14:21:00 -04:00
|
|
|
|
2010-04-27 12:32:59 -04:00
|
|
|
bool lockNamespaceOnServer( const Shard& shard, const string& ns ){
|
|
|
|
|
ShardConnection conn( shard );
|
2009-04-03 14:21:00 -04:00
|
|
|
bool res = lockNamespaceOnServer( conn.conn() , ns );
|
|
|
|
|
conn.done();
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool lockNamespaceOnServer( DBClientBase& conn , const string& ns ){
|
|
|
|
|
BSONObj lockResult;
|
|
|
|
|
return setShardVersion( conn , ns , grid.getNextOpTime() , true , lockResult );
|
|
|
|
|
}
|
2009-04-17 17:11:32 -04:00
|
|
|
|
|
|
|
|
|
2009-02-20 10:46:42 -05:00
|
|
|
}
|