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(DataStore): created and deleted model on one device appears as created on the other #3554

Merged

Conversation

MuniekMg
Copy link
Contributor

@MuniekMg MuniekMg commented Mar 6, 2024

Bug: #3553

General Checklist

  • Added new tests to cover change, if needed
  • Build succeeds with all target using Swift Package Manager
  • All unit tests pass
  • All integration tests pass
  • PR title conforms to conventional commit style

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@MuniekMg MuniekMg requested a review from a team as a code owner March 6, 2024 16:48
}

guard !localMetadatas.isEmpty else {
return remoteModels.compactMap { getDisposition($0, localMetadata: nil) }
Copy link
Contributor Author

@MuniekMg MuniekMg Mar 6, 2024

Choose a reason for hiding this comment

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

This line was causing the bug:

here we were geting an empty localMetadatas and remoteModels with two elements for the same model (the same ID):
eg. [
ModelA - id: aaa, version: 1, deleted: false,
ModelA - id: aaa, version: 2, deleted: true
]

and then "deleted: true disposition" was dropped so only "create disposition" was returned

Copy link
Member

Choose a reason for hiding this comment

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

This certainly appears to be a bug. Previously, the reconciliation for remoteModels was executed individually. We've transitioned to batching, but overlooked this particular scenario. I agree with your proposed approach; we should groupBy and optimize the remoteModels accordingly.

Copy link
Member

@lawmicha lawmicha Mar 7, 2024

Choose a reason for hiding this comment

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

Let me try to summarize this, the reconciliation path for DataStore takes the sync query remote model results and decides to apply the remote model to the local database or drop it. Previously the logic processed one model at a time. We changed this to a batch operation by putting the reconciliation logic under a transaction block, operating on the entire list of remote models. The necessary logic updates to perform the SQL operations more efficiently included constructing a predicate of all the model identifiers as a filter criteria for querying local data that is used for the reconciliation logic. There exists a bug with this optimization when having multiple entries of remote model belong to the same identifier and the changes to the remote model ends up as a deleted model on the latest version.

Bug example
remoteModels = [id=111 version=1 deleted=false, id=111 version=2 deleted=true]

When the disposition of the model is calculated for the create model, it is to create the model with the local database, because there is no local model, nor is there a local model with version less than the remote model.
When the disposition of the model is calculated for the delete model, it is to drop it because no local model yet to exist, so it thinks there is nothing to delete.

The end result is the model is created but not deleted. The fix we are applying in the PR is to optimize the reconciliation by only taking the latest versions of the remote model. In the example, this becomes [id=111 version=2, deleted=true]. The latest version represents the final state of the model that we want applied to the local database. In this case, the remote model will be dropped. There is no create disposition so it will not be created either.

@5d 5d changed the title fix(DataStore): created and deleted model on one device appears as created on the other #3553 fix(DataStore): created and deleted model on one device appears as created on the other Mar 7, 2024
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:54 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:54 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:54 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:55 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:55 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:55 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:55 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:55 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:55 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:55 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:55 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:55 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:55 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:55 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:55 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:55 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 7, 2024 21:55 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 17:32 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 17:32 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 17:32 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 17:32 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 17:32 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 17:32 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 17:32 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 17:32 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d temporarily deployed to IntegrationTest March 8, 2024 18:24 — with GitHub Actions Inactive
@5d 5d merged commit 9f8e690 into aws-amplify:main Mar 11, 2024
135 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants