2
// Tests cleanup of orphaned data in hashed sharded coll via the orphaned data cleanup command
5
var options = { separateConfig : true, shardOptions : { verbose : 2 } };
7
var st = new ShardingTest({ shards : 2, mongos : 1, other : options });
11
var admin = mongos.getDB( "admin" );
12
var shards = mongos.getCollection( "config.shards" ).find().toArray();
13
var coll = mongos.getCollection( "foo.bar" );
15
assert( admin.runCommand({ enableSharding : coll.getDB() + "" }).ok );
16
printjson( admin.runCommand({ movePrimary : coll.getDB() + "", to : shards[0]._id }) );
17
assert( admin.runCommand({ shardCollection : coll + "", key : { _id : "hashed" } }).ok );
19
// Create two orphaned data holes, one bounded by min or max on each shard
21
assert( admin.runCommand({ split : coll + "", middle : { _id : NumberLong(-100) } }).ok );
22
assert( admin.runCommand({ split : coll + "", middle : { _id : NumberLong(-50) } }).ok );
23
assert( admin.runCommand({ split : coll + "", middle : { _id : NumberLong(50) } }).ok );
24
assert( admin.runCommand({ split : coll + "", middle : { _id : NumberLong(100) } }).ok );
25
assert( admin.runCommand({ moveChunk : coll + "", bounds : [{ _id : NumberLong(-100) },
26
{ _id : NumberLong(-50) }],
28
_waitForDelete : true }).ok );
29
assert( admin.runCommand({ moveChunk : coll + "", bounds : [{ _id : NumberLong(50) },
30
{ _id : NumberLong(100) }],
32
_waitForDelete : true }).ok );
33
st.printShardingStatus();
35
jsTest.log( "Inserting some docs on each shard, so 1/2 will be orphaned..." );
37
for ( var s = 0; s < 2; s++ ) {
38
var shardColl = ( s == 0 ? st.shard0 : st.shard1 ).getCollection( coll + "" );
39
for ( var i = 0; i < 100; i++ ) shardColl.insert({ _id : i });
40
assert.eq( null, shardColl.getDB().getLastError() );
43
assert.eq( 200, st.shard0.getCollection( coll + "" ).find().itcount() +
44
st.shard1.getCollection( coll + "" ).find().itcount() );
45
assert.eq( 100, coll.find().itcount() );
47
jsTest.log( "Cleaning up orphaned data in hashed coll..." );
49
for ( var s = 0; s < 2; s++ ) {
50
var shardAdmin = ( s == 0 ? st.shard0 : st.shard1 ).getDB( "admin" );
52
var result = shardAdmin.runCommand({ cleanupOrphaned : coll + "" });
53
while ( result.ok && result.stoppedAtKey ) {
55
result = shardAdmin.runCommand({ cleanupOrphaned : coll + "",
56
startingFromKey : result.stoppedAtKey });
63
assert.eq( 100, st.shard0.getCollection( coll + "" ).find().itcount() +
64
st.shard1.getCollection( coll + "" ).find().itcount() );
65
assert.eq( 100, coll.find().itcount() );
67
jsTest.log( "DONE!" );