Skip to content

Commit ddcfa49

Browse files
authoredSep 1, 2022
fix(NODE-4557): randomize servers when there are only 2 eligible servers (#3390)
1 parent d2b3ce1 commit ddcfa49

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed
 

‎src/sdam/topology.ts

+1-3
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

+26
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,30 @@ 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 sequence',
171+
TEST_METADATA,
172+
/**
173+
* note that this test is NOT a prose test, but it lives in this file because it uses the
174+
* same setup as the operation count prose tests
175+
*/
176+
async function () {
177+
const collection = client.db('test-db').collection('collection0');
178+
179+
const { insertedId } = await collection.insertOne({ name: 'bumpy' });
180+
181+
const n = 1000;
182+
183+
for (let i = 0; i < n; ++i) {
184+
await collection.findOne({ _id: insertedId });
185+
}
186+
187+
const [host1, host2] = seeds.map(seed => seed.split(':')[1]);
188+
const percentageToHost1 = (counts[host1] / n) * 100;
189+
const percentageToHost2 = (counts[host2] / n) * 100;
190+
expect(percentageToHost1).to.be.greaterThan(40).and.lessThan(60);
191+
expect(percentageToHost2).to.be.greaterThan(40).and.lessThan(60);
192+
}
193+
);
168194
});

0 commit comments

Comments
 (0)
Please sign in to comment.