summaryrefslogtreecommitdiff
path: root/jstests/repl/pair1.js
blob: 84dd7b7315a66a556dd77b823d4deb8f745b2c37 (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
// Basic pairing test

var baseName = "jstests_pair1test";

debug = function( p ) {
//    print( p );
}

ismaster = function( n ) {
    var im = n.getDB( "admin" ).runCommand( { "ismaster" : 1 } );
//    print( "ismaster: " + tojson( im ) );
    assert( im, "command ismaster failed" );
    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 ) );
    check( s );
}

check = function( s ) {
    s.setSlaveOk();
    assert.soon( function() {
                return 1 == getCount( s );
                } );    
    sleep( 500 ); // wait for sync clone to finish up
}

// check that slave reads and writes are guarded
checkSlaveGuard = function( s ) {
    var t = s.getDB( baseName + "-temp" ).temp;
    assert.throws( t.find().count, [], "not master" );
    assert.throws( t.find(), [], "not master", "find did not assert" );
    
    checkError = function() {
        assert.eq( "not master", s.getDB( "admin" ).getLastError() );
        s.getDB( "admin" ).resetError();
    }
    s.getDB( "admin" ).resetError();
    t.save( {x:1} );
    checkError();
    t.update( {}, {x:2}, true );
    checkError();
    t.remove( {x:0} );
    checkError();
}

doTest = function( signal ) {

    ports = allocatePorts( 3 );
    
    a = new MongodRunner( ports[ 0 ], "/data/db/" + baseName + "-arbiter" );
    l = new MongodRunner( ports[ 1 ], "/data/db/" + baseName + "-left", "127.0.0.1:" + ports[ 2 ], "127.0.0.1:" + ports[ 0 ] );
    r = new MongodRunner( ports[ 2 ], "/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();
    
    checkSlaveGuard( rp.slave() );
    
    checkWrite( rp.master(), rp.slave() );
    
    debug( "kill first" );
    rp.killNode( rp.master(), signal );
    rp.waitForSteadyState( [ 1, null ], rp.slave().host );
    writeOne( rp.master() );
    
    debug( "restart first" );
    rp.start( true );
    rp.waitForSteadyState();
    check( rp.slave() );
    checkWrite( rp.master(), rp.slave() );

    debug( "kill second" );
    rp.killNode( rp.master(), signal );
    rp.waitForSteadyState( [ 1, null ], rp.slave().host );

    debug( "restart second" );
    rp.start( true );
    rp.waitForSteadyState( [ 1, 0 ], rp.master().host );
    checkWrite( rp.master(), rp.slave() );

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

}

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