Skip to content

Commit

Permalink
SERVER-2545 - start of a few shell helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Jun 15, 2012
1 parent a289a48 commit 297b940
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
18 changes: 13 additions & 5 deletions jstests/slowNightly/balance_tags1.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@ s.adminCommand( { shardcollection : "test.foo" , key : { _id : 1 } } );
for ( i=0; i<20; i++ )
s.adminCommand( { split : "test.foo" , middle : { _id : i } } );

db.printShardingStatus( true )

sh.status( true )
assert.soon( function() {
counts = s.chunkCounts( "foo" );
return counts["shard0000"] == 7 &&
counts["shard0001"] == 7 &&
counts["shard0002"] == 7;
} , "balance 1 didn't happen" , 1000 * 60 * 10 , 1000 )

s.config.shards.update( { _id : "shard0000" } , { $push : { tags : "a" } } );
s.config.shards.update( { _id : "shard0001" } , { $push : { tags : "a" } } );
// quick test of some shell helpers and setting up state
sh.addShardTag( "shard0000" , "a" )
assert.eq( [ "a" ] , s.config.shards.findOne( { _id : "shard0000" } ).tags );
sh.addShardTag( "shard0000" , "b" )
assert.eq( [ "a" , "b" ] , s.config.shards.findOne( { _id : "shard0000" } ).tags );
sh.removeShardTag( "shard0000" , "b" )
assert.eq( [ "a" ] , s.config.shards.findOne( { _id : "shard0000" } ).tags );

sh.addShardTag( "shard0001" , "a" )

sh.addTagRange( "test.foo" , { _id : -1 } , { _id : 1000 } , "a" )

s.config.tags.insert( { ns : "test.foo" , min : { _id : -1 } , max : { _id : 1000 } , tag : "a" } )
sh.status( true );

assert.soon( function() {
counts = s.chunkCounts( "foo" );
Expand Down
6 changes: 6 additions & 0 deletions src/mongo/shell/shardingtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,12 @@ printShardingStatus = function( configDB , verbose ){
else {
output( "\t\t\ttoo many chunks to print, use verbose if you want to force print" );
}

configDB.tags.find( { ns : coll._id } ).sort( { min : 1 } ).forEach(
function( tag ) {
output( "\t\t\t tag: " + tag.tag + " " + tojson( tag.min ) + " -->> " + tojson( tag.max ) );
}
)
}
}
)
Expand Down
34 changes: 34 additions & 0 deletions src/mongo/shell/utils_sh.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ sh.help = function() {
print( "\tsh.setBalancerState( <bool on or not> ) turns the balancer on or off true=on, false=off" );
print( "\tsh.getBalancerState() return true if on, off if not" );
print( "\tsh.isBalancerRunning() return true if the balancer is running on any mongos" );

print( "\tsh.addShardTag(shard,tag) adds the tag to the shard" );
print( "\tsh.removeShardTag(shard,tag) removes the tag from the shard" );

print( "\tsh.status() prints a general overview of the cluster" )
}
Expand Down Expand Up @@ -318,3 +321,34 @@ sh._lastMigration = function( ns ){
else return null
}

sh._checkLastError = function( mydb ) {
var err = mydb.getLastError();
if ( err )
throw "error: " + err;
}

sh.addShardTag = function( shard, tag ) {
var config = db.getSisterDB( "config" );
if ( config.shards.findOne( { _id : shard } ) == null ) {
throw "can't find a shard with name: " + shard;
}
config.shards.update( { _id : shard } , { $addToSet : { tags : tag } } );
sh._checkLastError( config );
}

sh.removeShardTag = function( shard, tag ) {
var config = db.getSisterDB( "config" );
if ( config.shards.findOne( { _id : shard } ) == null ) {
throw "can't find a shard with name: " + shard;
}
config.shards.update( { _id : shard } , { $pull : { tags : tag } } );
sh._checkLastError( config );
}

sh.addTagRange = function( ns, min, max, tag ) {
var config = db.getSisterDB( "config" );
config.tags.update( { ns : ns , min : min } ,
{ ns : ns , min : min , max : max , tag : tag } ,
true );
sh._checkLastError( config );
}

0 comments on commit 297b940

Please sign in to comment.