Copilot commented on code in PR #3696:
URL: https://github.com/apache/celeborn/pull/3696#discussion_r3341037219


##########
worker/src/main/scala/org/apache/celeborn/service/deploy/worker/storage/Flusher.scala:
##########
@@ -73,29 +73,38 @@ abstract private[worker] class Flusher(
             copyBytes = new Array[Byte](maxTaskSize.toInt)
           }
           while (!stopFlag.get()) {
-            val task = workingQueues(index).take()
-            val key = s"Flusher-$this-${Random.nextInt()}"
-            workerSource.sample(getFlushTimeMetric(), key) {
-              if (!task.notifier.hasException) {
-                try {
-                  val flushBeginTime = System.nanoTime()
-                  lastBeginFlushTime.set(index, flushBeginTime)
-                  task.flush(copyBytes)
-                  if (flushTimeMetric != null) {
-                    val delta = System.nanoTime() - flushBeginTime
-                    flushTimeMetric.update(delta)
+            val task = workingQueues(index).poll(1000, TimeUnit.MILLISECONDS)
+            if (task != null) {
+              val key = s"Flusher-$this-${Random.nextInt()}"
+              workerSource.sample(getFlushTimeMetric(), key) {
+                if (!task.notifier.hasException) {
+                  try {
+                    val flushBeginTime = System.nanoTime()
+                    lastBeginFlushTime.set(index, flushBeginTime)
+                    task.flush(copyBytes)
+                    if (flushTimeMetric != null) {
+                      val delta = System.nanoTime() - flushBeginTime
+                      flushTimeMetric.update(delta)
+                    }
+                  } catch {
+                    case t: Throwable =>
+                      val e = ExceptionUtils.wrapThrowableToIOException(t)
+                      task.notifier.setException(e)
+                      processIOException(e, DiskStatus.READ_OR_WRITE_FAILURE)
+                      logWarning(s"Flusher-$this-thread-$index encounter 
exception.", t)
                   }
-                } catch {
-                  case t: Throwable =>
-                    val e = ExceptionUtils.wrapThrowableToIOException(t)
-                    task.notifier.setException(e)
-                    processIOException(e, DiskStatus.READ_OR_WRITE_FAILURE)
-                    logWarning(s"Flusher-$this-thread-$index encounter 
exception.", t)
+                  lastBeginFlushTime.set(index, -1)
                 }

Review Comment:
   `lastBeginFlushTime` is only reset to `-1` when 
`!task.notifier.hasException`. If a task is skipped because `hasException` is 
already true, the thread can keep a stale `lastBeginFlushTime` value 
(previously it was reset for every dequeued task). Move the reset outside the 
guard so the thread is always marked idle after handling a task.



##########
worker/src/main/scala/org/apache/celeborn/service/deploy/worker/storage/Flusher.scala:
##########
@@ -73,29 +73,38 @@ abstract private[worker] class Flusher(
             copyBytes = new Array[Byte](maxTaskSize.toInt)
           }
           while (!stopFlag.get()) {
-            val task = workingQueues(index).take()
-            val key = s"Flusher-$this-${Random.nextInt()}"
-            workerSource.sample(getFlushTimeMetric(), key) {
-              if (!task.notifier.hasException) {
-                try {
-                  val flushBeginTime = System.nanoTime()
-                  lastBeginFlushTime.set(index, flushBeginTime)
-                  task.flush(copyBytes)
-                  if (flushTimeMetric != null) {
-                    val delta = System.nanoTime() - flushBeginTime
-                    flushTimeMetric.update(delta)
+            val task = workingQueues(index).poll(1000, TimeUnit.MILLISECONDS)
+            if (task != null) {
+              val key = s"Flusher-$this-${Random.nextInt()}"
+              workerSource.sample(getFlushTimeMetric(), key) {
+                if (!task.notifier.hasException) {
+                  try {
+                    val flushBeginTime = System.nanoTime()
+                    lastBeginFlushTime.set(index, flushBeginTime)
+                    task.flush(copyBytes)
+                    if (flushTimeMetric != null) {
+                      val delta = System.nanoTime() - flushBeginTime
+                      flushTimeMetric.update(delta)
+                    }
+                  } catch {
+                    case t: Throwable =>
+                      val e = ExceptionUtils.wrapThrowableToIOException(t)
+                      task.notifier.setException(e)
+                      processIOException(e, DiskStatus.READ_OR_WRITE_FAILURE)
+                      logWarning(s"Flusher-$this-thread-$index encounter 
exception.", t)
                   }
-                } catch {
-                  case t: Throwable =>
-                    val e = ExceptionUtils.wrapThrowableToIOException(t)
-                    task.notifier.setException(e)
-                    processIOException(e, DiskStatus.READ_OR_WRITE_FAILURE)
-                    logWarning(s"Flusher-$this-thread-$index encounter 
exception.", t)
+                  lastBeginFlushTime.set(index, -1)
                 }
-                lastBeginFlushTime.set(index, -1)
               }
               Utils.tryLogNonFatalError(returnBuffer(task.buffer, 
task.keepBuffer))
               task.notifier.numPendingFlushes.decrementAndGet()
+            } else {
+              allocator match {
+                case alloc: PooledByteBufAllocator =>
+                  // Free buffer pool memory to main direct memory when flush 
thread is idle.
+                  alloc.trimCurrentThreadCache
+                case _ =>
+              }

Review Comment:
   The new idle-path logic (`poll` timeout + `trimCurrentThreadCache`) changes 
Flusher thread behavior but doesn’t appear to be covered by tests. Adding a 
unit test that uses a mocked/spied `PooledByteBufAllocator` and asserts 
`trimCurrentThreadCache()` is invoked when the working queue stays empty would 
help prevent regressions.



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