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

Improve Version Vector Handling for Legacy SDK and Snapshots #933

Merged
merged 4 commits into from
Dec 10, 2024
Merged

Conversation

chacha912
Copy link
Contributor

@chacha912 chacha912 commented Dec 10, 2024

What this PR does / why we need it?

  1. Add {actorID:lamport} to accumulate VV when VV is empty in applyChange

    • Changes with VV length of 0 were created from legacy SDK (v0.5.2 or below)
    • In legacy SDK, we added lamport to VV to ensure information could be updated properly
  2. Use maxLamport() instead of serverSeq in applySnapshot

    • Fixed an issue where characters were not being deleted because some lamport values were larger than serverSeq when setting clocks in applySnapshot

Any background context you want to provide?

What are the relevant tickets?

Related yorkie-team/yorkie#1096

Checklist

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

Summary by CodeRabbit

  • New Features

    • Introduced a new method to the Version Vector class for removing an actor's version.
    • Updated the logic for setting clocks in the ChangeID class during snapshot application.
  • Bug Fixes

    • Enhanced the management of lamport clocks in the ChangeID class.
  • Tests

    • Implemented a new test to ensure version vectors reflect expected states after updates and deletions.

Copy link

coderabbitai bot commented Dec 10, 2024

Caution

Review failed

The head commit changed during the review from b302e29 to 59b499c.

Walkthrough

The pull request introduces several modifications across multiple files related to the handling of logical clocks and version vectors. Key changes include updates to the ChangeID class to enhance clock synchronization logic, modifications to the applySnapshot method in the Document class to utilize the maximum Lamport timestamp from a snapshot vector, and the addition of a new method in the VersionVector class for removing an actor's version. Additionally, a new test case is introduced to validate the behavior of version vectors concerning snapshot thresholds.

Changes

File Path Change Summary
packages/sdk/src/document/change/change_id.ts Modified syncClocks to include a conditional check for other.versionVector. Altered setClocks to adjust lamport clock calculation and unset InitialActorID.
packages/sdk/src/document/document.ts Changed applySnapshot to update changeID using snapshotVector.maxLamport() instead of serverSeq.
packages/sdk/src/document/time/version_vector.ts Added new public method unset(actorID: string): void to remove an actor's version from the VersionVector.
packages/sdk/test/integration/gc_test.ts Introduced DefaultSnapshotThreshold constant and added a new test case for snapshot version vector behavior.

Possibly related PRs

Suggested reviewers

  • hackerwins

🐇 In the realm of clocks and vectors,
A change was made for better connectors.
With snapshots and tests that now align,
Our logic flows like a well-timed rhyme.
So hop along, let’s celebrate,
For every change, we elevate! 🎉

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 eslint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/sdk/src/document/change/change_id.ts

Oops! Something went wrong! :(

ESLint: 8.19.0

ESLint couldn't find the plugin "@typescript-eslint/eslint-plugin".

(The package "@typescript-eslint/eslint-plugin" was not found when loaded as a Node module from the directory "/packages/sdk".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install @typescript-eslint/eslint-plugin@latest --save-dev

The plugin "@typescript-eslint/eslint-plugin" was referenced from the config file in "packages/sdk/.eslintrc.js » ../../.eslintrc.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.


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 generate docstrings to generate docstrings for this PR. (Experiment)
  • @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

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (4)
packages/sdk/test/integration/gc_test.ts (1)

1708-1708: Consider standardizing the docKey format across all tests

The docKey format has been changed from `${task.name}-${new Date().getTime()}` to `${new Date().getTime()}-${task.name}`. While this change works, it's inconsistent with other tests in the file.

Apply this change to maintain consistency:

-const docKey = toDocKey(`${new Date().getTime()}-${task.name}`);
+const docKey = toDocKey(`${task.name}-${new Date().getTime()}`);

Also applies to: 1882-1882

packages/sdk/src/document/time/version_vector.ts (1)

39-44: LGTM! Consider enhancing the documentation.

The implementation is correct and follows the single responsibility principle. Consider adding more details to the documentation about when this method should be used and its implications.

 /**
- * `unset` removes the version for the given actor from the VersionVector.
+ * `unset` removes the version for the given actor from the VersionVector.
+ * This is typically used when cleaning up version vectors, such as removing
+ * initial actor IDs or handling actor disconnections.
  */
packages/sdk/src/document/change/change_id.ts (1)

97-99: LGTM! Add comment for legacy SDK compatibility.

The implementation correctly handles empty version vectors from legacy SDK versions.

+    // Handle empty version vectors from legacy SDK (v0.5.2 or below)
+    // by including the actor's lamport in the version vector
     if (other.versionVector.size() === 0) {
       newID.versionVector.set(other.actor, other.lamport);
     }
packages/sdk/src/document/document.ts (1)

1453-1456: LGTM! Document the rationale for using maxLamport.

The change correctly uses the maximum lamport value from the snapshot vector instead of serverSeq, which fixes character deletion issues when certain lamport values exceed serverSeq.

+    // Use maxLamport from snapshotVector instead of serverSeq to handle cases
+    // where some lamport values are greater than serverSeq, which can cause
+    // character deletion issues when setting clocks
     this.changeID = this.changeID.setClocks(
       snapshotVector.maxLamport(),
       snapshotVector,
     );
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between db02eb9 and 4c1e2a7.

📒 Files selected for processing (4)
  • packages/sdk/src/document/change/change_id.ts (2 hunks)
  • packages/sdk/src/document/document.ts (1 hunks)
  • packages/sdk/src/document/time/version_vector.ts (1 hunks)
  • packages/sdk/test/integration/gc_test.ts (4 hunks)
🔇 Additional comments (2)
packages/sdk/test/integration/gc_test.ts (1)

2234-2357: Well-structured test case for version vector behavior with snapshots

The test case effectively validates the version vector behavior when dealing with snapshots. It follows a clear three-step structure:

  1. Updates exceeding snapshot threshold
  2. Local changes with snapshot pull
  3. Deletions after snapshot

A few observations:

  1. The test includes proper cleanup with client deactivation
  2. The timeout is appropriately set to 50000ms for this long-running test
  3. The assertions verify both version vectors and document content length

Let's verify the test coverage and similar test patterns:

✅ Verification successful

The shell script results show comprehensive test coverage for version vector behavior in the codebase:

  1. The test under review is part of a larger suite of GC (garbage collection) tests in gc_test.ts
  2. The version vector assertions are thoroughly tested across multiple scenarios with different client combinations
  3. The DefaultSnapshotThreshold constant is used consistently across multiple test files including:
    • integration/gc_test.ts
    • integration/snapshot_test.ts
    • integration/tree_test.ts
    • integration/client_test.ts
    • integration/presence_test.ts
    • unit/document/document_test.ts

The test case under review follows the established patterns and properly validates version vector behavior with snapshots. The test structure and assertions are consistent with other tests in the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for other snapshot-related tests and version vector assertions

# Look for other snapshot threshold tests
rg -A 2 "DefaultSnapshotThreshold" --type ts

# Look for version vector assertion patterns
ast-grep --pattern 'assert.equal(
  versionVectorHelper($_, $_),
  $_
)'

Length of output: 65754

packages/sdk/src/document/change/change_id.ts (1)

109-109: LGTM! Improved clock synchronization logic.

The changes enhance clock synchronization by:

  1. Ensuring proper lamport clock increment
  2. Cleaning up the version vector by removing the initial actor ID

Also applies to: 112-112

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 changed the title Fix version vector handling in applyChange and applySnapshot Improve Version Vector Handling for Legacy SDK and Snapshots Dec 10, 2024
@hackerwins hackerwins merged commit ba70776 into main Dec 10, 2024
2 checks passed
@hackerwins hackerwins deleted the fix-vv branch December 10, 2024 11:02
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