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: add retry max retry mechanism for backup creation #3442

Merged
merged 1 commit into from
Jan 10, 2025

Conversation

PhanLe1010
Copy link
Contributor

Prevent the backup CR from being stuck in the pending state forever. Backup creation will only be retried up until max retry

longhorn/longhorn#10090

Copy link

coderabbitai bot commented Jan 10, 2025

Warning

Rate limit exceeded

@PhanLe1010 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 7 minutes and 40 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 157e7e8 and 91991e2.

📒 Files selected for processing (3)
  • controller/backup_controller.go (9 hunks)
  • util/timed_counter.go (1 hunks)
  • util/timed_counter_test.go (1 hunks)

Walkthrough

This pull request introduces a new utility TimedCounter in the util package and updates the BackupController to use this counter for tracking retry attempts during backup creation. The TimedCounter provides a thread-safe mechanism for counting and managing time-based entries, with built-in garbage collection. The changes enhance error handling and state management in backup operations by implementing a more robust retry mechanism that can track and limit the number of retry attempts.

Changes

File Change Summary
controller/backup_controller.go - Added creationRetryCounter field of type *util.TimedCounter
- Enhanced checkMonitor method with retry tracking
- Updated Run method to include garbage collection for retry counter
util/timed_counter.go - New file implementing TimedCounter struct
- Added methods for count management, entry tracking, and garbage collection
- Supports thread-safe operations with mutex
util/timed_counter_test.go - Added comprehensive unit tests for TimedCounter
- Tested count retrieval, entry deletion, and garbage collection mechanisms

Sequence Diagram

sequenceDiagram
    participant BC as BackupController
    participant TC as TimedCounter
    participant BM as Backup Monitor

    BC->>TC: Initialize creationRetryCounter
    BC->>BM: Check Engine Readiness
    alt Engine Not Ready
        BC->>TC: Increase Retry Count
        BC->>BM: Retry Check
        TC->>BC: Track Retry Attempts
    end
    alt Max Retries Exceeded
        BC->>BC: Set Backup State to Error
    end
    BC->>TC: Periodic Garbage Collection
Loading

Possibly related PRs

Suggested reviewers

  • innobead
  • c3y1huang
  • derekbit
  • ChanYiLin

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. (Beta)
  • @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.

@derekbit
Copy link
Member

@mergify backport v1.8.x v1.7.x

Copy link

mergify bot commented Jan 10, 2025

backport v1.8.x v1.7.x

✅ Backports have been created

@PhanLe1010 PhanLe1010 force-pushed the 10090 branch 2 times, most recently from 22935f8 to 157e7e8 Compare January 10, 2025 03:28
@PhanLe1010 PhanLe1010 marked this pull request as ready for review January 10, 2025 03:56
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.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6ab8076 and 157e7e8.

📒 Files selected for processing (3)
  • controller/backup_controller.go (7 hunks)
  • util/timed_counter.go (1 hunks)
  • util/timed_counter_test.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Summary
🔇 Additional comments (4)
util/timed_counter.go (1)

1-88: Implementation of TimedCounter is correct and thread-safe.

The TimedCounter provides a thread-safe mechanism for counting and managing time-based entries. All methods correctly use synchronization primitives to ensure safe concurrent access. Initialization, incrementing, retrieval, deletion, and garbage collection of entries are properly handled.

util/timed_counter_test.go (1)

1-72: Unit tests effectively cover TimedCounter functionality.

The unit tests validate key functionalities of the TimedCounter, including count increment, deletion of entries, manual garbage collection, and automatic garbage collection via RunGC. This ensures robustness and correctness of the implementation.

controller/backup_controller.go (2)

891-892: Ensure cleanup of retry counters after successful backup creation.

After successfully creating the backup monitor, the creationRetryCounter entry for the backup is deleted:

// backup creation is succeeded, remove it from the counter
bc.creationRetryCounter.DeleteEntry(backup.Name)

This is correct to prevent stale entries from persisting in the counter.


44-46: Consistent definition of error messages and constants.

The new error messages and constants are well-defined and provide clear descriptions for failures related to engine and snapshot readiness:

  • FailedWaitingForSnapshotMessage
  • FailedWaitingForEngineMessage
  • creationRetryCounterExpiredDuration
  • creationRetryCounterGCDuration
  • maxCreationRetry

Also applies to: 56-59

Comment on lines 825 to 837
if bc.creationRetryCounter.GetCount(backup.Name) >= maxCreationRetry {
backup.Status.Error = fmt.Sprintf(FailedWaitingForEngineMessage, engine.Name)
backup.Status.State = longhorn.BackupStateError
backup.Status.LastSyncedAt = metav1.Time{Time: time.Now().UTC()}
bc.creationRetryCounter.DeleteEntry(backup.Name)
return nil, fmt.Errorf("failed waiting for the engine %v to be running before enabling backup monitor", engine.Name)
}
backup.Status.State = longhorn.BackupStatePending
backup.Status.Messages[MessageTypeReconcileInfo] = fmt.Sprintf(WaitForEngineMessage, engine.Name)
bc.creationRetryCounter.IncreaseCount(backup.Name)
return nil, fmt.Errorf("waiting for the engine %v to be running before enabling backup monitor", engine.Name)
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Shared retry counter may conflate retries for different conditions.

The creationRetryCounter is used to track retries for multiple conditions in the checkMonitor function, specifically:

  • Waiting for the engine to be ready (lines 825-835).
  • Waiting to retrieve the snapshot (lines 841-854).
  • Waiting for the snapshot to be ready (lines 855-869).

Using a single counter for different conditions can lead to unintended behavior where retries for one condition consume the retry counts for another. This may cause the backup process to fail prematurely if one condition incurs more retries, affecting the other conditions' ability to retry adequately.

Consider using separate counters for each condition to independently track retries. This ensures that the retry mechanisms for waiting for the engine, retrieving the snapshot, and waiting for the snapshot to be ready are functioning correctly without impacting each other. For example, introduce individual TimedCounter instances or maintain a map of counters keyed by condition and backup name.

Also applies to: 841-854, 855-869

Prevent the backup CR from being stuck in the pending state forever.
Backup creation will only be retried up until max retry

longhorn-10090

Signed-off-by: Phan Le <[email protected]>
Copy link
Member

@derekbit derekbit left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks @PhanLe1010

@derekbit derekbit merged commit 63a01a7 into longhorn:master Jan 10, 2025
8 of 9 checks passed
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.

3 participants