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

Update all projections on snapshot installation #290

Merged
merged 3 commits into from
Sep 4, 2024

Conversation

the-mikedavis
Copy link
Member

@the-mikedavis the-mikedavis commented Aug 23, 2024

When a snapshot is installed the machine state is "swapped" from the state before the snapshot to the one contained in the snapshot. This swap jumps over potentially many commands in the log which we would have used to trigger projections. When we install a snapshot we need to update the projection state but it's not as straightforward as when "restoring" projections (on machine recovery for example) because the projection tables will already exist and potentially contain records.

When we detect that a snapshot has been installed we need to diff the old and new states to find which projections have changed themselves and also update their associated projection tables.

This requires a change in Ra to expose the old machine state and version in the snapshot_installed callback: rabbitmq/ra#467

@the-mikedavis the-mikedavis added the bug Something isn't working label Aug 23, 2024
@the-mikedavis the-mikedavis added this to the v0.15.0 milestone Aug 23, 2024
@the-mikedavis the-mikedavis self-assigned this Aug 23, 2024
Copy link

codecov bot commented Aug 23, 2024

Codecov Report

Attention: Patch coverage is 92.59259% with 4 lines in your changes missing coverage. Please review.

Project coverage is 89.67%. Comparing base (b802393) to head (7dde526).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
src/khepri_machine.erl 90.47% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #290      +/-   ##
==========================================
- Coverage   89.68%   89.67%   -0.01%     
==========================================
  Files          21       21              
  Lines        3140     3187      +47     
==========================================
+ Hits         2816     2858      +42     
- Misses        324      329       +5     
Flag Coverage Δ
erlang-25 88.89% <92.59%> (-0.03%) ⬇️
erlang-26 89.61% <92.59%> (+0.12%) ⬆️
erlang-27 89.58% <92.59%> (+0.09%) ⬆️
os-ubuntu-latest 89.67% <92.59%> (+0.09%) ⬆️
os-windows-latest 89.64% <92.59%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

src/khepri_machine.erl Outdated Show resolved Hide resolved
src/khepri_machine.erl Outdated Show resolved Hide resolved
test/cluster_SUITE.erl Show resolved Hide resolved
@the-mikedavis the-mikedavis force-pushed the md/update-projections-on-snapshot-install branch 2 times, most recently from 7f5ea0e to 083d65c Compare August 27, 2024 15:07
src/khepri_machine.erl Outdated Show resolved Hide resolved
src/khepri_machine.erl Outdated Show resolved Hide resolved
A new `fold/3` will be created in the child commit which will fold over
all payloads. `fold_matching/5` is more specific in what it matches - it
takes a path and a tree and finds the matching payloads.
@the-mikedavis the-mikedavis force-pushed the md/update-projections-on-snapshot-install branch 2 times, most recently from 9df6398 to 09f2b9f Compare September 4, 2024 14:30
@the-mikedavis the-mikedavis marked this pull request as ready for review September 4, 2024 14:30
Copy link
Member

@dumbbell dumbbell left a comment

Choose a reason for hiding this comment

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

The patch looks good to me. Great work!

Could you just please squash the commit adding khepri_pattern_tree:fold() into the commit that uses it? This addition goes with it and is only tested with it. What do you think?

Ra 2.14.0 expands the `snapshot_installed` callback to pass the state
before the snapshot and snapshot metadata for the old state. We'll use
this in the child commit to "diff" the old and new states and find
updates we need to make to projections and projection tables.
When a snapshot is installed the machine state is "swapped" from the
state before the snapshot to the one contained in the snapshot. This
swap jumps over potentially many commands in the log which we would
have used to trigger projections. When we install a snapshot we need
to update the projection state but it's not as straightforward as when
"restoring" projections (on machine recovery for example) because the
projection tables will already exist and potentially contain records.

When we detect that a snapshot has been installed we need to diff the
old and new states to find which projections have changed themselves
and also update their associated projection tables.

This commit also introduces `khepri_pattern_tree:fold/3`: a more generic
version of `fold_matching/5` which folds over all payloads in the tree
rather than those which match a path. This is used in `khepri_machine`
to collect all projections in the tree into a `sets:set()`.
@the-mikedavis the-mikedavis force-pushed the md/update-projections-on-snapshot-install branch from 09f2b9f to 7dde526 Compare September 4, 2024 14:46
@the-mikedavis
Copy link
Member Author

I squashed the fold/3 change into the main change (last commit) and added a small blurb to that commit message about fold/3 👍

@the-mikedavis the-mikedavis merged commit 1ad4a86 into main Sep 4, 2024
10 checks passed
@the-mikedavis the-mikedavis deleted the md/update-projections-on-snapshot-install branch September 4, 2024 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants