Skip to content

Instantly share code, notes, and snippets.

@renctan
Created July 27, 2012 19:58
Show Gist options
  • Save renctan/3190168 to your computer and use it in GitHub Desktop.
Save renctan/3190168 to your computer and use it in GitHub Desktop.
QA-112
// based from: https://gist.github.com/3fcf2a1c8583416c6ef9
var host = "replicaset.local";
var rsName = 'rs';
/**
* @return the connection string of the bridged replica set.
*/
var getBridgedConnection = function (startDestPort, startBridgePort, delays) {
var bridgeHosts = [];
for (var i = 0; i < delays.length; ++i) {
var destPort = startDestPort + i;
var bridgePort = startBridgePort + i;
stopMongod(bridgePort);
startMongoProgram("mongobridge", "--port", bridgePort, "--dest", host + ":" + destPort, "--delay", delays[i]);
bridgeHosts.push(host + ":" + bridgePort);
}
var url = rsName + '/' + bridgeHosts.join(',');
return url;
};
var i, m, hit, reads;
var conn;
/* First test: no tagged read, mode "nearest", same latency for all. Reads
* should be evenly distributed among all 5 members.
*/
reads = {};
var url = getBridgedConnection(37117, 37017, [0, 0, 0, 0, 0]);
jsTest.log('20 iterations, nearest, delay[0, 0, 0, 0, 0]');
for (i = 0; i < 20; ++i) {
conn = new Mongo(url);
c = conn.getCollection('test.foo');
hit = c.find({x:1}).readPref("nearest").explain().server;
reads[hit] = reads[hit] ? reads[hit] + 1 : 1;
}
print('result: ' + tojson(reads));
/* Second test: three members slow. Reads should be evenly distributed among the
* fast two members.
*/
reads = {};
url = getBridgedConnection(37117, 37017, [0, 25, 25, 25, 0]);
sleep(60000); // wait for ReplicaSetMonitorWatcher to refresh
jsTest.log('20 iterations, nearest, delay[0, 25, 25, 25, 0]');
for (i = 0; i < 20; ++i) {
conn = new Mongo(url);
c = conn.getCollection('test.foo');
hit = c.find({x:1}).readPref("nearest").explain().server;
reads[hit] = reads[hit] ? reads[hit] + 1 : 1;
}
print('result: ' + tojson(reads));
/* Third test: primary slow with mode "primaryPreferred". Reads should target
* only the primary.
*/
reads = {};
url = getBridgedConnection(37117, 37017, [25, 0, 0, 0, 0]);
sleep(60000); // wait for ReplicaSetMonitorWatcher to refresh
jsTest.log('20 iterations, priPref, delay[25, 0, 0, 0, 0]');
for (i = 0; i < 20; ++i) {
conn = new Mongo(url);
c = conn.getCollection('test.foo');
hit = c.find({x:1}).readPref("primaryPreferred").explain().server;
reads[hit] = reads[hit] ? reads[hit] + 1 : 1;
}
print('result: ' + tojson(reads));
/* Fourth test: all secondaries slow with mode "secondaryPreferred". Reads
* should target only the secondaries.
*/
reads = {};
url = getBridgedConnection(37117, 37017, [0, 25, 25, 25, 25]);
sleep(60000); // wait for ReplicaSetMonitorWatcher to refresh
jsTest.log('20 iterations, secPref, delay[0, 25, 25, 25, 25]');
for (i = 0; i < 20; ++i) {
conn = new Mongo(url);
c = conn.getCollection('test.foo');
hit = c.find({x:1}).readPref("secondaryPreferred").explain().server;
reads[hit] = reads[hit] ? reads[hit] + 1 : 1;
}
print('result: ' + tojson(reads));
/* Fifth test: all but one secondary slow with mode "secondaryPreferred". Reads
* should target only the fast secondary.
*/
reads = {};
url = getBridgedConnection(37117, 37017, [0, 25, 0, 25, 25]);
sleep(60000); // wait for ReplicaSetMonitorWatcher to refresh
jsTest.log('20 iterations, secPref, delay[0, 25, 0, 25, 25]');
for (i = 0; i < 20; ++i) {
conn = new Mongo(url);
c = conn.getCollection('test.foo');
hit = c.find({x:1}).readPref("secondaryPreferred").explain().server;
reads[hit] = reads[hit] ? reads[hit] + 1 : 1;
}
print('result: ' + tojson(reads));
/* Sixth test: all but one secondary slow with mode "secondaryPreferred".
* Halfway during the queries, swap the fast secondary with another. Reads
* should be evenly distributed between the fast secondaries.
*/
reads = {};
url = getBridgedConnection(37117, 37017, [0, 25, 25, 0, 25]);
sleep(60000); // wait for ReplicaSetMonitorWatcher to refresh
jsTest.log('10 iterations, secPref, delay[0, 25, 25, 0, 25]');
for (i = 0; i < 10; ++i) {
conn = new Mongo(url);
c = conn.getCollection('test.foo');
hit = c.find({x:1}).readPref("secondaryPreferred").explain().server;
reads[hit] = reads[hit] ? reads[hit] + 1 : 1;
}
url = getBridgedConnection(37117, 37017, [0, 25, 25, 25, 0]);
sleep(60000); // wait for ReplicaSetMonitorWatcher to refresh
jsTest.log('10 iterations, secPref, delay[0, 25, 25, 25, 0]');
for (i = 0; i < 10; ++i) {
conn = new Mongo(url);
c = conn.getCollection('test.foo');
hit = c.find({x:1}).readPref("secondaryPreferred").explain().server;
reads[hit] = reads[hit] ? reads[hit] + 1 : 1;
}
print('result: ' + tojson(reads));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment