// Test that doing slaveOk reads from secondaries hits all the secondaries evenly function testReadLoadBalancing(numReplicas) { var s = new ShardingTest({ shards: { rs0: { nodes: numReplicas }}, verbose: 2, other: { chunksize: 1 }}); s.adminCommand({enablesharding : "test"}) s.config.settings.find().forEach(printjson) s.adminCommand({shardcollection : "test.foo", key : {_id : 1}}) s.getDB("test").foo.insert({a : 123}) primary = s._rs[0].test.liveNodes.master secondaries = s._rs[0].test.liveNodes.slaves function rsStats() { return s.getDB("admin").runCommand("connPoolStats")["replicaSets"][s.rs0.name]; } assert.eq( numReplicas , rsStats().hosts.length ); function isMasterOrSecondary( info ){ if ( ! info.ok ) return false; if ( info.ismaster ) return true; return info.secondary && ! info.hidden; } assert.soon( function() { var x = rsStats().hosts; printjson(x) for ( var i=0; i 0; } ) secondaries[i].getDB('test').setProfilingLevel(2) } // Primary may change with reconfig primary.getDB('test').setProfilingLevel(2) // Store references to the connection so they won't be garbage collected. var connections = []; for (var i = 0; i < secondaries.length * 10; i++) { conn = new Mongo(s._mongos[0].host) conn.setSlaveOk() conn.getDB('test').foo.findOne() connections.push(conn); } var profileCriteria = { op: 'query', ns: 'test.foo' }; for (var i = 0; i < secondaries.length; i++) { var profileCollection = secondaries[i].getDB('test').system.profile; assert.eq(10, profileCollection.find(profileCriteria).count(), "Wrong number of read queries sent to secondary " + i + " " + tojson( profileCollection.find().toArray() )); } db = primary.getDB( "test" ); printjson(rs.status()); c = rs.conf(); print( "config before: " + tojson(c) ); for ( i=0; i