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
3 changes: 3 additions & 0 deletions changelog.d/ITL-1767.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Changed

- Job compilation now refuses distinct steps that would share an execution graph vertex (ITL-1767). This includes a direct self-join alongside a join over two slots bound to one source, and the same function pod applied to both slots. Put the conflicting steps in separate jobs or use just one form.
8 changes: 8 additions & 0 deletions docs/concepts/pipeline-job.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,14 @@ print(job_c.nodes["bmi"].as_table().num_rows)

Every job also carries its blueprint as `job.pipeline`.

## Job graph limitations

Orcapod refuses to build a job graph when distinct steps would share an execution vertex (ITL-1767).
This includes a direct self-join alongside a join of two slots bound to the same source, or the
same function pod applied to both slots. Put the conflicting steps in separate jobs or use just
one form in the job.


## Seeing the graph

!!! caution "In development"
Expand Down
17 changes: 17 additions & 0 deletions docs/dev/design_issues/ITL-1767-PJCOLLISION1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## `src/orcapod/pipeline/job.py` — job execution graph identity

### PJCOLLISION1 — Equal steps in separate slots share one execution vertex
**Status:** open
**Severity:** high
**Issue:** ITL-1767

The capture graph keeps steps over different named source declarations. After those
slots are bound, a direct self-join and a two-slot join can compare equal as keys
in the execution graph, and a step below either join can do the same. Both steps
are visible in `job.nodes`, but only one receives current-run rows. Identical
unary pods over different slots bound to the same source have the same problem.

**Mitigation:** Job compilation refuses distinct steps that share an execution
vertex, even when no keys match or both steps already hold current results.
Sharing the source itself still works. The follow-up is to let both steps run
and read their rows independently.
2 changes: 1 addition & 1 deletion docs/reference/hashing.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ id is a hash (site 8b).
|---|---|
| observer events (`node_hash`) | `core/nodes/function_node.py:FunctionJobNode.execute`, `core/nodes/function_node.py:FunctionJobNode.async_execute`, `core/nodes/function_node.py:FunctionJobNode._execute_load_only`, `core/nodes/function_node.py:FunctionJobNode._async_execute_load_only`, `core/nodes/function_node.py:FunctionJobNode._async_execute_cache_only`, `core/nodes/operator_node.py:OperatorJobNode.execute`, `core/nodes/operator_node.py:OperatorJobNode.async_execute`, `core/nodes/source_node.py:SourceNodeBase.execute`, `core/nodes/source_node.py:SourceNodeBase.async_execute` |
| hooks (`pod_hash`) | `core/function_pod.py:_FunctionPodBase._build_post_run_payload` |
| in-memory graph keys and sort keys | `pipeline/base.py:AbstractPipelineBase.compile`, `pipeline/base.py:AbstractPipelineBase.compile.<locals>.<lambda>`, `pipeline/base.py:AbstractPipelineBase._validate_invocation`, `pipeline/base.py:AbstractPipelineBase._input_graph_key`, `pipeline/base.py:AbstractPipelineBase.to_invocations`, `pipeline/base.py:AbstractPipelineBase.from_invocations`, `pipeline/job.py:PipelineJob.compile`, `pipeline/restrictions.py:sources_read` (the source identity a restriction is kept under, ITL-1218) |
| in-memory graph keys and sort keys | `pipeline/base.py:AbstractPipelineBase._compile_nodes`, `pipeline/base.py:AbstractPipelineBase._compile_nodes.<locals>.<lambda>`, `pipeline/base.py:AbstractPipelineBase._validate_invocation`, `pipeline/base.py:AbstractPipelineBase._input_graph_key`, `pipeline/base.py:AbstractPipelineBase.to_invocations`, `pipeline/base.py:AbstractPipelineBase.from_invocations`, `pipeline/job.py:PipelineJob.compile`, `pipeline/restrictions.py:sources_read` (the source identity a restriction is kept under, ITL-1218) |
| in-memory capture keys (ITL-1590) | `pipeline/pod_invocation.py:PodInvocation.capture_keys` |
| in-memory source registration keys | `pipeline/source_consistency.py:require_consistent_sources` |
| in-memory key for an in-flight function result (ITL-1101) | `core/cached_function_pod.py:CachedFunctionPod._async_process_once` |
Expand Down
8 changes: 8 additions & 0 deletions src/orcapod/pipeline/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,12 @@ def compile(self) -> None:
ValueError: If one source is read under two different restrictions
(ITL-1218), judged from what the recorded streams read now.
"""
self._compile_nodes()

def _compile_nodes(
self, validate_nodes: Callable[[Iterable[NodeT]], None] | None = None
) -> None:
"""Build and publish compiled nodes, optionally checking them first."""
import networkx as _nx

from orcapod.pipeline.restrictions import require_one_restriction
Expand Down Expand Up @@ -718,6 +724,8 @@ def compile(self) -> None:
i += 1

node_map = self._ordered_node_map(node_map, self._hash_graph)
if validate_nodes is not None:
validate_nodes(node_map.values())
self._persistent_node_map = node_map
self._nodes.clear()
self._nodes.update((node.label, node) for node in node_map.values())
Expand Down
73 changes: 61 additions & 12 deletions src/orcapod/pipeline/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import logging
import sys
import uuid
from collections.abc import Iterable, Iterator, Sequence
from collections.abc import Callable, Iterable, Iterator, Sequence
from copy import deepcopy
from dataclasses import dataclass
from itertools import chain
Expand Down Expand Up @@ -104,6 +104,24 @@ def _backend(database: WriteDatabase) -> WriteDatabase:
return database


def _refuse_shadowed_steps(nodes: Iterable[JobNode]) -> None:
"""Refuse distinct steps that would share an object-keyed DAG vertex (ITL-1767)."""
steps: dict[JobNode, JobNode] = {}
for node in nodes:
# Source slots can safely read the same source. Steps hold their own run
# rows and each needs a vertex to receive them. Use the DAG's own Python
# key semantics: equal content hashes alone do not imply equal keys.
if isinstance(node, SourceJobNode):
continue
previous = steps.setdefault(node, node)
if previous is not node:
raise ValueError(
f"Steps {previous.label!r} and {node.label!r} have the same "
"identity in this job (ITL-1767). Put them in separate jobs or use "
"only one of the two forms."
)


def _supports_attachment_enrollment(database: WriteDatabase) -> bool:
"""Recognize stores whose durable rows we can distinguish from pending ones."""
return isinstance(
Expand Down Expand Up @@ -295,6 +313,7 @@ def __init__(
self._unresolved_database: dict[str, Any] | None = None
self._execution_context = execution_context
self._auto_compile = auto_compile
self._has_uncompiled_invocations = False
# name → bound concrete source (user-facing API: job.sources).
self._sources: dict[str, cp.StreamProtocol] = {}
# Saved configs for untouched slots whose sources could not be rebuilt on load.
Expand Down Expand Up @@ -328,7 +347,17 @@ def __exit__(self, exc_type=None, exc_value=None, traceback=None) -> None:
self, exc_type, exc_value, traceback
)
if exc_type is None and self._auto_compile:
self.compile()
if not self._compiled or self._has_uncompiled_invocations:
self.compile()
else:
# A block that accepted no invocations can leave bindings partway
# through a restore. Keep the normal refresh, and check the
# bindings on an explicit compile or before running.
self._compile_job()

def _record_invocation(self, invocation: PodInvocation) -> None:
super()._record_invocation(invocation)
self._has_uncompiled_invocations = True

def _input_graph_key(self, upstream: cp.StreamProtocol) -> str:
"""Reuse an active slot's recorded key when capturing from that slot."""
Expand Down Expand Up @@ -438,8 +467,7 @@ def _source_node_from_recording(self, key: str) -> SourceJobNode:
def compile(self) -> None:
"""Compile recorded invocations into a frozen DAG of job nodes.

Delegates to ``AbstractPipelineBase.compile()`` which uses
``source_node_class``, ``function_node_class``, and
The base class uses ``source_node_class``, ``function_node_class``, and
``operator_node_class`` to create job node instances.

Concrete source bindings already in ``_sources`` are preserved.
Expand All @@ -451,13 +479,24 @@ def compile(self) -> None:

If ``_database`` is set, distributes databases to all function and
operator job nodes.

Raises:
ValueError: If distinct steps would share a vertex in the job graph
and one would lose its current-run rows (ITL-1767).
"""
self._compile_job(_refuse_shadowed_steps)

def _compile_job(
self, validate_nodes: Callable[[Iterable[JobNode]], None] | None = None
) -> None:
"""Rebuild the graph and wire the compiled job's bindings and databases."""
previous_slots = [
node
for node in self._persistent_node_map.values()
if isinstance(node, SourceJobNode)
]
super().compile()
self._compile_nodes(validate_nodes)
self._has_uncompiled_invocations = False

for slot in previous_slots:
slot._set_binding_owner(None) # pyright: ignore[reportPrivateUsage] # the replaced slot no longer belongs to this job
Expand Down Expand Up @@ -599,7 +638,8 @@ def from_pipeline(
A new ``PipelineJob`` ready to run (or ``bind()`` further).

Raises:
ValueError: If *pipeline* has not been compiled.
ValueError: If *pipeline* has not been compiled or the supplied
bindings would make distinct steps share a job graph vertex (ITL-1767).
"""
from orcapod.core.nodes.source_node import SourceNodeBase

Expand Down Expand Up @@ -636,6 +676,7 @@ def from_pipeline(
# Bind concrete sources and distribute databases.
if bound_sources:
job.bind(sources=bound_sources)
_refuse_shadowed_steps(job._persistent_node_map.values())
if database is not None:
job._distribute_databases()

Expand Down Expand Up @@ -1570,9 +1611,6 @@ def _prepare_run(
(f"node {node.label!r}", node)
for node in self._persistent_node_map.values()
)
# Once source preflight passes, stored row counts may change during the run.
self._last_run_finished = False

hash_dag: OrcaDAG[str] = OrcaDAG()
for u, v in self._graph_edges:
hash_dag.add_edge(u, v)
Expand Down Expand Up @@ -1621,11 +1659,20 @@ def _prepare_run(
"results with node.as_source()."
)

# Later bindings can make previously distinct graph vertices equal.
runnable_nodes = [
node
for key, node in self._persistent_node_map.items()
if key not in excluded_hashes
]
_refuse_shadowed_steps(runnable_nodes)
# Once preflight passes, stored row counts may change during the run.
self._last_run_finished = False

# --- Build execution OrcaDAG from existing node objects (no cloning) ---
exec_dag: OrcaDAG = OrcaDAG()
for node_hash, node in self._persistent_node_map.items():
if node_hash not in excluded_hashes:
exec_dag.add_node(node)
for node in runnable_nodes:
exec_dag.add_node(node)
for u_hash, v_hash in self._graph_edges:
if (
u_hash not in excluded_hashes
Expand Down Expand Up @@ -2621,6 +2668,8 @@ def load(
# node map. This makes job.dag consistent with the compiled-job path
# (where compile() builds _node_graph from job nodes).
# Callers needing blueprint nodes for rendering use job.pipeline.dag.
# Stored views read the persistent nodes directly. Check for shadowed
# steps if a load-only run builds an execution graph to fill live views.
job_dag: OrcaDAG = OrcaDAG()
for node in job._persistent_node_map.values():
if isinstance(node, RunHoldingNode):
Expand Down
Loading
Loading