Skip to content

perf(spiraldb): one client and schema-table read per process; as_table counts as it reads (ITL-1457) - #682

Merged
eywalker merged 1 commit into
mainfrom
eywalker/spiral-cold-build
Nov 30, 2026
Merged

eywalker merged 1 commit into
mainfrom
eywalker/spiral-cold-build

Conversation

@eywalker

@eywalker eywalker commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Part of ITL-1457. Replaces #553 and #555. #554 was already superseded by #675 (ITL-1774, pinned-handle reuse).

What changes

  • One SDK client per process, per service address and sign-in. The registry is weak: the client goes with the last connector that uses it, and closing a source affects no other. The key is the address, a digest of the whole environment and the state of the saved login's file (its content is not read). A client whose call failed is given to no new source, so the next new source signs in again. A forked child starts with no clients and no kept rows (os.register_at_fork); an inherited connector is still refused, as on main. No lock is held while a client is made. There are no threads and no new public argument. feat(spiraldb): sources that are given one connector use its client (ITL-1457) #555's connector= argument is not needed.
  • The schema table's rows are kept per process, for the exact snapshot they were read from (table id and version; keyed by address, project and dataset). Every lookup still opens the schema table, which is the one snapshot-check round trip left. Its rows are read again whenever that snapshot differs. A changed or damaged schema table is read and refused as on main, and rows of another snapshot are never served. Row counts are kept by the shared client (same account), keyed by table id and version.
  • Fix 7: as_table() with max_rows no longer counts the keys first. It holds every row until it ends, so it reads at most max_rows + 1 rows and raises the same SourceTooLargeError (same text). iter_data and the async read hand rows on as they go, so they still count first (a sync job run reads through as_table, an async one through the async read).

What was measured and not done

  • Dropping the schema table's separate schema() call: no gain. spiral-sdk's scan plan fetches the schema itself when it was not fetched before. Interleaved fresh processes: schema()+scan 0.71–0.76 s, scan alone 0.69–0.78 s. I kept main's call pattern.
  • Reading only this table's row by key range: not done. The completion check digests every row of the schema table against its marker, so reading one row would drop the refusal of an interrupted or changed migration. The cost is round trips (a plan and a read), not bytes, so a key-range scan would still make both.

Live check (agent workload; read-only keyed tables of another project; nothing written)

Differential, main vs this branch, 12 tables (1 to over 10,000 rows): identical on every route. Routes compared: source_id, content and pipeline hash, schema, as_table (all info and plain), iter_data, the async read, head, row_count_estimate, the max_rows=1 refusal text from as_table and from iter_data, limit within the cap, and a job run sync and async, each followed by a load-only run. The repository's live source tests pass (9). generate_golden_hashes.py --check passes with no golden change.

Timings: interleaved fresh processes, 5 rounds, medians. Ratios are branch against main, and branch against native spiral-sdk.

vs main vs SDK
first source in a process (cold build) 1.0x (unchanged) 2.7x the SDK
a second source of the same dataset 3.0x faster 1.6x the SDK
the same table built again 2.6x faster
first as_table() after a build (32 rows) 1.3x faster (no count) faster than the SDK's first scan
refusal of as_table() over max_rows=10,000 (narrow table) 2.0x slower

The last row is the cost of fix 7. On the refusal path, the read is now max_rows + 1 rows instead of one key column. On a wide table this costs more, in proportion to the width. Only reads that end in the refusal pay it.

What remains of the cold build is a chain of round trips: the client, the table's open and schema(), the schema table's open, schema(), scan plan and read. They run one after another. Overlapping the schema-table read with the table's open would need concurrency, and #553 tried that; its review findings were about ordering. That is not in this PR.

Tests

On the fake, in tests/test_core/sources/test_spiraldb_lookups.py:

  • a second source makes no client and no schema-table read, and both read alike, sync and async, in both roles and both arrangements;
  • a new snapshot of the schema table is read again;
  • an interrupted migration is refused on every build after rows were kept;
  • a changed environment gives a new client;
  • a client whose call failed is given to no new source;
  • the client goes with the last source that uses it;
  • six sources built in threads share one client;
  • the fork hook leaves the child empty.

Updated budgets:

  • as_table makes no count (test_each_read_resolves_once_and_reuses_its_client, test_job_reuses_client_but_refreshes_table_each_run, TestRowCap, with the new test_a_whole_table_over_the_cap_is_refused_without_a_count comparing the refusal text with the counted one);
  • the preview count test releases the first client so its count is not shared.

Review findings on #553/#554/#555 that apply here:

Finding Here
#553-2, a client of another account the key includes the whole environment and the login file's state: test_a_changed_environment_signs_in_again
#553 parallel checks and thread-moved lookups (findings 3–5) no threads: every call is in the calling thread, in main's order
#554-1, a warm read skips schema() unchanged from main (#675's pinned handle)
#555-2, a repeated schema-table failure not seen a damaged schema table is never kept: test_a_damaged_schema_table_is_refused_after_rows_were_kept

No golden value, hash, source id, stored path or identity moves.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Performance
    • Sources using the same service address and sign-in now reuse a client and cached schema data within a process. New sources check for schema changes and refresh cached data when needed.
    • Whole-table reads check for oversized results while scanning, avoiding a separate preliminary count. Streaming reads retain their existing count-first behavior.
  • Reliability
    • New sources use a fresh client after credential or environment changes, or when a client call fails. Closing one source does not disrupt others.

…e counts as it reads (ITL-1457)

The SpiralDB sources of one process that use one service address and one sign-in share
one spiral-sdk client (a weak registry keyed by the address, a digest of the environment
and the state of the saved login's file; a client whose call failed is given to no new
source; a forked child starts empty). The rows of a dataset's schema table are kept per
process for the exact snapshot (id and version) they were read from, so a later source
opens the schema table to see its version and reads it again only when it moved. Row
counts are kept by the shared client.

as_table() with max_rows no longer counts the table's keys first: it reads at most one
row past the cap and raises the same SourceTooLargeError. Streaming reads still count
first.

Rows, hashes, source ids, stored paths and refusals are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

  • On-demand review failed. Comment @coderabbitai review --use-credits to try again.

This review includes 8 billable files and costs up to $2.00.

  • Ask an admin to make reviews automatic

Open in CodeRabbit

Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing.

Or wait 53 minutes for your next included review.

Check out review usage here.

View limit details

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

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 511b784 and 54cec09.

Files selected for processing: 8

  • changelog.d/ITL-1457.md
  • docs/concepts/sources.md
  • src/orcapod/core/sources/spiraldb_table_source.py
  • src/orcapod/databases/spiraldb_connector.py
  • src/orcapod/databases/spiraldb_schema_table.py
  • tests/test_core/sources/test_spiraldb_lookups.py
  • tests/test_core/sources/test_spiraldb_table_source_streaming.py
  • tests/test_databases/test_spiraldb_reading.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Architecture findings (2)

Advisory architecture findings (2)

  • Minor — A new connector can reuse schema rows populated under another sign-in in the same process. Unlike the merge base, it does not have to scan those rows itself. If SpiralDB allows a sign-in to open the matching snapshot without permission to scan it, cached schema metadata can influence schema results or warnings that the merge-base connector could not produce. Snapshot equality establishes freshness, not read authority.
  • Trivial — The process registry retains a cache entry for every endpoint, project, and dataset tuple used to construct a connector, and retains one decoded snapshot for each populated entry. Closing the last connector does not evict either. Unlike the connector-owned cache in the merge base, memory can accumulate with dataset churn in a long-lived worker and affect other sources in that process.
Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 511b784 and 54cec09.

Files selected for processing: 8

  • changelog.d/ITL-1457.md
  • docs/concepts/sources.md
  • src/orcapod/core/sources/spiraldb_table_source.py
  • src/orcapod/databases/spiraldb_connector.py
  • src/orcapod/databases/spiraldb_schema_table.py
  • tests/test_core/sources/test_spiraldb_lookups.py
  • tests/test_core/sources/test_spiraldb_table_source_streaming.py
  • tests/test_databases/test_spiraldb_reading.py

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

Review details

Context from checks skipped due to timeout: 1

  • GitHub Check: CodeRabbit
Additional context used

Code guidelines: 1

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

Source excerpt: Every fenced `python` block in `README.md`, `docs/index.md`, `docs/getting-started.md`, `docs/troubleshooting.md`, `docs/concepts/`, `docs/reference/` and `docs/tutorial/` is executed by `tests/test_docs/test_doc_samples.py`...

CodeRabbit inference engine (CLAUDE.md)

Files:

  • docs/concepts/sources.md
Source excerpt: `docs/` is the only source.

CodeRabbit inference engine (CLAUDE.md)

Files:

  • docs/concepts/sources.md
Source excerpt: What runs where (developer, November 16, 2026; ITL-1181): Existing type errors and warnings are recorded in `.basedpyright/baseline.json`, a debt list to pay down.

CodeRabbit inference engine (CLAUDE.md)

Files:

  • changelog.d/ITL-1457.md
  • docs/concepts/sources.md
  • src/orcapod/core/sources/spiraldb_table_source.py
  • src/orcapod/databases/spiraldb_connector.py
  • src/orcapod/databases/spiraldb_schema_table.py
  • tests/test_core/sources/test_spiraldb_lookups.py
  • tests/test_core/sources/test_spiraldb_table_source_streaming.py
  • tests/test_databases/test_spiraldb_reading.py
Source excerpt: A pull request never edits `CHANGELOG.md` (developer, November 11, 2026; ITL-961).

CodeRabbit inference engine (CLAUDE.md)

Files:

  • changelog.d/ITL-1457.md
Source excerpt: What is published is decided in `scripts/docs_site_content.py` (`PUBLISHED_FILES`, `PUBLISHED_FOLDERS`, `EXCLUDED`).

CodeRabbit inference engine (CLAUDE.md)

Files:

  • docs/concepts/sources.md
Source excerpt: Tests use the **public interface** wherever possible (developer, November 10, 2026): drive an operator by calling it, `op(stream)`, and read the result through the stream's public methods.

CodeRabbit inference engine (CLAUDE.md)

Files:

  • tests/test_core/sources/test_spiraldb_lookups.py
  • tests/test_core/sources/test_spiraldb_table_source_streaming.py
  • tests/test_databases/test_spiraldb_reading.py
Learnings: 1

Common learnings

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

Timestamp: 2026-11-30T07:43:43.467Z
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: 2

src/orcapod/databases/spiraldb_connector.py: 1

403-403: Data Integrity & Integration

The claim depends on two premises that the available evidence cannot decide. First, the pinned spiral-sdk must choose a different service when an environment endpoint setting changes while api_url is None. Second, the SDK must not guarantee unique resource and version identities across services. The repository code shows the cache key gap, but the SDK source is needed to decide whether the gap can cause a wrong-service rows reuse. This is a missing-evidence result, not a refutation.

tests/test_core/sources/test_spiraldb_table_source_streaming.py: 1

335-335: LGTM!

Also applies to: 344-360

@eywalker
eywalker merged commit 26efe35 into main Nov 30, 2026
12 checks passed
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