Skip to content

Commit

Permalink
read only command support in ClusterSyncConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Mar 11, 2010
1 parent f20775a commit 43ab678
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
7 changes: 5 additions & 2 deletions client/dbclient.h
Expand Up @@ -369,7 +369,6 @@ namespace mongo {
Basically just invocations of connection.$cmd.findOne({...});
*/
class DBClientWithCommands : public DBClientInterface {
bool isOk(const BSONObj&);
set<string> _seenIndexes;
public:

Expand All @@ -391,7 +390,7 @@ namespace mongo {
set.
@return true if the command returned "ok".
*/
bool runCommand(const string &dbname, const BSONObj& cmd, BSONObj &info, int options=0);
virtual bool runCommand(const string &dbname, const BSONObj& cmd, BSONObj &info, int options=0);

/** Authorize access to a particular database.
Authentication is separate for each database on the server -- you may authenticate for any
Expand Down Expand Up @@ -510,6 +509,7 @@ namespace mongo {
ProfileOff = 0,
ProfileSlow = 1, // log very slow (>100ms) operations
ProfileAll = 2

};
bool setDbProfilingLevel(const string &dbname, ProfilingLevel level, BSONObj *info = 0);
bool getDbProfilingLevel(const string &dbname, ProfilingLevel& level, BSONObj *info = 0);
Expand Down Expand Up @@ -664,6 +664,9 @@ namespace mongo {
return ns.substr( pos + 1 );
}

protected:
bool isOk(const BSONObj&);

};

/**
Expand Down
34 changes: 33 additions & 1 deletion client/syncclusterconnection.cpp
Expand Up @@ -122,8 +122,40 @@ namespace mongo {
auto_ptr<DBClientCursor> SyncClusterConnection::query(const string &ns, Query query, int nToReturn, int nToSkip,
const BSONObj *fieldsToReturn, int queryOptions, int batchSize ){

uassert( 10021 , "$cmd not support yet in SyncClusterConnection::query" , ns.find( "$cmd" ) == string::npos );
if ( ns.find( ".$cmd" ) != string::npos ){
string cmdName = query.obj.firstElement().fieldName();

int lockType = 0;

map<string,int>::iterator i = _lockTypes.find( cmdName );
if ( i == _lockTypes.end() ){
BSONObj info;
uassert( 13053 , "help failed" , _commandOnActive( "admin" , BSON( cmdName << "1" << "help" << 1 ) , info ) );
lockType = info["lockType"].numberInt();
_lockTypes[cmdName] = lockType;
}
else {
lockType = i->second;
}

uassert( 13054 , "write $cmd not supported in SyncClusterConnection" , lockType <= 0 );
}

return _queryOnActive( ns , query , nToReturn , nToSkip , fieldsToReturn , queryOptions , batchSize );
}

bool SyncClusterConnection::_commandOnActive(const string &dbname, const BSONObj& cmd, BSONObj &info, int options ){
auto_ptr<DBClientCursor> cursor = _queryOnActive( dbname + ".$cmd" , cmd , 1 , 0 , 0 , options , 0 );
if ( cursor->more() )
info = cursor->next().copy();
else
info = BSONObj();
return isOk( info );
}

auto_ptr<DBClientCursor> SyncClusterConnection::_queryOnActive(const string &ns, Query query, int nToReturn, int nToSkip,
const BSONObj *fieldsToReturn, int queryOptions, int batchSize ){

for ( size_t i=0; i<_conns.size(); i++ ){
try {
auto_ptr<DBClientCursor> cursor =
Expand Down
9 changes: 9 additions & 0 deletions client/syncclusterconnection.h
Expand Up @@ -62,11 +62,20 @@ namespace mongo {

virtual string toString();
private:

bool _commandOnActive(const string &dbname, const BSONObj& cmd, BSONObj &info, int options=0);

auto_ptr<DBClientCursor> _queryOnActive(const string &ns, Query query, int nToReturn, int nToSkip,
const BSONObj *fieldsToReturn, int queryOptions, int batchSize );

bool _isReadOnly( const string& name );

void _checkLast();

void _connect( string host );
vector<DBClientConnection*> _conns;

map<string,int> _lockTypes;
};


Expand Down
6 changes: 5 additions & 1 deletion jstests/sharding/sync1.js
Expand Up @@ -5,8 +5,12 @@ db = test.conn.getDB( "test" )
t = db.sync1
t.save( { x : 1 } )
assert.eq( 1 , t.find().itcount() , "A1" );
assert.eq( 1 , t.find().count() , "A2" );
t.save( { x : 2 } )
assert.eq( 2 , t.find().itcount() , "A2" );
assert.eq( 2 , t.find().itcount() , "A3" );
assert.eq( 2 , t.find().count() , "A4" );

test.checkHashes( "test" , "A3" );


test.stop();
12 changes: 12 additions & 0 deletions shell/servers.js
Expand Up @@ -648,3 +648,15 @@ SyncCCTest.prototype.stop = function(){
stopMongod( 30000 + i );
}
}

SyncCCTest.prototype.checkHashes = function( dbname , msg ){
var hashes = this._connections.map(
function(z){
return z.getDB( dbname ).runCommand( "dbhash" ).md5
}
);

for ( var i=1; i<hashes.length; i++ ){
assert.eq( hashes[0] , hashes[i] , "checkHash on " + dbname + " " + msg )
}
}

0 comments on commit 43ab678

Please sign in to comment.