Skip to content

Conversation

@vpapidokha
Copy link
Collaborator

@vpapidokha vpapidokha commented Apr 17, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Corrected formatting in the documentation for the "Keep Alive Lease" endpoint.
  • Refactor

    • Updated lease presence checks to return the actual lease ID instead of a boolean indicator.
    • Adjusted related logic and tests to accommodate the new lease ID-based approach.
    • Improved debug logging for lease caching operations.
  • Tests

    • Updated test cases and mocks to reflect the change from boolean presence checks to lease ID returns.

@coderabbitai
Copy link

coderabbitai bot commented Apr 17, 2025

"""

Walkthrough

This set of changes refactors the lease presence checking mechanism throughout the application. The CheckLeasePresence method in the storage layer and all its implementations now return an integer lease ID instead of a boolean presence flag. Corresponding updates are made in the application logic, command handlers, mocks, and tests to handle and propagate lease IDs. The lease cache logic is adjusted to only add leases when a new lease is created, and test setups are modified to reflect the new lease ID-based presence checks. Documentation is also updated for minor formatting corrections.

Changes

File(s) Change Summary
internal/infrastructure/storage/storage.go Changed CheckLeasePresence interface method to return (int64, error) instead of (bool, error).
internal/infrastructure/storage/etcd/etcd.go Updated CheckLeasePresence to return lease ID (int64) or zero, instead of a boolean; logic adapted accordingly.
internal/infrastructure/storage/mock/mock.go Changed ExistingLeases map to store lease IDs; updated CheckLeasePresence and CreateLease to use lease IDs.
internal/application/command/leasemanagement/leaseManagement.go Replaced boolean lease presence check with lease ID; logic now propagates lease IDs instead of presence flags.
internal/application/command/leasemanagement/leasemanagement_test.go Updated mocks and tests to use lease IDs instead of booleans for lease presence; method signatures and expectations adjusted.
internal/application/application.go Deferred metric increment wrapped in a function; only adds to cache if status is StatusCreated; logs cache additions.
internal/application/application_test.go Test setup for existing leases changed to use lease cache with lease ID; expectations updated for new lease ID logic.
README.md Removed a trailing backslash in markdown for formatting correction.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Application
    participant Storage

    Client->>Application: CreateLease(key, ttl)
    Application->>Storage: CheckLeasePresence(key)
    Storage-->>Application: leaseID (0 if not present)
    alt Lease exists (leaseID != 0)
        Application-->>Client: Return "accepted", leaseID
    else Lease does not exist
        Application->>Storage: CreateLease(key, ttl)
        Storage-->>Application: leaseID, status
        Application->>Application: Add to lease cache if status is StatusCreated
        Application-->>Client: Return status, leaseID
    end
Loading

Possibly related PRs

  • tentens-tech/shared-lock#7: Refactors CreateLease and lease presence handling, closely related to the lease ID propagation in this PR.
  • tentens-tech/shared-lock#8: Modifies CreateLease and cache logic, similar to the cache handling and lease ID logic changes in this PR.

Poem

In the warren where leases are found,
We swapped out a flag for a number profound.
Now lease IDs hop, no more boolean disguise,
Through cache and through storage, with fewer surprise.
A tidy-up here, a markdown fix there—
This rabbit’s code garden is tended with care!
🐇✨
"""

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4c9430f and e521bac.

📒 Files selected for processing (1)
  • internal/infrastructure/storage/mock/mock.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/infrastructure/storage/mock/mock.go
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build-and-push

🪧 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.
  • @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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 41bb16b and 4c9430f.

📒 Files selected for processing (8)
  • README.md (1 hunks)
  • internal/application/application.go (2 hunks)
  • internal/application/application_test.go (2 hunks)
  • internal/application/command/leasemanagement/leaseManagement.go (1 hunks)
  • internal/application/command/leasemanagement/leasemanagement_test.go (8 hunks)
  • internal/infrastructure/storage/etcd/etcd.go (1 hunks)
  • internal/infrastructure/storage/mock/mock.go (1 hunks)
  • internal/infrastructure/storage/storage.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
internal/application/application.go (2)
internal/infrastructure/metrics/metrics.go (2)
  • LeaseOperations (13-19)
  • LeaseOperationGet (9-9)
internal/infrastructure/storage/storage.go (1)
  • StatusCreated (9-9)
internal/application/application_test.go (2)
internal/infrastructure/cache/cache.go (1)
  • LeaseCacheRecord (29-32)
internal/infrastructure/storage/storage.go (1)
  • StatusCreated (9-9)
internal/infrastructure/storage/mock/mock.go (2)
internal/infrastructure/storage/storage.go (3)
  • Storage (12-16)
  • StatusAccepted (8-8)
  • StatusCreated (9-9)
internal/application/command/leasemanagement/leaseManagement.go (2)
  • DefaultPrefix (14-14)
  • CreateLease (17-46)
🔇 Additional comments (15)
README.md (1)

78-78: Correct minor markdown formatting for the "Keep Alive Lease" section.

The trailing backslash has been removed so that the - **Responses**: heading renders correctly in Markdown.

internal/application/application.go (3)

35-37: Improved metric collection with deferred anonymous function

The change to use a deferred anonymous function ensures metrics correctly capture the final leaseStatus value, even in error paths. This provides more accurate operation metrics compared to directly calling Inc().


46-46: Return actual leaseID instead of zero on error

Good improvement to return the actual leaseID rather than hardcoded zero when an error occurs. This provides more accurate information to callers about which lease encountered the error.


49-52: Cache optimization with conditional addition

The optimization to only cache leases with StatusCreated status is a good improvement. This prevents unnecessary cache entries for leases that didn't complete creation successfully. The added debug log also improves observability.

internal/application/application_test.go (2)

55-55: Standardized expectedID value

The change to use 123 as the expected ID in this test case makes it consistent with the ID used in the cache setup below.


68-71: Improved test setup using lease cache

This change properly simulates a cache hit for an existing lease, which better reflects the actual application behavior. The test now directly populates the cache with a lease record containing the same ID expected in the test assertion.

internal/application/command/leasemanagement/leaseManagement.go (2)

25-25: Improved lease presence check with direct ID return

Good refactoring to get the lease ID directly rather than a boolean flag. This provides more information and avoids additional lookups when the ID is needed.


29-30: Better conditional logic using lease ID

The change from a boolean check to leaseID != 0 is more idiomatic and directly returns the found lease ID instead of zero. This makes the code more consistent and simplifies lease tracking.

internal/infrastructure/storage/storage.go (1)

13-13: Improved Storage interface with direct lease ID return

This is an excellent architectural improvement to the CheckLeasePresence method. Returning the actual lease ID instead of a boolean flag:

  1. Provides more information to callers
  2. Eliminates the need for separate lookups when the ID is needed
  3. Makes the API more consistent with the rest of the storage operations

This change drives all the other improvements throughout the codebase.

internal/infrastructure/storage/etcd/etcd.go (1)

68-85: Return type change for CheckLeasePresence appears correct

The method has been refactored to return the actual leaseID (int64) instead of a boolean presence flag. This change provides more meaningful information to callers while maintaining the same error handling pattern. The implementation correctly initializes leaseID to 0 (Go's zero value) and extracts the lease ID from the etcd response when the key exists.

internal/application/command/leasemanagement/leasemanagement_test.go (3)

13-24: Mock interface correctly updated for new return type

The MockStorage struct and its CheckLeasePresence method have been properly updated to return (int64, error) instead of (bool, error). The default return value of 0 (instead of false) is appropriate as the zero value for an int64 type.


40-68: Test cases correctly adapted for lease ID return value

The test cases have been appropriately updated to use checkLeaseID instead of a boolean presence flag. The test for "Lease already exists" now checks for a specific lease ID (456) rather than a boolean, which provides more precise test coverage.


134-146: Mock function implementation correctly updated

The mock function implementation has been properly updated to return the lease ID value, maintaining the test's behavioral expectations.

internal/infrastructure/storage/mock/mock.go (2)

13-22: Mock storage data structure properly updated

The ExistingLeases map type has been correctly changed from map[string]bool to map[string]int64 to store lease IDs instead of boolean presence flags, which aligns with the interface changes.


24-32: CheckLeasePresence method correctly returns lease ID

The implementation now returns the actual lease ID if it exists in the map, otherwise it returns 0. This aligns with the updated interface and the etcd implementation.

@vpapidokha vpapidokha merged commit 165c341 into main Apr 17, 2025
3 checks passed
@vpapidokha vpapidokha deleted the cache-bug-fix branch April 17, 2025 14:28
@coderabbitai coderabbitai bot mentioned this pull request Apr 17, 2025
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