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 design for garbage collection and version vector #1019

Merged
merged 6 commits into from
Oct 4, 2024

Conversation

JOOHOJANG
Copy link
Contributor

@JOOHOJANG JOOHOJANG commented Sep 26, 2024

What this PR does / why we need it:
Since we're gonna use version vector to run GC after this.
I just updated design document for garbage collection and version vector.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


Additional documentation:


Checklist:

  • Added relevant tests or not required
  • Didn't break anything

@JOOHOJANG JOOHOJANG self-assigned this Sep 26, 2024
Copy link

coderabbitai bot commented Sep 26, 2024

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@hackerwins hackerwins requested review from emplam27 and removed request for hackerwins September 27, 2024 06:57
@JOOHOJANG JOOHOJANG changed the title Update design for garbage collection Update design for garbage collection and version vector Sep 30, 2024
Copy link
Contributor

@emplam27 emplam27 left a comment

Choose a reason for hiding this comment

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

What a great document! it's good to understand about VV and GC👍👍👍


A Version Vector is a mechanism used in distributed systems to track the causal relationships between different replicas or nodes. Each node maintains a vector, where each element corresponds to a version counter for every other node in the system, including itself. Whenever a node updates its state, it increments its own counter. When nodes exchange information, they merge their vectors by taking the maximum value for each element, ensuring that the system knows which updates happened concurrently or which one came first. Version Vectors are particularly useful for detecting conflicts and maintaining consistency in distributed databases by providing a more detailed view of the system's state than a single logical clock.

![version-vector-example](media/version-vector-example.jpg)
Copy link
Contributor

Choose a reason for hiding this comment

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

I cannot understand about this traditional version vector image version-vector-example.jpg. From A@2 to B@3, why the B@3 value is [2, 2, 0] not [2, 1, 0]? I understood version vector From A@2 to B@3 is work like [max(2, 0), max(0, 0)+1, max(0, 0)].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whenever a node updates its state, it increments its own counter. When nodes exchange information, they merge their vectors by taking the maximum value for each element, 

This part can be confusing because the case that you've mentioned is Vector Clock.
Also, implementation can be diverged but the important part is maintaining the concept of Version Vector.
version at B@3 is always identical at any time.

For your information, here is a specific explanation. A's lamport increases at A@2, and when B gets A@2, it'll be max([2, 0, 0], [0, 0, 0]) => [2, 0, 0], then we need to compute B's new lamport which is max(2, 1).

Copy link
Contributor

Choose a reason for hiding this comment

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

thx! understand it!

### How It Works

Garbage collection checks that deleted nodes are no longer referenced remotely and purges them completely.

Server records the logical timestamp of the last change pulled by the client whenever the client requests PushPull. And Server returns the smallest logical timestamp, `min_synced_seq` of all clients in response PushPull to the client. `min_synced_seq` is used to check whether deleted nodes are no longer to be referenced remotely or not.
Server records the version vector of the last change pulled by the client whenever the client requests PushPull. And Server returns the vin version vector, `minVersionVector` of all clients in response PushPull to the client. `minVersionVector` is used to check whether deleted nodes are no longer to be referenced remotely or not.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is 'vin' a typo for 'min'?

And Server returns the vin version vector, `minVersionVector` of all clients in response PushPull to the client.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh my bad. I'll fix it


After client receive this minVersionVector, it will filter its version vector to remove detached client's lamport.
The next pushpull request will contains filtered version vector so that eventually db.version vector will store attached client's version vector only.
![filter-version-vector](media/filter-version-vector.jpg)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the image is different from the code above. If the image sync with the code above, It would be easier to understand!

Copy link
Contributor

Choose a reason for hiding this comment

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

This code filtered c1's vv. i think this code will be filterd c3's vv

minVersionVector = {c1:3, c2:3, c3:5}.Filter([c1]) = {c1:3, c2:3}


Meanwhile, client `c2` inserts `"c"` after textnode `"b"`.
Meanwhile, `client b` inserts `"c"` after textnode `"b"`.
Copy link
Contributor

Choose a reason for hiding this comment

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

client b's change is local change, so there are not send change 2b to server and it changes only in local. Am I understanding this correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right. client b's change has not been sent in that moment(so it stays as local change).
Only client a's change has been sent.

Copy link
Contributor

Choose a reason for hiding this comment

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

How about explaining local change for unfamiliar member to yorkie?

=> [c1:2, c2:1, c3:4, c4:0]

```
### An example of garbage collection:
#### State 1

![garbage-collection-1](media/garbage-collection-1.png)
Copy link
Contributor

Choose a reason for hiding this comment

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

How is the image's situation created? Is it like this below or just an example situation?

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A updates and sends changes to the server, and at the same time, B does the same thing. This situation could happen.


#### State 4

![garbage-collection-4](media/garbage-collection-4.png)

Finally, after client `c1` receives change `4` from server, purges textnode `"b"` because it is no longer referenced remotely.
`Client a` pushpull but nothing to push or pull. `minVersionVector` is still `{a:1, b:1}`, so no GC happens.
Copy link
Contributor

Choose a reason for hiding this comment

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

client a is pull change 3b in this image. is it correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep you're right. Thanks

Comment on lines +98 to +100
![garbage-collection-4](media/garbage-collection-5.png)

`Client b` pushpull but nothing to push or pull. `minVersionVector` is now `{a:3, b:1}`, node "b" removedAt is `3@a`, and `minVersionVector[a] = 3 >= 3`, thus `client b` meets GC condition
Copy link
Contributor

Choose a reason for hiding this comment

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

why client b pull minVersionVector{a:3, b:1}? Above image garbage-collection-4.png, client a pull minVersionVector{a:1, b:1}.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah my bad, I'll fix it
Thanks for your correction

Copy link
Contributor

@emplam27 emplam27 left a comment

Choose a reason for hiding this comment

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

LGTM!😄

@JOOHOJANG JOOHOJANG merged commit 30eaa9f into hybrid-clock Oct 4, 2024
1 check passed
@JOOHOJANG JOOHOJANG deleted the update-design-for-garbage-collection branch October 4, 2024 02:33
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.

2 participants