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


##########
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:
   **Advisory:**
   
   Neither a kill nor a fatal error tears this service down; both only shut the 
client down (`ExecutionRuntimeService.scala:108`, 
`SyncExecutionResource.scala:382`, line 362 here). `WorkflowService` does it at 
the next run (`WorkflowService.scala:241`) or about 30 s after the last tab 
disconnects (`WorkflowService.scala:175`). Until then the stats endpoint misses 
every row since the last 4096-row flush, often the whole run. I'd also call 
`commitAndStop()` from the `FatalError` callback (line 358), where a failed run 
ends, and keep this fallback for kills:
   
   ```suggestion
     /**
       * Commits a run that never reported a terminal state, such as a user 
kill, when
       * WorkflowService tears the service down: at the next run or on session 
cleanup.
       */
   ```



##########
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:
   **Polish:**
   
   This shutdown makes the "Known, pre-existing" paragraph in 
`WorkflowExecutionServiceSpec.scala:92-95` false: it says 
`ExecutionStatsService` does not override `unsubscribeAll` and that the 
teardown test leaks the persist executor. Delete that paragraph in this PR so 
the suite header stops pointing at a leak that is gone.



##########
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:
   **Advisory:**
   
   The description defers this same fix for `ExecutionConsoleService`, whose 
writers are still closed on the callback thread 
(`ExecutionConsoleService.scala:214-219`) while `putOne` runs on 
`consoleWriterThread` (`ExecutionConsoleService.scala:294-301`). No issue 
tracks it yet. Please file one and link it from the description, so the known 
race does not depend on someone remembering this PR.



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