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

Implementing GC Using Version Vector #981

Merged
merged 62 commits into from
Sep 26, 2024

Conversation

JOOHOJANG
Copy link
Contributor

@JOOHOJANG JOOHOJANG commented Aug 27, 2024

What this PR does / why we need it:
The purpose of this PR is to address a defect found in the existing GC that used syncedSeq, by introducing VersionVector to resolve the issue.

The diagram of the structure can be found at this link.​

Here are the four key points to focus on:

  1. What is Lamport Synced Version Vector and how to use it
  2. How to store version vector into database and update it when receives change
  3. What is min version vector and how to solve GC problem
  4. How to handle detached client's version vector

1. What is Lamport Synced Version Vector

Definition of Version Vector
Since we're using lamport timestamp to handle CRDT, we implement Lamport Synced Version Vector which is quite different from original Version Vector.
Lamport Synced Version Vector allows us to continue using the existing method that relied on Lamport.
It is key:value map consist of actorID and lamport.

image
As you can see from the image above, which is typical version vector example, client's value(lamport in our case) is not the largest at every moment.

image At the above image, you can see that the client’s Lamport is always the largest, which is consistent with how we used to update and utilize lamport in the past.

For convenience, I will refer to Lamport Synced Version Vector as the version vector from now on.

When creating a new change, we increment both the lamport and the lamport in the version vector to use as the ID. Thus, we don't need to use version vector as ID, but use as a part of ID. You can see how it is used in the modified code (see changeID parts).

When receiving a new change, compute max version vector by using version vector from change and document's version vector.

max(vv1, vv2) =>
for (key in vv1 || key in vv2) {
    maxVV[key] = max(vv1[key] || 0, vv2[key] || 0)
}


ex)
max([c1:2, c2:3, c3:4], [c1:3, c2:1, c3:5, c4:3])
=> [c1:3, c2:3, c3:5, c4:3]

min(vv1, vv2) =>
for (key in vv1 || key in vv2) {
    minVV[key] = min(vv1[key] || 0, vv2[key] || 0)
}

ex)
min([c1:2, c2:3, c3:4], [c1:3, c2:1, c3:5, c4:3])
=> [c1:2, c2:1, c3:4, c4:0]

2. How to store version vector into database and update it when receives change

  1. Attach
image
  • set initial version vector ([c1: 0])
  • make change which includes updated changeID (version vector [c1: 1])
  • push change into server and change table in database.
  • push version vector into version vector table in database
    • you can see the detailed logic from modified codes (updateVersionVector, updateAndFindMinSyncedVersionVectorAfterPushPull)
  1. Pushpull
  • pushonly
    • there's no received changes when its pushonly, thus no need to update document's version vector.
  • pushpull
    • after receives change, we update document's version vector when every change has been applied.
1. update changeID
(except vv, everything is same as before)

doc.vv = max(doc.vv, doc.vv)
doc.vv[doc.actorid] = maxLamport + 1

3. What is min version vector and how to solve GC problem

Min version vector is the vector which consists of minimum value of every version vector stored in version vector table in database.

Conceptually, min version vector is version vector that is uniformly applied to all users. It ensures that all users have at least this version applied. Which means that it is safe to perform GC.

You can see detailed logic from UpdateAndFindMinVersionVectorAfterPushPull in modified codes.
Suppose clientA is performing pushpull

  1. if there're version vectors in database, collect them.
  2. collect clientA's version vector from change pack
  3. store clientA's version vector into database
  4. compute min from collected version vectors

GC algorithm

if (removedAt.lamport <= minSyncedVersionVector[removedAt.actor]) {
    GC()
}

If node.removedAt.lamport is smaller or equal to every value in min version vector, then run GC.
It's okay to run GC when its equal, because the meaning of min version vector is that every client's version vector is at least larger then min version vector.

4. How to handle detached client's version vector

If detached client's version vector remains in database and local document, its memory wasting and also GC will not performed.

1. minVV = [c1:3, c2:4, c3:4]
2. c3 detach
3. c2 remove nodes k , k > 4
4. c1, c2 keep updating and perform pushpull
5. minVV at some moment [c1: n, c2: m, c3:4] where n, m > k > 4
6. k > 4, can't perform GC
image

So it's necessary to remove detached client's version vector and its lamport from other's version vector.

  1. When client detaches, remove its version vector from version vector table
  2. remove client's lamport from every version vector stored in database.
  3. when other client perform pushing and its version vector includes detached client's lamport, filter it by using min version vector.
    • the reason why we're using min version vector in this case is that we already compute min version vector (to return min version vector to perform GC).
    • min version vector includes all attached users in the document.
  4. store filtered version vector into database.
  5. when other client perform pulling, we also use min version vector to filter it after apply change.
  6. after few times of pushpull, detached client's lamport will be totally removed from database and others document.

By the way, the code is a bit messy, so any reviews are always welcome. If you have any ideas for improvements, feel free to share them.


Which issue(s) this PR fixes:

Address #723

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

Copy link

coderabbitai bot commented Aug 27, 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.

Copy link
Member

@hackerwins hackerwins 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 your contribution. I hope the GC issue will be resolved by this.

It would be good to update design document after merging this. https://github.com/yorkie-team/yorkie/blob/main/design/garbage-collection.md

And I left a few comments.

api/yorkie/v1/resources.proto Show resolved Hide resolved
pkg/document/change/id.go Show resolved Hide resolved
pkg/document/document.go Outdated Show resolved Hide resolved
pkg/document/time/version_vector.go Outdated Show resolved Hide resolved
pkg/document/time/version_vector.go Outdated Show resolved Hide resolved
pkg/document/time/version_vector.go Outdated Show resolved Hide resolved
pkg/document/time/version_vector.go Outdated Show resolved Hide resolved
server/backend/database/mongo/client.go Show resolved Hide resolved
server/backend/database/version_vector.go Show resolved Hide resolved
test/integration/gc_test.go Outdated Show resolved Hide resolved
@JOOHOJANG JOOHOJANG self-assigned this Sep 13, 2024
server/backend/database/mongo/client.go Outdated Show resolved Hide resolved
server/backend/database/mongo/client.go Outdated Show resolved Hide resolved
…ake version vector comments to a single line
@hackerwins hackerwins self-requested a review September 26, 2024 01:26
Copy link
Member

@hackerwins hackerwins 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 your contribution.

@hackerwins hackerwins merged commit eec25b3 into hybrid-clock Sep 26, 2024
1 check passed
@hackerwins hackerwins deleted the hybrid-clock-version-vector branch September 26, 2024 01:27
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