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: stale caches for sync state when peer is added #732

Merged
merged 1 commit into from
Jul 30, 2024
Merged

Conversation

EvanHahn
Copy link
Contributor

This fixes the following bug:

  1. Device A creates a project.
  2. Device A creates an observation.
  3. Device A reads the sync state.
  4. Device A invites Device B.
  5. Device A reads the sync state again. It's wrong! It's the same state as in Step 3.

This happened because of two stale state caches, both of which I fixed.

  • NamespaceSyncState's cache needed to be invalidated when a peer gets added. I did this by adding an update in CoreSyncState#addPeer, which NamespaceSyncState "listens to".

  • SyncState's cache also needed to be invalidated. I investigated doing this before realizing that its cache only saves a single method call to NamespaceSyncState.prototype.getState, which is itself cached. Rather than properly fix the cache (which would add some additional complexity), I simply removed it.

Fixes #724.

This fixes the following bug:

1. Device A creates a project.
2. Device A creates an observation.
3. Device A reads the sync state.
4. Device A invites Device B.
5. Device A reads the sync state again. It's wrong! It's the same state
   as in Step 3.

This happened because of two stale state caches, both of which I fixed.

- `NamespaceSyncState`'s cache needed to be invalidated when a peer gets
  added. I did this by adding an update in `CoreSyncState#addPeer`,
  which `NamespaceSyncState` "listens to".

- `SyncState`'s cache also needed to be invalidated. I investigated
  doing this before realizing that its cache only saves a single method
  call to `NamespaceSyncState.prototype.getState`, which is itself
  cached. Rather than properly fix the cache (which would add some
  additional complexity), I simply removed it.

Fixes [#724].

[#724]: #724
@EvanHahn EvanHahn requested a review from achou11 July 30, 2024 16:29
Copy link
Member

@gmaclennan gmaclennan left a comment

Choose a reason for hiding this comment

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

Thanks for all the research into this, and for finding both a fix and tests for the failing case. Apologies that this code is a bit complicated - I found it hard to simplify! The solution makes sense, and it does seem like we were "over-caching" and I think it's fine to remove that unnecessary cache. At some point I would like to gather some metrics via Sentry about how long the state calculations are taking on low-end devices, when there are lots of cores and lots of data.

@EvanHahn EvanHahn merged commit f096734 into main Jul 30, 2024
7 checks passed
@EvanHahn EvanHahn deleted the evanhahn/724 branch July 30, 2024 18:10
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.

incorrect sync state when data collected for a project happens before new device is added to project
2 participants