Skip to content

fix: tolerate concurrently deleted charges in charge list and get reads - #4961

Merged
borosr merged 1 commit into
feat/v3-charges-spend-api-convertfrom
feat/v3-charges-tolerant-typed-loads
Aug 19, 2026
Merged

borosr merged 1 commit into
feat/v3-charges-spend-api-convertfrom
feat/v3-charges-tolerant-typed-loads

Conversation

@borosr

@borosr borosr commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #4933.

The typed charge loads deliberately run outside the search transaction (the realtime usage expand calls ClickHouse per charge, and a remote call must not pin a pooled Postgres connection), so a charge deleted between the search snapshot and the typed loads failed the whole ListCharges/GetByIDs read with a not-found error. A single read-committed transaction would not close the race either: each statement takes its own snapshot.

This defines the missing-ID contract explicitly instead:

  • entutils.InIDOrderSkipMissing orders like InIDOrder but drops target IDs with no matching entity.
  • The typed GetByIDsInputs (usage-based, flat fee, credit purchase) gain an opt-in AllowMissing flag; strict not-found stays the default for every other caller.
  • Only the union charge expansion opts in: a charge that vanishes between the two reads is dropped from the list page, and GetByID keeps returning its not-found error through the empty-result path.

Covered by a DB-backed test feeding the expansion a search snapshot with a phantom ID.

Summary by CodeRabbit

  • Bug Fixes
    • Charge lookups now gracefully omit records that are no longer available instead of failing the entire request.
    • Returned charges continue to follow the requested ID order, excluding missing entries.
    • Improved handling of charges that disappear between search and loading.

Greptile Summary

The PR makes union charge reads tolerate charges deleted between the initial search and subsequent typed loads while preserving strict missing-ID behavior for other callers.

  • Adds opt-in missing-ID handling to all three typed charge loaders.
  • Filters vanished charges while retaining the requested order of loaded results.
  • Adds a database-backed concurrent-deletion scenario.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
openmeter/billing/charges/service/get.go Enables missing-row tolerance for union expansion and restores search ordering across the surviving typed charges.
pkg/framework/entutils/idorder.go Adds an opt-in ordering helper that skips target IDs without matching entities while retaining strict behavior by default.
openmeter/billing/charges/usagebased/adapter/charge.go Selects strict or missing-tolerant ID ordering according to the new input flag.
openmeter/billing/charges/flatfee/adapter/charge.go Selects strict or missing-tolerant ID ordering according to the new input flag.
openmeter/billing/charges/creditpurchase/adapter/charge.go Selects strict or missing-tolerant ID ordering according to the new input flag.
openmeter/billing/charges/service/api_list_test.go Adds coverage showing that union expansion returns surviving charges when an earlier search snapshot contains a deleted charge.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant Search as Charge search
    participant Expand as Union expansion
    participant Typed as Typed charge loaders
    Caller->>Search: List/Get charge IDs
    Search-->>Expand: Snapshot of IDs by type
    Note over Search,Typed: A charge may be deleted here
    Expand->>Typed: "GetByIDs(AllowMissing=true)"
    Typed-->>Expand: Existing charges only
    Expand-->>Caller: Loaded charges in search order
Loading

Reviews (3): Last reviewed commit: "fix: tolerate concurrently deleted charg..." | Re-trigger Greptile

Context used:

@borosr
borosr requested a review from a team as a code owner August 19, 2026 10:26
@coderabbitai

coderabbitai Bot commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: db5b5049-be6d-42e8-9c00-fbada06ceabd

📥 Commits

Reviewing files that changed from the base of the PR and between d42bb91 and 44dd7e1.

📒 Files selected for processing (9)
  • openmeter/billing/charges/creditpurchase/adapter.go
  • openmeter/billing/charges/creditpurchase/adapter/charge.go
  • openmeter/billing/charges/flatfee/adapter.go
  • openmeter/billing/charges/flatfee/adapter/charge.go
  • openmeter/billing/charges/service/api_list_test.go
  • openmeter/billing/charges/service/get.go
  • openmeter/billing/charges/usagebased/adapter/charge.go
  • openmeter/billing/charges/usagebased/service.go
  • pkg/framework/entutils/idorder.go

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

Charge lookups now support missing IDs. Strict reads keep existing errors. Tolerant reads omit missing charges. Typed charge loading uses tolerant reads and preserves the order of charges that remain.

Changes

Missing-charge loading

Layer / File(s) Summary
Missing-aware ID ordering
pkg/framework/entutils/idorder.go
InIDOrderSkipMissing orders matching results while omitting missing target IDs. InIDOrder keeps strict missing-ID errors.
Charge adapter options
openmeter/billing/charges/{creditpurchase,flatfee}/..., openmeter/billing/charges/usagebased/...
Charge lookup inputs now expose AllowMissing. Credit-purchase, flat-fee, and usage-based adapters select strict or tolerant ordering based on this flag.
Typed charge loading
openmeter/billing/charges/service/get.go, openmeter/billing/charges/service/api_list_test.go
Typed loads allow missing records, filter IDs to loaded charges, and test omission of a charge removed after search.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 44dd7

This localized change makes charge reads tolerate concurrent deletion while preserving strict behavior for other callers; no actionable merge-blocking risk remains beyond normal checks and review.

Sequence Diagram(s)

sequenceDiagram
  participant Search
  participant ChargeService
  participant ChargeAdapter
  participant IDOrdering

  Search->>ChargeService: Return charge IDs
  ChargeService->>ChargeAdapter: Load typed charges with AllowMissing
  ChargeAdapter->>IDOrdering: Order fetched charges
  IDOrdering-->>ChargeAdapter: Available charges in requested order
  ChargeAdapter-->>ChargeService: Return loaded charges
  ChargeService-->>Search: Return ordered available charges
Loading

Possibly related PRs

Suggested labels: release-note/bug-fix, area/billing

Suggested reviewers: turip, galexihu

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: charge list and get reads now tolerate concurrently deleted charges.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/v3-charges-tolerant-typed-loads

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@borosr
borosr force-pushed the feat/v3-charges-tolerant-typed-loads branch from 44dd7e1 to 303805a Compare August 19, 2026 12:12
rolosp
rolosp previously approved these changes Aug 19, 2026
@borosr borosr self-assigned this Aug 19, 2026
@borosr borosr added the release-note/misc Miscellaneous changes label Aug 19, 2026
The typed charge loads deliberately run outside the search transaction
(the realtime usage expand calls ClickHouse per charge), so a charge
deleted between the search snapshot and the typed loads failed the
whole ListCharges/GetByIDs read with a not-found error.

Define the missing-ID contract explicitly: entutils.InIDOrderSkipMissing
orders like InIDOrder but drops target IDs with no matching entity, and
the typed GetByIDs inputs gain an opt-in AllowMissing flag (strict
remains the default for every other caller). Only the union charge
expansion opts in: a vanished charge is dropped from the list page, and
GetByID keeps returning its not-found error through the empty result.
@borosr
borosr dismissed rolosp’s stale review August 19, 2026 13:40

The merge-base changed after approval.

@borosr
borosr force-pushed the feat/v3-charges-spend-api-convert branch from 52c75ef to 8506f61 Compare August 19, 2026 13:40
@borosr
borosr force-pushed the feat/v3-charges-tolerant-typed-loads branch from 303805a to ec8d388 Compare August 19, 2026 13:40
@borosr
borosr merged commit 5174edd into feat/v3-charges-spend-api-convert Aug 19, 2026
27 checks passed
@borosr
borosr deleted the feat/v3-charges-tolerant-typed-loads branch August 19, 2026 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/misc Miscellaneous changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants