Skip to content

Commit 7be4c09

Browse files
committed
Add a test plus a comment
1 parent 1b6ec1d commit 7be4c09

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

packages/data-service/src/connect.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,63 @@ describe('connect', function () {
483483
});
484484
});
485485

486+
it('connects to sharded with readPreferenceTags', async function () {
487+
const options = envs.getConnectionOptions('sharded');
488+
/*
489+
See ticket COMPASS-9111
490+
491+
This test is using readPreference=nearest because this cluster has node with the tag
492+
ANALYTICS. readPreference=secondary would more closely mirror the
493+
original ticket, but this cluster also has no secondaries so that would
494+
fail regardless of readPreferenceTags.
495+
496+
Ideally people would use readPreference=secondaryPreferred, but that works
497+
regardless so isn't a good test and if it was the case that people used
498+
that in the first place we'd never need this ticket.
499+
500+
readPreference=nearest tries to find one that matches the criteria and
501+
since the config server doesn't know about tags the following operations
502+
would hang unless we remove the tags. You can confirm this manually by
503+
hacking maybeOverrideReadPreference in data-service.ts.
504+
*/
505+
const connectionString =
506+
options.connectionString +
507+
'&readPreference=nearest&readPreferenceTags=nodeType:ANALYTICS';
508+
const connectionOptions = {
509+
...options,
510+
connectionString,
511+
};
512+
await testConnection(connectionOptions, {
513+
authenticatedUserRoles: [{ db: 'admin', role: 'root' }],
514+
authenticatedUsers: [{ db: 'admin', user: 'root' }],
515+
});
516+
517+
const dataService = await connect({
518+
connectionOptions,
519+
});
520+
521+
/*
522+
Without us removing the read preference tags these operations would fail.
523+
524+
Normal database operations like find or aggregate will still fail
525+
regardless because the cluster does not have a node with the ANALYTICS
526+
tag, but this test never executes any of those
527+
*/
528+
try {
529+
const databases = await dataService.listDatabases();
530+
const databaseNames = databases
531+
.map((d) => d.name)
532+
.filter((name) => !['local'].includes(name));
533+
for (const databaseName of databaseNames) {
534+
// don't really care what's in there, just that the calls succeed
535+
await dataService.listCollections(databaseName);
536+
await dataService.databaseStats(databaseName);
537+
}
538+
} finally {
539+
await dataService.disconnect();
540+
}
541+
});
542+
486543
describe('ssh', function () {
487544
it('connects with ssh (sshPassword)', async function () {
488545
await testConnection(envs.getConnectionOptions('sshPassword'), {

packages/data-service/src/data-service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,10 @@ function maybeOverrideReadPreference(
144144
): {
145145
readPreference?: ReadPreference;
146146
} {
147+
// see ticket COMPASS-9111 for why this is necessary
147148
if (isMongos && readPreference?.tags) {
148-
return { readPreference: readPreferenceWithoutTags(readPreference) };
149+
const newReadPreference = readPreferenceWithoutTags(readPreference);
150+
return { readPreference: newReadPreference };
149151
}
150152

151153
return {};

0 commit comments

Comments
 (0)