Repository navigation
fix: snapshot invoice quantities in parallel - #3034
Conversation
📝 WalkthroughWalkthroughThis change introduces a new configuration parameter, Changes
Possibly related PRs
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 (17)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (16)
⏰ Context from checks skipped due to timeout of 90000ms (9)
✨ 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 (
|
5f69ded to
f6af73b
Compare
There was a problem hiding this comment.
Actionable comments posted: 0
🔭 Outside diff range comments (1)
app/config/billing.go (1)
19-34: Add validation for MaxParallelQuantitySnapshots.The new configuration field should be validated to ensure it has a reasonable value.
func (c BillingConfiguration) Validate() error { var errs []error + + if c.MaxParallelQuantitySnapshots <= 0 { + errs = append(errs, errors.New("MaxParallelQuantitySnapshots must be greater than 0")) + } + if err := c.Worker.Validate(); err != nil { errs = append(errs, err) }
🧹 Nitpick comments (2)
openmeter/billing/service/service.go (1)
92-95: Consider adding an upper bound validation.While validating that
MaxParallelQuantitySnapshotsis greater than zero is good, consider adding an upper bound (e.g., 100) to prevent potential resource exhaustion from extremely high concurrency values.if c.MaxParallelQuantitySnapshots < 1 { return errors.New("max parallel snapshots must be greater than 0") } +if c.MaxParallelQuantitySnapshots > 100 { + return errors.New("max parallel snapshots must not exceed 100") +}openmeter/billing/service/invoiceline.go (1)
299-348: Well-implemented parallel processing with comprehensive error handling.The parallel implementation includes several good practices:
- Proper context cancellation handling
- Panic recovery in goroutines
- Comprehensive error collection and joining
- Appropriate channel management and synchronization
However, there's one potential improvement for robustness:
Consider adding validation for the
maxParallelQuantitySnapshotsconfiguration to ensure it's greater than zero:+ if s.maxParallelQuantitySnapshots <= 0 { + return fmt.Errorf("maxParallelQuantitySnapshots must be greater than zero") + }This would prevent potential issues if misconfigured.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (17)
app/common/billing.go(1 hunks)app/config/billing.go(2 hunks)app/config/config_test.go(1 hunks)cmd/billing-worker/wire_gen.go(1 hunks)cmd/jobs/internal/wire_gen.go(1 hunks)cmd/server/wire_gen.go(1 hunks)openmeter/billing/service/invoice.go(2 hunks)openmeter/billing/service/invoiceline.go(4 hunks)openmeter/billing/service/invoicestate.go(1 hunks)openmeter/billing/service/lineservice/feeline.go(1 hunks)openmeter/billing/service/lineservice/service.go(1 hunks)openmeter/billing/service/lineservice/usagebasedline.go(2 hunks)openmeter/billing/service/lineservice/usagebasedlineflat.go(1 hunks)openmeter/billing/service/service.go(3 hunks)test/app/testenv.go(1 hunks)test/billing/suite.go(1 hunks)test/subscription/framework_test.go(1 hunks)
🔇 Additional comments (22)
app/config/billing.go (2)
14-14: LGTM: New configuration field added correctly.The
MaxParallelQuantitySnapshotsfield is properly integrated into the configuration struct.
53-53: LGTM: Appropriate default value set.The default value of 4 for parallel quantity snapshots seems reasonable for performance optimization without overwhelming the system.
app/config/config_test.go (1)
144-145: LGTM: Test configuration updated correctly.The test configuration properly includes the new
MaxParallelQuantitySnapshotsfield with the correct default value of 4.test/billing/suite.go (1)
186-186: LGTM: Test uses appropriate concurrency level.Setting
MaxParallelQuantitySnapshotsto 2 in tests is reasonable to avoid resource contention while still testing the parallel functionality.test/app/testenv.go (1)
283-283: LGTM: Consistent test configuration.The test environment correctly includes the new configuration parameter with a value of 2, consistent with other test files.
test/subscription/framework_test.go (1)
93-93: LGTM: Subscription test configuration updated correctly.The subscription framework test properly includes the new
MaxParallelQuantitySnapshotsparameter with a value of 2, maintaining consistency with other test configurations.cmd/jobs/internal/wire_gen.go (1)
289-289: LGTM! Configuration parameter update aligns with parallel snapshotting feature.The change correctly passes the full
billingConfigurationto enable access to the newMaxParallelQuantitySnapshotsfield instead of just the advancement strategy.cmd/server/wire_gen.go (1)
283-283: LGTM! Consistent configuration parameter update.The change aligns with the parallel snapshotting refactor by passing the complete billing configuration.
openmeter/billing/service/lineservice/feeline.go (1)
31-31: LGTM! Method signature updated for parallel snapshotting.The parameter change from
*billing.Invoiceto[]stringcorrectly implements the interface update to support subject-key-based parallel quantity snapshotting.openmeter/billing/service/lineservice/usagebasedlineflat.go (1)
91-91: LGTM! Consistent method signature update for parallel processing.The parameter change from
*billing.Invoiceto[]stringaligns with the parallel snapshotting refactor while maintaining existing functionality.cmd/billing-worker/wire_gen.go (1)
281-281: LGTM! Completes the consistent configuration refactor across wire files.The change correctly passes the full billing configuration to support the new parallel snapshotting feature.
openmeter/billing/service/lineservice/service.go (1)
216-216: LGTM: Interface refactored to support parallel processing.The method signature change from accepting the full invoice to just subject keys is well-designed. This reduces coupling and enables efficient parallel processing where each line service only needs the relevant subject keys for snapshotting.
openmeter/billing/service/lineservice/usagebasedline.go (1)
149-173: LGTM: Implementation correctly updated to match interface change.The method implementation properly follows the new interface signature, directly using the provided subject keys instead of extracting them from the invoice object. The core snapshotting logic remains intact and correct.
openmeter/billing/service/service.go (1)
38-38: LGTM: Configuration field properly integrated.The
maxParallelQuantitySnapshotsfield is correctly added to both Service and Config structs with proper initialization in the constructor.Also applies to: 52-52, 115-115
openmeter/billing/service/invoice.go (3)
143-146: LGTM: Customer profile expansion properly configured.The customer profile is correctly fetched with expanded customer data to ensure
UsageAttribution.SubjectKeysare available for the parallel snapshotting operation.
178-178: LGTM: Timestamp correctly updated after successful snapshot.The
QuantitySnapshotedAttimestamp is properly set after the parallel snapshotting operation completes successfully.
160-162: ```shell
#!/bin/bashLocate the Service struct definition across the repo
rg -n "type Service struct" -A20 .
Find where maxParallelQuantitySnapshots is declared and its type
rg -n "maxParallelQuantitySnapshots" -C3 .
</details> <details> <summary>openmeter/billing/service/invoicestate.go (1)</summary> `725-738`: **LGTM: State machine properly updated for parallel snapshotting.** The `snapshotQuantityAsNeeded` method has been correctly refactored to: 1. Convert invoice lines to line services 2. Use the new parallel snapshotting approach with customer subject keys 3. Update the timestamp after successful completion This change is consistent with the overall refactoring and maintains the same logical flow. </details> <details> <summary>openmeter/billing/service/invoiceline.go (2)</summary> `289-291`: **LGTM! Parallel processing implementation looks correct.** The refactoring to use parallel snapshotting is well-implemented and should provide the performance improvements mentioned in the PR objectives. --- `374-374`: **Interface change correctly implemented.** The change from passing the entire invoice to just the subject keys is a good refactoring that reduces coupling and makes the interface cleaner. </details> <details> <summary>app/common/billing.go (2)</summary> `51-51`: **Configuration parameter change is well-structured.** The change from passing just the advancement strategy to the full billing configuration provides better flexibility and maintains clean separation of concerns. --- `65-67`: **Service initialization correctly updated.** The initialization properly extracts both the advancement strategy and the new parallel snapshots configuration from the billing config, maintaining backward compatibility while adding the new feature. </details> </blockquote></details> </details> <!-- This is an auto-generated comment by CodeRabbit for review status -->
Given for long sending invoices we can have quite slow queries, let's add an option to parallely fetch the invoice quantities. This will speed up invoice creation and gathering invoice listing in such cases.
f6af73b to
71847f4
Compare
Overview
Given invoices with a lot of events we can have quite slow queries, let's add an option to fetch the invoice quantities parallel. This will speed up invoice creation and gathering the invoice listing in such cases.
Summary by CodeRabbit
New Features
Refactor
Tests