Skip to content

feat: Add instanceId to distributed lock keys for multi-instance isolation #40966

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

Merged
merged 6 commits into from
Jun 19, 2025

Conversation

abhvsn
Copy link
Contributor

@abhvsn abhvsn commented Jun 17, 2025

Description

This PR modifies the DistributedLockAspect to include the instanceId in Redis lock keys, ensuring proper isolation between different Appsmith instances in distributed deployments.

Changes Made

  • Updated lock key format from lock:{key} to lock::{instanceId}:{key}
  • Adapted all handler methods (handleMono, handleFlux, handleBlocking) to work with the new async lock details creation

Why This Change?

Previously, distributed locks were shared across all Appsmith instances using the same Redis instance, which could lead to:

  • Lock conflicts between different instances
  • Difficulty in debugging lock-related issues

Related Issues

Fixes issues related to distributed lock conflicts in multi-instance Appsmith deployments with shared Redis.

/test Sanity

🔍 Cypress test results

Tip

🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/15732404717
Commit: 06c6f14
Cypress dashboard.
Tags: @tag.Sanity
Spec:


Wed, 18 Jun 2025 20:47:06 UTC

Communication

Should the DevRel and Marketing teams inform users about this change?

  • Yes
  • No

Summary by CodeRabbit

  • New Features

    • Improved distributed locking by including an instance identifier in lock keys for enhanced uniqueness and traceability.
    • Introduced reactive support for instance ID retrieval to optimize lock key generation.
  • Bug Fixes

    • Enhanced reliability of lock acquisition in distributed environments by asynchronously obtaining lock details.

Copy link
Contributor

coderabbitai bot commented Jun 17, 2025

Walkthrough

The DistributedLockAspect class was updated to include an instance identifier from an InstanceIdProvider in distributed lock keys. Lock key creation became asynchronous, and relevant methods were refactored to handle reactive lock detail retrieval. A new InstanceIdProvider interface and its implementation were added to supply the instance ID reactively. Tests and lock release logic were updated accordingly.

Changes

File(s) Change Summary
app/server/reactive-caching/src/main/java/com/appsmith/caching/aspects/DistributedLockAspect.java Constructor updated to accept InstanceIdProvider; lock key creation made asynchronous with instance ID; methods refactored for reactive lock details retrieval.
app/server/reactive-caching/src/main/java/com/appsmith/caching/components/InstanceIdProvider.java New interface added to provide instance ID reactively.
app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/InstanceIdProviderImpl.java New implementation of InstanceIdProvider using ConfigService.
app/server/appsmith-server/src/test/java/com/appsmith/server/aspect/DistributedLockAspectTest.java Tests updated to mock InstanceIdProvider and use instance ID in lock keys; helper method added for key formatting.
app/server/appsmith-server/src/test/java/com/appsmith/server/aspect/TestLockService.java releaseLock method updated to include instanceId parameter and adjust Redis key accordingly.
app/server/reactive-caching/src/test/java/com/appsmith/testcaching/TestInstanceIdProvider.java New test implementation of InstanceIdProvider returning fixed instance ID.
app/server/reactive-caching/src/test/java/com/appsmith/testcaching/TestConfig.java New test configuration class providing InstanceIdProvider bean.
app/server/reactive-caching/src/test/java/com/appsmith/testcaching/test/TestCachingMethods.java Added @ContextConfiguration to specify TestConfig for test context setup.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant DistributedLockAspect
    participant InstanceIdProvider
    participant Redis

    Caller->>DistributedLockAspect: Request lock (handleMono/handleFlux/handleBlocking)
    DistributedLockAspect->>InstanceIdProvider: getInstanceId()
    InstanceIdProvider-->>DistributedLockAspect: instanceId (or "unknown")
    DistributedLockAspect->>DistributedLockAspect: createLockDetails(lock, instanceId)
    DistributedLockAspect->>Redis: Attempt to acquire lock with key "lock:<instanceId>:<lockKey>"
    Redis-->>DistributedLockAspect: Lock acquired/rejected
    DistributedLockAspect-->>Caller: Result (proceed or error)
Loading

Poem

🔐 Locks now wear a brand new face,
With instance IDs in their place.
Async calls fetch keys so fine,
Making Redis keys align.
Tests updated, code refined,
Distributed locks now well-defined.
Cheers to locks that know their space! 🎉

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

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.

@github-actions github-actions bot added the Enhancement New feature or request label Jun 17, 2025
Copy link
Contributor

@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.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2dfe9d2 and 36dbdb6.

📒 Files selected for processing (1)
  • app/server/reactive-caching/src/main/java/com/appsmith/caching/aspects/DistributedLockAspect.java (6 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: server-spotless / spotless-check
🔇 Additional comments (4)
app/server/reactive-caching/src/main/java/com/appsmith/caching/aspects/DistributedLockAspect.java (4)

4-4: LGTM!

The ConfigService dependency is properly injected through the constructor.

Also applies to: 27-27, 31-34


63-71: Verify the lock key format with double colon.

The lock key format will be lock::{instanceId}:{key} due to the concatenation of LOCK_PREFIX ("lock:") with an additional colon. Is this intentional?


88-109: LGTM!

The reactive chain correctly handles the async lock details creation.


113-134: LGTM!

Correctly uses flatMapMany for the Mono to Flux transition.

…nceId for lock keys

- Modified DistributedLockAspect to utilize InstanceIdProvider for generating lock keys, ensuring multi-instance isolation.
- Updated test cases in DistributedLockAspectTest to reflect changes in lock key generation, improving test reliability and clarity.
- Adjusted releaseLock method in TestLockService to include instanceId in lock key deletion, enhancing lock management.
Copy link
Contributor

@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

🧹 Nitpick comments (2)
app/server/appsmith-server/src/test/java/com/appsmith/server/aspect/DistributedLockAspectTest.java (2)

32-40: DRY up common test utilities

LOCK_PREFIX, TEST_INSTANCE_ID, and getLockKey(...) are great, but every test still has to remember to stub instanceIdProvider. Moving the stub and these constants into a @BeforeEach (or a nested static util class) would reduce duplication and the chance of forgetting the stub in future tests.

@BeforeEach
void setUp() {
    when(instanceIdProvider.getInstanceId()).thenReturn(Mono.just(TEST_INSTANCE_ID));
}

All individual when(...).thenReturn(...) calls below can then be removed.


145-146: Order of parameters is non-intuitive

releaseLock("persistent-lock", redisOperations, TEST_INSTANCE_ID) passes the Redis client before the instanceId, whereas most APIs put context (instanceId) before infrastructure (redis). Swapping them or using named builders would improve readability, but at minimum document the order to avoid accidental misuse.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 36dbdb6 and d07b317.

📒 Files selected for processing (5)
  • app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/InstanceIdProviderImpl.java (1 hunks)
  • app/server/appsmith-server/src/test/java/com/appsmith/server/aspect/DistributedLockAspectTest.java (6 hunks)
  • app/server/appsmith-server/src/test/java/com/appsmith/server/aspect/TestLockService.java (1 hunks)
  • app/server/reactive-caching/src/main/java/com/appsmith/caching/aspects/DistributedLockAspect.java (6 hunks)
  • app/server/reactive-caching/src/main/java/com/appsmith/caching/components/InstanceIdProvider.java (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • app/server/reactive-caching/src/main/java/com/appsmith/caching/components/InstanceIdProvider.java
  • app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/InstanceIdProviderImpl.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/server/reactive-caching/src/main/java/com/appsmith/caching/aspects/DistributedLockAspect.java
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: server-unit-tests / server-unit-tests
  • GitHub Check: server-spotless / spotless-check
🔇 Additional comments (1)
app/server/appsmith-server/src/test/java/com/appsmith/server/aspect/DistributedLockAspectTest.java (1)

38-40: Delimiter differs from PR description – verify once

The helper builds keys as lock:<instanceId>:<key> (single colon after lock).
The PR description mentions lock::<instanceId>:<key> (double colon). Please confirm the delimiter is consistent across production code, docs, and tests to avoid elusive bugs in multi-instance deployments.

abhvsn and others added 4 commits June 18, 2025 15:57
…hub.com:appsmithorg/appsmith into chore/add-instance-id-for-distributed-lock-keys
- Introduced TestConfig class to provide a mock InstanceIdProvider for testing purposes.
- Created TestInstanceIdProvider class to return a static instance ID, facilitating unit tests for caching components.
@appsmithorg appsmithorg deleted a comment from github-actions bot Jun 18, 2025
Copy link
Contributor

@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.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d07b317 and 06c6f14.

📒 Files selected for processing (3)
  • app/server/reactive-caching/src/test/java/com/appsmith/testcaching/TestConfig.java (1 hunks)
  • app/server/reactive-caching/src/test/java/com/appsmith/testcaching/TestInstanceIdProvider.java (1 hunks)
  • app/server/reactive-caching/src/test/java/com/appsmith/testcaching/test/TestCachingMethods.java (3 hunks)
✅ Files skipped from review due to trivial changes (2)
  • app/server/reactive-caching/src/test/java/com/appsmith/testcaching/test/TestCachingMethods.java
  • app/server/reactive-caching/src/test/java/com/appsmith/testcaching/TestInstanceIdProvider.java
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: server-unit-tests / server-unit-tests
  • GitHub Check: server-spotless / spotless-check

@appsmithorg appsmithorg deleted a comment from github-actions bot Jun 18, 2025
@abhvsn abhvsn requested a review from trishaanand June 18, 2025 12:09
@abhvsn abhvsn added the ok-to-test Required label for CI label Jun 18, 2025
@abhvsn abhvsn merged commit 24ec795 into release Jun 19, 2025
73 of 77 checks passed
@abhvsn abhvsn deleted the chore/add-instance-id-for-distributed-lock-keys branch June 19, 2025 06:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request ok-to-test Required label for CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants