Skip to content

Conversation

@crehbichler
Copy link

@crehbichler crehbichler commented Nov 24, 2025

Description

Summary of Changes

The TopologyDescription constructor initialized commonWireVersion to 0, but the update() method only updates this field when its previous value is null. As a result, commonWireVersion remained stuck at 0 even when all servers reported valid non-zero maxWireVersion values.

This caused secondaryWritableServerSelector() to treat the cluster as pre–MongoDB 5.0 (because wireVersion < 13) and always fall back to primary read preference. User-provided read preferences (e.g. secondary, secondaryPreferred) were therefore ignored for aggregation pipelines containing write stages ($merge and $out).

The fix initializes commonWireVersion to null, allowing it to be correctly updated to the minimum non-zero maxWireVersion across server descriptions.

A new test file test/unit/sdam/topology_description.test.ts has been added to cover initialization and update behavior.

Release Highlight

Release notes highlight

Double check the following

  • Lint is passing (npm run check:lint)
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

@crehbichler crehbichler requested a review from a team as a code owner November 24, 2025 16:59
@tadjik1 tadjik1 added tracked-in-jira Ticket filed in MongoDB's Jira system External Submission PR submitted from outside the team labels Nov 25, 2025
@tadjik1 tadjik1 self-assigned this Nov 27, 2025
@tadjik1 tadjik1 added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Nov 27, 2025
@tadjik1
Copy link
Member

tadjik1 commented Nov 27, 2025

Fantastic job @crehbichler, thanks a lot! Can you please also add integration test for the specific scenario you've found in the jira ticket, i.e. aggregate with write stages that must be issued against secondary and not primary.
For this test we can utilize command monitoring feature, similar to one in the snippet that I've shared.

@crehbichler
Copy link
Author

crehbichler commented Nov 27, 2025

Added!

I wasn’t 100% sure whether to place the integration test in aggregation or server-selection/read-preference, since the issue was more in server selection, but the result was a violation of the aggregation spec for writable stages, so I opted for aggregation.

Also, in the test I am checking the server address (I used the hello command) because the correct $readPreference was already attached to the command before the fix (just the wrong server was being selected).

Please let me know if this is fine or if I should adjust anything.

heartbeatFrequencyMS: number;
localThresholdMS: number;
commonWireVersion: number;
commonWireVersion: number | null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically TopologyDescription is public 🫤 .

So, widening the type like this is technically a breaking change. Is it possible to fix this bug in a way that doesn't change the type of commonWireVersion?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Any type/class/object marked with the @public tag is a part of our public API).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Submission PR submitted from outside the team Primary Review In Review with primary reviewer, not yet ready for team's eyes tracked-in-jira Ticket filed in MongoDB's Jira system

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants