summaryrefslogtreecommitdiff
path: root/jstests/repl/replacePeer2.js
blob: c2983dcc28636702089deebefea21164bd207f59 (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
// test replace peer on slave

var baseName = "jstests_replacepeer2test";

ismaster = function( n ) {
    im = n.getDB( "admin" ).runCommand( { "ismaster" : 1 } );
//    print( "ismaster: " + tojson( im ) );
    assert( im );
    return im.ismaster;
}

var writeOneIdx = 0;

writeOne = function( n ) {
    n.getDB( baseName ).z.save( { _id: new ObjectId(), i: ++writeOneIdx } );
}

getCount = function( n ) {
    return n.getDB( baseName ).z.find( { i: writeOneIdx } ).toArray().length;
}

checkWrite = function( m, s ) {
    writeOne( m );
    assert.eq( 1, getCount( m ) );
    s.setSlaveOk();
    assert.soon( function() {
                return 1 == getCount( s );
                } );
}

doTest = function( signal ) {

    ports = allocatePorts( 4 );

    a = new MongodRunner( ports[ 0 ], "/data/db/" + baseName + "-arbiter" );
    l = new MongodRunner( ports[ 1 ], "/data/db/" + baseName + "-left", "127.0.0.1:" + ports[ 3 ], "127.0.0.1:" + ports[ 0 ] );
    r = new MongodRunner( ports[ 3 ], "/data/db/" + baseName + "-right", "127.0.0.1:" + ports[ 1 ], "127.0.0.1:" + ports[ 0 ] );
    
    rp = new ReplPair( l, r, a );
    rp.start();
    rp.waitForSteadyState( [ 1, 0 ] );
    leftSlave = ( rp.slave().host == rp.left().host );
    
    checkWrite( rp.master(), rp.slave() );

    // allow slave to finish initial sync
    assert.soon( 
        function() { 
            var res = rp.slave().getDB( "admin" ).runCommand( {replacepeer:1} );
            if ( res.ok == 1 )
                return true;
            printjson( res );
            return false;
        } 
    );
    
    // Should not be saved to slave.
    writeOne( rp.master() );
    // Make sure there would be enough time to save to l if we hadn't called replacepeer.
    sleep( 10000 );

    ports.forEach( function( x ) { stopMongod( x, signal ); } );

    if ( leftSlave ) {
        l = new MongodRunner( ports[ 1 ], "/data/db/" + baseName + "-left", "127.0.0.1:" + ports[ 2 ], "127.0.0.1:" + ports[ 0 ] );
        o = new MongodRunner( ports[ 2 ], "/data/db/" + baseName + "-right", "127.0.0.1:" + ports[ 1 ], "127.0.0.1:" + ports[ 0 ] );
        rp = new ReplPair( l, o, a );
        resetDbpath( "/data/db/" + baseName + "-right" );
    } else {
        o = new MongodRunner( ports[ 2 ], "/data/db/" + baseName + "-left", "127.0.0.1:" + ports[ 3 ], "127.0.0.1:" + ports[ 0 ] );
        r = new MongodRunner( ports[ 3 ], "/data/db/" + baseName + "-right", "127.0.0.1:" + ports[ 2 ], "127.0.0.1:" + ports[ 0 ] );
        rp = new ReplPair( o, r, a );
        resetDbpath( "/data/db/" + baseName + "-left" );
    }
    
    rp.start( true );
    rp.waitForSteadyState( [ 1, 0 ] );

    rp.slave().setSlaveOk();
    assert.eq( 1, rp.slave().getDB( baseName ).z.find().toArray().length );
    assert.eq( 1, rp.master().getDB( baseName ).z.find().toArray().length );

    checkWrite( rp.master(), rp.slave() );
    assert.eq( 2, rp.slave().getDB( baseName ).z.find().toArray().length );

    ports.forEach( function( x ) { stopMongod( x ); } );

}

doTest( 15 ); // SIGTERM
doTest( 9 );  // SIGKILL

print("replace2Peer finishes");