spark-test commented on code in PR #60264:
URL: https://github.com/apache/spark/pull/60264#discussion_r4556985717
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -468,6 +474,50 @@ class MicroBatchExecution(
}
}
+ /**
+ * Rejects source names that do not fit the source tracking of this run.
Runs before any source is
+ * created, so that a rejected run leaves nothing behind in the checkpoint.
+ */
+ private def checkSourceNames(): Unit = {
+ val sources = StreamingSourceIdentifyingName.collectSources(analyzedPlan)
+ val namedSources = sources.map(_._2).filter(_.nameOpt.isDefined)
+ val unnamedSources = sources.zipWithIndex.collect {
+ case ((source, Unassigned), i) => s"Leaf position $i - $source"
+ }
+ def unnamedSourcesError: Throwable =
+ QueryCompilationErrors.unnamedStreamingSourcesWithEnforcementError(
+ unnamedSources.mkString("\n"))
+ val checkpointVersion = initialLatestOffsetSeq.map(_._2.version)
+ // Offset log VERSION_1 cannot track sources by name, so the config alone
is rejected there.
+ checkpointVersion.filter(_ < OffsetSeqLog.VERSION_2).foreach { version =>
+ if
(sparkSessionForStream.sessionState.conf.enableStreamingSourceEvolution) {
+ throw
QueryCompilationErrors.cannotEnableSourceEvolutionOnExistingCheckpointError(version)
+ }
+ }
+ (enforceNamedInCheckpoint, checkpointVersion) match {
+ case (Some(false), Some(version)) if namedSources.nonEmpty =>
+ // A positional checkpoint cannot start tracking sources by name.
Spark 4.2 ran the named
+ // sources of catalog tables by position, with their metadata in
sources/<name>, and a
+ // checkpoint written that way keeps running as before.
+ val fileManager = CheckpointFileManager.create(
+ new Path(resolvedCheckpointRoot),
sparkSession.sessionState.newHadoopConf())
+ val writtenUnderNames = namedSources.forall { name =>
+ fileManager.exists(new Path(
+ StreamingUtils.getMetadataPath(name, new AtomicLong(0L),
resolvedCheckpointRoot)))
+ }
+ if (!writtenUnderNames) {
+ throw
QueryCompilationErrors.cannotEnableSourceEvolutionOnExistingCheckpointError(version)
+ }
+ case _ if namedSources.nonEmpty && unnamedSources.nonEmpty =>
Review Comment:
**Blocking (P1):** This case rejects every plan that holds both a named and
an unnamed source, without consulting `enforceNamed`. A new query started with
`spark.sql.streaming.queryEvolution.enableSourceEvolution` off tracks its
sources by position, and the base branch runs it. A name reaches such a start
when its DataFrame was analyzed while the config was on. For example, with the
config on, create `a = spark.readStream.name("a").format("rate").load()`; turn
the config off, create `b = spark.readStream.format("rate").load()`, and start
`a.union(b)` on a new checkpoint. Reading the code, the base branch runs this
query by position with its source metadata in `sources/a` and `sources/0`,
while this change fails the start with
`UNNAMED_STREAMING_SOURCES_WITH_ENFORCEMENT`, whose message says the config is
enabled. Restarted on a positional checkpoint the base branch wrote, the same
query still runs, because the positional-checkpoint case above matches first
and finds `sources/a`, so
its first start and its restarts now disagree.
Only a run that tracks its sources by name needs every source named.
Guarding this case with `enforceNamed` keeps it ahead of
`CANNOT_DISABLE_ON_EXISTING_CHECKPOINT` for named checkpoints and leaves
positional runs as they are on the base branch.
This extends an [existing
discussion](https://github.com/apache/spark/pull/60264#discussion_r4520317211).
**Verification:**
- **Regression:** With the source evolution config on when one rate source
is named, and off when an unnamed rate source is created and their union starts
on a new checkpoint, the query processes data with source metadata in
sources/<name> and sources/0, then restarts unchanged and processes new data.
- **Behavior:** A checkpoint that tracks its sources by name, restarted with
one named and one unnamed source, still fails with
UNNAMED_STREAMING_SOURCES_WITH_ENFORCEMENT rather than
CANNOT_DISABLE_ON_EXISTING_CHECKPOINT, and creates no source.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -468,6 +474,50 @@ class MicroBatchExecution(
}
}
+ /**
+ * Rejects source names that do not fit the source tracking of this run.
Runs before any source is
+ * created, so that a rejected run leaves nothing behind in the checkpoint.
+ */
+ private def checkSourceNames(): Unit = {
+ val sources = StreamingSourceIdentifyingName.collectSources(analyzedPlan)
+ val namedSources = sources.map(_._2).filter(_.nameOpt.isDefined)
+ val unnamedSources = sources.zipWithIndex.collect {
+ case ((source, Unassigned), i) => s"Leaf position $i - $source"
+ }
+ def unnamedSourcesError: Throwable =
+ QueryCompilationErrors.unnamedStreamingSourcesWithEnforcementError(
+ unnamedSources.mkString("\n"))
+ val checkpointVersion = initialLatestOffsetSeq.map(_._2.version)
+ // Offset log VERSION_1 cannot track sources by name, so the config alone
is rejected there.
+ checkpointVersion.filter(_ < OffsetSeqLog.VERSION_2).foreach { version =>
+ if
(sparkSessionForStream.sessionState.conf.enableStreamingSourceEvolution) {
+ throw
QueryCompilationErrors.cannotEnableSourceEvolutionOnExistingCheckpointError(version)
+ }
+ }
+ (enforceNamedInCheckpoint, checkpointVersion) match {
+ case (Some(false), Some(version)) if namedSources.nonEmpty =>
+ // A positional checkpoint cannot start tracking sources by name.
Spark 4.2 ran the named
+ // sources of catalog tables by position, with their metadata in
sources/<name>, and a
+ // checkpoint written that way keeps running as before.
+ val fileManager = CheckpointFileManager.create(
+ new Path(resolvedCheckpointRoot),
sparkSession.sessionState.newHadoopConf())
+ val writtenUnderNames = namedSources.forall { name =>
+ fileManager.exists(new Path(
+ StreamingUtils.getMetadataPath(name, new AtomicLong(0L),
resolvedCheckpointRoot)))
+ }
+ if (!writtenUnderNames) {
+ throw
QueryCompilationErrors.cannotEnableSourceEvolutionOnExistingCheckpointError(version)
Review Comment:
**Nit (P3):** This reuses `CANNOT_ENABLE_ON_EXISTING_CHECKPOINT`, whose text
says the checkpoint uses an offset log format version "which does not support
the named source tracking required by streaming source evolution" and offers
only a fresh checkpoint. That held for its only caller on the base branch, the
config turned on below version 2. Here it is raised for other causes:
- A positional checkpoint at version 2, the `OffsetMap` format that named
tracking uses (`getMinRequiredOffsetLogVersion`), as the new positional v2 and
Spark 4.2 layout tests pin. Restarting the 4.2 layout with `IDENTIFIED BY
other` is told the format is the problem, while restarting it with `IDENTIFIED
BY src` runs.
- A run with the config off that reaches this case through a source named
while the config was on, which is told it is enabling source evolution.
Users are pointed at a fresh checkpoint, which discards the query's progress
and state, when restarting under the names their sources' metadata is stored
under would run: the stored names for a checkpoint written under names, or no
names for one written without them. Dropping the names on a checkpoint written
under them is no remedy for a source that keeps metadata there, because the
source would then start from an empty positional directory.
This extends an [existing
discussion](https://github.com/apache/spark/pull/60264#discussion_r4520317226).
See **Shared repair plan 1** in the review body.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -227,14 +243,9 @@ class MicroBatchExecution(
val disabledSources =
Utils.stringToSeq(sparkSession.sessionState.conf.disabledV2StreamingMicroBatchReaders)
- // Read the source evolution enforcement from the last written offset log
entry. If no entries
- // are found, use the session config value.
- val enforceNamed = initialLatestOffsetSeq.flatMap { case (_, offsetSeq) =>
- offsetSeq.metadataOpt.flatMap { metadata =>
- OffsetSeqMetadata.readValueOpt(metadata,
SQLConf.ENABLE_STREAMING_SOURCE_EVOLUTION)
- .map(_.toBoolean)
- }
-
}.getOrElse(sparkSessionForStream.sessionState.conf.enableStreamingSourceEvolution)
+ checkSourceNames()
+ // Every offset log entry records the decision from the query's session.
+
sparkSessionForStream.conf.set(SQLConf.ENABLE_STREAMING_SOURCE_EVOLUTION.key,
enforceNamed)
Review Comment:
**Nit (P3):** On a restart, `enforceNamed` is the checkpoint's recorded
value, and this line writes it into the query's session before
`populateStartOffsets` calls `OffsetSeqMetadata.setSessionConf`. That method
logs "Updating the value of conf ... in current session from X to Y" only when
the session holds an explicit value that differs from the recorded one.
Reading the code: restart a positional offset log version-2 checkpoint with
the config set to `true` in the user's session, as the new `OffsetSeqLogSuite`
restart test does. On the base branch the query's session still holds `true`
when the restore sets `false`, so the override is logged. With this change the
values already match, nothing is logged, and the restart runs by position
without any signal; log searches and alerts on that warning stop matching for
this key. The PR description says this commit changes nothing users see.
Results are unaffected.
Writing the decision only when the checkpoint records none would not be a
safe fix: in a Real-Time Mode restart that purges every offset entry, the
decision still comes from the purged entry while the restore no longer runs.
Logging the override here, before the write, keeps both the warning and the
recorded decision.
**Verification:**
- **Regression:** Restarting a positional checkpoint with the source
evolution config set to true in the user's session logs a warning naming the
config and the change from true to false, while the query runs by position.
- **Behavior:** Starting a new query, and restarting with a value that
matches the checkpoint's, log no override warning for the source evolution
config.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -468,6 +474,50 @@ class MicroBatchExecution(
}
}
+ /**
+ * Rejects source names that do not fit the source tracking of this run.
Runs before any source is
+ * created, so that a rejected run leaves nothing behind in the checkpoint.
+ */
+ private def checkSourceNames(): Unit = {
+ val sources = StreamingSourceIdentifyingName.collectSources(analyzedPlan)
+ val namedSources = sources.map(_._2).filter(_.nameOpt.isDefined)
+ val unnamedSources = sources.zipWithIndex.collect {
+ case ((source, Unassigned), i) => s"Leaf position $i - $source"
+ }
+ def unnamedSourcesError: Throwable =
+ QueryCompilationErrors.unnamedStreamingSourcesWithEnforcementError(
+ unnamedSources.mkString("\n"))
+ val checkpointVersion = initialLatestOffsetSeq.map(_._2.version)
+ // Offset log VERSION_1 cannot track sources by name, so the config alone
is rejected there.
+ checkpointVersion.filter(_ < OffsetSeqLog.VERSION_2).foreach { version =>
+ if
(sparkSessionForStream.sessionState.conf.enableStreamingSourceEvolution) {
+ throw
QueryCompilationErrors.cannotEnableSourceEvolutionOnExistingCheckpointError(version)
+ }
+ }
+ (enforceNamedInCheckpoint, checkpointVersion) match {
+ case (Some(false), Some(version)) if namedSources.nonEmpty =>
+ // A positional checkpoint cannot start tracking sources by name.
Spark 4.2 ran the named
+ // sources of catalog tables by position, with their metadata in
sources/<name>, and a
+ // checkpoint written that way keeps running as before.
+ val fileManager = CheckpointFileManager.create(
+ new Path(resolvedCheckpointRoot),
sparkSession.sessionState.newHadoopConf())
+ val writtenUnderNames = namedSources.forall { name =>
Review Comment:
**Blocking (P1):** This requires `sources/<name>` to exist for every named
source, but Spark only builds that path and hands it to the source.
`RatePerMicroBatchProvider`, `TextSocketSourceProvider` and `PythonScan`, which
every Python data source uses, ignore the `checkpointLocation` they receive,
and `Scan.toMicroBatchStream` documents it only as scratch space that can be
used.
Reading the code: with the config on, create `df =
spark.readStream.name("a").format("rate-micro-batch").load()`, then turn the
config off and start `df` on a new checkpoint. No case of `checkSourceNames`
matches, so the run tracks its sources by position, records offset log entries,
and never creates `sources/a`. Restarting `df` on the same checkpoint with the
config still off reaches this branch, finds no `sources/a`, and fails with
`CANNOT_ENABLE_ON_EXISTING_CHECKPOINT` and `existingVersion` 1, while the base
branch restarts it by position. This change thus accepts the first start and
refuses every restart, and its message says the user is enabling source
evolution although the config is off.
Connectors outside Spark that keep their progress elsewhere are refused the
same way, including Spark 4.2 or 4.3 offset log version-2 checkpoints of such a
catalog-table source restarted with the config on, which the comment above says
keep running. A source that keeps nothing under its metadata path has no
metadata to misread, so the refusal protects nothing for it.
This extends an [existing
discussion](https://github.com/apache/spark/pull/60264#discussion_r4520317239).
See **Shared repair plan 1** in the review body.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -468,6 +474,50 @@ class MicroBatchExecution(
}
}
+ /**
+ * Rejects source names that do not fit the source tracking of this run.
Runs before any source is
+ * created, so that a rejected run leaves nothing behind in the checkpoint.
+ */
+ private def checkSourceNames(): Unit = {
+ val sources = StreamingSourceIdentifyingName.collectSources(analyzedPlan)
+ val namedSources = sources.map(_._2).filter(_.nameOpt.isDefined)
+ val unnamedSources = sources.zipWithIndex.collect {
+ case ((source, Unassigned), i) => s"Leaf position $i - $source"
+ }
+ def unnamedSourcesError: Throwable =
+ QueryCompilationErrors.unnamedStreamingSourcesWithEnforcementError(
+ unnamedSources.mkString("\n"))
+ val checkpointVersion = initialLatestOffsetSeq.map(_._2.version)
+ // Offset log VERSION_1 cannot track sources by name, so the config alone
is rejected there.
+ checkpointVersion.filter(_ < OffsetSeqLog.VERSION_2).foreach { version =>
+ if
(sparkSessionForStream.sessionState.conf.enableStreamingSourceEvolution) {
+ throw
QueryCompilationErrors.cannotEnableSourceEvolutionOnExistingCheckpointError(version)
+ }
+ }
+ (enforceNamedInCheckpoint, checkpointVersion) match {
+ case (Some(false), Some(version)) if namedSources.nonEmpty =>
Review Comment:
**Non-blocking (P2):** This case treats an existing `sources/<name>` as
proof that the checkpoint keeps that source's metadata there, and it runs only
when the restart names a source. Reading the code, three restarts still start a
source from metadata that does not match its committed offsets, the failure
this check stops for a missing directory:
- `FileStreamSource` creates its metadata log, and so the directory, in its
constructor. A file-source query named `src` with the config on and stopped
before its first batch leaves an empty `sources/src` and no offset entry. An
unnamed run at offset log version 2 is then a new positional query, because
`verifyNewCheckpointDirectory` checks only offsets, commits and state, and its
batches keep their metadata in `sources/0`. Restarting it named `src` with the
config on passes this check, and the source reads the empty log.
- Digit-only names are valid, so on a positional checkpoint with `sources/0`
and `sources/1`, naming the two sources `1` and `0` in that order passes the
check, and each source reads the other's metadata.
- A checkpoint written under names, such as the Spark 4.2 layout the new
test builds with its metadata in `sources/src`, restarted without `IDENTIFIED
BY` names no source, so this case never runs and the source starts from an
empty `sources/0`. Spark 4.2 wrote this layout by the author's account, and at
its default offset log version 1 the name needs the config, which version 1
refuses, so dropping the name is the restart such a checkpoint is left with.
The base branch behaves the same for these inputs, and no comment defers
them.
This extends an [existing
discussion](https://github.com/apache/spark/pull/60264#discussion_r4520317248).
See **Shared repair plan 1** in the review body.
--
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]