sarutak commented on code in PR #59947:
URL: https://github.com/apache/spark/pull/59947#discussion_r4439081632
##########
docs/sql-native-data-sources.md:
##########
@@ -325,12 +325,26 @@ A session finds the packages in two places, and the
executors get them automatic
The packages of a session take precedence over the installed libraries, and
the package that a
session added last over its other packages, also the configured ones: a
session cannot remove an
-artifact, or add a file with the same name again, so it upgrades a package by
adding the new
-version under another file name, such as `rust_range-1.1.sparkpkg`. Distinct
file names are also
-needed because the sessions that are not isolated share the files they add. A
query fails if more
-than one configured package provides its data source. Spark ignores, and logs,
a package that it
-cannot read or whose version it does not support, unless its manifest lists
the data source that a
-query uses and no other package or installed library provides it.
+artifact, or add another file with the name of one that it added, so it
upgrades a package by adding
+the new version under another file name, such as `rust_range-1.1.sparkpkg`.
Distinct file names are
+also needed because the sessions that are not isolated share the files they
add. A query fails if
+more than one configured package provides its data source. `spark.addArtifact`
fails for a file that
+is not a valid package, or for a package whose version Spark does not support,
or that has no
+library for the platform of the driver, which loads the library to plan a
query, instead of adding a
+package that the session could not use: the session would ignore it and
silently keep using the
+package or library that it used before, or fail every query on its data
sources. After the failure,
+the session keeps the packages that it had, and a call that adds several
artifacts in Scala or Java
+still adds the others. In Python, a rejected package stops the rest of the
call: without Spark
+Connect, `spark.addArtifacts` adds the packages first, one at a time, so only
the packages before it
+are added; with Spark Connect, it adds the paths one at a time, so the paths
before the package are
+added. Spark does not load the library when it checks a package, so a library
that fails to load
+fails the queries that use it. Nor does it check that the driver can run
native data sources at all,
+which requires Java 22 or later and a platform where the C data interface of
Arrow Java loads:
+there, the add succeeds, and the queries fail with
`NATIVE_DATA_SOURCE_UNSUPPORTED_JAVA_VERSION` or
Review Comment:
Right, thanks. Fixed in 3517d4f733a in the three places: the guide, the doc
of `checkAdded` and the description now say that before Java 22 a valid package
is added and the queries fail, and that on a platform where the C data
interface of Arrow Java does not load, such as Linux with musl, only a package
with a library for that platform is added, and the queries then fail. The guide
also names the errors, `NATIVE_DATA_SOURCE_UNSUPPORTED_JAVA_VERSION` and
`NATIVE_DATA_SOURCE_UNSUPPORTED_PLATFORM`.
##########
sql/core/src/main/scala/org/apache/spark/sql/artifact/ArtifactManager.scala:
##########
@@ -339,7 +451,8 @@ class ArtifactManager(session: SparkSession) extends
AutoCloseable with Logging
artifact.path,
d.path,
fragment = None,
- deleteStagedFile = false)
+ deleteStagedFile = false,
+ sourceName = Some(d.path.toString))
Review Comment:
Good point, I'd rather keep that. Fixed in 3517d4f733a as you suggested:
`addLocalArtifacts` uses the private overload only for a local native data
source package that Spark checks, whose errors name the file of the user, and
the other local files go through the public `addArtifact` again, as before this
PR. A new test in `ArtifactManagerSuite`, "Add local artifacts through the
public addArtifact", overrides the public `addArtifact` in a subclass and
checks that it sees a local file that `addLocalArtifacts` adds; it fails if
every local file goes through the overload. A local `.sparkpkg` file artifact
does not reach an override while native data sources are enabled. Such a file
has been a package since SPARK-60101, in 4.4, and the migration guide covers
that change; in 4.3, classic Python added such files with `sc.addFile`, not
through `addLocalArtifacts`.
##########
sql/core/src/main/scala/org/apache/spark/sql/artifact/ArtifactManager.scala:
##########
@@ -204,6 +210,96 @@ class ArtifactManager(session: SparkSession) extends
AutoCloseable with Logging
// Convert the normalized string back to a Path object
Paths.get(normalizedPathString).normalize()
}
+
+ /**
+ * Returns true if a file artifact with the same content is at `target`
already, which makes
+ * adding it idempotent, and fails if another file is there: an artifact is
not overwritten.
+ */
+ private def artifactExists(
+ normalizedRemoteRelativePath: Path,
+ serverLocalStagingPath: Path,
+ target: Path): Boolean = {
+ if (Files.exists(target)) {
+ if (Utils.contentEquals(target.toFile, serverLocalStagingPath.toFile)) {
+ return true
+ }
+ throw artifactAlreadyExistsError(normalizedRemoteRelativePath)
+ }
+ false
+ }
+
+ private def artifactAlreadyExistsError(normalizedRemoteRelativePath: Path):
Throwable = {
+ new SparkRuntimeException(
+ "ARTIFACT_ALREADY_EXISTS",
+ Map("normalizedRemoteRelativePath" ->
normalizedRemoteRelativePath.toString)
+ )
+ }
+
+ /**
+ * Transfers a native data source package to `target` if Spark can use it,
see
+ * [[NativeDataSourcePackage.checkAdded]], so that it never is at the path
of an artifact
+ * otherwise. Returns false if the session has the same file already. A file
of the user is
+ * compared with the artifact first, so that adding the same file again does
not copy it. It is
+ * then copied to a temporary directory, outside of the artifacts, which the
file server serves
+ * and a clone of the session copies, and the copy is checked and then
moved, so that the session
+ * gets the bytes that are checked. Errors name the file of the user, or the
artifact if Spark
+ * staged the file for it, such as for a Spark Connect client.
+ */
+ private def addNativeDataSourcePackage(
+ normalizedRemoteRelativePath: Path,
+ serverLocalStagingPath: Path,
+ target: Path,
+ deleteStagedFile: Boolean,
+ sourceName: Option[String]): Boolean = {
+ def exists(source: Path): Boolean = {
+ // Utils.contentEquals fails for a directory, which cannot be the same
as a package anyway,
+ // so a directory with the name of an artifact, or an artifact that is a
directory, such
+ // as files/x.sparkpkg after files/x.sparkpkg/y was added, fails as an
existing artifact.
+ if (Files.exists(target) && (Files.isDirectory(source) ||
Files.isDirectory(target))) {
+ throw artifactAlreadyExistsError(normalizedRemoteRelativePath)
+ }
+ artifactExists(normalizedRemoteRelativePath, source, target)
+ }
+ if (!deleteStagedFile && exists(serverLocalStagingPath)) {
Review Comment:
Agreed, done in 3517d4f733a: both kinds are compared with the artifact
first, before any copy or check, then a local file is copied, the package is
checked without `Try`, and it is compared again right before the transfer. The
`Try`, `check.get`, the comment about the order and `!deleteStagedFile &&` are
gone, and a Spark Connect client that sends the same package again no longer
has it opened. As you say, only an add that races with another add of the same
artifact can now get the error of the check before `ARTIFACT_ALREADY_EXISTS`,
which the description says. That also changes what I wrote in three earlier
replies. In https://github.com/apache/spark/pull/59947#discussion_r4429231301
and https://github.com/apache/spark/pull/59947#discussion_r4429231709, the
re-add from bytes and the invalid bytes with the name of an artifact now end at
the first comparison too, so the comparison right before the transfer is only
reached by a racing add, and no test covers it; the description list
s it as not covered. The test with the invalid bytes now checks that a staged
package is compared with the artifact before the check: it fails if only local
files are compared first. In
https://github.com/apache/spark/pull/59947#discussion_r4422637258, a package is
no longer checked before the comparison and reported after it; it is compared
first, checked, and compared again right before the transfer, so the window
between the last comparison and the transfer is still as short as before this
PR. The `Try` of
https://github.com/apache/spark/pull/59947#discussion_r4429232078 is gone, as
above.
##########
sql/core/src/main/scala/org/apache/spark/sql/artifact/ArtifactManager.scala:
##########
@@ -204,6 +210,96 @@ class ArtifactManager(session: SparkSession) extends
AutoCloseable with Logging
// Convert the normalized string back to a Path object
Paths.get(normalizedPathString).normalize()
}
+
+ /**
+ * Returns true if a file artifact with the same content is at `target`
already, which makes
+ * adding it idempotent, and fails if another file is there: an artifact is
not overwritten.
+ */
+ private def artifactExists(
+ normalizedRemoteRelativePath: Path,
+ serverLocalStagingPath: Path,
+ target: Path): Boolean = {
+ if (Files.exists(target)) {
+ if (Utils.contentEquals(target.toFile, serverLocalStagingPath.toFile)) {
+ return true
+ }
+ throw artifactAlreadyExistsError(normalizedRemoteRelativePath)
+ }
+ false
+ }
+
+ private def artifactAlreadyExistsError(normalizedRemoteRelativePath: Path):
Throwable = {
+ new SparkRuntimeException(
+ "ARTIFACT_ALREADY_EXISTS",
+ Map("normalizedRemoteRelativePath" ->
normalizedRemoteRelativePath.toString)
+ )
+ }
+
+ /**
+ * Transfers a native data source package to `target` if Spark can use it,
see
+ * [[NativeDataSourcePackage.checkAdded]], so that it never is at the path
of an artifact
+ * otherwise. Returns false if the session has the same file already. A file
of the user is
+ * compared with the artifact first, so that adding the same file again does
not copy it. It is
+ * then copied to a temporary directory, outside of the artifacts, which the
file server serves
+ * and a clone of the session copies, and the copy is checked and then
moved, so that the session
+ * gets the bytes that are checked. Errors name the file of the user, or the
artifact if Spark
+ * staged the file for it, such as for a Spark Connect client.
+ */
+ private def addNativeDataSourcePackage(
+ normalizedRemoteRelativePath: Path,
+ serverLocalStagingPath: Path,
+ target: Path,
+ deleteStagedFile: Boolean,
+ sourceName: Option[String]): Boolean = {
+ def exists(source: Path): Boolean = {
Review Comment:
Fixed in 3517d4f733a: they are now `alreadyAdded` and
`artifactAlreadyAdded`, and their docs say that they return true if the same
file was added, and fail with `ARTIFACT_ALREADY_EXISTS` if another one was. The
doc of `addNativeDataSourcePackage`, which returns a `Boolean` too, says that
it returns false if the same file was added, and fails the same way if another
one was, or with the error of the check. The calls in `addArtifact` have a
short comment that they fail if another file is the artifact, and the first
call in `addNativeDataSourcePackage` says that `ARTIFACT_ALREADY_EXISTS` comes
before the check.
--
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]