File: hash_shard1.js

package info (click to toggle)
mongodb 1%3A2.4.10-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,464 kB
  • sloc: cpp: 740,225; ansic: 152,098; sh: 13,820; python: 11,864; makefile: 1,012; perl: 922; pascal: 617; java: 452; lisp: 222; asm: 174
file content (61 lines) | stat: -rw-r--r-- 2,272 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Basic test of sharding with a hashed shard key
//  - Test basic migrations with moveChunk, using different chunk specification methods

var s = new ShardingTest( { name : jsTestName() , shards : 3 , mongos : 1, verbose : 1 } );
var dbname = "test";
var coll = "foo";
var ns = dbname + "." + coll;
var db = s.getDB( dbname );
var t = db.getCollection( coll );
db.adminCommand( { enablesharding : dbname } );

// for simplicity start by turning off balancer
s.stopBalancer();

// shard a fresh collection using a hashed shard key
t.drop();
var res = db.adminCommand( { shardcollection : ns , key : { a : "hashed" } } );
assert.eq( res.ok , 1 , "shardcollection didn't work" );
db.printShardingStatus();

// insert stuff
var numitems = 1000;
for(i = 0; i < numitems; i++ ){
    t.insert( { a: i } )
}
// check they all got inserted
assert.eq( t.find().count() , numitems , "count off after inserts" );
printjson( t.find().explain() );

// find a chunk that's not on shard0000
var chunk = s.config.chunks.findOne( {shard : {$ne  : "shard0000"} } );
printjson(chunk);

// try to move the chunk using an invalid specification method. should fail.
var res = db.adminCommand( { movechunk : ns ,
                             find : { a : 0 } ,
                             bounds : [ chunk.min , chunk.max ] ,
                             to:  "shard0000"  } );
assert.eq( res.ok , 0 , "moveChunk shouldn't work with invalid specification method")

// now move a chunk using the lower/upper bound method. should work.
var res = db.adminCommand( { movechunk : ns ,
                             bounds : [ chunk.min , chunk.max ] ,
                             to:  "shard0000"  } );
printjson( res );
assert.eq( res.ok , 1 , "movechunk using lower/upper bound method didn't work " );

// check count still correct.
assert.eq( t.find().itcount() , numitems , "count off after migrate" );
printjson( t.find().explain() );

// move a chunk using the find method
var res = db.adminCommand( { movechunk : ns , find : { a : 2 } , to:  "shard0002"  } );
printjson( res );
assert.eq( res.ok , 1 , "movechunk using find query didn't work" );

// check counts still correct
assert.eq( t.find().itcount() , numitems , "count off after migrate" );
printjson( t.find().explain() );

s.stop()