-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
feat(NODE-2014)!: return executor result from withSession and withTransaction #3783
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some questions
src/sessions.ts
Outdated
} | ||
|
||
return attemptTransactionCommit(session, startTime, fn, options); | ||
return attemptTransactionCommit(session, startTime, fn, options).then(() => result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if we need to retry the transaction? I think we'd get incorrect behavior, because the first call to attemptTransaction
would always return the result of the first call to fn
- but I'd assume that we want to return the result
associated with the successful attemptTransaction
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this is okay and expected behavior? We document that Client.withSession
may call its callback more than once - we should here too at the very least. But I think since we may call the callback twice, it's an acceptable limitation that the callback should be idempotent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
edit: it is noted for this method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pending slack convo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to return the last result and added a test
@@ -591,18 +589,18 @@ function attemptTransaction<TSchema>( | |||
|
|||
if (!isPromiseLike(promise)) { | |||
session.abortTransaction().catch(() => null); | |||
throw new MongoInvalidArgumentError( | |||
'Function provided to `withTransaction` must return a Promise' | |||
return Promise.reject( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why'd you revert the throw
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it ends up being equivalent only because the caller is now an async function
, but throwing here is incorrect since it changes the expectation that this function is always promise returning. I don't feel strongly about keeping this, esp with the ticket to refactor this properly coming up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just curious, this change is fine. It'll be moot once Malik gets to this code anyways
}) | ||
.finally(async () => await session.endSession()); | ||
|
||
expect(withTransactionResult).to.equal(counter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expect(withTransactionResult).to.equal(counter); | |
expect(withTransactionResult).to.equal(3); |
either we do this, or we somehow assert that the transaction was retried. this test would pass as-is if the failpoint were never triggered
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed this and the manual abort test
@@ -591,18 +589,18 @@ function attemptTransaction<TSchema>( | |||
|
|||
if (!isPromiseLike(promise)) { | |||
session.abortTransaction().catch(() => null); | |||
throw new MongoInvalidArgumentError( | |||
'Function provided to `withTransaction` must return a Promise' | |||
return Promise.reject( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just curious, this change is fine. It'll be moot once Malik gets to this code anyways
Description
What is changing?
withSession
&withTransaction
return the value returned by the provided functionsIs there new documentation needed for these changes?
Yes, API docs have been updated. Look for docs changes needed on the ticket
What is the motivation for this change?
See highlight.
Release Highlight
withSession
andwithTransaction
return the value returned by the provided functionThe
await client.withSession(async session => {})
now returns the value that the provided function returns. Previously, this function returnedvoid
this is a feature to align with the following breaking change.The
await session.withTransaction(async () => {})
method now returns the value that the provided function returns. Previously, this function returned the server command response which is subject to change depending on the server version or type the driver is connected to. The return value got in the way of writing robust, reliable, consistent code no matter the backing database supporting the application.withTransaction
breaking changeImportant
When upgrading to this version of the driver audit the usages of
withTransaction
forif
statements or other conditional checks on the return value ofwithTransaction
. Previously, the return value was the command response if the transaction was committed, andundefined
if it had been manually aborted. It would only throw if an operation or the author of the function threw an error. Since thus far there has not been the ability to get the result of the function passed towithTransaction
we suspect most existing usages to returnvoid
, makingwithTransaction
avoid
returning function in this major release. Take care to ensure that the return values of your function match the expectation of the code that follows the completion ofwithTransaction
.Double check the following
npm run check:lint
scripttype(NODE-xxxx)[!]: description
feat(NODE-1234)!: rewriting everything in coffeescript