summaryrefslogtreecommitdiff
path: root/jstests/replsets/capped_id.js
blob: 3cd51a617b45d84de89e9c95491372b0a4060314 (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
// Check that capped collections get an _id index when replicated

// Test #
// 0) create capped collection on replset, check _id index appears on secondary
// 1) create normal collection, then convertToCapped, check _id index on secondaries
// 2) create capped collection, do updates instead of inserts, check _id index on secondaries

// Create a new replica set test with name 'testSet' and 3 members
var replTest = new ReplSetTest( {name: 'testSet', nodes: 3} );

// call startSet() to start each mongod in the replica set
// this returns a list of nodes
var nodes = replTest.startSet();

// Call initiate() to send the replSetInitiate command
// This will wait for initiation
replTest.initiate();

// Call getMaster to return a reference to the node that's been
// elected master
var master = replTest.getMaster();

// wait for secondaries to be up, since we'll be reading from them
replTest.awaitSecondaryNodes();
// And get the slaves from the liveNodes
var slave1 = replTest.liveNodes.slaves[0];
var slave2 = replTest.liveNodes.slaves[1];

// Calling getMaster made available the liveNodes structure,
// which looks like this:
// liveNodes = {master: masterNode, slaves: [slave1, slave2] }
printjson( replTest.liveNodes );

// define db names to use for this test
var dbname = "dbname";
var masterdb = master.getDB( dbname );
var slave1db = slave1.getDB( dbname );
var slave2db = slave2.getDB( dbname );

var numtests = 3;
for( testnum=0; testnum < numtests; testnum++ ){

    //define collection name
    coll = "coll" + testnum;

    // drop the coll on the master (just in case it already existed)
    // and wait for the drop to replicate
    masterdb.getCollection( coll ).drop();
    replTest.awaitReplication();

    if ( testnum == 0 ){
        // create a capped collection on the master
        // insert a bunch of things in it
        // wait for it to replicate
        masterdb.runCommand( {create : coll , capped : true , size : 1024} );
        for(i=0; i < 500 ; i++){
            masterdb.getCollection( coll ).insert( {a: 1000} );
        }
        replTest.awaitReplication();
    }
    else if ( testnum == 1 ){
        // create a non-capped collection on the master
        // insert a bunch of things in it
        // wait for it to replicate
        masterdb.runCommand( {create : coll } );
        for(i=0; i < 500 ; i++){
            masterdb.getCollection( coll ).insert( {a: 1000} );
        }
        replTest.awaitReplication();

        // make sure _id index exists on primary
        assert.eq( 1 ,
                   masterdb.system.indexes.find( { key:{"_id" : 1}, ns: dbname + "." + coll } ).count() ,
                   "master does not have _id index on normal collection");

        // then convert it to capped
        masterdb.runCommand({convertToCapped: coll , size: 1024 } );
        replTest.awaitReplication();
    }
    else if ( testnum == 2 ){
        // similar to first test, but check that a bunch of updates instead
        // of inserts triggers the _id index creation on secondaries.
        masterdb.runCommand( {create : coll , capped : true , size : 1024} );
        masterdb.getCollection( coll ).insert( {a : 0} );
        for(i=0; i < 500 ; i++){
            masterdb.getCollection( coll ).update( {} , {$inc : {a : 1} } );
        }
        replTest.awaitReplication();
    }

    // what indexes do we have?
    print("**********Master indexes:**********");
    masterdb.system.indexes.find().forEach(printjson);
    print("");

    print("**********Slave1 indexes:**********");
    slave1db.system.indexes.find().forEach(printjson);
    print("");

    print("**********Slave2 indexes:**********");
    slave2db.system.indexes.find().forEach(printjson);
    print("");

    // insure each slave has _id index, but not master
    assert.eq( 0 ,
               masterdb.system.indexes.find( { key:{"_id" : 1}, ns: dbname + "." + coll } ).count() ,
               "master has an _id index on capped collection");
    assert.eq( 1 ,
               slave1db.system.indexes.find( { key:{"_id" : 1}, ns: dbname + "." + coll } ).count() ,
               "slave1 does not have _id index on capped collection");
    assert.eq( 1 ,
               slave2db.system.indexes.find( { key:{"_id" : 1}, ns: dbname + "." + coll } ).count() ,
               "slave2 does not have _id index on capped collection");

    print("capped_id.js Test # " + testnum + " SUCCESS");
}

//Finally, stop set
replTest.stopSet();