2010-10-01 12:45:36 -04:00
|
|
|
// @file strategy.cpp
|
|
|
|
|
|
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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-04-27 15:27:52 -04:00
|
|
|
#include "pch.h"
|
2010-10-01 12:45:36 -04:00
|
|
|
|
2009-02-20 10:46:42 -05:00
|
|
|
#include "../client/connpool.h"
|
|
|
|
|
#include "../db/commands.h"
|
2010-07-28 14:24:55 -04:00
|
|
|
|
|
|
|
|
#include "grid.h"
|
2010-10-01 12:45:36 -04:00
|
|
|
#include "request.h"
|
|
|
|
|
#include "server.h"
|
|
|
|
|
#include "writeback_listener.h"
|
|
|
|
|
|
|
|
|
|
#include "strategy.h"
|
2009-02-20 10:46:42 -05:00
|
|
|
|
|
|
|
|
namespace mongo {
|
|
|
|
|
|
2009-02-23 21:47:25 -05:00
|
|
|
// ----- Strategy ------
|
|
|
|
|
|
2010-07-22 23:05:02 -04:00
|
|
|
void Strategy::doWrite( int op , Request& r , const Shard& shard , bool checkVersion ){
|
2010-07-21 14:12:32 -04:00
|
|
|
ShardConnection conn( shard , r.getns() );
|
2010-07-22 23:05:02 -04:00
|
|
|
if ( ! checkVersion )
|
|
|
|
|
conn.donotCheckVersion();
|
2010-07-23 00:47:24 -04:00
|
|
|
else if ( conn.setVersion() ){
|
|
|
|
|
conn.done();
|
|
|
|
|
throw StaleConfigException( r.getns() , "doWRite" , true );
|
|
|
|
|
}
|
2010-07-21 14:12:32 -04:00
|
|
|
conn->say( r.m() );
|
|
|
|
|
conn.done();
|
2009-02-20 10:46:42 -05:00
|
|
|
}
|
2010-07-23 00:47:24 -04: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-05-20 13:36:29 -04:00
|
|
|
ShardConnection dbcon( shard , r.getns() );
|
2010-03-22 11:47:37 -04:00
|
|
|
DBClientBase &c = dbcon.conn();
|
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();
|
2010-07-18 13:34:16 -04:00
|
|
|
if ( qr->resultFlags() & 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);
|
2010-05-28 14:23:37 -04:00
|
|
|
r.reply( response , c.getServerAddress() );
|
2009-02-21 23:39:41 -05:00
|
|
|
dbcon.done();
|
|
|
|
|
}
|
|
|
|
|
catch ( AssertionException& e ) {
|
|
|
|
|
BSONObjBuilder err;
|
2010-06-21 13:41:34 -04:00
|
|
|
e.getInfo().append( err );
|
2009-02-21 23:39:41 -05:00
|
|
|
BSONObj errObj = err.done();
|
2010-07-18 13:34:16 -04:00
|
|
|
replyToQuery(ResultFlag_ErrSet, r.p() , r.m() , errObj);
|
2009-02-21 23:39:41 -05:00
|
|
|
}
|
|
|
|
|
}
|
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 ){
|
2010-05-20 13:36:29 -04:00
|
|
|
ShardConnection dbcon( shard , ns );
|
2010-07-02 00:51:41 -04:00
|
|
|
if ( dbcon.setVersion() ){
|
|
|
|
|
dbcon.done();
|
2010-07-01 17:44:13 -04:00
|
|
|
throw StaleConfigException( ns , "for insert" );
|
2010-07-02 00:51:41 -04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
}
|