You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celeborn.apache.org by zh...@apache.org on 2023/02/13 04:04:49 UTC

[incubator-celeborn] branch main updated: [CELEBORN-279] Add user level push data speed metric (#1213)

This is an automated email from the ASF dual-hosted git repository.

zhouky pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-celeborn.git


The following commit(s) were added to refs/heads/main by this push:
     new 2068e6ae3 [CELEBORN-279] Add user level push data speed metric (#1213)
2068e6ae3 is described below

commit 2068e6ae377ca7d7936b9f2aff1b7363ec1bf064
Author: Rex(Hui) An <an...@126.com>
AuthorDate: Mon Feb 13 12:04:44 2023 +0800

    [CELEBORN-279] Add user level push data speed metric (#1213)
---
 .../common/metrics/source/AbstractSource.scala     | 40 ++++++++++++--
 .../celeborn/common/metrics/source/Source.scala    |  1 +
 .../congestcontrol/CongestionController.java       | 61 +++++++++++++++-------
 .../celeborn/service/deploy/worker/Worker.scala    |  6 +--
 .../service/deploy/worker/WorkerSource.scala       |  3 +-
 .../congestcontrol/TestCongestionController.java   | 57 +++++++++++++++++++-
 6 files changed, 137 insertions(+), 31 deletions(-)

diff --git a/common/src/main/scala/org/apache/celeborn/common/metrics/source/AbstractSource.scala b/common/src/main/scala/org/apache/celeborn/common/metrics/source/AbstractSource.scala
index 6b87b7694..23f59903b 100644
--- a/common/src/main/scala/org/apache/celeborn/common/metrics/source/AbstractSource.scala
+++ b/common/src/main/scala/org/apache/celeborn/common/metrics/source/AbstractSource.scala
@@ -65,21 +65,28 @@ abstract class AbstractSource(conf: CelebornConf, role: String)
   protected val namedGauges: java.util.List[NamedGauge[_]] =
     new java.util.ArrayList[NamedGauge[_]]()
 
-  def addGauge[T](name: String, f: Unit => T): Unit = addGauge(name, f, Map.empty[String, String])
+  def addGauge[T](
+      name: String,
+      gauge: Gauge[T],
+      labels: Map[String, String]): Unit = {
+    namedGauges.add(NamedGauge(name, gauge, labels + roleLabel))
+  }
 
   def addGauge[T](
       name: String,
       f: Unit => T,
       labels: Map[String, String]): Unit = {
-    val supplier: MetricRegistry.MetricSupplier[Gauge[_]] = new GaugeSupplier[T](f)
     if (!metricRegistry.getGauges.containsKey(metricNameWithLabels(name, labels + roleLabel))) {
+      val supplier: MetricRegistry.MetricSupplier[Gauge[_]] = new GaugeSupplier[T](f)
       val gauge = metricRegistry.gauge(metricNameWithLabels(name, labels + roleLabel), supplier)
-      namedGauges.add(NamedGauge(name, gauge, labels + roleLabel))
+      addGauge(name, gauge, labels)
     }
   }
 
+  def addGauge[T](name: String, f: Unit => T): Unit = addGauge(name, f, Map.empty[String, String])
+
   def addGauge[T](name: String, gauge: Gauge[T]): Unit = {
-    namedGauges.add(NamedGauge(name, gauge, Map(roleLabel)))
+    addGauge(name, gauge, Map.empty[String, String])
   }
 
   protected val namedTimers =
@@ -137,6 +144,20 @@ abstract class AbstractSource(conf: CelebornConf, role: String)
     }
   }
 
+  def removeGauge(name: String, labels: Map[String, String]): Unit = {
+    val labelString = MetricLabels.labelString(labels + roleLabel)
+
+    val iter = namedGauges.iterator()
+    while (iter.hasNext) {
+      val namedGauge = iter.next()
+      if (namedGauge.name.equals(name) && namedGauge.labelString.equals(labelString)) {
+        iter.remove()
+        metricRegistry.remove(metricNameWithLabels(name, labels + roleLabel))
+        return
+      }
+    }
+  }
+
   override def sample[T](metricsName: String, key: String)(f: => T): T = {
     sample(metricsName, key, Map.empty[String, String])(f)
   }
@@ -353,6 +374,17 @@ abstract class AbstractSource(conf: CelebornConf, role: String)
     sb.toString()
   }
 
+  override def destroy(): Unit = {
+    metricsCleaner.shutdown()
+    namedCounters.clear()
+    namedGauges.clear()
+    namedTimers.clear()
+    innerMetrics.clear()
+    metricRegistry.removeMatching(new MetricFilter {
+      override def matches(s: String, metric: Metric): Boolean = true
+    })
+  }
+
   protected def normalizeKey(key: String): String = {
     s"metrics_${key.replaceAll("[^a-zA-Z0-9]", "_")}_"
   }
diff --git a/common/src/main/scala/org/apache/celeborn/common/metrics/source/Source.scala b/common/src/main/scala/org/apache/celeborn/common/metrics/source/Source.scala
index 8135286a4..2e02efada 100644
--- a/common/src/main/scala/org/apache/celeborn/common/metrics/source/Source.scala
+++ b/common/src/main/scala/org/apache/celeborn/common/metrics/source/Source.scala
@@ -27,4 +27,5 @@ trait Source {
   def stopTimer(metricsName: String, key: String): Unit
   def incCounter(metricsName: String, incV: Long): Unit
   def getMetrics(): String
+  def destroy(): Unit
 }
diff --git a/worker/src/main/java/org/apache/celeborn/service/deploy/worker/congestcontrol/CongestionController.java b/worker/src/main/java/org/apache/celeborn/service/deploy/worker/congestcontrol/CongestionController.java
index 60496cf65..4d49f5932 100644
--- a/worker/src/main/java/org/apache/celeborn/service/deploy/worker/congestcontrol/CongestionController.java
+++ b/worker/src/main/java/org/apache/celeborn/service/deploy/worker/congestcontrol/CongestionController.java
@@ -30,11 +30,13 @@ import org.slf4j.LoggerFactory;
 import org.apache.celeborn.common.identity.UserIdentifier;
 import org.apache.celeborn.common.network.server.memory.MemoryManager;
 import org.apache.celeborn.common.util.ThreadUtils;
+import org.apache.celeborn.service.deploy.worker.WorkerSource;
 
 public class CongestionController {
 
   private static final Logger logger = LoggerFactory.getLogger(CongestionController.class);
   private static volatile CongestionController _INSTANCE = null;
+  private final WorkerSource workerSource;
 
   private final int sampleTimeWindowSeconds;
   private final long highWatermark;
@@ -50,12 +52,14 @@ public class CongestionController {
   private final ScheduledExecutorService removeUserExecutorService;
 
   protected CongestionController(
+      WorkerSource workerSource,
       int sampleTimeWindowSeconds,
       long highWatermark,
       long lowWatermark,
       long userInactiveTimeMills) {
     assert (highWatermark > lowWatermark);
 
+    this.workerSource = workerSource;
     this.sampleTimeWindowSeconds = sampleTimeWindowSeconds;
     this.highWatermark = highWatermark;
     this.lowWatermark = lowWatermark;
@@ -67,17 +71,25 @@ public class CongestionController {
         ThreadUtils.newDaemonSingleThreadScheduledExecutor("remove-inactive-user");
 
     this.removeUserExecutorService.scheduleWithFixedDelay(
-        this::removeInactiveUsers, 0, userInactiveTimeMills, TimeUnit.SECONDS);
+        this::removeInactiveUsers, 0, userInactiveTimeMills, TimeUnit.MILLISECONDS);
+
+    this.workerSource.addGauge(
+        WorkerSource.PotentialConsumeSpeed(), this::getPotentialConsumeSpeed);
   }
 
   public static synchronized CongestionController initialize(
+      WorkerSource workSource,
       int sampleTimeWindowSeconds,
       long highWatermark,
       long lowWatermark,
       long userInactiveTimeMills) {
     _INSTANCE =
         new CongestionController(
-            sampleTimeWindowSeconds, highWatermark, lowWatermark, userInactiveTimeMills);
+            workSource,
+            sampleTimeWindowSeconds,
+            highWatermark,
+            lowWatermark,
+            userInactiveTimeMills);
     return _INSTANCE;
   }
 
@@ -145,24 +157,18 @@ public class CongestionController {
         return false;
       }
 
-      UserBufferInfo userBufferInfo = userBufferStatuses.get(userIdentifier);
-      if (userBufferInfo != null) {
-        BufferStatusHub.BufferStatusNode userBufferStatus =
-            userBufferInfo.getBufferStatusHub().sum();
-
-        // If the user produce speed is higher that the avg consume speed, will congest it
-        long userProduceSpeed = userBufferStatus.numBytes() / sampleTimeWindowSeconds;
-        if (logger.isDebugEnabled()) {
-          logger.debug(
-              "The user {}, produceSpeed is {},"
-                  + " while consumeSpeed is {}, need to congest it: {}",
-              userIdentifier,
-              userProduceSpeed,
-              avgConsumeSpeed,
-              userProduceSpeed > avgConsumeSpeed);
-        }
-        return userProduceSpeed > avgConsumeSpeed;
+      // If the user produce speed is higher that the avg consume speed, will congest it
+      long userProduceSpeed = getUserProduceSpeed(userBufferStatuses.get(userIdentifier));
+      if (logger.isDebugEnabled()) {
+        logger.debug(
+            "The user {}, produceSpeed is {},"
+                + " while consumeSpeed is {}, need to congest it: {}",
+            userIdentifier,
+            userProduceSpeed,
+            avgConsumeSpeed,
+            userProduceSpeed > avgConsumeSpeed);
       }
+      return userProduceSpeed > avgConsumeSpeed;
     }
 
     return false;
@@ -176,7 +182,12 @@ public class CongestionController {
             user -> {
               logger.info("New user {} comes, initializing its rate status", user);
               BufferStatusHub bufferStatusHub = new BufferStatusHub(sampleTimeWindowSeconds);
-              return new UserBufferInfo(currentTimeMillis, bufferStatusHub);
+              UserBufferInfo userInfo = new UserBufferInfo(currentTimeMillis, bufferStatusHub);
+              workerSource.addGauge(
+                  WorkerSource.UserProduceSpeed(),
+                  () -> getUserProduceSpeed(userInfo),
+                  userIdentifier.toMap());
+              return userInfo;
             });
 
     BufferStatusHub.BufferStatusNode node = new BufferStatusHub.BufferStatusNode(numBytes);
@@ -208,6 +219,15 @@ public class CongestionController {
         / ((long) sampleTimeWindowSeconds * userBufferStatuses.size());
   }
 
+  private long getUserProduceSpeed(UserBufferInfo userBufferInfo) {
+    if (userBufferInfo != null) {
+      BufferStatusHub.BufferStatusNode userBufferStatus = userBufferInfo.getBufferStatusHub().sum();
+      return userBufferStatus.numBytes() / sampleTimeWindowSeconds;
+    }
+
+    return 0L;
+  }
+
   private void removeInactiveUsers() {
     try {
       long currentTimeMillis = System.currentTimeMillis();
@@ -220,6 +240,7 @@ public class CongestionController {
         UserBufferInfo userBufferInfo = next.getValue();
         if (currentTimeMillis - userBufferInfo.getTimestamp() >= userInactiveTimeMills) {
           userBufferStatuses.remove(userIdentifier);
+          workerSource.removeGauge(WorkerSource.UserProduceSpeed(), userIdentifier.toMap());
           logger.info(
               String.format(
                   "User: %s has been expired, remove it from rate limit list", userIdentifier));
diff --git a/worker/src/main/scala/org/apache/celeborn/service/deploy/worker/Worker.scala b/worker/src/main/scala/org/apache/celeborn/service/deploy/worker/Worker.scala
index 94b401caa..800cec31d 100644
--- a/worker/src/main/scala/org/apache/celeborn/service/deploy/worker/Worker.scala
+++ b/worker/src/main/scala/org/apache/celeborn/service/deploy/worker/Worker.scala
@@ -111,15 +111,11 @@ private[celeborn] class Worker(
     }
 
     CongestionController.initialize(
+      workerSource,
       conf.workerCongestionControlSampleTimeWindowSeconds.toInt,
       conf.workerCongestionControlHighWatermark.get,
       conf.workerCongestionControlLowWatermark.get,
       conf.workerCongestionControlUserInactiveIntervalMs)
-
-    val rateLimitController = CongestionController.instance()
-    workerSource.addGauge(
-      WorkerSource.PotentialConsumeSpeed,
-      _ => rateLimitController.getPotentialConsumeSpeed)
   }
 
   var controller = new Controller(rpcEnv, conf, metricsSystem)
diff --git a/worker/src/main/scala/org/apache/celeborn/service/deploy/worker/WorkerSource.scala b/worker/src/main/scala/org/apache/celeborn/service/deploy/worker/WorkerSource.scala
index bcf8e19f3..df6625429 100644
--- a/worker/src/main/scala/org/apache/celeborn/service/deploy/worker/WorkerSource.scala
+++ b/worker/src/main/scala/org/apache/celeborn/service/deploy/worker/WorkerSource.scala
@@ -98,6 +98,7 @@ object WorkerSource {
   val DeviceCelebornFreeCapacity = "DeviceCelebornFreeCapacity(B)"
   val DeviceCelebornTotalCapacity = "DeviceCelebornTotalCapacity(B)"
 
-  // Rate limit
+  // Congestion control
   val PotentialConsumeSpeed = "PotentialConsumeSpeed"
+  val UserProduceSpeed = "UserProduceSpeed"
 }
diff --git a/worker/src/test/java/org/apache/celeborn/service/deploy/worker/congestcontrol/TestCongestionController.java b/worker/src/test/java/org/apache/celeborn/service/deploy/worker/congestcontrol/TestCongestionController.java
index b8456a711..fbb79fde7 100644
--- a/worker/src/test/java/org/apache/celeborn/service/deploy/worker/congestcontrol/TestCongestionController.java
+++ b/worker/src/test/java/org/apache/celeborn/service/deploy/worker/congestcontrol/TestCongestionController.java
@@ -17,23 +17,32 @@
 
 package org.apache.celeborn.service.deploy.worker.congestcontrol;
 
+import java.util.Map;
+
+import scala.collection.JavaConverters;
+
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
+import org.apache.celeborn.common.CelebornConf;
 import org.apache.celeborn.common.identity.UserIdentifier;
+import org.apache.celeborn.service.deploy.worker.WorkerSource;
 
 public class TestCongestionController {
 
   private CongestionController controller;
+  private WorkerSource source = new WorkerSource(new CelebornConf());
+
   private long pendingBytes = 0L;
+  private final long userInactiveTimeMills = 2000L;
 
   @Before
   public void initialize() {
     // Make sampleTimeWindow a bit larger in case the tests run time exceed this window.
     controller =
-        new CongestionController(10, 1000, 500, 1000) {
+        new CongestionController(source, 10, 1000, 500, userInactiveTimeMills) {
           @Override
           public long getTotalPendingBytes() {
             return pendingBytes;
@@ -49,6 +58,7 @@ public class TestCongestionController {
   @After
   public void clear() {
     controller.close();
+    source.destroy();
   }
 
   @Test
@@ -97,4 +107,49 @@ public class TestCongestionController {
     Assert.assertFalse(controller.isUserCongested(user1));
     Assert.assertFalse(controller.isUserCongested(user2));
   }
+
+  @Test
+  public void testUserMetrics() throws InterruptedException {
+    UserIdentifier user = new UserIdentifier("test", "celeborn");
+    Assert.assertFalse(controller.isUserCongested(user));
+    controller.produceBytes(user, 800);
+
+    Assert.assertTrue(
+        isGaugeExist(
+            WorkerSource.UserProduceSpeed(),
+            JavaConverters.mapAsJavaMapConverter(user.toMap()).asJava()));
+
+    Thread.sleep(userInactiveTimeMills * 2);
+
+    Assert.assertFalse(
+        isGaugeExist(
+            WorkerSource.UserProduceSpeed(),
+            JavaConverters.mapAsJavaMapConverter(user.toMap()).asJava()));
+  }
+
+  private boolean isGaugeExist(String name, Map<String, String> labels) {
+    return source.namedGauges().stream()
+            .filter(
+                gauge -> {
+                  if (gauge.name().equals(name)) {
+                    return labels.entrySet().stream()
+                        .noneMatch(
+                            entry -> {
+                              // Filter entry not exist in the gauge's labels
+                              if (gauge.labels().get(entry.getKey()).nonEmpty()) {
+                                return !gauge
+                                    .labels()
+                                    .get(entry.getKey())
+                                    .get()
+                                    .equals(entry.getValue());
+                              } else {
+                                return true;
+                              }
+                            });
+                  }
+                  return false;
+                })
+            .count()
+        == 1;
+  }
 }