ericm-db commented on code in PR #60263:
URL: https://github.com/apache/spark/pull/60263#discussion_r4544298087


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -466,6 +464,52 @@ 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(
+      enforceNamed: Boolean,
+      enforceNamedInCheckpoint: Option[Boolean]): 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:
   Fixed the entry-without-metadata case in 5ca70516781: an offset entry with 
no metadata line now counts as positional, so naming sources on it is rejected 
as on any positional checkpoint (test: `positional checkpoint without offset 
metadata - restart with names is rejected before creating sources`). Without 
it, #60265, which drops the v1 check, would start such a checkpoint by name.
   
   The other layouts (a dropped name, an unnamed source beside named ones, 
empty or reused directories) start the same way as on master, and the spec 
keeps an unnamed restart of a positional checkpoint running by position. 
Checking the `sources/` layout at start-up adds a listing and new rejections to 
every positional restart, so I'll do it in a separate follow-up PR.
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -466,6 +464,52 @@ 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(
+      enforceNamed: Boolean,
+      enforceNamedInCheckpoint: Option[Boolean]): 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:
   The stack keeps message changes in #60265 (SPARK-61311), which rewrites this 
one to "The query names its streaming sources, but the checkpoint tracks them 
by position. To use source evolution, start the query with a fresh checkpoint." 
It no longer mentions the offset log version.
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -466,6 +464,52 @@ 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(
+      enforceNamed: Boolean,
+      enforceNamedInCheckpoint: Option[Boolean]): 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 =>
+        throw unnamedSourcesError

Review Comment:
   Since SPARK-60270, `FindDataSourceTable` keeps a catalog table's 
`NamedStreamingRelation`, so with the config off both examples now fail in 
analysis with `SOURCE_NAMING_NOT_SUPPORTED` before this check, on master as 
well as with this PR. With the config off, this case is reached only by a plan 
whose names were resolved under the config. #60265 also rewords the message to 
"when any source in the query is named, or when 
spark.sql.streaming.queryEvolution.enableSourceEvolution is enabled".
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/MicroBatchExecution.scala:
##########
@@ -466,6 +464,52 @@ 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.

Review Comment:
   Fixed in 5ca70516781: the comment now says a rejected run writes nothing 
under the checkpoint's `sources/` directory, which is what the existence check 
relies on.
   



##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/test/StreamingSourceEvolutionSuite.scala:
##########
@@ -530,6 +532,119 @@ class StreamingSourceEvolutionSuite extends StreamTest {
       s"Expected default value 'false' for missing config in V2, but got: 
$valueV2")
   }
 
+  // ================================
+  // Checkpoint Source Tracking Tests

Review Comment:
   Fixed in 5ca70516781. Applying each of the four changes to 
`checkSourceNames` now fails exactly one new test:
   1. `new query with the config - unnamed source analyzed without it is 
rejected before creating sources`
   2. `positional checkpoint - restart is rejected unless every named source is 
written under its name`
   3. `positional checkpoint written under source names keeps running with an 
unnamed source`
   4. `named checkpoint - restart with an unnamed source is rejected before 
creating sources`
   
   For 1, an unnamed catalog table read with the config on now fails in 
analysis (SPARK-60270), so the test reaches this case with a source analyzed 
before the config was set.
   



-- 
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]

Reply via email to