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
40 changes: 40 additions & 0 deletions changelog.d/ITL-1848.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
### Breaking changes and migration

- A pipeline or job no longer has a `graph` attribute. Use `job.dag` (or `pipeline.dag`)
for the compiled graph and `job.nodes[label]` for a node.
- `job.dag` (and `pipeline.dag`) is a `CompiledDAG`, a subclass of `OrcaDAG`, and its
`nodes()` view is a `CompiledNodeView`.
- `node in job.dag` (and in `pipeline.dag`) answers only for the DAG's own nodes. Any other
node, stream or invocation object raises `UndefinedGraphMembershipError`, a new error in
`orcapod.errors`: look the node up by label instead (ITL-1559).

### Changed

- A compiled pipeline or job holds each node under its node hash, and a node hash names one
node: two distinct nodes with one node hash are refused, never told apart (ITL-1848,
part of ITL-1559). A custom operator, which has no node hash yet, and the nodes below it
keep their capture key. Saved files are unchanged; a loaded graph keeps its saved keys
until saved files record node hashes (ITL-1864).
- One source object read through an explicit slot (`SourceNode.from_stream(source,
name=...)`) and directly in one pipeline or job is refused with `InconsistentSourceError`
at the capture that brings the second form, whether the names agree or not, and also
when the slot takes the source's own name (`SourceNode.from_stream(source)`). Graphs
imported with `from_invocations` are checked the same way.
- Two slots that would have one node hash are refused with `InconsistentSourceError` at the
capture that brings the second one: an explicit slot and another source object with the
same name, or a source and the slot another pipeline's compile built over it
(`other.nodes["a"]`). Read the source through one of them, or declare the second source
under its own slot name; for a compiled slot, which keeps its name, capture the source
itself in its place, or read every use through one slot declared from the source
(`SourceNode.from_stream(source, name=...)`).
- Setting `upstreams` to other streams on a step that a live pipeline or job holds as a
captured input, or as an upstream of one, raises `InconsistentSourceError`; build a new
step for the new inputs (following such a change is ITL-1847). Setting the same streams
again is still accepted, and so is the setter of a pipeline's or job's own compiled step
(`job.nodes[label].upstreams`), as before.
- Adding to a compiled DAG a node whose node hash another of its nodes holds raises
`InconsistentSourceError`, as does `job.as_pipeline()` when two of the job's nodes would
share one node hash.
- Loading a saved pipeline in which two distinct nodes share one node hash raises
`InconsistentSourceError`. Such a file could be written by an earlier
`Pipeline.save`; build the pipeline again and save it.
2 changes: 2 additions & 0 deletions docs/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ These exceptions and warnings report problems running pipelines, processing rows

::: orcapod.errors.PipelineJobRequiredError

::: orcapod.errors.UndefinedGraphMembershipError

::: orcapod.errors.DataFunctionUnavailableError

::: orcapod.errors.OutputTypeError
Expand Down
17 changes: 17 additions & 0 deletions docs/concepts/pipeline-job.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,23 @@ This includes a direct self-join alongside a join of two slots bound to the same
same function pod applied to both slots. Put the conflicting steps in separate jobs or use just
one form in the job.

Within one pipeline or job, a node hash names one node: two distinct nodes with one node
hash are refused, never told apart. A step's inputs are fixed while a live pipeline or job
holds it as a captured input, or as an upstream of one: setting its `upstreams` to other
streams raises `InconsistentSourceError` (following such a change is ITL-1847); build a new
step for the new inputs. The setter of a pipeline's or job's own compiled step
(`job.nodes[label].upstreams`) still works as before. The same rule holds wherever nodes reach a compiled graph: adding to `job.dag` a node
whose node hash another of its nodes holds, `job.as_pipeline()` of a job whose steps would
give two nodes one node hash, and loading a saved file in which two distinct nodes share one
node hash all raise `InconsistentSourceError`.

The compiled graph is `job.dag` (or `pipeline.dag`), a `CompiledDAG`, which is an `OrcaDAG`;
`job.dag.nodes()` is a `CompiledNodeView`. `job.nodes[label]` gives a node by its label. A
pipeline or job has no `graph` attribute. `node in job.dag` answers for the
DAG's own nodes; for any other node, stream or invocation object it raises
`UndefinedGraphMembershipError`, since membership of a node object is not defined yet
(ITL-1559): look the node up by label instead.


## Seeing the graph

Expand Down
14 changes: 14 additions & 0 deletions docs/concepts/sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ original bindings are restored. InconsistentSourceError names the changed
slots. Restore the original bindings or build a new job. A job can compile
and run with its replacement bindings. ITL-1559 tracks capture after rebinding.

A slot holds one source object. Read a source either through an explicit slot
(`SourceNode.from_stream(source, name="units")`) or directly, never both in one pipeline
or job: the capture that brings the second form raises `InconsistentSourceError`, under
the same name or another, and also when the slot takes the source's own name
(`SourceNode.from_stream(source)`). Likewise, two slots that would have one node hash are
refused: an explicit slot `units` and another source object named `units`, or a source and
the slot another pipeline's compile built over it (`other_pipeline.nodes["units"]`). The
capture that brings the second one raises `InconsistentSourceError`. For two sources, read
the source through one of them, or give the second its own slot name
(`SourceNode.from_stream(other, name="units_2")`). For another pipeline's compiled slot,
which keeps its name, capture the source itself in its place, or read every use through one
slot declared from the source (`SourceNode.from_stream(source, name="units_2")`). Until source equivalence is
defined (ITL-1553), a slot holds one source object.

An output stream returned by a run uses the slot bindings that were in effect when
its rows were produced. Binding any slot that feeds it makes that output stale,
even if you bind A to B and then back to A, or never hashed the output before
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/hashing.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,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_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 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), `pipeline/graph_keys.py:compiled_graph_key`, `pipeline/graph_keys.py:defined_node_hash` and `pipeline/base.py:AbstractPipelineBase._describe_capture_leaf` (a compiled node's node hash, the key the compiled graph holds it under, ITL-1848) |
| 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
33 changes: 31 additions & 2 deletions src/orcapod/core/identity_chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,39 @@ def _identity_read() -> Iterator[None]:
_reading_chains.reset(token)


_undefined_identity_notes: ContextVar[list[str] | None] = ContextVar(
"undefined_identity_notes", default=None
)


@contextmanager
def noting_undefined_identity() -> Iterator[list[str]]:
"""Note each undefined-identity refusal raised inside the block.

The refusal stays main's ``ValueError``; a compiled graph reads the notes to tell
a node without a node hash (a custom operator, and below it) from another
``ValueError`` (ITL-1848).
"""
notes: list[str] = []
token = _undefined_identity_notes.set(notes)
try:
yield notes
finally:
_undefined_identity_notes.reset(token)


def _undefined_identity(message: str) -> ValueError:
"""Main's refusal of an undefined identity, noted for a compiled graph."""
notes = _undefined_identity_notes.get()
if notes is not None:
notes.append(message)
return ValueError(message)


def refuse_identity_io(reason: str = "this source requires a schema") -> None:
"""Report why an identity read would need rows or a source callback."""
if reading_identity_chains():
raise ValueError(f"Pipeline identity is undefined: {reason}.")
raise _undefined_identity(f"Pipeline identity is undefined: {reason}.")


def reading_identity_chains() -> bool:
Expand All @@ -67,7 +96,7 @@ def require_builtin_operator_identity(operator: object) -> None:
from orcapod.core.operators import BUILTIN_OPERATOR_TYPES

if type(operator) not in BUILTIN_OPERATOR_TYPES:
raise ValueError(
raise _undefined_identity(
"Pipeline identity is undefined for a custom operator in this step."
)

Expand Down
10 changes: 10 additions & 0 deletions src/orcapod/core/nodes/function_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,18 @@ def upstreams(self) -> tuple[StreamProtocol, ...]:

@upstreams.setter
def upstreams(self, value: tuple[StreamProtocol, ...]) -> None:
"""Replace the node's input.

Raises:
ValueError: If ``value`` does not hold exactly one stream.
InconsistentSourceError: If a live pipeline or job captured this node as
an input and ``value`` holds another input (ITL-1848); nothing
changes.
"""
if len(value) != 1:
raise ValueError("FunctionPod can only have one upstream")
if self._captured_rewire_is_noop(tuple(value)):
return
with self._identity_lock:
self._input_stream = value[0]
invalidate_output_lineage()
Expand Down
4 changes: 4 additions & 0 deletions src/orcapod/core/nodes/operator_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,15 @@ def upstreams(self, value: tuple[StreamProtocol, ...]) -> None:
"""Replace the node's inputs.

Raises:
InconsistentSourceError: If a live pipeline or job captured this node as
an input and ``value`` holds other inputs (ITL-1848); nothing changes.
ValueError: If the node caches (``LOG`` or ``REPLAY``) and a source
feeding ``value`` is restricted (ITL-1218); nothing changes.
"""
from orcapod.pipeline.restrictions import refuse_restricted_operator_cache

if self._captured_rewire_is_noop(tuple(value)):
return
refuse_restricted_operator_cache(self, self.cache_mode, streams=value)
with self._identity_lock:
self._input_streams = value
Expand Down
13 changes: 13 additions & 0 deletions src/orcapod/core/nodes/source_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1743,8 +1743,21 @@ def from_stream(
# A loaded job's slot keeps all its saved state (ITL-1138, ITL-640),
# through the one method every copy of a slot uses.
node._take_saved_state_of(stream)
else:
# The stream a slot was declared from, named or not: a pipeline refuses
# to capture it directly as well (ITL-1848). Only weakly held.
try:
node._declared_from = weakref.ref(stream)
except TypeError:
pass
return node

# The stream a slot declared with ``from_stream`` was declared from (ITL-1848).
_declared_from: weakref.ReferenceType[StreamProtocol] | None = None
# Whether a pipeline's compile built this slot over a source it captured
# directly, rather than a user declaring it (ITL-1848).
_built_by_compile: bool = False


class SourceJobNode(ProvenanceMethods, SourceNodeBase):
"""Execution-ready source node wrapping an optional concrete stream.
Expand Down
43 changes: 43 additions & 0 deletions src/orcapod/core/streams/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,49 @@ def _was_recorded_by(self, tracker: TrackerProtocol) -> bool:
"""Check recording provenance without comparing stream hashes."""
return any(recording() is tracker for recording in self._recording_trackers)

# The pipelines and jobs that captured this stream as an input, or a stream
# above it (ITL-1848). Their compiled graphs hold the slot this stream names.
_capturing_graphs: tuple[ReferenceType[TrackerProtocol], ...] = ()

def _hold_for_capture(self, graph: TrackerProtocol) -> None:
"""Keep this stream's inputs as they are while *graph* holds the capture."""
held = tuple(entry for entry in self._capturing_graphs if entry() is not None)
if not any(entry() is graph for entry in held):
self._capturing_graphs = (*held, ref(graph))

def _captured_rewire_is_noop(self, inputs: tuple[StreamProtocol, ...]) -> bool:
"""Refuse new inputs for a stream a live pipeline or job captured (ITL-1848).

A captured stream names its slot by its content, so new inputs would rename
a slot a compiled graph already holds. Following such a change is ITL-1847.

Args:
inputs: The proposed inputs.

Returns:
``True`` when the stream is held and *inputs* are its current inputs, in
the same positions: assigning them again changes nothing. ``False`` when
no live pipeline or job holds the stream.

Raises:
InconsistentSourceError: If a live pipeline or job holds the stream and
*inputs* differ from its current inputs.
"""
if not any(entry() is not None for entry in self._capturing_graphs):
return False
current = self.upstreams
if len(inputs) == len(current) and all(
new is old for new, old in zip(inputs, current, strict=True)
):
return True
from orcapod.errors import InconsistentSourceError

raise InconsistentSourceError(
f"Cannot rewire {self.label!r} because a pipeline or job captured it as "
"an input (ITL-1559). Create a new descriptor for the new inputs; "
"rewiring a captured input is ITL-1847."
)

@property
def _identity_hasher(self) -> SemanticHasherProtocol:
"""Use the stream's structural hasher for the walk."""
Expand Down
10 changes: 10 additions & 0 deletions src/orcapod/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ class TypeIdentityError(ValueError):
"""


class UndefinedGraphMembershipError(TypeError):
"""Raised when ``in`` asks a compiled pipeline DAG about an object it does not hold.

A compiled DAG (``pipeline.dag``, ``job.dag``) answers ``in`` for the node objects
it holds. Asking about any other node, stream or pod invocation, even one that
compares equal, has no defined answer yet (ITL-1559): access a node by label with
``pipeline.nodes[label]`` or ``job.nodes[label]``.
"""


class RecursiveTypeError(TypeError):
"""Raised when a class refers back to itself through its fields.

Expand Down
Loading
Loading