Skip to content

Commit 6596261

Browse files
fix: shuffle servers when there are only two servers
1 parent 6f52add commit 6596261

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/sdam/topology.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,7 @@ function processWaitQueue(topology: Topology) {
904904
} else if (selectedDescriptions.length === 1) {
905905
selectedServer = topology.s.servers.get(selectedDescriptions[0].address);
906906
} else {
907-
// don't shuffle the array if there are only two elements
908-
const descriptions =
909-
selectedDescriptions.length === 2 ? selectedDescriptions : shuffle(selectedDescriptions, 2);
907+
const descriptions = shuffle(selectedDescriptions, 2);
910908
const server1 = topology.s.servers.get(descriptions[0].address);
911909
const server2 = topology.s.servers.get(descriptions[1].address);
912910

test/integration/server-selection/server_selection.prose.operation_count.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,27 @@ describe('operationCount-based Selection Within Latency Window - Prose Test', fu
165165
expect(percentageToHost1).to.be.greaterThan(40).and.lessThan(60);
166166
expect(percentageToHost2).to.be.greaterThan(40).and.lessThan(60);
167167
});
168+
169+
it(
170+
'equally distributes operations with both hosts when requests are in parallel',
171+
TEST_METADATA,
172+
async function () {
173+
const collection = client.db('test-db').collection('collection0');
174+
175+
const { insertedId } = await collection.insertOne({ name: 'bumpy' });
176+
177+
const n = 1000;
178+
179+
for (let i = 0; i < n; ++i) {
180+
await collection.findOne({ _id: insertedId });
181+
}
182+
183+
// Step 9: Using command monitoring events, assert that each mongos was selected roughly 50% of the time (within +/- 10%).
184+
const [host1, host2] = seeds.map(seed => seed.split(':')[1]);
185+
const percentageToHost1 = (counts[host1] / n) * 100;
186+
const percentageToHost2 = (counts[host2] / n) * 100;
187+
expect(percentageToHost1).to.be.greaterThan(40).and.lessThan(60);
188+
expect(percentageToHost2).to.be.greaterThan(40).and.lessThan(60);
189+
}
190+
);
168191
});

0 commit comments

Comments
 (0)