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


##########
client/src/main/scala/org/apache/celeborn/client/ApplicationHeartbeater.scala:
##########
@@ -42,10 +43,14 @@ class ApplicationHeartbeater(
         (Long, Long, Map[String, java.lang.Long], Map[String, 
java.lang.Long])),
     workerStatusTracker: WorkerStatusTracker,
     registeredShuffles: ConcurrentHashMap.KeySetView[Int, java.lang.Boolean],
-    cancelAllActiveStages: String => Unit) extends Logging {
+    cancelAllActiveStages: String => Unit,
+    clientMetrics: () => util.Map[String, ClientMetric] =
+      () => new util.HashMap[String, ClientMetric]()) extends Logging {

Review Comment:
   The default `clientMetrics` supplier allocates a new `HashMap` every time it 
is invoked. Since the map is only read when serializing heartbeats, returning 
an immutable empty map avoids allocations on the hot heartbeat path.



##########
common/src/main/scala/org/apache/celeborn/common/protocol/message/ControlMessages.scala:
##########
@@ -389,7 +390,10 @@ object ControlMessages extends Logging {
       applicationFallbackCounts: util.Map[String, java.lang.Long],
       needCheckedWorkerList: util.List[WorkerInfo],
       override var requestId: String = ZERO_UUID,
-      shouldResponse: Boolean = false) extends MasterRequestMessage
+      shouldResponse: Boolean = false,
+      clientMetrics: util.Map[String, ClientMetric] = new util.HashMap[String, 
ClientMetric](),
+      metricLabels: util.Map[String, String] = new util.HashMap[String, 
String]())

Review Comment:
   These default arguments allocate two `HashMap`s for every 
`HeartbeatFromApplication` constructed without explicit maps. Since the message 
treats these maps as read-only, using `Collections.emptyMap()` avoids 
unnecessary allocations.



##########
client/src/main/scala/org/apache/celeborn/client/ApplicationHeartbeater.scala:
##########
@@ -85,7 +90,10 @@ class ApplicationHeartbeater(
                 tmpApplicationFallbackCounts.asJava,
                 workerStatusTracker.getNeedCheckedWorkers().toList.asJava,
                 ZERO_UUID,
-                true)
+                true,
+                if (appMetricLabels.isEmpty) new util.HashMap[String, 
ClientMetric]()
+                else clientMetrics(),
+                appMetricLabels)

Review Comment:
   When no app metric labels are configured, this code allocates a new 
`HashMap` on every heartbeat even though the metrics payload will be empty. 
Using `Collections.emptyMap()` avoids per-heartbeat allocations.



##########
client/src/test/scala/org/apache/celeborn/client/WorkerStatusTrackerSuite.scala:
##########
@@ -159,6 +160,31 @@ class WorkerStatusTrackerSuite extends CelebornFunSuite {
       errors.get())
   }
 
+  test("recordWorkerFailure increments client worker-excluded counter and 
gauge") {

Review Comment:
   The test name claims both a counter and a gauge are incremented, but this 
test only validates the excluded-worker gauge (and 
WorkerStatusTracker.recordWorkerFailure does not increment any counter). 
Renaming the test will avoid misleading future readers.



##########
master/src/main/scala/org/apache/celeborn/service/deploy/master/ApplicationMetricsSource.scala:
##########
@@ -0,0 +1,80 @@
+/*
+ * 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.celeborn.service.deploy.master
+
+import java.util.{Map => JMap}
+import java.util.concurrent.TimeUnit
+
+import scala.collection.JavaConverters._
+
+import org.apache.celeborn.common.CelebornConf
+import org.apache.celeborn.common.internal.Logging
+import org.apache.celeborn.common.metrics.{ClientMetric, MetricType}
+import org.apache.celeborn.common.metrics.source.{AbstractSource, Role}
+import org.apache.celeborn.common.util.{JavaUtils, Utils}
+
+class ApplicationMetricsSource(conf: CelebornConf)
+  extends AbstractSource(conf, Role.MASTER) with Logging {
+  override val sourceName = "application"
+
+  private val masterClientMetricsEnabled = conf.masterClientMetricsEnabled
+  private val removedAppRetentionMs = 
conf.masterClientMetricsRemovedAppRetentionMs
+
+  // Tracking applications that have been terminated
+  private val removedAppIds =
+    JavaUtils.newConcurrentHashMap[String, java.lang.Long]()
+
+  startRemovedAppCleaner()
+
+  private def startRemovedAppCleaner(): Unit = {
+    val cleanTask: Runnable = new Runnable {
+      override def run(): Unit = Utils.tryLogNonFatalError {
+        val cutoff = System.currentTimeMillis() - removedAppRetentionMs
+        removedAppIds.entrySet().asScala.foreach { entry =>
+          if (entry.getValue < cutoff) {
+            removedAppIds.remove(entry.getKey, entry.getValue)
+          }
+        }
+      }
+    }
+    metricsCleaner.scheduleWithFixedDelay(cleanTask, 10, 10, TimeUnit.MINUTES)
+  }

Review Comment:
   `startRemovedAppCleaner()` runs even when master-side client metrics are 
disabled, and it evicts at a fixed 10-minute cadence, which can keep removed 
app IDs much longer than 
`celeborn.metrics.master.clientMetrics.removedApp.retentionMs` implies. 
Consider only starting the cleaner when enabled and scheduling based on the 
configured retention to better honor the setting and reduce background work.



##########
client/src/main/scala/org/apache/celeborn/client/LifecycleManager.scala:
##########
@@ -236,7 +255,10 @@ class LifecycleManager(val appUniqueId: String, val conf: 
CelebornConf) extends
       },
       workerStatusTracker,
       registeredShuffle,
-      reason => cancelAllActiveStages(reason))
+      reason => cancelAllActiveStages(reason),
+      () =>
+        clientSource.map(_.getMetricsSnapshot().asJava)
+          .getOrElse(new util.HashMap[String, ClientMetric]()))

Review Comment:
   When `clientSource` is disabled, this supplier allocates a new `HashMap` 
every time metrics are requested. Returning `Collections.emptyMap()` avoids 
repeated allocations.



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