Skip to content

perf(spiraldb): read the schema table while the first source opens its table (ITL-1801) - #685

Closed
eywalker wants to merge 1 commit into
mainfrom
eywalker/itl-1801-spiraldb-source-the-first-source-in-a
Closed

eywalker wants to merge 1 commit into
mainfrom
eywalker/itl-1801-spiraldb-source-the-first-source-in-a

Conversation

@eywalker

Copy link
Copy Markdown
Contributor

Fixes ITL-1801

Related: ITL-1774, ITL-1768

What

The first SpiralDBTableSource in a process made its build round trips one after another: open the table, read its schema, then open the dataset's schema table, read its schema, plan its scan and read its rows. ITL-1457 keeps the schema-table rows per process, so a second source is fast; the first one paid the whole chain.

The schema-table read does not depend on the table's open. The build now starts it on a worker thread first (SpiralDBConnector.schema_rows_meanwhile) and hands the pending rows to SpiralDBTable.arrow_schema, which waits for them where the sequential code read them.

  • Errors are unchanged. The rows are asked for at the same point as before. An error raised before that point wins (a missing table, a failed open, a table without a primary key, a failed schema() of the table). A schema-table error is raised by the call that asks for the rows, with the same class, message, notes and TableSchemaError.table. When both fail, the table's error is raised, as the sequential order did. A read nobody asks for is waited for and dropped.
  • No thread outlives the build. The thread pool is made for the block and joined when it ends, on success, on error and on KeyboardInterrupt. A fork after the build sees no thread. A name that lookup_table refuses without any call (empty, qualified, or the schema table's own) starts nothing, so it still makes no SDK call.
  • The build stays atomic. The thread is joined inside the constructor's try, before close() runs on failure.
  • SchemaTable.row is removed; its one caller now takes the rows it is given.
  • Side effect: a build of a missing table now also reads the schema table. The call-budget test says so.

Proof

Live timings. Read-only, as the agent workload, on a keyed SpiralDB table of about 63k rows, reading 10 rows (limit=10). Each sample is a fresh interpreter, imports outside the timer. 9 interleaved rounds of SDK / main / branch; medians.

build build + read ratio to SDK (build + read)
spiral-sdk (client, keyed_table, schema(), scan) 1.00x 1.00x 1.00x
main (26efe35aa) 2.56x SDK build 1.75x 1.75x
this PR 2.26x SDK build 1.60x 1.60x

The build is about 12% faster (median −0.20 s). Phase timing of the branch build shows why it stops there. The schema-table chain (open, schema(), scan plan, rows: four dependent round trips, about 0.8 s) now runs beside the table's open and schema() (about 0.45 s), and it is the critical path. Two measurements back this:

  • spiral-sdk releases the GIL. Two opens in parallel take ~0.37 s, against ~0.72 s one after the other.
  • Dropping the schema table's own schema() gains nothing. The scan plan fetches the schema anyway, which matches the ITL-1457 measurement.

The target of about 1.5x is not quite reached for this case. What is left is listed under What is left below.

Live differential, main against this branch. Run twice on main as a control. Digests are identical for:

  • the source id, content hash and pipeline hash;
  • as_table(all_info=True), iter_data, async_iter_data;
  • a sync job and an async job (a function pod over the source: rows, schema, node content and pipeline hash);
  • each job saved, loaded and run load_only=True.

The function output's own source-info token holds a random result id. It is excluded from every digest, on main too.

Live refusals are identical: a missing table, an empty name, a qualified name and a missing project give the same class, message and notes.

Golden: generate_golden_hashes.py --check passes (no golden file changed).

Tests (tests/test_core/sources/test_spiraldb_first_build.py, fake SDK):

  • overlap: the table's open blocks until the schema-table open has started;
  • no thread left after a build, after a failed build (missing table / schema table / both), or after six concurrent builds;
  • errors: a schema-table read error and an incomplete schema table match the sequential path exactly (class, message, notes, table). Both failing gives the table-open error, and a failed table schema() beats the schema-table error;
  • an unrecorded table still warns in the caller's thread;
  • a child forked after a build inherits no thread, and builds its own first source with a new client.

Every SpiralDB test file passes (3,668 passed). ruff, ruff format and basedpyright are clean, with no new error.

The run-start check (measured, report only, not changed here)

Measured on main, warm, against a warm SDK read of 10 rows on a kept handle. check_for_new_data costs about 4.3x that read: 3 round trips, namely the lookup, the new handle's schema() and the schema-table open. The read itself is 0.9x.

What is left

  • The first-source build floor is now the schema-table chain itself. Going further needs a design decision. One option is a persistent cache of the schema-table rows by snapshot (id and version): only the schema-table open would remain, an estimated ~1.2x. Another is reading the table's row by key, which weakens the completion check.

🤖 Generated with Claude Code

…s table (ITL-1801)

Building a SpiralDBTableSource opened its table, read its schema, then opened the
dataset's schema table and read its rows, one round trip after another. The schema
table does not depend on the table's open, so the build now reads it on a worker
thread made for the build and joined before the constructor returns
(SpiralDBConnector.schema_rows_meanwhile). Its rows are asked for where the sequential
read asked for them, so every error (class, message, notes) is the one raised before:
when both fail, the table's error wins. A name refused without a call starts nothing.

SchemaTable.row is removed: its one caller now takes the rows it is given.

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

  • Run on-demand review

This review includes 6 billable files and costs up to $1.50.

  • 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 20 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 98 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 26efe35 and 70b754a.

Files selected for processing: 6

  • changelog.d/ITL-1801.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_first_build.py
  • tests/test_core/sources/test_spiraldb_lookups.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@eywalker

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

Posted by a Claude Code agent on behalf of @eywalker.

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@eywalker

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

Posted by a Claude Code agent on behalf of @eywalker.

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@eywalker

Copy link
Copy Markdown
Contributor Author

Closing this PR without merging. The first source's build stays sequential, as on main.

Why the overlap cannot be kept:

  • The SDK's threading contract does not cover it. spiral-sdk 0.16.74 says only one thing is safe from several threads at once: PreparedEvaluation.evaluate (spiral/client.py, Client.prepare_evaluation docstring: "evaluations may run at once from several threads"). It says nothing about opening a keyed table, reading its schema or scanning it at the same time on one Client. Those calls forward to the compiled library with no Python lock. Without a documented contract, this PR's two threads cannot share one client.
  • A second client per first build costs about what it saves. Giving the worker thread its own client adds an SDK client and a sign-in to every first build, refused builds included. That is more I/O than main does on a refusal.
  • A refused build must not read the schema table before the table lookup succeeds. On a missing table, this PR waited for a speculative schema-table read. A failure of that read also marked the shared client unhealthy, so a later build signed in again. Starting the read only after the lookup succeeds gives back main's sequential order.

The independent review also found two problems that closing this PR resolves along with the rest:

  • On a KeyboardInterrupt during the build, the worker thread could keep running after the constructor raised.
  • Every concurrent build started its own worker thread, with no limit on the total.

The work is kept open on the tracker issue. It can be revisited if spiral-sdk documents that a client is safe to use from several threads, or adds an async API. The branch is kept.

Posted by a Claude Code agent on behalf of @eywalker.

@eywalker eywalker closed this Nov 30, 2026
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