Preserve collection changes until transaction has succeeded #2635
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
While debugging test failures on sharded clusters, I noticed that UnitOfWork loses track of collection changes if a subsequent operation fails. This is due to collection snapshots being taken directly after a document operation, which means the collection sees itself as unmodified when the transaction is retried.
To fix this, we need to hold off on taking collection snapshots until all writes have completed. Unfortunately, this means that the behaviour for collections modified in
post
events changes. Previously, these collections were considered dirty, whereas they are now considered clean. Note that it is not advised to change documents inpost
, as other operations may or may not influenced by such operations, depending on execution order.Fixing this would require us to compute collection changes way earlier in the flow, which would also change the expectation that changes to collections happening in
pre
events are properly handled. As there is no good way of fixing this, I'm more comfortable breaking the flow of changing collections inpost
rather than inpre
.