summaryrefslogtreecommitdiff
path: root/jstests/replsets/capped_id.js
blob: 83942f6405f2103eeda585145b2c7558c6717f68 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// 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
// 3) create capped collection with autoIndexId=false. make sure no _id index. then create one
//    and check it got created 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 getPrimary to return a reference to the node that's been
// elected master
var master = replTest.getPrimary();

// 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 getPrimary 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 );

function countIdIndexes(theDB, coll) {
       return theDB[coll].getIndexes().filter(function(idx) {
           return friendlyEqual(idx.key, {_id: 1});
       }).length;
}

var numtests = 4;
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 ,
                   countIdIndexes(masterdb, coll),
                   "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();
    }
    else if  ( testnum == 3 ){
        // explicitly set autoIndexId : false
        masterdb.runCommand( {create : coll , capped : true , size : 1024 , autoIndexId : false } )
        for(i=0; i < 500 ; i++){
            masterdb.getCollection( coll ).insert( {a: 1000} );
        }
        replTest.awaitReplication();

        assert.eq( 0 ,
                countIdIndexes(masterdb, coll),
                "master has an _id index on capped collection when autoIndexId is false");
        assert.eq( 0 ,
                countIdIndexes(slave1db, coll),
                "slave1 has an _id index on capped collection when autoIndexId is false");
        assert.eq( 0 ,
                countIdIndexes(slave2db, coll),
                "slave2 has an _id index on capped collection when autoIndexId is false");

        // now create the index and make sure it works
        masterdb.getCollection( coll ).ensureIndex( { "_id" : 1 } );
        replTest.awaitReplication();
    }

    // what indexes do we have?
    print("**********Master indexes on " + dbname + "." + coll + ":**********");
    masterdb[coll].getIndexes().forEach(printjson);
    print("");

    print("**********Slave1 indexes on " + dbname + "." + coll + ":**********");
    slave1db[coll].getIndexes().forEach(printjson);
    print("");

    print("**********Slave2 indexes on " + dbname + "." + coll + ":**********");
    slave2db[coll].getIndexes().forEach(printjson);
    print("");

    // ensure all nodes have _id index
    assert.eq( 1 ,
               countIdIndexes(masterdb, coll),
               "master has an _id index on capped collection");
    assert.eq( 1 ,
               countIdIndexes(slave1db, coll),
               "slave1 does not have _id index on capped collection");
    assert.eq( 1 ,
               countIdIndexes(slave2db, coll),
               "slave2 does not have _id index on capped collection");

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

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