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(upgrades): upgrades when creating a new ledger on an already migration bucket #602

Merged
merged 1 commit into from
Dec 5, 2024

Conversation

gfyrag
Copy link
Contributor

@gfyrag gfyrag commented Dec 5, 2024

No description provided.

@gfyrag gfyrag requested a review from a team as a code owner December 5, 2024 17:11
Copy link

coderabbitai bot commented Dec 5, 2024

Walkthrough

The changes in this pull request encompass extensive documentation updates in internal/README.md, primarily focusing on formatting and the removal of hyperlinks from type and function declarations. Additionally, the error handling in createLedger has been enhanced to include a new error condition for outdated buckets. New methods for checking bucket initialization have been introduced in the Bucket interface and its implementations. Furthermore, the logic for ledger creation and retrieval has been refined in the root.go file, improving error handling and control flow.

Changes

File Path Change Summary
internal/README.md Updated formatting and structure; removed hyperlinks from type/function declarations.
internal/api/v2/controllers_ledgers_create.go Added error handling for system.ErrBucketOutdated in createLedger function.
internal/api/v2/controllers_ledgers_create_test.go Introduced a new test case for handling ErrBucketOutdated in TestLedgersCreate.
internal/controller/system/errors.go Added new error variable ErrBucketOutdated.
internal/storage/bucket/bucket.go Added method IsInitialized(ctx context.Context) (bool, error) to Bucket interface.
internal/storage/bucket/default_bucket.go Added method func (b *DefaultBucket) IsInitialized(ctx context.Context) (bool, error).
internal/storage/driver/buckets_generated_test.go Added method func (m *MockBucket) IsInitialized(arg0 context.Context) (bool, error) to MockBucket.
internal/storage/driver/driver.go Enhanced CreateLedger method with checks for bucket initialization and versioning.
internal/storage/driver/driver_test.go Updated TestLedgersCreate to include expectation for bucket initialization check.
tools/generator/cmd/root.go Modified logic for ledger creation and retrieval, refined error handling.
docs/api/README.md Updated API documentation with new endpoints, response schemas, and detailed descriptions for clarity.
internal/api/common/errors.go Added new constant ErrBucketOutdated with value "BUCKET_OUTDATED".

Suggested reviewers

  • flemzord
  • paul-nicolas
  • Dav-14

🐇 In the meadow, I hop and play,
With changes brightening up the day.
Documentation neat, errors now clear,
Buckets initialized, let’s all cheer!
A ledger’s path, we’ve paved anew,
Hopping with joy, I share this view! 🐰✨


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 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.

Comment on lines 36 to 38
case errors.Is(err, system.ErrInvalidLedgerConfiguration{}) ||
errors.Is(err, ledger.ErrInvalidLedgerName{}) ||
errors.Is(err, system.ErrBucketUpgrading) ||
Copy link
Contributor

Choose a reason for hiding this comment

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

you can put case toto, tata: instead of ||

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All switch at api level use the same pattern, I will change later, but not in this PR.

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

🧹 Outside diff range and nitpick comments (2)
tools/generator/cmd/root.go (1)

158-170: Consider restructuring error handling for better clarity.

The nested error handling structure could be simplified for better readability and maintainability.

Consider this alternative structure:

-		if err != nil {
-			if !errors.As(err, &sdkError) || (sdkError.ErrorCode != components.V2ErrorsEnumLedgerAlreadyExists &&
-				sdkError.ErrorCode != components.V2ErrorsEnumValidation) {
-				return fmt.Errorf("failed to create ledger: %w", err)
-			}
-		}
+		if err == nil {
+			return nil
+		}
+		var sdkError *sdkerrors.V2ErrorResponse
+		if !errors.As(err, &sdkError) {
+			return fmt.Errorf("failed to create ledger: %w", err)
+		}
+		if sdkError.ErrorCode != components.V2ErrorsEnumLedgerAlreadyExists &&
+			sdkError.ErrorCode != components.V2ErrorsEnumValidation {
+			return fmt.Errorf("failed to create ledger: %w", err)
+		}
internal/storage/driver/driver.go (1)

51-60: Consider adding debug logs for bucket state

The bucket state checks are critical for understanding system behavior. Consider adding debug logs to help with troubleshooting.

 if isInitialized {
+    logging.FromContext(ctx).Debugf("Bucket %s is initialized, checking if up to date", l.Bucket)
     upToDate, err := b.IsUpToDate(ctx)
     if err != nil {
         return nil, fmt.Errorf("checking if bucket is up to date: %w", err)
     }

     if !upToDate {
+        logging.FromContext(ctx).Debugf("Bucket %s is not up to date, returning ErrBucketUpgrading", l.Bucket)
         return nil, systemcontroller.ErrBucketUpgrading
     }
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between b69289b and 079c13e.

📒 Files selected for processing (10)
  • internal/README.md (98 hunks)
  • internal/api/v2/controllers_ledgers_create.go (1 hunks)
  • internal/api/v2/controllers_ledgers_create_test.go (1 hunks)
  • internal/controller/system/errors.go (1 hunks)
  • internal/storage/bucket/bucket.go (1 hunks)
  • internal/storage/bucket/default_bucket.go (2 hunks)
  • internal/storage/driver/buckets_generated_test.go (1 hunks)
  • internal/storage/driver/driver.go (1 hunks)
  • internal/storage/driver/driver_test.go (1 hunks)
  • tools/generator/cmd/root.go (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • internal/README.md
🔇 Additional comments (9)
internal/controller/system/errors.go (1)

10-10: LGTM: Error definition is clear and well-placed.

The new error variable ErrBucketUpgrading is appropriately defined and follows the existing error declaration pattern.

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

18-18: Clarify the distinction between initialization checks.

The new IsInitialized method seems to overlap with the existing HasMinimalVersion method. Could you clarify the distinction between these methods to prevent confusion?

Let's check the implementation differences:

internal/api/v2/controllers_ledgers_create.go (1)

38-38: LGTM: Error handling is appropriate and consistent.

The addition of ErrBucketUpgrading to the validation errors group is logical, as it represents a temporary state where the request can be retried later. The BadRequest status code is appropriate for this scenario.

internal/api/v2/controllers_ledgers_create_test.go (1)

82-88: LGTM! Well-structured test case for bucket upgrade scenario.

The test case properly validates the error handling when attempting to create a ledger on an upgrading bucket. It follows the existing test pattern and has appropriate assertions.

tools/generator/cmd/root.go (1)

150-157: LGTM! Good addition of ledger existence check.

The addition of GetLedger before creation improves the robustness of the ledger creation process.

internal/storage/driver/buckets_generated_test.go (1)

85-98: LGTM! Generated mock implementation looks correct.

The generated mock implementation for IsInitialized follows the established pattern and correctly implements the interface.

internal/storage/driver/driver_test.go (1)

212-215: LGTM: Test coverage for bucket initialization check

The test correctly verifies the new initialization check before bucket migration.

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

47-50: LGTM: Proper initialization check

The code properly checks if the bucket is initialized and handles errors appropriately.

internal/storage/bucket/default_bucket.go (1)

26-35: LGTM: Clean implementation of IsInitialized

The implementation correctly handles all cases:

  • Existing version table
  • Missing version table
  • Other errors

@gfyrag gfyrag force-pushed the fix/ledger-creation-on-upgrading-bucket branch from 079c13e to a850d03 Compare December 5, 2024 17:18
@gfyrag gfyrag enabled auto-merge December 5, 2024 17:19
@gfyrag gfyrag added this pull request to the merge queue Dec 5, 2024
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

🧹 Outside diff range and nitpick comments (2)
internal/api/common/errors.go (1)

20-20: Consider adding documentation for the error constant

To improve maintainability, consider adding a comment explaining when this error occurs. This would help other developers understand the specific scenario where a bucket is considered outdated.

+// ErrBucketOutdated is returned when attempting to create a ledger on a bucket
+// that requires migration or updates to meet the current version requirements
 ErrBucketOutdated = "BUCKET_OUTDATED"
internal/storage/driver/driver.go (1)

47-60: LGTM: Good defensive checks added.

The added validation steps help prevent issues with outdated buckets by:

  1. Checking if the bucket is initialized
  2. Verifying if an initialized bucket is up to date
  3. Returning appropriate errors in each case

Consider wrapping the error messages with more context about the bucket:

-    return nil, fmt.Errorf("checking if bucket is initialized: %w", err)
+    return nil, fmt.Errorf("checking if bucket '%s' is initialized: %w", l.Bucket, err)
-    return nil, fmt.Errorf("checking if bucket is up to date: %w", err)
+    return nil, fmt.Errorf("checking if bucket '%s' is up to date: %w", l.Bucket, err)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 079c13e and a850d03.

⛔ Files ignored due to path filters (2)
  • openapi.yaml is excluded by !**/*.yaml
  • openapi/v2.yaml is excluded by !**/*.yaml
📒 Files selected for processing (12)
  • docs/api/README.md (1 hunks)
  • internal/README.md (98 hunks)
  • internal/api/common/errors.go (1 hunks)
  • internal/api/v2/controllers_ledgers_create.go (1 hunks)
  • internal/api/v2/controllers_ledgers_create_test.go (1 hunks)
  • internal/controller/system/errors.go (1 hunks)
  • internal/storage/bucket/bucket.go (1 hunks)
  • internal/storage/bucket/default_bucket.go (2 hunks)
  • internal/storage/driver/buckets_generated_test.go (1 hunks)
  • internal/storage/driver/driver.go (1 hunks)
  • internal/storage/driver/driver_test.go (1 hunks)
  • tools/generator/cmd/root.go (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • internal/README.md
🚧 Files skipped from review as they are similar to previous changes (7)
  • internal/controller/system/errors.go
  • internal/storage/bucket/bucket.go
  • internal/api/v2/controllers_ledgers_create.go
  • internal/storage/bucket/default_bucket.go
  • internal/api/v2/controllers_ledgers_create_test.go
  • tools/generator/cmd/root.go
  • internal/storage/driver/driver_test.go
👮 Files not reviewed due to content moderation or server errors (1)
  • internal/storage/driver/buckets_generated_test.go
🔇 Additional comments (2)
internal/api/common/errors.go (1)

20-20: LGTM! Error constant follows conventions

The new error constant follows the established naming patterns and is appropriately placed with other ledger-related errors.

docs/api/README.md (1)

3247-3247: LGTM: Documentation updated to reflect new error code.

The addition of BUCKET_OUTDATED to the V2ErrorsEnum aligns with the code changes that introduce bucket version validation.

Merged via the queue into main with commit af53ed5 Dec 5, 2024
8 checks passed
@gfyrag gfyrag deleted the fix/ledger-creation-on-upgrading-bucket branch December 5, 2024 17:28
This was referenced Dec 6, 2024
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