Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-4557): randomize servers when there are only 2 eligible servers #3390

Merged
merged 6 commits into from Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/sdam/topology.ts
Expand Up @@ -903,10 +903,22 @@ function processWaitQueue(topology: Topology) {
continue;
} else if (selectedDescriptions.length === 1) {
selectedServer = topology.s.servers.get(selectedDescriptions[0].address);
} else if (selectedDescriptions.length === 2) {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
// If there are only two servers, we avoid shuffling the array of
// server descriptions
const server1 = topology.s.servers.get(selectedDescriptions[0].address);
const server2 = topology.s.servers.get(selectedDescriptions[1].address);

if (server1?.s.operationCount === server2?.s.operationCount) {
selectedServer = Math.floor(Math.random() * 2) === 0 ? server1 : server2;
durran marked this conversation as resolved.
Show resolved Hide resolved
} else {
selectedServer =
server1 && server2 && server1.s.operationCount < server2.s.operationCount
? server1
: server2;
}
} 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);
}
);
});