Files
mongo/s/strategy.cpp

85 lines
2.6 KiB
C++
Raw Normal View History

// @file strategy.cpp
/*
* 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"
2009-02-20 10:46:42 -05:00
#include "../client/connpool.h"
#include "../db/commands.h"
#include "grid.h"
#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 ------
void Strategy::doWrite( int op , Request& r , const Shard& shard , bool checkVersion ){
ShardConnection conn( shard , r.getns() );
if ( ! checkVersion )
conn.donotCheckVersion();
2010-07-23 00:47:24 -04:00
else if ( conn.setVersion() ){
conn.done();
throw StaleConfigException( r.getns() , "doWRite" , true );
}
conn->say( r.m() );
conn.done();
2009-02-20 10:46:42 -05:00
}
2010-07-23 00:47:24 -04:00
void Strategy::doQuery( Request& r , const Shard& shard ){
try{
2010-05-20 13:36:29 -04:00
ShardConnection dbcon( shard , r.getns() );
DBClientBase &c = dbcon.conn();
Message response;
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 ){
dbcon.done();
throw StaleConfigException( r.getns() , "Strategy::doQuery" );
2009-04-07 15:19:27 -04:00
}
}
uassert( 10200 , "mongos: error calling db", ok);
2010-05-28 14:23:37 -04:00
r.reply( response , c.getServerAddress() );
dbcon.done();
}
catch ( AssertionException& e ) {
BSONObjBuilder err;
2010-06-21 13:41:34 -04:00
e.getInfo().append( err );
BSONObj errObj = err.done();
2010-07-18 13:34:16 -04:00
replyToQuery(ResultFlag_ErrSet, r.p() , r.m() , errObj);
}
}
2009-02-20 13:46:57 -05: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 );
if ( dbcon.setVersion() ){
dbcon.done();
throw StaleConfigException( ns , "for insert" );
}
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
}
}