summaryrefslogtreecommitdiff
path: root/jstests/sharding/sync_cluster_config/sync2.js
blob: c3d2f4cc0fa44aca373d03d44b63c61fe326955a (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
(function () {

var s = new ShardingTest({ name: "sync2",
                           shards: 3,
                           mongos: 2,
                           other: { sync : true } });

var s2 = s._mongos[1];

s.adminCommand( { enablesharding : "test" } );
s.ensurePrimaryShard('test', 'shard0001');
s.adminCommand( { shardcollection : "test.foo" , key : { num : 1 } } );

s.config.settings.update( { _id: "balancer" }, { $set : { stopped: true } } , true );

s.getDB( "test" ).foo.insert( { num : 1 } );
s.getDB( "test" ).foo.insert( { num : 2 } );
s.getDB( "test" ).foo.insert( { num : 3 } );
s.getDB( "test" ).foo.insert( { num : 4 } );
s.getDB( "test" ).foo.insert( { num : 5 } );
s.getDB( "test" ).foo.insert( { num : 6 } );
s.getDB( "test" ).foo.insert( { num : 7 } );

assert.eq( 7 , s.getDB( "test" ).foo.find().toArray().length , "normal A" );
assert.eq( 7 , s2.getDB( "test" ).foo.find().toArray().length , "other A" );

s.adminCommand({ split : "test.foo" , middle : { num : 4 } });
s.adminCommand({ movechunk: "test.foo",
                 find: { num: 3 },
                 to: s.getFirstOther(s.getServer("test" )).name,
                 _waitForDelete: true });

assert( s._connections[0].getDB( "test" ).foo.find().toArray().length > 0 , "shard 0 request" );
assert( s._connections[1].getDB( "test" ).foo.find().toArray().length > 0 , "shard 1 request" );
assert.eq(7,
          s._connections[0].getDB( "test" ).foo.find().toArray().length +
            s._connections[1].getDB( "test" ).foo.find().toArray().length,
          "combined shards");

assert.eq( 7 , s.getDB( "test" ).foo.find().toArray().length , "normal B" );
assert.eq( 7 , s2.getDB( "test" ).foo.find().toArray().length , "other B" );

s.adminCommand( { split : "test.foo" , middle : { num : 2 } } );
s.printChunks();

print( "* A" );

assert.eq( 7 , s.getDB( "test" ).foo.find().toArray().length , "normal B 1" );
assert.eq( 7 , s2.getDB( "test" ).foo.find().toArray().length , "other B 2" );

print( "* B" );

assert.eq( 7 , s.getDB( "test" ).foo.find().toArray().length , "normal B 3" );
assert.eq( 7 , s2.getDB( "test" ).foo.find().toArray().length , "other B 4" );

for ( var i=0; i<10; i++ ){
    print( "* C " + i );
    assert.eq( 7 , s2.getDB( "test" ).foo.find().toArray().length , "other B " + i );
}

assert.eq( 0 , s.config.big.find().itcount() , "C1" );

for ( i=0; i<50; i++ ){
    s.config.big.insert( { _id : i } );
}

assert.eq( 50 , s.config.big.find().itcount() , "C2" );
assert.eq( 50 , s.config.big.find().count() , "C3" );
assert.eq( 50 , s.config.big.find().batchSize(5).itcount() , "C4" );

var hashes = [];

for ( i=0; i<3; i++ ){
    print( i );
    s._connections[i].getDB( "config" ).chunks.find( {} , { lastmod : 1 } ).forEach( printjsononeline );
    hashes[i] = s._connections[i].getDB( "config" ).runCommand( "dbhash" );
}

printjson( hashes );

var hashMismatch = false;

for (i = 1; i < hashes.length; i++) {
    if (hashes[0].md5 == hashes[i].md5)  {
        continue;
    }
    
    assert.eq(Object.keys(hashes[0].collections).length ,
              Object.keys(hashes[i].collections).length ,
              "number of collections do not match:\n" +
              tojson(hashes[0].collections) + '\n' +
              tojson(hashes[i].collections));

    for ( var k in hashes[0].collections ) {
        if (hashes[0].collections[k] == hashes[i].collections[k]) {
            continue;
        }
        
        if (k == "mongos" || k == "changelog" || k == "locks" || k == "lockpings") {
            continue;
        }
        
        print("collection " + k + " is different");
        hashMismatch = true;
        
        print( "----" );
        s._connections[0].getDB( "config" ).getCollection( k ).find().sort( { _id : 1 } ).forEach( printjsononeline );

        print( "----" );

        s._connections[i].getDB( "config" ).getCollection( k ).find().sort( { _id : 1 } ).forEach( printjsononeline );
        print( "----" );
    }

    if (hashMismatch) {
        throw Error("hashes different");
    }
}

s.stop();

})();