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


##########
client/src/main/scala/org/apache/celeborn/client/CelebornClientSource.scala:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.client
+
+import java.util.concurrent.ConcurrentHashMap
+
+import scala.collection.JavaConverters._
+
+import org.apache.celeborn.common.CelebornConf
+import org.apache.celeborn.common.metrics.{ClientMetric, MetricType}
+import org.apache.celeborn.common.metrics.source.{AbstractSource, Role}
+
+/**
+ * Metrics source for the Celeborn client
+ */
+class CelebornClientSource(conf: CelebornConf) extends AbstractSource(conf, 
Role.CLIENT) {
+  override val sourceName = "client"
+
+  import CelebornClientSource._
+
+  // Tracks the counter baselines that have been acknowledged by the master, 
so we can send
+  // deltas rather than cumulative counts.
+  private val counterPrev = new ConcurrentHashMap[String, java.lang.Long]()
+
+  // Counter values captured by the most recent getMetricsSnapshot() that have 
not yet been
+  // acknowledged.
+  private val pendingCounterValues = new ConcurrentHashMap[String, 
java.lang.Long]()
+
+  addCounter(REGISTER_SHUFFLE_COUNT)
+  addCounter(REGISTER_SHUFFLE_FAIL_COUNT)
+  addCounter(UNREGISTER_SHUFFLE_COUNT)
+  addCounter(REVIVE_REQUEST_COUNT)
+  addCounter(REVIVE_FAIL_COUNT)
+  addCounter(SLOT_RESERVATION_FAIL_COUNT)
+  addCounter(SHUFFLE_FETCH_FAILURE_COUNT)
+  addCounter(SHUFFLE_DATA_LOST_COUNT)
+
+  def getMetricsSnapshot(): Map[String, ClientMetric] = {
+    val counterMetrics = counters().flatMap { c =>
+      val current = c.counter.getCount
+      val prev = 
Option(counterPrev.get(c.name)).map(_.longValue()).getOrElse(0L)
+      val delta = current - prev
+      pendingCounterValues.put(c.name, current)
+      if (delta > 0) Some(c.name -> ClientMetric(delta, MetricType.Counter))
+      else None
+    }
+    // Gauges: send the latest value as-is.
+    val gaugeMetrics = gauges().map(g =>
+      g.name -> 
ClientMetric(g.gauge.getValue.asInstanceOf[Number].longValue(), 
MetricType.Gauge))
+    (counterMetrics ++ gaugeMetrics).toMap
+  }
+
+  def commitSnapshot(): Unit = {
+    pendingCounterValues.entrySet().asScala.foreach { entry =>
+      counterPrev.put(entry.getKey, entry.getValue)
+    }
+    pendingCounterValues.clear()
+  }
+
+  def start(): Unit = startCleaner()

Review Comment:
   Updated



##########
common/src/main/scala/org/apache/celeborn/common/metrics/source/AbstractSource.scala:
##########
@@ -186,16 +205,20 @@ abstract class AbstractSource(conf: CelebornConf, role: 
String)
       })
   }
 
-  def addCounter(name: String): Unit = addCounter(name, Map.empty[String, 
String])
+  def addCounter(name: String): NamedCounter = addCounter(name, 
Map.empty[String, String])
 
-  def addCounter(name: String, labels: Map[String, String]): Unit = {
+  def addCounter(name: String, labels: Map[String, String]): NamedCounter = {
     val metricNameWithLabel = metricNameWithCustomizedLabels(name, labels)
-    namedCounters.putIfAbsent(
+    // Atomically get-or-create so the returned NamedCounter is guaranteed to 
be the instance
+    // currently resident in namedCounters. Callers must never 
re-.get(metricNameWithLabel)
+    // afterwards, since a concurrent removeCounter could have removed it in 
the meantime.
+    namedCounters.computeIfAbsent(

Review Comment:
   Done



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