malinjawi commented on code in PR #13678:
URL: https://github.com/apache/gluten/pull/13678#discussion_r4652514900


##########
backends-velox/src-delta33/test/scala/org/apache/spark/sql/delta/GlutenDeltaStatsLifecycleSuite.scala:
##########
@@ -146,6 +150,151 @@ class GlutenDeltaStatsLifecycleSuite extends 
GlutenQueryTest with SharedSparkSes
     }
   }
 
+  for (failBeforeSubmission <- Seq(true, false)) {
+    val timing = if (failBeforeSubmission) "before" else "during"
+    test(s"newRow releases the batch when statistics fail $timing submission") 
{
+      val before = statisticsThreads
+      val workerReady = new CountDownLatch(1)
+      val failWorker = new CountDownLatch(1)
+      val submitting = new CountDownLatch(1)
+      val closeCompleted = new CountDownLatch(1)
+      val completed = new CountDownLatch(1)
+      val closingThread = new AtomicReference[Thread]()
+      val failures = new ConcurrentLinkedQueue[Throwable]()
+      val completionChecks = new ConcurrentLinkedQueue[() => Unit]()
+      val workerFailure = new IllegalStateException("injected statistics 
worker failure")
+      val aggregate = spark.range(1).selectExpr(
+        "to_json(named_struct('numRecords', count(*))) AS stats")
+        .queryExecution.analyzed.collectFirst { case a: Aggregate => a }.get
+      val failStatistics: String => String = _ => {
+        assert(Thread.currentThread().getName.startsWith("Gluten Delta 
Statistics Writer - "))
+        workerReady.countDown()
+        assert(failWorker.await(30, TimeUnit.SECONDS), "Statistics worker was 
not released")
+        throw workerFailure
+      }
+      val statsExpr = ScalaUDF(
+        failStatistics,
+        StringType,
+        Seq(aggregate.aggregateExpressions.head))
+      val tracker = GlutenDeltaJobStatsTracker(new DeltaJobStatisticsTracker(
+        spark.sessionState.newHadoopConf(),
+        new Path("file:/tmp/gluten-stats-worker-failure"),
+        aggregate.child.output,
+        statsExpr))
+      val conf = spark.sessionState.conf.clone()
+      conf.setConfString(GlutenConfig.BENCHMARK_TASK_STAGEID.key, "0")
+      val thread = new Thread(
+        () => {
+          SparkSession.clearActiveSession()
+          try {
+            SQLConf.withExistingConf(conf) {
+              TaskResources.runUnsafe {
+                val context = TaskContext.get()
+                val usage = TaskResources.getSharedUsage()
+                completionChecks.add(
+                  () => {
+                    assert(context.isCompleted())
+                    assert(usage.current() == 0)
+                  })
+                val taskTracker = tracker.newTaskInstance()
+                val file = "file:/tmp/gluten-stats-worker-failure/part.parquet"
+                taskTracker.newFile(file)
+                // End the empty input to reach a controlled failure in the 
actual worker.
+                val closer = new Thread(
+                  () => {
+                    try {
+                      intercept[ExecutionException] {
+                        taskTracker.closeFile(file)

Review Comment:
   nit: both cases fail the worker after `closeFile` has sent `None`, so a 
worker dying mid-stream isn't exercised directly. Same `isDone` branch, so not 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to