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

Lock local changes to assert transactions #203

Merged
merged 4 commits into from
Jun 13, 2024
Merged

Lock local changes to assert transactions #203

merged 4 commits into from
Jun 13, 2024

Conversation

skhugh
Copy link
Contributor

@skhugh skhugh commented Jun 12, 2024

What this PR does / why we need it?

When detach and other changes happen almost asynchonously, duplicated local changes could be sent to the server.
This PR uses mutex to assert transactions for push-pull and local application.

Any background context you want to provide?

What are the relevant tickets?

Fixes #

Checklist

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

Summary by CodeRabbit

  • New Features

    • Added a new test to ensure duplicated local changes are not sent to the server.
  • Refactor

    • Introduced synchronization for document operations to enhance concurrency management.
  • Chores

    • Updated the Android Gradle Plugin version to 8.4.2.

@skhugh skhugh requested a review from 7hong13 June 12, 2024 08:51
@skhugh skhugh self-assigned this Jun 12, 2024
@skhugh skhugh requested a review from hackerwins June 12, 2024 08:51
Copy link

coderabbitai bot commented Jun 12, 2024

Walkthrough

The recent updates introduce a new test to ensure duplicated local changes aren't sent to the server, enhance concurrency management in document operations using Mutex, and update the Android Gradle Plugin version from 8.4.1 to 8.4.2. These changes aim to improve the reliability and performance of document handling in the Yorkie framework, ensuring better synchronization and testing coverage.

Changes

File/Path Change Summary
yorkie/src/androidTest/.../ClientTest.kt Added test_duplicated_local_changes_not_sent_to_server() to ensure duplicated local changes are not sent to the server.
yorkie/src/main/kotlin/dev/yorkie/core/Client.kt Introduced Mutex for concurrency management in document operations, with new imports and properties.
yorkie/src/main/kotlin/dev/yorkie/document/Document.kt Removed Mutex usage and refactored applyChangePack method to simplify logic.
gradle/libs.versions.toml Updated Android Gradle Plugin version from 8.4.1 to 8.4.2.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Document
    participant Server

    Client->>Document: Attach Document
    Document->>Client: Document Attached
    Client->>Document: Make Local Changes
    Client->>Client: Check for Duplicates
    alt No Duplicates
        Client->>Server: Push Changes
    else Duplicates Found
        Client->>Server: No Action
    end
    Server->>Client: Acknowledge Changes
Loading

Poem

In the land of code, where changes flow,
Mutex guards the docs, ensuring they grow.
Duplicates no more, in tests we see,
Gradle updates too, for stability.
Yorkie strides with a hop so bright,
In sync and strong, through day and night. 🌟


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

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f0163c7 and 84c5054.

Files selected for processing (3)
  • yorkie/src/androidTest/kotlin/dev/yorkie/core/ClientTest.kt (1 hunks)
  • yorkie/src/main/kotlin/dev/yorkie/core/Client.kt (6 hunks)
  • yorkie/src/main/kotlin/dev/yorkie/document/Document.kt (3 hunks)
Additional comments not posted (2)
yorkie/src/main/kotlin/dev/yorkie/document/Document.kt (1)

258-278: The refactoring of the applyChangePack method appears to be well-structured and logically sound. However, ensure that the removal of Mutex does not introduce any race conditions, especially with the asynchronous operations involved.

Verification successful

The Document.kt file uses withContext with a dispatcher for various operations, ensuring thread safety. The launch block operates within a coroutine scope, which is consistent with the usage of withContext for synchronization. Therefore, the removal of Mutex does not introduce race conditions.

  • The launch block is within a coroutine scope, ensuring proper synchronization.
  • The surrounding code uses withContext to ensure thread safety.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that no race conditions are introduced due to Mutex removal.

# Test: Search for concurrent modifications in Document class. Expect: Proper synchronization mechanisms in place.
rg --type kotlin $'Document.*async|launch' yorkie/src/main/kotlin/dev/yorkie/document/Document.kt

Length of output: 158



Script:

#!/bin/bash
# Description: Locate and review the usage of `launch` in the Document.kt file to ensure proper synchronization mechanisms are in place.

# Test: Search for the exact usage of `launch` in the Document.kt file.
rg 'launch' yorkie/src/main/kotlin/dev/yorkie/document/Document.kt -A 10 -B 10

Length of output: 1941



Script:

#!/bin/bash
# Description: Check for synchronization mechanisms around the `launch` block in Document.kt.

# Test: Search for usage of synchronization primitives or context ensuring thread safety.
rg 'synchronized|Mutex|withContext' yorkie/src/main/kotlin/dev/yorkie/document/Document.kt -A 10 -B 10

Length of output: 5980

yorkie/src/main/kotlin/dev/yorkie/core/Client.kt (1)

75-76: The introduction of Mutex for document synchronization in the Client class is a crucial enhancement for ensuring thread safety during document operations. The use of withLock in various methods like syncAsync, attachAsync, detachAsync, and removeAsync ensures that these operations are atomic and thread-safe. This is a significant improvement in handling concurrency issues.

Also applies to: 114-118, 246-274

Comment on lines 844 to 892
@Test
fun test_duplicated_local_changes_not_sent_to_server() {
withTwoClientsAndDocuments(detachDocuments = false) { c1, c2, d1, d2, _ ->
val d1Events = mutableListOf<Document.Event>()
val d2Events = mutableListOf<Document.Event>()
val collectJobs = listOf(
launch(start = CoroutineStart.UNDISPATCHED) {
d1.events.filter { it is RemoteChange || it is LocalChange }
.collect(d1Events::add)
},
launch(start = CoroutineStart.UNDISPATCHED) {
d2.events.filter { it is RemoteChange || it is LocalChange }
.collect(d2Events::add)
},
)

listOf(
d1.updateAsync { root, _ ->
root.setNewTree(
"t",
element("doc") {
element("p") { text { "12" } }
element("p") { text { "34" } }
},
)
},
c1.syncAsync(),
c1.syncAsync(),
c1.syncAsync(),
c1.detachAsync(d1),
)

withTimeout(GENERAL_TIMEOUT) {
while (d2Events.isEmpty()) {
delay(50)
}
}
assertIs<RemoteChange>(d2Events.first())
assertTreesXmlEquals("<doc><p>12</p><p>34</p></doc>", d1, d2)

delay(500)

assert(d2Events.size == 1)

c2.detachAsync(d2).await()

collectJobs.forEach(Job::cancel)
}
}
Copy link

Choose a reason for hiding this comment

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

The test test_duplicated_local_changes_not_sent_to_server is well-structured and aligns with the PR's objective to prevent duplicated local changes from being sent to the server. However, consider adding more detailed comments within the test to explain each step, especially the multiple syncAsync calls and their purpose in this context. This will improve maintainability and readability for other developers who may work on this test in the future.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 84c5054 and 2d94b10.

Files selected for processing (1)
  • yorkie/src/androidTest/kotlin/dev/yorkie/core/ClientTest.kt (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • yorkie/src/androidTest/kotlin/dev/yorkie/core/ClientTest.kt

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2d94b10 and fc488ac.

Files selected for processing (1)
  • gradle/libs.versions.toml (1 hunks)
Files skipped from review due to trivial changes (1)
  • gradle/libs.versions.toml

@skhugh skhugh merged commit f1a80fd into main Jun 13, 2024
4 of 6 checks passed
@skhugh skhugh deleted the lock_local_changes branch June 13, 2024 01:37
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