danny0405 commented on code in PR #20516:
URL: https://github.com/apache/hudi/pull/20516#discussion_r4545737131


##########
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/spark/sql/hudi/streaming/HoodieStreamingSourceSafety.scala:
##########
@@ -0,0 +1,204 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.hudi.streaming
+
+import org.apache.hudi.{DataSourceReadOptions, DefaultSource, 
SparkAdapterSupport}
+import org.apache.hudi.common.model.WriteOperationType
+import org.apache.hudi.common.table.HoodieTableMetaClient
+import org.apache.hudi.common.table.timeline.{HoodieInstant, HoodieTimeline}
+import org.apache.hudi.common.util.CleanerUtils
+import org.apache.hudi.exception.HoodieException
+
+import org.apache.spark.{Dependency, OneToOneDependency, Partition, 
TaskContext}
+import org.apache.spark.rdd.RDD
+import org.apache.spark.sql.{DataFrame, SQLContext}
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.execution.LogicalRDD
+import org.apache.spark.sql.execution.datasources.{HadoopFsRelation, 
LogicalRelation}
+import org.apache.spark.sql.types.StructType
+
+import scala.collection.JavaConverters._
+
+/** Conservative reset and history validation for checkpointed latest-state 
streaming consumers. */
+private[streaming] object HoodieStreamingSourceSafety extends 
SparkAdapterSupport {
+  private class RebuildRequired(message: String) extends 
HoodieException(message)
+
+  // Spark re-plans the last committed batch during restart without executing 
it. Defer both
+  // validation and relation construction until the scheduler asks for this 
batch's partitions.
+  def defer(sqlContext: SQLContext, schema: StructType)(batch: => DataFrame): 
DataFrame = {
+    val rows = new DeferredBatchRDD(sqlContext, () => 
batch.queryExecution.logical match {
+      case relation: LogicalRDD => relation.rdd
+      case plan =>
+        // Keep the file-group reader's required filters and projection while 
executing this
+        // individual micro-batch as a bounded scan beneath the outer 
streaming LogicalRDD.
+        val boundedPlan = plan.transform {
+          case relation: LogicalRelation => relation.copy(isStreaming = false)
+        }
+        
sparkAdapter.getUnsafeUtils.createDataFrameFrom(sqlContext.sparkSession, 
boundedPlan).queryExecution.toRdd
+    })
+    sparkAdapter.internalCreateDataFrame(sqlContext.sparkSession, rows, 
schema, isStreaming = true)

Review Comment:
   Fixed in 6221a07dd42. Healthy batches now validate and return their scan 
plans directly; only safety rejections are deferred so Spark can discard 
restart plans. Added COW/MOR checks for both offset versions asserting the 
LogicalRelation remains visible, and corrected the batch test helper to 
preserve filters/projection. This also removes the deferred scan wrapper from 
the out-of-order regression that failed on Spark 4.2. Both streaming suites 
passed: Spark 3.5 25 passed with 2 expected skips; Spark 4.2 all 27 passed.



##########
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/spark/sql/hudi/streaming/HoodieStreamingSourceSafety.scala:
##########
@@ -0,0 +1,204 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.hudi.streaming
+
+import org.apache.hudi.{DataSourceReadOptions, DefaultSource, 
SparkAdapterSupport}
+import org.apache.hudi.common.model.WriteOperationType
+import org.apache.hudi.common.table.HoodieTableMetaClient
+import org.apache.hudi.common.table.timeline.{HoodieInstant, HoodieTimeline}
+import org.apache.hudi.common.util.CleanerUtils
+import org.apache.hudi.exception.HoodieException
+
+import org.apache.spark.{Dependency, OneToOneDependency, Partition, 
TaskContext}
+import org.apache.spark.rdd.RDD
+import org.apache.spark.sql.{DataFrame, SQLContext}
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.execution.LogicalRDD
+import org.apache.spark.sql.execution.datasources.{HadoopFsRelation, 
LogicalRelation}
+import org.apache.spark.sql.types.StructType
+
+import scala.collection.JavaConverters._
+
+/** Conservative reset and history validation for checkpointed latest-state 
streaming consumers. */
+private[streaming] object HoodieStreamingSourceSafety extends 
SparkAdapterSupport {
+  private class RebuildRequired(message: String) extends 
HoodieException(message)
+
+  // Spark re-plans the last committed batch during restart without executing 
it. Defer both
+  // validation and relation construction until the scheduler asks for this 
batch's partitions.
+  def defer(sqlContext: SQLContext, schema: StructType)(batch: => DataFrame): 
DataFrame = {
+    val rows = new DeferredBatchRDD(sqlContext, () => 
batch.queryExecution.logical match {
+      case relation: LogicalRDD => relation.rdd
+      case plan =>
+        // Keep the file-group reader's required filters and projection while 
executing this
+        // individual micro-batch as a bounded scan beneath the outer 
streaming LogicalRDD.
+        val boundedPlan = plan.transform {
+          case relation: LogicalRelation => relation.copy(isStreaming = false)
+        }
+        
sparkAdapter.getUnsafeUtils.createDataFrameFrom(sqlContext.sparkSession, 
boundedPlan).queryExecution.toRdd
+    })
+    sparkAdapter.internalCreateDataFrame(sqlContext.sparkSession, rows, 
schema, isStreaming = true)
+  }
+
+  private class DeferredBatchRDD(sqlContext: SQLContext,
+                                 @transient private val build: () => 
RDD[InternalRow])
+    extends RDD[InternalRow](sqlContext.sparkContext, Nil) {
+    @transient private lazy val delegate = build()
+
+    override protected def getPartitions: Array[Partition] = 
delegate.partitions
+
+    override protected def getDependencies: Seq[Dependency[_]] = Seq(new 
OneToOneDependency(delegate))
+
+    override def compute(split: Partition, context: TaskContext): 
Iterator[InternalRow] =
+      firstParent[InternalRow].iterator(split, context)
+  }
+
+  def strategy(parameters: Map[String, String]): String = {
+    val config = DataSourceReadOptions.STREAMING_READ_RECOVERY_STRATEGY
+    val value = parameters.getOrElse(config.key(), config.defaultValue())
+    require(Set("FAIL", "READ_UPTO_LATEST_COMMIT", 
"READ_LATEST").contains(value),
+      s"Invalid ${config.key()}=$value; expected FAIL, READ_UPTO_LATEST_COMMIT 
or READ_LATEST")
+    value
+  }
+
+  def recover(sqlContext: SQLContext,
+              metaClient: HoodieTableMetaClient,
+              schema: => StructType,
+              parameters: Map[String, String],
+              strategy: String,
+              start: String,
+              end: String,
+              completionTime: Boolean,
+              resumed: Boolean): Option[DataFrame] = {
+    lazy val recoverySchema = schema
+    try {
+      validate(metaClient, start, end, completionTime, resumed)
+      None
+    } catch {
+      case error: RebuildRequired => strategy match {
+        case "FAIL" => throw error

Review Comment:
   Documented in 6221a07dd42 in the config description, operational guide, 
migration note, and PR impact section. FAIL requires the sink to propagate 
execution errors. Hudi sinks must keep 
hoodie.datasource.write.streaming.ignore.failed.batch=false (the default); 
enabling it, or swallowing errors in a custom sink, can acknowledge a rejected 
batch and skip unread data. Rejections remain deferred to preserve Spark 
restart behavior; healthy scan plans are now eager.



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

Reply via email to