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 miscalculation of tree size in concurrent editing #846

Merged
merged 3 commits into from
Jun 7, 2024
Merged

Conversation

hackerwins
Copy link
Member

@hackerwins hackerwins commented Jun 5, 2024

What this PR does / why we need it?

Fix miscalculation of tree size in concurrent editing

This commit addresses the issue where the updateAncestorsSize method
subtracts the size of a tombstoned node from its ancestors' sizes when
the node is removed. However, the previous logic did not consider
cases where ancestors node are tombstoned. In such cases, the update
should not propagate to the parent's ancestors. This commit introduces
recursive checks on ancestors and stops updating process if a
tombstoned ancestor is encountered.

Furthermore, the PR ensures consistency between the size maintenance
policies of updateDescendantsSize and updateAncestorsSize.

Any background context you want to provide?

What are the relevant tickets?

Address yorkie-team/yorkie#889

Checklist

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

Summary by CodeRabbit

  • Bug Fixes

    • Improved accuracy in size calculations when child nodes are removed in the index tree.
  • Tests

    • Added new test cases for concurrent editing and snapshot consistency of the index tree.

Copy link

coderabbitai bot commented Jun 5, 2024

Walkthrough

The recent updates streamline the logic for managing tree structures in the codebase. Specifically, the CRDTTreeNode class in tree.ts now has a simplified method for updating ancestor sizes, while the IndexTreeNode class in index_tree.ts has improved handling of descendant sizes when child nodes are removed. Additionally, new test cases have been added to ensure the integrity of index tree size calculations during concurrent editing and snapshots.

Changes

File Path Change Summary
src/document/crdt/tree.ts Simplified the logic for updating ancestors' size in CRDTTreeNode by directly calling updateAncestorsSize() when alived.
src/util/index_tree.ts Modified updateDescendantsSize method in IndexTreeNode to handle child node removal for accurate size calculations.
test/integration/tree_test.ts Added imports for converter and toXML, and new test cases for index tree size calculations during concurrent editing and snapshot consistency.

Poem

In the land of code, where trees do grow,
Ancestors and descendants now flow,
With logic refined, sizes align,
Concurrent edits, tests combine.
The forest stands tall, in harmony,
A testament to code's symphony. 🌳🐇


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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

codecov bot commented Jun 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.68%. Comparing base (9e12b9e) to head (896a868).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #846   +/-   ##
=======================================
  Coverage   80.68%   80.68%           
=======================================
  Files          59       59           
  Lines        4586     4586           
  Branches      932      932           
=======================================
  Hits         3700     3700           
  Misses        617      617           
  Partials      269      269           

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

@hackerwins hackerwins marked this pull request as ready for review June 5, 2024 11:43
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: 1

Outside diff range and nitpick comments (2)
src/document/crdt/tree.ts (2)

Line range hint 727-727: Consider using optional chaining for better safety and readability.

- if (parentNode.isRemoved) {
+ if (parentNode?.isRemoved) {

This change will prevent potential runtime errors if parentNode is null or undefined.


Line range hint 1533-1533: Remove unnecessary return statement in a void function.

- return this.indexTree.tokensBetween(fromIdx, toIdx, callback);
+ this.indexTree.tokensBetween(fromIdx, toIdx, callback);

The traverseInPosRange method is declared to return void, so any return statement within it should be removed to avoid confusion and adhere to TypeScript's type expectations.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2305dbc and 896a868.

Files selected for processing (3)
  • src/document/crdt/tree.ts (1 hunks)
  • src/util/index_tree.ts (2 hunks)
  • test/integration/tree_test.ts (3 hunks)
Additional context used
Biome
src/document/crdt/tree.ts

[error] 727-727: Change to an optional chain. (lint/complexity/useOptionalChain)

Unsafe fix: Change to an optional chain.


[error] 1533-1533: The function should not return a value because its return type is void. (lint/correctness/noVoidTypeReturn)

The function is here:

'void' signals the absence of value. The returned value is likely to be ignored by the caller.

test/integration/tree_test.ts

[error] 33-75: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 77-108: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 110-135: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 137-194: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 196-244: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 246-316: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 318-407: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 409-507: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 509-541: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 543-579: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 581-617: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 619-676: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 680-704: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 706-733: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 735-847: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 849-873: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 875-899: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 901-931: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 933-957: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.


[error] 959-983: This function expression can be turned into an arrow function. (lint/complexity/useArrowFunction)

Function expressions that don't use this can be turned into arrow functions.
Safe fix: Use an arrow function instead.

Additional comments not posted (5)
src/util/index_tree.ts (1)

146-149: Ensure proper handling of removed parent nodes in updateAncestorsSize.

src/document/crdt/tree.ts (1)

553-553: Ensure correct ancestor size update logic.

The change to directly call updateAncestorsSize() when a node is marked as removed ensures that the tree size is recalculated immediately, which is crucial for maintaining accurate tree metrics during concurrent modifications.

test/integration/tree_test.ts (3)

18-18: LGTM! The addition of converter aligns with the changes described in the PR.


30-30: LGTM! The addition of toXML aligns with the changes described in the PR.


4126-4212: LGTM! The test case effectively checks the size calculations and consistency of the index tree during concurrent editing, aligning with the PR's objectives.

src/util/index_tree.ts Show resolved Hide resolved
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.

1 participant