Repository navigation
fix: paginate recalculation job - #2877
Conversation
📝 WalkthroughWalkthroughThe internal implementation of the Changes
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (1.64.8)Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (12)
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
openmeter/entitlement/balanceworker/recalculate.go (1)
129-161: Pagination implementation is correct but consider memory usage implications.The pagination logic is well-implemented with proper break conditions and error handling. However, there are a few considerations:
- Memory usage: This still loads all entitlements into memory at once, just in chunks. For very large namespaces, this could be a concern.
- Concurrent modifications: If entitlements are added/removed during pagination, there could be missed or duplicate items (though this is a general pagination limitation).
The implementation correctly:
- Initializes an empty accumulator slice
- Uses proper pagination parameters
- Has two break conditions to prevent infinite loops
- Preserves error handling
- Accumulates results before processing
For future enhancement, consider processing entitlements as they're fetched rather than accumulating them all in memory:
- affectedEntitlements := []entitlement.Entitlement{} - - page := 1 - - for { - affectedEntitlementsPage, err := r.opts.Entitlement.EntitlementRepo.ListEntitlements( - ctx, - entitlement.ListEntitlementsParams{ - Namespaces: []string{ns}, - IncludeDeleted: true, - IncludeDeletedAfter: time.Now().Add(-DefaultIncludeDeletedDuration), - Page: pagination.Page{ - PageNumber: page, - PageSize: defaultPageSize, - }, - }) - if err != nil { - return err - } - - if len(affectedEntitlementsPage.Items) == 0 { - break - } - - affectedEntitlements = append(affectedEntitlements, affectedEntitlementsPage.Items...) - - if len(affectedEntitlements) >= affectedEntitlementsPage.TotalCount { - break - } - - page++ - } - - return r.processEntitlements(ctx, affectedEntitlements) + page := 1 + var allErrors error + + for { + affectedEntitlementsPage, err := r.opts.Entitlement.EntitlementRepo.ListEntitlements( + ctx, + entitlement.ListEntitlementsParams{ + Namespaces: []string{ns}, + IncludeDeleted: true, + IncludeDeletedAfter: time.Now().Add(-DefaultIncludeDeletedDuration), + Page: pagination.Page{ + PageNumber: page, + PageSize: defaultPageSize, + }, + }) + if err != nil { + return err + } + + if len(affectedEntitlementsPage.Items) == 0 { + break + } + + // Process this page immediately to reduce memory usage + if err := r.processEntitlements(ctx, affectedEntitlementsPage.Items); err != nil { + allErrors = errors.Join(allErrors, err) + } + + if len(affectedEntitlementsPage.Items) < defaultPageSize { + break // Last page + } + + page++ + } + + return allErrors
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
openmeter/entitlement/balanceworker/recalculate.go(3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (9)
- GitHub Check: E2E
- GitHub Check: CI
- GitHub Check: Quickstart
- GitHub Check: Lint
- GitHub Check: Developer environment
- GitHub Check: Commit hooks
- GitHub Check: Test
- GitHub Check: Build
- GitHub Check: Analyze (go)
🔇 Additional comments (4)
openmeter/entitlement/balanceworker/recalculate.go (4)
23-23: Import addition looks good.The pagination import is necessary for the new pagination functionality.
32-32: Page size constant is appropriate.The 20,000 page size is a reasonable choice that stays well under the 64,000 parameter limit while minimizing the number of database calls.
126-129: Clear documentation of the fix rationale.The comment effectively explains why pagination is needed and acknowledges that the root cause should ideally be fixed in ent itself.
163-163: Method call updated correctly.The call to
processEntitlementswith the accumulated entitlements maintains the existing processing logic.
Overview
If a namespace has more than 64k entitlements, the edge expansion fails due to the parameter count being more than the 64k allowed by ent.
Summary by CodeRabbit