Skip to content

Reduce SpiralDB source metadata round trips - #527

Merged
eywalker merged 3 commits into
mainfrom
eywalker/itl-1431-spiraldb-source-remove-redundant-metadata
Nov 23, 2026
Merged

eywalker merged 3 commits into
mainfrom
eywalker/itl-1431-spiraldb-source-remove-redundant-metadata

Conversation

@eywalker

@eywalker eywalker commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes ITL-1431

Changes

  • Resolve a table by name once for each read. The scan and any manifest row count use that handle at the same pinned version.
  • Reuse the source-owned SDK client for construction, reads and job freshness checks. The source releases it on explicit close or context exit. A restricted copy has its own lazy connection.
  • Replace constructor enumeration with an exact lookup. A missing table keeps the existing source error.

Findings and coverage

Finding Regression test
Construction enumerated the dataset before looking up the requested table test_a_source_looks_its_table_up_once_to_build, test_missing_table_matches_source_diagnostic, test_lookup_preserves_unrelated_sdk_errors
Full reads and bounded heads resolved the table again for the count or scan test_each_read_resolves_once_and_reuses_its_client across two key orders in sync and async
Each operation constructed another SDK client test_each_read_resolves_once_and_reuses_its_client, test_job_reuses_client_but_refreshes_table_each_run, test_restricted_copy_owns_its_client, test_every_connector_is_closed
Client reuse must preserve freshness decisions test_a_check_still_finds_a_table_created_again, test_freshness_catches_metadata_schema_changes, test_freshness_pins_newer_version
Read bytes and identities must match main Live byte comparison of enriched Arrow IPC with schema metadata, IDs, tokens and system tags. Per-row content hashes and source content and pipeline hashes matched on all four pinned snapshots.

Fake SDK operation counts

Each case used a fake table with two tags and two data columns. The counts match across both key orders and sync and async execution. A restricted copy opens its own client on first use. Repeated reads of that copy reuse it.

Operation Table lookups Dataset listings Metadata KV reads Manifest counts SDK constructions
Construction 1 → 1 1 → 0 1 → 1 0 → 0 1 → 1
Full read 2 → 1 0 → 0 1 → 1 1 → 1 1 → 0
Restricted read, first use of a copy 1 → 1 0 → 0 1 → 1 0 → 0 1 → 1
Head with an estimate 2 → 1 0 → 0 1 → 1 1 → 1 2 → 0
First job run 3 → 2 0 → 0 2 → 2 1 → 1 2 → 0
Second job run 1 → 1 0 → 0 1 → 1 0 → 0 1 → 0

Live before and after

Live wall seconds, before → after. Five interleaved samples per cell.

Shape Construct Full Restricted Head Job 1 Job 2 Bare
Small 0.862 → 0.926 1.634 → 0.801 1.074 → 1.062 1.872 → 0.812 2.634 → 1.453 0.758 → 0.598 0.053 → 0.052
Wide 0.865 → 0.764 1.756 → 0.676 1.077 → 1.094 1.870 → 0.941 2.593 → 1.424 0.944 → 0.595 0.112 → 0.087
Many 1.075 → 0.766 1.898 → 1.006 1.400 → 1.495 1.832 → 0.972 2.672 → 1.782 0.759 → 0.663 0.197 → 0.190
Nested 0.870 → 0.773 1.687 → 0.967 1.169 → 1.181 1.813 → 0.829 2.612 → 1.457 0.771 → 0.603 0.170 → 0.160

Live visible HTTP request counts, before → after. Native SDK storage requests are outside this counter.

Shape Construct Full Restricted Head Job 1 Job 2 Bare
Small 3 → 2 3 → 2 2 → 2 3 → 2 5 → 4 2 → 2 0 → 0
Wide 3 → 2 3 → 2 2 → 2 3 → 2 5 → 4 2 → 2 0 → 0
Many 3 → 2 3 → 2 2 → 2 3 → 2 5 → 4 2 → 2 0 → 0
Nested 3 → 2 3 → 2 2 → 2 3 → 2 5 → 4 2 → 2 0 → 0

One-minute load average 2.39–6.20. Retried samples: 0.

The first restricted sample used a new copy in both versions. It still made two HTTP requests. The second job run refreshed the source once and reused the held result.

Validation

  • Targeted SpiralDB, source, alignment and job tests in one pytest process (3,148 passed, 58 skipped).
  • All 16 construction, read and job round-trip regression cases failed against the pre-change baseline.
  • Whole-repository basedpyright, Ruff lint and Ruff format check.
  • Golden hash generator in check mode and changelog validation.

Summary by CodeRabbit

  • New Features
    • Table sources can now be managed with a context manager and explicitly closed.
  • Bug Fixes
    • Repeated reads and freshness checks reuse the same database connection while refreshing table information.
    • Reads now detect when a table has been replaced since the source was created.
    • Missing tables and invalid table names receive clearer handling, while unrelated database errors continue to surface.

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between ba0eb75 and e5b6aa6.

Files selected for processing: 7

  • changelog.d/ITL-1431.md
  • src/orcapod/core/sources/spiraldb_table_source.py
  • src/orcapod/databases/spiraldb_connector.py
  • tests/test_core/sources/test_spiraldb_lookups.py
  • tests/test_core/sources/test_spiraldb_table_source.py
  • tests/test_core/sources/test_spiraldb_table_source_streaming.py
  • tests/test_databases/fake_pyspiral.py

Limit details: You’ve used the included review currently available. Your 107 included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

Recent review details

Context from checks skipped due to timeout: 3

  • GitHub Check: CodeRabbit
  • GitHub Check: Tests (3.13)
  • GitHub Check: Ruff and basedpyright
Additional context used

Code guidelines: 1

CLAUDE.md — auto-discovered
Path-based instructions: 1

Source excerpt: The file holds only the text that would have gone under the release heading in `CHANGELOG.md`: the same category headings `CHANGELOG.md` uses (`### Breaking changes and migration`, `### Added`, `### Changed`, `### Fixed`, `#...

CodeRabbit inference engine (CLAUDE.md)

Files:

  • changelog.d/ITL-1431.md
Learnings: 1

Common learnings

Learnt from: CR
Repo: metamorphic-labs/orcapod-python

Timestamp: 2026-11-23T19:22:32.208Z
Learning: Source excerpt:
# Claude Code instructions for orcapod-python

## Lint, format and type checks

What runs where (developer, November 16, 2026; ITL-1181):

- New and changed code must be ruff-clean and ruff-formatted, and must add no
  basedpyright errors. Warnings are reported but do not fail the check
  (`failOnWarnings = false`); still do not add them needlessly. Run
  `uv run ruff format .` and `uv run ruff check .` before committing;
  `uv run pre-commit install` once per clone runs both on every commit.
Additional comments: 1

src/orcapod/core/sources/spiraldb_table_source.py: 1

343-345: Data Integrity & Integration

The review comment's Ray serialization concern is not supported. The Ray executor receives only the user function and the per-call kwargs, and no inspected code places a SpiralDBTableSource in either value. The repository also rebuilds sources from config (from_config) when they move between components. The threading.Lock is not picklable, but no inspected path pickles a live source. No regression is established for the Ray workflow.

Walkthrough

SpiralDBTableSource now reuses an SDK client and resolves table handles for initialization, reads, and freshness checks. The source and its restricted copies manage connector lifetimes. Table scans and row-count estimates use the resolved handle and pinned version.

Changes: SpiralDB source operations

Layer / File(s) Summary
Connector lookup and table operations
src/orcapod/databases/spiraldb_connector.py, tests/test_databases/fake_pyspiral.py
The connector adds exact table lookup, client sharing, and table-handle batch-scan and row-count operations. The fake SDK adds matching identifier resolution and manifest-statistics behavior.
Source initialization and connector lifetime
src/orcapod/core/sources/spiraldb_table_source.py, tests/test_core/sources/test_spiraldb_table_source.py, tests/test_core/sources/test_spiraldb_lookups.py
The source owns a connector, uses direct table lookup during construction, and closes the connector on initialization failure or context exit. Restricted copies use separate connectors that share the SDK client. Tests cover connector closure, lookup behavior, and client reuse.
Read and freshness table resolution
src/orcapod/core/sources/spiraldb_table_source.py, tests/test_core/sources/test_spiraldb_lookups.py, tests/test_core/sources/test_spiraldb_table_source_streaming.py, changelog.d/ITL-1431.md
Reads and freshness checks resolve a table handle and use it for metadata, row-count, and scan operations. Reads use the pinned version and reject a replacement table whose ID differs from the captured ID. Tests cover read lookup counts, freshness checks, and table replacement.

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

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant SpiralDBTableSource
  participant SpiralDBConnector
  participant SpiralDBTable
  SpiralDBTableSource->>SpiralDBConnector: lookup_table
  SpiralDBConnector-->>SpiralDBTableSource: resolved table handle
  opt Row count estimate needed
    SpiralDBTableSource->>SpiralDBTable: row_count_estimate at pinned version
    SpiralDBTable-->>SpiralDBTableSource: estimated row count
  end
  SpiralDBTableSource->>SpiralDBTable: scan_batches at pinned version
  SpiralDBTable-->>SpiralDBTableSource: record batches
Loading

Possibly related PRs

Merge Risk: Minimal · up to e5b6a

This change reduces SpiralDB round trips by reusing one client and one resolved table handle per read. It adds explicit source closing, and a source that has been closed now raises RuntimeError if used. Reads against a dropped and recreated table are now refused. The reviewed code and test coverage show no concrete merge-blocking problem.

Security architecture risk: Low · up to e5b6a

Reads now keep the table identity and version consistent. The remaining risk is limited to reads on a source that is closed while work is still in progress, including after async cancellation. Closing the source can interrupt a read, but it does not reliably stop a scan that has already started.

Retained concerns

  • Low · reliability · inferred: The head lets source shutdown close a connector still in use by a read; the base kept the connector scoped to the operation. Although callers are told to close only after reads finish, cancelling an async consumer can leave its worker running. An overlapping close can make a read fail between its open check and SDK access, while an already-created scan can continue. The effect is scoped to work using the connector being closed; separately closable copies are not invalidated.
Security review details

Security Blast Radius

  • inferred — Client sharing couples a source and its restricted copies to the same SDK session, but it does not add another configured principal, project, or dataset. Closing a wrapper is local cleanup, not credential revocation or shutdown of the shared client.

Trust Boundaries and Controls

  • observed — The connector qualifies the supplied table name with its configured dataset and rejects empty or dotted dataset and table names before calling the SDK. It translates only the exact SDK not-found error and propagates other failures. The source separately verifies the resolved table ID before scanning or counting.

Hardening Proposals

  • proposed — Define shutdown and cancellation behavior together. Let active reads retain the client until they finish, while rejecting new reads after close, or ensure that async consumers wait for their worker before exiting the source context.
Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Reduce SpiralDB source metadata round trips" matches the main change. The PR resolves the table once per read, reuses the source-owned SDK client across construction, reads, and freshness c…
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.
Full details: Title check

Explanation

The title "Reduce SpiralDB source metadata round trips" matches the main change. The PR resolves the table once per read, reuses the source-owned SDK client across construction, reads, and freshness checks, and reduces visible HTTP requests. The title is short, specific, and reads clearly in history.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

Note

This review was completed with usage-based billing: files reviewed beyond your plan's included limits are billed at $0.25/file. View usage-based billing.

@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.00000% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/orcapod/core/sources/spiraldb_table_source.py 98.16% 2 Missing ⚠️
src/orcapod/databases/spiraldb_connector.py 97.56% 1 Missing ⚠️
Files with missing lines Coverage Δ
src/orcapod/databases/spiraldb_connector.py 98.54% <97.56%> (-0.17%) ⬇️
src/orcapod/core/sources/spiraldb_table_source.py 97.08% <98.16%> (-0.03%) ⬇️

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant