// sharding_balance4.js // check that doing updates done during a migrate all go to the right place s = new ShardingTest( "slow_sharding_balance4" , 2 , 1 , 1 , { chunksize : 1 } ) s.stopBalancer(); 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"; N = 3000 num = 0; counts = {} // // TODO: Rewrite to make much clearer. // // The core behavior of this test is to add a bunch of documents to a sharded collection, then // incrementally update each document and make sure the counts in the document match our update // counts while balancing occurs (doUpdate()). Every once in a while we also check (check()) // our counts via a query. // // If during a chunk migration an update is missed, we trigger an assertion and fail. // function doUpdate( includeString, optionalId ){ var up = { $inc : { x : 1 } } if ( includeString ) up["$set"] = { s : bigString }; var myid = optionalId == undefined ? Random.randInt( N ) : optionalId db.foo.update( { _id : myid } , up , true ); counts[myid] = ( counts[myid] ? counts[myid] : 0 ) + 1; return myid; } // Initially update all documents from 1 to N, otherwise later checks can fail because no document // previously existed for ( i = 0; i < N; i++ ){ doUpdate( true, i ) } for ( i=0; i .99 ){ db.getLastError() check( "random late check" ); // SERVER-1430 } var x = s.chunkCounts( "foo" ) if ( Math.random() > .999 ) printjson( x ) return Math.max( x.shard0000 , x.shard0001 ) - Math.min( x.shard0000 , x.shard0001 ); } assert.lt( 20 , diff1() ,"initial load" ); print( diff1() ) s.startBalancer(); assert.soon( function(){ var d = diff1(); return d < 5; } , "balance didn't happen" , 1000 * 60 * 3 , 1 ); s.stop();