[OPIK-7231] [BE] feat: ingest cipx spend blocks into dedicated table with derived allocation#7392
Open
BorisTkachenko wants to merge 5 commits into
Conversation
9bab23b to
0b884f2
Compare
Contributor
⏱️ pre-commit per-hook timing
⏭️ 39 skipped (no matching files changed)
|
…with derived allocation Retrieval used to ARRAY JOIN cipx_spends.blocks (~363 blocks/span) and re-derive per-span token allocation with window functions on every request. Ingestion now writes one row per block into the new cipx_spend_blocks table with the derived columns (tier, lane, bd_lane, label, is_definition, alloc) computed once in Java, plus residual rows for billed tiers no block absorbs, so retrieval becomes plain GROUP BYs. block_idx (raw array position; 65531 + tier ordinal for residuals) keeps the ReplacingMergeTree key unique and replay-deduplicable. - drop cipx_spends.blocks (000097); create cipx_spend_blocks (000098) - recreate cipx_trace_identities with user_uuid in the sorting key (000099) - new MySQL cipx_user_mappings (000088), populated from trace identity on create and update, for email -> user_uuid resolution at retrieval - span ingestion is create-only (cipx call data is immutable): drop SpanCostIntelligenceChanged and the span-update resolve path; the trace identity update path stays Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
0b884f2 to
9c5d762
Compare
9c5d762 to
58caf03
Compare
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Details
Corresponding retrieval path changes: https://github.com/comet-ml/ai-cost-backend/pull/8
Moves cipx block data out of
cipx_spends.blocks(anArray(Tuple)holding ~99% of the rowweight that every composition/breakdown query had to ARRAY JOIN and re-derive with window
functions) into a dedicated
cipx_spend_blockstable with one row per block. Token allocationand dashboard groupings (
tier,lane,bd_lane,label,is_definition,alloc) are nowderived once in Java at ingestion time, so retrieval becomes plain GROUP BYs.
cipx_spend_blockstable (migration 000098) fed byCostIntelligenceIngestionListeneron span create events; span ingestion is create-only (cipx call data is immutable), so the
SpanCostIntelligenceChangedspan-update event is removed.cipx_spendsdrops theblockscolumn (000097);cipx_trace_identitiesis recreated withuser_uuidin the primary key (000099); new MySQLcipx_user_mappingsemail -> uuid lookuptable (000088) populated from trace identity events.
span fans out to ~350 block rows, and the R2DBC driver resolves each named bind with a linear
scan over the statement's parameter list — a 200-span batch is ~1.7M parameters, which is
O(n²) (hours of CPU) and wedged the event-bus subscriber. The remaining cipx DAOs
(
CipxSpendDAO,CipxTraceIdentityDAO) switch to O(1) positional binds, and the listenerlogs subscribe/complete per insert (cipx-bearing events only) for ingestion visibility.
Change checklist
Issues
AI-WATERMARK
AI-WATERMARK: yes
the resulting fixes: v2-client bulk insert for
cipx_spend_blocks, positional binds in thecipx DAOs, ingestion visibility logs in the listener; test data replay scripts.
Testing
CostIntelligenceIngestionTestcovers the listener end-to-end: cipx span create ->cipx_spends+cipx_spend_blocksrows (attributed + residual, allocation math), tracecreate/update ->
cipx_trace_identities+cipx_user_mappings.original IDs preserved) through the local BE REST endpoints
(
/v1/private/{traces,spans}/batch, batches of 200):cipx_spendsrows, 5.98Mcipx_spend_blocksrows(~367 blocks/span), 14,490 trace identities; 0 errors.
event-bus subscriber monitor (only 202 of 16,281 spans landed); after the fix the same
insert completes in ~340 ms.
ingestion gap: its
cipx_spendsis missing 50 spans that exist in its rawspanstable).Documentation
N/A — schema and derivation rules are documented in the migration files and DAO javadocs.