0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

SERVER-1280 Allow balancer to be turned off

This commit is contained in:
Alberto Lerner 2010-07-06 19:58:37 -04:00
parent b03810c020
commit 4717034108
2 changed files with 90 additions and 3 deletions

View File

@ -0,0 +1,71 @@
// sharding_balance3.js
s = new ShardingTest( "slow_sharding_balance3" , 2 , 2 , 1 , { chunksize : 1 } );
s.adminCommand( { enablesharding : "test" } );
s.adminCommand( { shardcollection : "test.foo" , key : { _id : 1 } } );
assert.eq( 1 , s.config.chunks.count() , "setup1" );
s.config.settings.find().forEach( printjson );
db = s.getDB( "test" );
bigString = ""
while ( bigString.length < 10000 )
bigString += "asdasdasdasdadasdasdasdasdasdasdasdasda";
inserted = 0;
num = 0;
while ( inserted < ( 20 * 1024 * 1024 ) ){
db.foo.insert( { _id : num++ , s : bigString } );
inserted += bigString.length;
}
db.getLastError();
assert.lt( 20 , s.config.chunks.count() , "setup2" );
function dist(){
var x = {}
s.config.chunks.find( { ns : "test.foo" } ).forEach(
function(z){
if ( x[z.shard] )
x[z.shard]++
else
x[z.shard] = 1;
}
);
return x;
}
function diff(){
var x = dist();
printjson( x )
return Math.max( x.shard0 , x.shard1 ) - Math.min( x.shard0 , x.shard1 );
}
assert.lt( 20 , diff() );
// Wait for balancer to kick in.
var initialDiff = diff();
var maxRetries = 3;
while ( diff() == initialDiff ){
sleep( 5000 );
assert.lt( 0, maxRetries--, "Balancer did not kick in.");
}
print("* A");
print( "disabling the balancer" );
s.config.settings.update( { _id : "balancer" }, { $set : { stopped : true } } );
s.config.settings.find().forEach( printjson );
print("* B");
print( diff() )
var currDiff = diff();
assert.repeat( function(){
var d = diff();
return d != currDiff;
} , "balance with stopped flag should not have happened" , 1000 * 30 , 5000 );
s.stop()

View File

@ -42,8 +42,17 @@ namespace mongo {
log(2) << "balancer: " << x << endl;
if ( ! x.isEmpty() ){
if ( x["who"].String() == _myid ){
log(2) << "balancer: i'm the current balancer" << endl;
// If need be, we can stop the balancer by creating a 'stopped : true' field in its
// entry.
if ( x["stopped"].type() && x["stopped"].Bool() ){
log() << "balancer: stopped flag true" << endl;
return false;
}
return true;
}
@ -70,11 +79,18 @@ namespace mongo {
BSONObjBuilder updateQuery;
updateQuery.append( "_id" , "balancer" );
if ( x["x"].type() )
if ( x["x"].type() ){
updateQuery.append( x["x"] );
else
// Carry on the stopped flag, if it existed.
if ( ! x["stopped"].type() ){
updateQuery.append( "stopped" , x["stopped"].Bool() );
}
} else {
updateQuery.append( "x" , BSON( "$exists" << false ) );
}
conn.update( ShardNS::settings ,
updateQuery.obj() ,
BSON( "$set" << BSON( "who" << _myid << "x" << incarnation ) ) ,