kunwp1 commented on code in PR #9628:
URL: https://github.com/apache/texera/pull/9628#discussion_r4662121929


##########
amber/src/main/scala/org/apache/texera/web/service/ExecutionStatsService.scala:
##########
@@ -207,17 +216,47 @@ class ExecutionStatsService(
     client.registerCallback[ExecutionStateUpdate] {
       case ExecutionStateUpdate(state: WorkflowAggregatedState.Recognized)
           if state == COMPLETED || state == FAILED || state == KILLED =>
-        logger.info("Workflow execution terminated. Commit runtime 
statistics.")
-        try {
-          runtimeStatsWriter.close()
-        } catch {
-          case e: Exception =>
-            logger.error("Failed to close runtime statistics writer", e)
-        }
+        commitAndStop()
       case _ =>
     }
   )
 
+  /** A user kill or a fatal error tears the service down without a terminal 
state; commit here. */

Review Comment:
   Done in 518e2a7b9. The `FatalError` callback now calls `commitAndStop()` 
after recording the failure, so a failed run is committed as soon as it fails, 
and the doc comment is the one you suggested. Kills still commit on teardown; 
that commit now runs in the background so starting the next run doesn't wait 
for it.



##########
amber/src/main/scala/org/apache/texera/web/service/ExecutionStatsService.scala:
##########
@@ -207,17 +216,47 @@ class ExecutionStatsService(
     client.registerCallback[ExecutionStateUpdate] {
       case ExecutionStateUpdate(state: WorkflowAggregatedState.Recognized)
           if state == COMPLETED || state == FAILED || state == KILLED =>
-        logger.info("Workflow execution terminated. Commit runtime 
statistics.")
-        try {
-          runtimeStatsWriter.close()
-        } catch {
-          case e: Exception =>
-            logger.error("Failed to close runtime statistics writer", e)
-        }
+        commitAndStop()
       case _ =>
     }
   )
 
+  /** A user kill or a fatal error tears the service down without a terminal 
state; commit here. */
+  override def unsubscribeAll(): Unit = {
+    super.unsubscribeAll()
+    commitAndStop()
+  }
+
+  /**
+    * Commits the runtime statistics and stops the persist thread, once. The 
engine reports the
+    * final statistics right before the terminal state, and that report may 
still be queued on
+    * the persist thread. Closing the writer there, behind it, commits it; 
closing on the calling
+    * thread would flush before it and strand it in the buffer.
+    */
+  private def commitAndStop(): Unit = {
+    if (committed.compareAndSet(false, true)) {
+      logger.info("Workflow execution terminated. Commit runtime statistics.")
+      try {
+        // Bounded so a stuck write cannot hang the caller; shutdown still 
lets the close finish.
+        metricsPersistThread
+          .submit((() => runtimeStatsWriter.close()): Runnable)

Review Comment:
   Filed #9641 and linked it from the description's "Not included" paragraph.



##########
amber/src/main/scala/org/apache/texera/web/service/ExecutionStatsService.scala:
##########
@@ -207,17 +216,47 @@ class ExecutionStatsService(
     client.registerCallback[ExecutionStateUpdate] {
       case ExecutionStateUpdate(state: WorkflowAggregatedState.Recognized)
           if state == COMPLETED || state == FAILED || state == KILLED =>
-        logger.info("Workflow execution terminated. Commit runtime 
statistics.")
-        try {
-          runtimeStatsWriter.close()
-        } catch {
-          case e: Exception =>
-            logger.error("Failed to close runtime statistics writer", e)
-        }
+        commitAndStop()
       case _ =>
     }
   )
 
+  /** A user kill or a fatal error tears the service down without a terminal 
state; commit here. */
+  override def unsubscribeAll(): Unit = {
+    super.unsubscribeAll()
+    commitAndStop()
+  }
+
+  /**
+    * Commits the runtime statistics and stops the persist thread, once. The 
engine reports the
+    * final statistics right before the terminal state, and that report may 
still be queued on
+    * the persist thread. Closing the writer there, behind it, commits it; 
closing on the calling
+    * thread would flush before it and strand it in the buffer.
+    */
+  private def commitAndStop(): Unit = {
+    if (committed.compareAndSet(false, true)) {
+      logger.info("Workflow execution terminated. Commit runtime statistics.")
+      try {
+        // Bounded so a stuck write cannot hang the caller; shutdown still 
lets the close finish.
+        metricsPersistThread
+          .submit((() => runtimeStatsWriter.close()): Runnable)
+          .get(CommitTimeoutSeconds, TimeUnit.SECONDS)
+      } catch {
+        case e: InterruptedException =>
+          Thread.currentThread().interrupt()
+          logger.error("Interrupted while closing runtime statistics writer", 
e)
+        case _: TimeoutException =>
+          logger.warn(
+            s"Runtime statistics commit exceeded ${CommitTimeoutSeconds}s; 
continuing in background"
+          )
+        case e: Exception =>
+          logger.error("Failed to close runtime statistics writer", e)
+      } finally {
+        metricsPersistThread.shutdown()

Review Comment:
   Removed that paragraph from `WorkflowExecutionServiceSpec` in 518e2a7b9.



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