summaryrefslogtreecommitdiff
path: root/jstests/replsets/replset1.js
blob: 6a18dffaaa2b516dbc89a4a8b14b570f01fd0daa (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
doTest = function( signal ) {

    // Test basic replica set functionality.
    // -- Replication
    // -- Failover

    // Replica set testing API
    // Create a new replica set test. Specify set name and the number of nodes you want.
    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();

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

    // Here's how you save something to master
    master.getDB("foo").foo.save({a: 1000});

    // This method will check the oplogs of the master
    // and slaves in the set and wait until the change has replicated.
    replTest.awaitReplication();


    cppconn = new Mongo( replTest.getURL() ).getDB( "foo" );
    assert.eq( 1000 , cppconn.foo.findOne().a , "cppconn 1" );

    {
        // check c++ finding other servers
        var temp = replTest.getURL();
        temp = temp.substring( 0 , temp.lastIndexOf( "," ) );
        temp = new Mongo( temp ).getDB( "foo" );
        assert.eq( 1000 , temp.foo.findOne().a , "cppconn 1" );        
    }


    // Here's how to stop the master node
    var master_id = replTest.getNodeId( master );
    replTest.stop( master_id );

    // Now let's see who the new master is:
    var new_master = replTest.getMaster();

    // Is the new master the same as the old master?
    var new_master_id = replTest.getNodeId( new_master );

    assert( master_id != new_master_id, "Old master shouldn't be equal to new master." );

    { 
        // this may fail since it has to reconnect
        try {
            cppconn.foo.findOne()
        }
        catch ( e ){
        }
        assert.eq( 1000 , cppconn.foo.findOne().a , "cppconn 2" );

    }

    // Now let's write some documents to the new master
    for(var i=0; i<1000; i++) {
        new_master.getDB("bar").bar.save({a: i});
    }
    new_master.getDB("admin").runCommand({getlasterror: 1});

    // Here's how to restart the old master node:
    slave = replTest.restart( master_id );


    // Now, let's make sure that the old master comes up as a slave
    assert.soon(function() {
        var res = slave.getDB("admin").runCommand({ismaster: 1});
        printjson(res);
        return res['ok'] == 1 && res['ismaster'] == false;
    });

    // And we need to make sure that the replset comes back up
    assert.soon(function() {
        var res = new_master.getDB("admin").runCommand({replSetGetStatus: 1});
        printjson( res );
        return res.myState == 1;
    });

    // And that both slave nodes have all the updates
    new_master = replTest.getMaster();
    assert.eq( 1000 , new_master.getDB( "bar" ).runCommand( { count:"bar"} ).n , "assumption 2")
    replTest.awaitReplication();

    slaves = replTest.liveNodes.slaves;
    assert( slaves.length == 2, "Expected 2 slaves but length was " + slaves.length );
    slaves.forEach(function(slave) {
        slave.setSlaveOk();
        var count = slave.getDB("bar").runCommand({count: "bar"});
        printjson( count );
        assert.eq( 1000 , count.n , "slave count wrong: " + slave );
    });

    // Shut down the set and finish the test.
    replTest.stopSet( signal );
}

doTest( 15 );