HeartSaVioR opened a new pull request, #60657:
URL: https://github.com/apache/spark/pull/60657
### What changes were proposed in this pull request?
This PR makes the Python code of a streaming query see the artifacts of the
session, as it does in a batch query.
- `StreamExecution` runs the stream thread with the artifact state of
`sparkSessionForStream`. This is the clone of the session that runs the
batches. It has its own copy of the artifacts.
- `PythonMicroBatchStream.createReaderFactory` passes the current artifact
state to the reader factory. Before, it always passed none.
It also removes the unused `jobArtifactState` field of `StreamExecution`.
### Why are the changes needed?
With artifact isolation on (the default in Spark Connect), a streaming query
can't use the files and Python files that the session added with `addArtifacts`:
- In a Python UDF (with or without Arrow), `SparkFiles.get` returns a path
in the shared `userFiles-...` folder, not the session's folder, so the file
isn't found. A Python file added with `pyfile=True` can't be imported.
- A streaming Python data source has the same problem in `read`.
The same code works in a batch query.
SPARK-49249 made `SQLExecution` apply the session's artifact state when it
runs the jobs, and removed the wrapper that set the caller's state on the
stream thread. But a micro-batch is planned before `SQLExecution` starts, and
the Python plan nodes keep the state they are built with. So they got no state.
The removed wrapper used the caller's session. The batches run in a clone of
it with its own UUID (SPARK-49854). This PR uses the clone's state, so planning
and execution use the same state.
Alternatives considered:
- Restore the removed wrapper as it was. This sets the parent session's
state, while the jobs run under the clone's state. The Python workers would
look in the parent's folder, which only has the files if a batch query of the
parent ran on that executor first.
- Wrap only the planning of a micro-batch. This needs a change in each
engine (micro-batch, continuous) and misses other work on the stream thread,
for example building the sources and sinks.
- Use `artifactManager.withResources` around the thread. This also sets the
session's class loader on the stream thread. That is not needed for this bug,
so this PR only sets the artifact state, like the original wrapper.
### Does this PR introduce _any_ user-facing change?
Yes. Python UDFs and streaming Python data sources in a streaming query can
now use the session's files and Python files, with artifact isolation on.
Before, they failed with `FileNotFoundError` or `ModuleNotFoundError`.
Without artifact isolation (for example PySpark Classic, which turns it
off), nothing changes: the stream session's artifact state is empty, as before.
### How was this patch tested?
New tests:
- `ArtifactManagerSuite`: the stream thread plans a batch with the artifact
state of the stream's session; and with no state when isolation is off.
- `PythonStreamingDataSourceSuite`: a streaming Python data source reads a
file of the session.
- `test_artifact.py` (`ArtifactTestsMixin`, so it runs with Spark Connect,
Spark Connect on a local cluster, and Spark Classic):
- a streaming Python UDF, with and without Arrow, reads a file of the
session;
- a streaming Python UDF imports a Python file of the session;
- a streaming Python data source reads a file of the session.
Each new test with isolation on fails without the fix. The test with
isolation off passes with and without the fix, as it checks that nothing
changes.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
This pull request and its description were written by Isaac.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]