You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by jl...@apache.org on 2021/02/28 23:33:43 UTC

[incubator-pinot] branch master updated: Override equals and hashCode methods for PinotMetricName (#6622)

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

jlli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pinot.git


The following commit(s) were added to refs/heads/master by this push:
     new d6bb2c9  Override equals and hashCode methods for PinotMetricName (#6622)
d6bb2c9 is described below

commit d6bb2c9fedf932223e5cf7aacbab60c768104aa6
Author: Jialiang Li <jl...@linkedin.com>
AuthorDate: Sun Feb 28 15:33:28 2021 -0800

    Override equals and hashCode methods for PinotMetricName (#6622)
    
    Co-authored-by: Jack Li(Analytics Engineering) <jl...@jlli-mn1.linkedin.biz>
---
 .../common/metrics/yammer/YammerMetricName.java    | 25 +++++++++++++++++++++-
 .../pinot/common/metrics/MetricsHelperTest.java    | 12 +++++++++++
 .../apache/pinot/spi/metrics/PinotMetricName.java  | 15 +++++++++++++
 3 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/pinot-common/src/main/java/org/apache/pinot/common/metrics/yammer/YammerMetricName.java b/pinot-common/src/main/java/org/apache/pinot/common/metrics/yammer/YammerMetricName.java
index 70726c0..fc21012 100644
--- a/pinot-common/src/main/java/org/apache/pinot/common/metrics/yammer/YammerMetricName.java
+++ b/pinot-common/src/main/java/org/apache/pinot/common/metrics/yammer/YammerMetricName.java
@@ -23,7 +23,7 @@ import org.apache.pinot.spi.metrics.PinotMetricName;
 
 
 public class YammerMetricName implements PinotMetricName {
-  private MetricName _metricName;
+  private final MetricName _metricName;
 
   public YammerMetricName(Class<?> klass, String name) {
     _metricName = new MetricName(klass, name);
@@ -37,4 +37,27 @@ public class YammerMetricName implements PinotMetricName {
   public MetricName getMetricName() {
     return _metricName;
   }
+
+  /**
+   * Overrides equals method by calling the equals from the actual metric name.
+   */
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj) {
+      return true;
+    }
+    if (obj == null || getClass() != obj.getClass()) {
+      return false;
+    }
+    YammerMetricName that = (YammerMetricName) obj;
+    return _metricName.equals(that._metricName);
+  }
+
+  /**
+   * Overrides hashCode method by calling the hashCode method from the actual metric name.
+   */
+  @Override
+  public int hashCode() {
+    return _metricName.hashCode();
+  }
 }
diff --git a/pinot-common/src/test/java/org/apache/pinot/common/metrics/MetricsHelperTest.java b/pinot-common/src/test/java/org/apache/pinot/common/metrics/MetricsHelperTest.java
index 24507c9..ea70bd6 100644
--- a/pinot-common/src/test/java/org/apache/pinot/common/metrics/MetricsHelperTest.java
+++ b/pinot-common/src/test/java/org/apache/pinot/common/metrics/MetricsHelperTest.java
@@ -23,6 +23,7 @@ import java.util.Map;
 import java.util.concurrent.TimeUnit;
 import org.apache.pinot.common.exception.InvalidConfigException;
 import org.apache.pinot.spi.metrics.PinotMeter;
+import org.apache.pinot.spi.metrics.PinotMetricName;
 import org.apache.pinot.spi.metrics.PinotMetricsRegistry;
 import org.apache.pinot.spi.env.PinotConfiguration;
 import org.testng.Assert;
@@ -88,4 +89,15 @@ public class MetricsHelperTest {
     pinotMeter.mark(2L);
     Assert.assertEquals(pinotMeter.count(), 3L);
   }
+
+  @Test
+  public void testPinotMetricName() {
+    PinotMetricName testMetricName1 =
+        PinotMetricUtils.generatePinotMetricName(MetricsHelperTest.class, "testMetricName");
+    PinotMetricName testMetricName2 =
+        PinotMetricUtils.generatePinotMetricName(MetricsHelperTest.class, "testMetricName");
+    Assert.assertNotNull(testMetricName1);
+    Assert.assertNotNull(testMetricName2);
+    Assert.assertEquals(testMetricName1, testMetricName2);
+  }
 }
diff --git a/pinot-spi/src/main/java/org/apache/pinot/spi/metrics/PinotMetricName.java b/pinot-spi/src/main/java/org/apache/pinot/spi/metrics/PinotMetricName.java
index 95c52b2..e4de876 100644
--- a/pinot-spi/src/main/java/org/apache/pinot/spi/metrics/PinotMetricName.java
+++ b/pinot-spi/src/main/java/org/apache/pinot/spi/metrics/PinotMetricName.java
@@ -23,5 +23,20 @@ package org.apache.pinot.spi.metrics;
  */
 public interface PinotMetricName {
 
+  /**
+   * Returns the actual metric name.
+   */
   Object getMetricName();
+
+  /**
+   * Overrides the equals method. This is needed as {@link PinotMetricName} is used as the key of the key-value pair
+   * inside the hashmap in MetricsRegistry. Without overriding equals() and hashCode() methods, all the existing k-v pairs
+   * stored in hashmap cannot be retrieved by initializing a new key.
+   */
+  boolean equals(Object obj);
+
+  /**
+   * Overrides the hashCode method. This method's contract is the same as equals() method.
+   */
+  int hashCode();
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org