nsivabalan commented on code in PR #20495: URL: https://github.com/apache/hudi/pull/20495#discussion_r4452610469
########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/functional/TestStreamingSinkRecovery.scala: ########## @@ -0,0 +1,156 @@ +/* + * 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.hudi.functional + +import org.apache.hudi.{DataSourceWriteOptions, HoodieStreamingSink} +import org.apache.hudi.client.SparkRDDWriteClient +import org.apache.hudi.client.embedded.EmbeddedTimelineService +import org.apache.hudi.common.testutils.HoodieTestUtils +import org.apache.hudi.testutils.HoodieSparkClientTestBase + +import org.apache.spark.sql.{DataFrame, SQLContext} +import org.apache.spark.sql.execution.streaming.Sink +import org.apache.spark.sql.sources.StreamSinkProvider +import org.apache.spark.sql.streaming.{OutputMode, StreamingQueryException, Trigger} +import org.junit.jupiter.api.Assertions.{assertEquals, assertFalse, assertThrows, assertTrue} +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource + +import java.net.{InetSocketAddress, Socket} +import java.util.concurrent.{ConcurrentHashMap, CountDownLatch, TimeUnit} + +import scala.collection.JavaConverters._ + +/** Injects failure after Hudi commits but before addBatch returns to Spark's checkpoint writer. */ +class RecoveryTestSource extends StreamSinkProvider { + override def createSink(context: SQLContext, options: Map[String, String], partitions: Seq[String], + mode: OutputMode): Sink = { + val sink = new RecoveryTestSink(context, options, partitions, mode) + RecoveryTestSource.sinks.put(options("path"), sink) + sink + } +} + +object RecoveryTestSource { + val sinks = new ConcurrentHashMap[String, RecoveryTestSink]() +} + +class RecoveryTestSink(context: SQLContext, options: Map[String, String], partitions: Seq[String], mode: OutputMode) + extends HoodieStreamingSink(context, options, partitions, mode) { + val released = new CountDownLatch(1) + @volatile var serverPort = -1 + @volatile var resourcesCleared = false + + private def field(name: String): Option[AnyRef] = { + val f = classOf[HoodieStreamingSink].getDeclaredField(name) + f.setAccessible(true) + f.get(this).asInstanceOf[Option[AnyRef]] + } + + override def addBatch(batchId: Long, data: DataFrame): Unit = { + super.addBatch(batchId, data) + val client = field("writeClient").get.asInstanceOf[SparkRDDWriteClient[_]] + val service = field("embeddedTimelineService").get.asInstanceOf[EmbeddedTimelineService] + serverPort = service.getRemoteFileSystemViewConfig(client.getConfig).getRemoteViewServerPort + if (options.getOrElse("test.fail.after.commit", "false").toBoolean) { + throw new IllegalStateException("injected failure after Hudi commit before Spark checkpoint") + } + } + + override protected def reset(force: Boolean): Unit = { + super.reset(force) + resourcesCleared = field("writeClient").isEmpty && field("embeddedTimelineService").isEmpty + released.countDown() + } +} + +class TestStreamingSinkRecovery extends HoodieSparkClientTestBase { + @ParameterizedTest + @ValueSource(strings = Array("available-now", "stop", "failure")) + def testTerminalCleanupAndCommittedBatchReplay(termination: String): Unit = { + val spark = sqlContext.sparkSession + val source = s"$basePath/source" + val target = s"$basePath/target" + val checkpoint = s"$basePath/checkpoint" + val input = spark.sql("select 1 as id, 10L as ts, 'first' as value") + input.coalesce(1).write.json(source) + val options = Map( + "hoodie.table.name" -> "recovery", + "hoodie.datasource.write.recordkey.field" -> "id", + "hoodie.datasource.write.precombine.field" -> "ts", + "hoodie.datasource.write.partitionpath.field" -> "", + "hoodie.datasource.write.operation" -> "insert", Review Comment: The replay check (`canSkipBatch`) is only exercised on a COW insert. MOR, where commits are deltacommits and async compaction may be running, is where skip-on-replay is most likely to go wrong. Could the test also run for MOR (e.g. parameterize table type alongside `termination`)? Non-blocking. ########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/functional/TestStreamingSinkRecovery.scala: ########## @@ -0,0 +1,156 @@ +/* + * 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.hudi.functional + +import org.apache.hudi.{DataSourceWriteOptions, HoodieStreamingSink} +import org.apache.hudi.client.SparkRDDWriteClient +import org.apache.hudi.client.embedded.EmbeddedTimelineService +import org.apache.hudi.common.testutils.HoodieTestUtils +import org.apache.hudi.testutils.HoodieSparkClientTestBase + +import org.apache.spark.sql.{DataFrame, SQLContext} +import org.apache.spark.sql.execution.streaming.Sink +import org.apache.spark.sql.sources.StreamSinkProvider +import org.apache.spark.sql.streaming.{OutputMode, StreamingQueryException, Trigger} +import org.junit.jupiter.api.Assertions.{assertEquals, assertFalse, assertThrows, assertTrue} +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource + +import java.net.{InetSocketAddress, Socket} +import java.util.concurrent.{ConcurrentHashMap, CountDownLatch, TimeUnit} + +import scala.collection.JavaConverters._ + +/** Injects failure after Hudi commits but before addBatch returns to Spark's checkpoint writer. */ +class RecoveryTestSource extends StreamSinkProvider { + override def createSink(context: SQLContext, options: Map[String, String], partitions: Seq[String], + mode: OutputMode): Sink = { + val sink = new RecoveryTestSink(context, options, partitions, mode) + RecoveryTestSource.sinks.put(options("path"), sink) + sink + } +} + +object RecoveryTestSource { + val sinks = new ConcurrentHashMap[String, RecoveryTestSink]() +} + +class RecoveryTestSink(context: SQLContext, options: Map[String, String], partitions: Seq[String], mode: OutputMode) + extends HoodieStreamingSink(context, options, partitions, mode) { + val released = new CountDownLatch(1) + @volatile var serverPort = -1 + @volatile var resourcesCleared = false + + private def field(name: String): Option[AnyRef] = { Review Comment: Reading the private `writeClient` / `embeddedTimelineService` fields by name means a rename breaks this at runtime rather than compile time. A `private[hudi]` accessor on `HoodieStreamingSink` would be safer. Non-blocking. -- 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]
