Skip to content

Commit

Permalink
fix: shuffle servers when there are only two servers
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Aug 30, 2022
1 parent 6f52add commit 6596261
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/sdam/topology.ts
Expand Up @@ -904,9 +904,7 @@ function processWaitQueue(topology: Topology) {
} else if (selectedDescriptions.length === 1) {
selectedServer = topology.s.servers.get(selectedDescriptions[0].address);
} else {
// don't shuffle the array if there are only two elements
const descriptions =
selectedDescriptions.length === 2 ? selectedDescriptions : shuffle(selectedDescriptions, 2);
const descriptions = shuffle(selectedDescriptions, 2);
const server1 = topology.s.servers.get(descriptions[0].address);
const server2 = topology.s.servers.get(descriptions[1].address);

Expand Down
Expand Up @@ -165,4 +165,27 @@ describe('operationCount-based Selection Within Latency Window - Prose Test', fu
expect(percentageToHost1).to.be.greaterThan(40).and.lessThan(60);
expect(percentageToHost2).to.be.greaterThan(40).and.lessThan(60);
});

it(
'equally distributes operations with both hosts when requests are in parallel',
TEST_METADATA,
async function () {
const collection = client.db('test-db').collection('collection0');

const { insertedId } = await collection.insertOne({ name: 'bumpy' });

const n = 1000;

for (let i = 0; i < n; ++i) {
await collection.findOne({ _id: insertedId });
}

// Step 9: Using command monitoring events, assert that each mongos was selected roughly 50% of the time (within +/- 10%).
const [host1, host2] = seeds.map(seed => seed.split(':')[1]);
const percentageToHost1 = (counts[host1] / n) * 100;
const percentageToHost2 = (counts[host2] / n) * 100;
expect(percentageToHost1).to.be.greaterThan(40).and.lessThan(60);
expect(percentageToHost2).to.be.greaterThan(40).and.lessThan(60);
}
);
});

0 comments on commit 6596261

Please sign in to comment.