0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/jstests/repl/replacePeer1.js

83 lines
2.7 KiB
JavaScript
Raw Normal View History

// test replace peer on master
2009-03-18 00:02:21 +01:00
2009-03-23 16:14:48 +01:00
var baseName = "jstests_replacepeer1test";
2009-03-18 00:02:21 +01:00
ismaster = function( n ) {
im = n.getDB( "admin" ).runCommand( { "ismaster" : 1 } );
2009-03-30 20:31:31 +02:00
// print( "ismaster: " + tojson( im ) );
2009-03-18 00:02:21 +01:00
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 ) {
2009-04-02 00:21:07 +02:00
ports = allocatePorts( 4 );
2009-07-13 22:33:03 +02:00
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 ] );
2009-07-13 22:33:03 +02:00
rp = new ReplPair( l, r, a );
rp.start();
rp.waitForSteadyState( [ 1, 0 ] );
rightMaster = ( rp.master().host == rp.right().host );
checkWrite( rp.master(), rp.slave() );
2009-07-13 22:33:03 +02:00
rp.killNode( rp.slave(), signal );
2009-03-18 00:02:21 +01:00
writeOne( rp.master() );
2009-03-23 16:14:48 +01:00
assert.commandWorked( rp.master().getDB( "admin" ).runCommand( {replacepeer:1} ) );
2009-07-13 22:33:03 +02:00
rp.killNode( rp.master(), signal );
rp.killNode( rp.arbiter(), signal );
if ( rightMaster ) {
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" );
} else {
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" );
}
2009-07-13 22:33:03 +02:00
rp.start( true );
rp.waitForSteadyState( [ 1, 0 ] );
2009-07-13 22:33:03 +02:00
rp.slave().setSlaveOk();
assert.eq( 2, rp.master().getDB( baseName ).z.find().toArray().length );
assert.eq( 2, rp.slave().getDB( baseName ).z.find().toArray().length );
checkWrite( rp.master(), rp.slave() );
assert.eq( 3, rp.slave().getDB( baseName ).z.find().toArray().length );
ports.forEach( function( x ) { stopMongod( x ); } );
2009-03-18 00:02:21 +01:00
}
doTest( 15 ); // SIGTERM
doTest( 9 ); // SIGKILL