You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@samza.apache.org by pm...@apache.org on 2020/08/19 16:50:30 UTC

[samza] branch master updated: SAMZA-2583: Fix NPE in update SamzaHistogram Gauge values (#1418)

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

pmaheshwari pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/samza.git


The following commit(s) were added to refs/heads/master by this push:
     new 44fe776  SAMZA-2583: Fix NPE in update SamzaHistogram Gauge values (#1418)
44fe776 is described below

commit 44fe7765224fefbac8bc0a78ac0da1e4012a7266
Author: Vishwajith Shivappa <vi...@gmail.com>
AuthorDate: Wed Aug 19 09:45:54 2020 -0700

    SAMZA-2583: Fix NPE in update SamzaHistogram Gauge values (#1418)
    
    * SAMZA-2583: Fix NPE in update SamzaHistogram Gauge values
    
    SamzaHistogram gauges are instantiated via passed in metric registry
    newGauge api. The new Gauge api in turn tries to instantiate
    HistogramGauge. The newGauge api calls getValue on HistogramGauge
    which in turn tries to update the Histogram Gauge values. This means
    we are updating Histogram Gauge values in process of creating it.
    As a fix, we add  null check in Gauge getValue to avoid NPE.
---
 .../org/apache/samza/metrics/SamzaHistogram.java   |  8 ++++
 .../apache/samza/metrics/TestSamzaHistogram.java   | 49 ++++++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/samza-api/src/main/java/org/apache/samza/metrics/SamzaHistogram.java b/samza-api/src/main/java/org/apache/samza/metrics/SamzaHistogram.java
index 38cf0fb..bdbe403 100644
--- a/samza-api/src/main/java/org/apache/samza/metrics/SamzaHistogram.java
+++ b/samza-api/src/main/java/org/apache/samza/metrics/SamzaHistogram.java
@@ -81,6 +81,14 @@ public class SamzaHistogram {
      * and do not have MetricsReporter to update the values.
      */
     public Double getValue() {
+      /*
+       * We cannot updateGaugeValues if the histogram gauges are being instantiated
+       * and getValue is called in this context.
+       */
+      if (gauges == null) {
+        return 0.0;
+      }
+
       updateGaugeValues(percentile);
       return super.getValue();
     }
diff --git a/samza-api/src/test/java/org/apache/samza/metrics/TestSamzaHistogram.java b/samza-api/src/test/java/org/apache/samza/metrics/TestSamzaHistogram.java
new file mode 100644
index 0000000..ef4fc20
--- /dev/null
+++ b/samza-api/src/test/java/org/apache/samza/metrics/TestSamzaHistogram.java
@@ -0,0 +1,49 @@
+/*
+ * 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.samza.metrics;
+
+import org.junit.Test;
+import org.mockito.stubbing.Answer;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+
+public class TestSamzaHistogram {
+  private static final String GROUP = "Group0";
+  private static final String METRIC_NAME = "Metric1";
+
+  private MetricsRegistry metricsRegistry;
+
+  @Test
+  public void testCreateHistogramGaugeNullCheck() {
+    metricsRegistry = mock(MetricsRegistry.class);
+
+    doAnswer((Answer<Gauge<Double>>) invocation -> {
+      Object[] args = invocation.getArguments();
+      return new Gauge<>((String) args[0], (Double) ((Gauge) args[1]).getValue());
+    }).when(metricsRegistry).newGauge(anyString(), any(Gauge.class));
+
+    SamzaHistogram histogram = new SamzaHistogram(metricsRegistry, GROUP, METRIC_NAME);
+    assertNotNull(histogram);
+  }
+}
\ No newline at end of file