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-5925): driver throws error when non-read operation in a transaction has a ReadPreferenceMode other than 'primary' #4075

Merged
merged 8 commits into from
Apr 15, 2024

Conversation

aditi-khare-mongoDB
Copy link
Contributor

@aditi-khare-mongoDB aditi-khare-mongoDB commented Apr 11, 2024

Description

Within a transaction, readPreferences != 'primary' are not supported, and our error logic must support that to comply with spec. An error should only be thrown in these circumstances when a read operation is being executed.

What is changing?

In a transaction, when a user provides a readPreference != 'primary' and then tries to perform a command that involves a read, the Node Driver will now throw a

Is there new documentation needed for these changes?

Yes, update Transactions Node.js Manual to reflect desired readPreference inputs.

What is the motivation for this change?

See Release Highlights

Release Highlight

Don't throw error when non-read operation in a transaction has a ReadPreferenceMode other than 'primary'

The following error will now only be thrown when a user provides a ReadPreferenceMode other than primary and then tries to perform a command that involves a read:

new MongoTransactionError('Read preference in a transaction must be primary');

Prior to this change, the Node Driver would incorrectly throw this error even when the operation does not perform a read.
Note: a RunCommandOperation is treated as a read operation for this error.

Double check the following

  • Ran npm run check:lint script
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

@aditi-khare-mongoDB aditi-khare-mongoDB changed the title fix(NODE-5925): Attempting a read on txn with a non-primary readPreference throws an error fix(NODE-5925): Throw error on txn read with a non-primary readPreference mode Apr 11, 2024
@aditi-khare-mongoDB aditi-khare-mongoDB changed the title fix(NODE-5925): Throw error on txn read with a non-primary readPreference mode fix(NODE-5925): Don't throw error when non-read operation in a transaction has a ReadPreferenceMode other than 'primary' Apr 11, 2024
@aditi-khare-mongoDB aditi-khare-mongoDB marked this pull request as ready for review April 11, 2024 20:24
@nbbeeken nbbeeken self-assigned this Apr 15, 2024
@nbbeeken nbbeeken added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Apr 15, 2024
@nbbeeken nbbeeken self-requested a review April 15, 2024 14:26
if (
inTransaction &&
!readPreference.equals(ReadPreference.primary) &&
(operation.hasAspect(Aspect.READ_OPERATION) || operation.commandName === 'runCommand')
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
(operation.hasAspect(Aspect.READ_OPERATION) || operation.commandName === 'runCommand')
(operation.hasAspect(Aspect.READ_OPERATION) || operation.commandName === 'runCommand')

Very minor but a handful of lines down we check for read/write aspect, can we move those variable declarations up here and reuse the result of this call? I figure we can just reuse the same work once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I've moved the isReadOperation up in the function!

@aditi-khare-mongoDB aditi-khare-mongoDB force-pushed the NODE-5925/no-secondary-readpref-on-txn branch from 5b89077 to 565b193 Compare April 15, 2024 17:08
@nbbeeken nbbeeken added Team Review Needs review from team and removed Primary Review In Review with primary reviewer, not yet ready for team's eyes labels Apr 15, 2024
@aditi-khare-mongoDB aditi-khare-mongoDB changed the title fix(NODE-5925): Don't throw error when non-read operation in a transaction has a ReadPreferenceMode other than 'primary' fix(NODE-5925): Throw error when non-read operation in a transaction has a ReadPreferenceMode other than 'primary' Apr 15, 2024
@nbbeeken nbbeeken changed the title fix(NODE-5925): Throw error when non-read operation in a transaction has a ReadPreferenceMode other than 'primary' fix(NODE-5925): driver throws error when non-read operation in a transaction has a ReadPreferenceMode other than 'primary' Apr 15, 2024
@nbbeeken nbbeeken merged commit 39fc198 into main Apr 15, 2024
18 of 26 checks passed
@nbbeeken nbbeeken deleted the NODE-5925/no-secondary-readpref-on-txn branch April 15, 2024 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team Review Needs review from team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants