Repository navigation
refactor: high watermark cache to filter framework - #3036
Conversation
📝 WalkthroughWalkthroughThis change refactors entitlement event filtering and recalculation logic. It introduces a structured filter request for entitlement scope checks, adds a high watermark cache filter, centralizes event time validation, and updates event processing to consistently handle calculation timestamps. High watermark cache logic is removed from the worker and replaced with the new filter-based approach. 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 (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ 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 (
|
543773e to
1d766d6
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
openmeter/entitlement/balanceworker/filters.go (1)
80-82: Minor: Consider consistent struct initializationFor better readability, consider using a pointer directly:
-return EntitlementFilters{ - filters: []filters.NamedFilter{notificationFilter, highWatermarkCache}, -}.WithMetrics(cfg.MetricMeter) +filters := &EntitlementFilters{ + filters: []filters.NamedFilter{notificationFilter, highWatermarkCache}, +} +return filters.WithMetrics(cfg.MetricMeter)openmeter/entitlement/balanceworker/filters/filter.go (1)
52-56: Consider adding validation to RecordLastCalculationRequest.For consistency with
EntitlementFilterRequest, consider adding aValidate()method to ensure required fields are present.type RecordLastCalculationRequest struct { Entitlement entitlement.Entitlement CalculatedAt time.Time IsDeleted bool } + +func (r RecordLastCalculationRequest) Validate() error { + errs := []error{} + + if r.Entitlement.ID == "" { + errs = append(errs, fmt.Errorf("entitlement.id is required")) + } + + if r.Entitlement.Namespace == "" { + errs = append(errs, fmt.Errorf("entitlement.namespace is required")) + } + + if r.CalculatedAt.IsZero() { + errs = append(errs, fmt.Errorf("calculatedAt is required")) + } + + return errors.Join(errs...) +}
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (9)
cmd/jobs/entitlement/recalculatesnapshots.go(2 hunks)openmeter/entitlement/balanceworker/entitlementhandler.go(6 hunks)openmeter/entitlement/balanceworker/filters.go(5 hunks)openmeter/entitlement/balanceworker/filters/filter.go(1 hunks)openmeter/entitlement/balanceworker/filters/highwatermarkinmem.go(1 hunks)openmeter/entitlement/balanceworker/filters/notifications.go(2 hunks)openmeter/entitlement/balanceworker/ingesthandler.go(4 hunks)openmeter/entitlement/balanceworker/recalculate.go(6 hunks)openmeter/entitlement/balanceworker/worker.go(7 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (10)
- GitHub Check: Quickstart
- GitHub Check: CI
- GitHub Check: Commit hooks
- GitHub Check: E2E
- GitHub Check: Developer environment
- GitHub Check: Build
- GitHub Check: Migration Checks
- GitHub Check: Lint
- GitHub Check: Test
- GitHub Check: Analyze (go)
🔇 Additional comments (10)
cmd/jobs/entitlement/recalculatesnapshots.go (1)
4-5: LGTM!The addition of the timestamp parameter to track recalculation start time is appropriate and correctly implemented.
Also applies to: 27-27
openmeter/entitlement/balanceworker/ingesthandler.go (1)
12-12: LGTM!Consistent replacement of local
NamespacedIDtype with the sharedpkgmodels.NamespacedIDimproves code consistency across the codebase.Also applies to: 33-33, 56-56, 79-84
openmeter/entitlement/balanceworker/filters/notifications.go (1)
79-106: LGTM!The method signature update to accept
EntitlementFilterRequestaligns well with the new filter framework design, and the validation check ensures request integrity.openmeter/entitlement/balanceworker/worker.go (1)
98-98: LGTM!The removal of the high watermark cache components and consistent replacement of the local
NamespacedIDtype withpkgmodels.NamespacedIDsuccessfully decouples the cache logic from the worker implementation.Also applies to: 132-136, 191-191, 202-202, 214-214, 226-226, 238-238
openmeter/entitlement/balanceworker/filters.go (1)
75-82: Well-structured filter chain implementation!The addition of the high watermark cache filter and the new
RecordLastCalculationmethod successfully centralizes the calculation time tracking within the filter framework. The inverted filter logic that returns false immediately is more efficient.Also applies to: 123-137, 155-169, 171-183
openmeter/entitlement/balanceworker/filters/filter.go (1)
13-39: Well-structured request object with comprehensive validation.The
EntitlementFilterRequeststruct and its validation method are well-implemented. Good use oferrors.Joinfor combining multiple validation errors.openmeter/entitlement/balanceworker/filters/highwatermarkinmem.go (1)
1-88: Well-implemented in-memory high watermark cache filter.The implementation is clean and follows good practices:
- Proper use of LRU cache for memory management
- Correct clock drift handling for distributed systems
- Safe defaults (returning true for cache misses)
- Clear separation of concerns with the filter interfaces
openmeter/entitlement/balanceworker/entitlementhandler.go (1)
39-45: Good addition of validation method.The validation ensures eventAt is required, preventing potential issues downstream.
openmeter/entitlement/balanceworker/recalculate.go (2)
148-198: Well-structured timestamp propagation and error handling.The changes properly propagate the recalculation timestamp through the processing pipeline and handle errors appropriately using
errors.Joinfor aggregating multiple errors.Also applies to: 200-240
242-244: Clean refactoring to return calculation timestamps.The introduction of
sendEntitlementEventResultand the consistent updates to all send methods provide a clean way to track and propagate calculation timestamps back to the caller.Also applies to: 246-355
Move the high watermark cache to filter framework. This allows us to have a new redis based backend for the high watermark cache, which in turn would make sure that the recalculator job and the consumer shares high watermark cache state.
1d766d6 to
49cd5f4
Compare
Overview
Move the high watermark cache to filter framework. This will allows us to have a new redis based backend for the high watermark cache, which in turn would make sure that the recalculator job and the consumer shares high watermark cache state.
Summary by CodeRabbit
New Features
Improvements
Refactor