Repository navigation
fix(billing): avoid long-lived invoice sequence locks - #4681
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (19)
📝 WalkthroughWalkthroughSequence definitions now require explicit commit modes. Allocation supports caller-bound or independent transactions, uses an atomic SQL upsert, records allocation metrics, and receives meters through application and test wiring. ChangesSequence allocation and transaction flow
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant Caller
participant SequenceService
participant Transaction
participant SequenceAdapter
participant Database
Caller->>SequenceService: Generate invoice sequence number
alt Caller-bound mode
SequenceService->>SequenceAdapter: Allocate using caller transaction
else Independent mode
SequenceService->>Transaction: Run allocation in new transaction
Transaction->>SequenceAdapter: Allocate using fresh transaction
end
SequenceAdapter->>Database: Upsert and return next sequence
Database-->>SequenceAdapter: Next sequence value
SequenceAdapter-->>SequenceService: Sequence value
SequenceService-->>Caller: Invoice sequence number
Possibly related PRs
Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Summary
Why
Previously, sequence allocation participated in the surrounding invoice transaction. The sequence row for a namespace and scope could therefore remain locked for the full invoice operation, serializing otherwise independent invoice work.
Independent allocation keeps contention limited to the allocation itself. The deliberate tradeoff is that invoice numbers are not guaranteed to be contiguous: a caller rollback can leave a gap, but an allocated number is not reused.
This is a follow-up to #4676.
Transaction considerations
transaction.RunInNewTransactionis intentionally exceptional. For Ent-backed adapters it starts a separate transaction on another pooled database connection. The callback cannot observe caller-uncommitted writes, and concurrent callers can add connection-pool pressure. The sequence callback only uses in-memory inputs and touches the sequence table, so it does not depend on caller-held customer or invoice locks.A local OpenMeter Cloud dual-schema harness verified caller commit/rollback behavior and concurrent uniqueness using Cloud's shared Cloud/OpenMeter Ent driver. It also confirmed the additional pooled-connection requirement.
Validation
nix develop --impure .#ci -c make lintnix develop --impure .#ci -c make test-nocacheGreptile Summary
This PR changes billing sequence allocation to reduce long-lived invoice sequence locks. The main changes are:
Confidence Score: 5/5
This looks safe to merge.
Important Files Changed
Flowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A[Generate invoice sequence number] --> B[Validate sequence definition] B --> C{Commit mode} C -->|with_caller| D[Allocate in caller transaction] C -->|independent| E[Start independent transaction] E --> F[Allocate sequence number] D --> G[Atomic upsert returns next value] F --> G G --> H[Render invoice number]%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%% flowchart TD A[Generate invoice sequence number] --> B[Validate sequence definition] B --> C{Commit mode} C -->|with_caller| D[Allocate in caller transaction] C -->|independent| E[Start independent transaction] E --> F[Allocate sequence number] D --> G[Atomic upsert returns next value] F --> G G --> H[Render invoice number]Reviews (1): Last reviewed commit: "fix(billing): allocate invoice sequences..." | Re-trigger Greptile
Context used (3)
Summary by CodeRabbit
New Features
Bug Fixes
Tests