Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions changelog.d/ITL-1412.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Fixed

- Function pipeline records are published only after their referenced results commit.
A failed or discarded result batch withdraws its pending pipeline records, so
later flushes cannot publish dangling references. Failed table commits identify
the store and table. A configured no-op result database still records the processed
inputs in the pipeline store and explicitly discards its outputs.

### Changed

- The internal `ArrowDatabaseProtocol` now requires `pending_result`,
`guard_record`, `fail_pending_result`, `flush_result_batch`, `flush_batch`,
and `stores_records`.
7 changes: 7 additions & 0 deletions docs/concepts/pipeline-job.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ run and computes nothing on a second `run()`.
A job created without a database can be recorded but not run. Give it one later with
`job.bind(database=...)`; `run()` without a database raises `ValueError`.

### Result commit ordering

A function node publishes its pipeline records after the referenced results
commit. Failed result batches withdraw their pending pipeline records.
`NoOpArrowDatabase` explicitly discards results and still allows pipeline
bookkeeping to be written.

### Connector databases

A `ConnectorArrowDatabase` stores a column only when its connector can store it as written.
Expand Down
44 changes: 44 additions & 0 deletions docs/dev/design_issues/ITL-1412-FC1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## `src/orcapod/databases/` — function result and pipeline commits

### FC1 — Function pipeline records could outlive their discarded results
**Status:** resolved
**Severity:** critical
**Issue:** ITL-1412

A failed result write left pipeline records pending. A database-wide flush after the
failure could commit those records even when the result still failed or the database
had already dropped it. With separate result and pipeline stores, a successful async
branch could also publish its sibling's pipeline records before the sibling's result
table had committed.

**Fix:** A pending pipeline record follows the commit attempt for its referenced
result. The database keeps the record in its normal readable queue while the attempt
is pending, makes it eligible for publication after success, and immediately withdraws
it on failure. Where a store supports scoped flushes, a function node flushes the
result and pipeline tables individually.
Nodes that share a physical table can still publish ready rows together. The
dependency state is held in memory and adds no columns or changes to stored identities.

Each guarded record takes one dependency reference and one entry in the reverse
lookup. A flush checks just its queued dependencies and partitions the Arrow batch
only when some rows must wait. A failed write removes its dependent records by ID
and leaves other pending rows intact. Result retries may still store results that
were buffered, and the next run can reuse those durable results to write new records.

In-memory tables use the same queue dependencies. A separately attached in-memory
result table is promoted at the node's batch boundary before publishing references
to it. Previously it stayed pending. Reads of the pending and promoted rows
return the same records. A no-op pipeline store leaves a separate in-memory result
table pending because it has no references to publish.

A no-op result database is an explicit discard. Its function nodes still write their
pipeline records for processed inputs, as before. Those records refer to output
that was never stored.

The internal `ArrowDatabaseProtocol` requires `pending_result`, `guard_record`,
and `fail_pending_result` for dependency bookkeeping and `flush_result_batch`
and `flush_batch` for committing. Its `stores_records` property distinguishes
retaining stores from explicit discards. Every library database implements these
operations, including `DeltaTableDatabase`, `InMemoryArrowDatabase`,
`ConnectorArrowDatabase`, `NoOpArrowDatabase`, and the forwarding
`ExtensionAwareDatabase` wrapper.
149 changes: 80 additions & 69 deletions src/orcapod/core/nodes/function_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
PipelineJobRequiredError,
OrcapodDatabaseError,
TypeIdentityError,
OrcapodDatabaseReadError,
OrcapodDatabaseWriteError,
UnstorableColumnError,
)
Expand All @@ -86,6 +85,7 @@
)
from orcapod.utils.database_errors import (
commit_best_effort,
database_write_path,
keep_database_error,
database_error_of,
)
Expand All @@ -98,7 +98,6 @@
TagProtocol,
TrackerManagerProtocol,
)
from orcapod.databases.utils import as_table_committing
from orcapod.protocols.database_protocols import ArrowDatabaseProtocol
from orcapod.protocols.observability_protocols import (
DataExecutionLoggerProtocol,
Expand Down Expand Up @@ -2325,66 +2324,86 @@ def _batched_commits(self, rows_per_commit: int) -> Iterator[None]:
for pod in pods:
pod.set_auto_flush(True)

def _ephemeral_database_in_use(self) -> ArrowDatabaseProtocol | None:
"""Return the ephemeral database this node writes results to, if it does."""
if self._node_config.is_result_ephemeral and self._ephemeral_cached_pod:
return self.ephemeral_result_database
return None

def _commit_result_table(self) -> None:
"""Commit this node's pending result rows, before its pipeline records.

Every view of a database shares one set of pending writes, and a flush commits
them table by table in no guaranteed order. Committing the result table on
its own first means a crash between the two commits leaves results without
pipeline records (found in the result cache on the rerun, so nothing is
recomputed), never pipeline records pointing at results that were lost.
Only a database that commits table by table
(``TableCommittingDatabaseProtocol``, the Delta database) needs and offers this;
for another database the pipeline flush that follows commits everything.
"""
pod = self._cached_function_pod
if not isinstance(pod, CachedFunctionPod):
return
result_database = as_table_committing(pod.result_database)
if result_database is not None:
result_database.flush_batch(pod.record_path)
def _flush_node_table(
self,
database: ArrowDatabaseProtocol,
path: tuple[str, ...],
*,
result: bool = False,
publish_records: bool = True,
) -> None:
"""Flush one batch and withdraw dependent records if a result write fails."""
try:
if result:
database.flush_result_batch(path, publish_records=publish_records)
else:
database.flush_batch(path)
except BaseException as exc:
if result:
database.fail_pending_result(path)
if not isinstance(exc, Exception) or isinstance(exc, OrcapodDatabaseError):
raise
raise OrcapodDatabaseWriteError(
database_write_path(database, path),
node_label=self.label,
flush=True,
table_flush=True,
) from exc

def _commit_pending_writes(self) -> None:
"""Commit this node's pending writes, results first (see ``_batched_commits``).

Raises:
OrcapodDatabaseReadError: If a stored table could not be read; it names this node.
OrcapodDatabaseWriteError: If a commit failed, naming this node and the database
(``flush=True``, no row: the batch holds many rows). The failed
writes stay pending, and earlier commits are kept.
"""
pipeline_database = self._require_pipeline_database()
ephemeral = self._ephemeral_database_in_use()
"""Commit results before publishing any records that depend on them."""
try:
if ephemeral is not None:
try:
ephemeral.flush()
except OrcapodDatabaseReadError:
raise
except Exception as exc:
raise OrcapodDatabaseWriteError(
ephemeral.base_path, node_label=self.label, flush=True
) from exc
pipeline_database = self._require_pipeline_database()
ephemeral = self._ephemeral_cached_pod
if self._node_config.is_result_ephemeral and ephemeral is not None:
self._flush_node_table(
ephemeral.result_database, ephemeral.record_path, result=True
)
pod = self._cached_function_pod
if isinstance(pod, CachedFunctionPod):
self._flush_node_table(
pod.result_database,
pod.record_path,
result=True,
publish_records=pipeline_database.stores_records,
)
self._flush_node_table(pipeline_database, self._versioned_pipeline_path)
except BaseException as exc:
self._commit_failed = True
if isinstance(exc, OrcapodDatabaseError):
exc.add_context(node_label=self.label)
raise
self._rows_since_commit = 0

def _write_pipeline_record(
self,
record_id: bytes,
record: pa.Table,
result_id: bytes,
is_ephemeral: bool,
) -> None:
"""Queue a readable record with a dependency on its result's commit."""
database = self._require_pipeline_database()
path = self._versioned_pipeline_path
pod = self._ephemeral_cached_pod if is_ephemeral else self._cached_function_pod
if self._commit_every_rows is not None and isinstance(pod, CachedFunctionPod):
result_database = pod.result_database
try:
self._commit_result_table()
pipeline_database.flush()
except OrcapodDatabaseReadError:
dependency = result_database.pending_result(pod.record_path, result_id)
except OrcapodDatabaseError:
raise
except Exception as exc:
raise OrcapodDatabaseWriteError(
pipeline_database.base_path, node_label=self.label, flush=True
database_write_path(result_database, pod.record_path),
node_label=self.label,
flush=True,
table_flush=True,
) from exc
except OrcapodDatabaseError as exc:
self._commit_failed = True
exc.add_context(node_label=self.label)
raise
self._rows_since_commit = 0
if dependency is not None:
with database.guard_record(path, record_id, dependency):
database.add_record(path, record_id, record, skip_duplicates=True)
return
database.add_record(path, record_id, record, skip_duplicates=True)

def _row_processed(self) -> None:
"""Count a processed row; commit once a batch is full (only inside a job run)."""
Expand All @@ -2402,21 +2421,16 @@ def _commit_at_node_end(self) -> None:
def _commit_after_failure(self, run_error: BaseException) -> None:
"""Commit what succeeded before a failure, never replacing the run's error.

The job flushes its database after a failed run, but not a node's ephemeral
database, a separate database, and not table by table: so the node commits its
ephemeral results and then its result table here, best-effort (a failure
is noted on ``run_error``). Skipped when the failure was this node's own
commit: the job's flush is that commit's one retry.
A node that already tried its commit withdrew records whose results
failed. The job can still retry pending writes through its database.
A failure here is noted on the run's original error.

Args:
run_error: The error the node is failing with.
"""
if self._commit_every_rows is None or self._commit_failed:
return
ephemeral = self._ephemeral_database_in_use()
if ephemeral is not None:
commit_best_effort(run_error, ephemeral.flush)
commit_best_effort(run_error, self._commit_result_table)
commit_best_effort(run_error, self._commit_pending_writes)

@staticmethod
def _commit_or_end_node(
Expand Down Expand Up @@ -3202,13 +3216,10 @@ def _add_pipeline_record(
)

try:
pipeline_database.add_record(
self._versioned_pipeline_path,
versioned_entry_id,
combined_record,
skip_duplicates=True,
self._write_pipeline_record(
versioned_entry_id, combined_record, data_record_id.bytes, is_ephemeral
)
except OrcapodDatabaseReadError:
except OrcapodDatabaseError:
# The stored table could not be read: report the read failure itself.
raise
except Exception as exc:
Expand Down
Loading
Loading