You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by sz...@apache.org on 2016/03/01 21:08:21 UTC

hive git commit: HIVE-12757 : Fix TestCodahaleMetrics#testFileReporting (Szehon, reviewed by Aihua Xu)

Repository: hive
Updated Branches:
  refs/heads/master db4216d6c -> 14e927036


HIVE-12757 : Fix TestCodahaleMetrics#testFileReporting (Szehon, reviewed by Aihua Xu)


Project: http://git-wip-us.apache.org/repos/asf/hive/repo
Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/14e92703
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/14e92703
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/14e92703

Branch: refs/heads/master
Commit: 14e927036e4f7122f694bad10e1957e076a6ec25
Parents: db4216d
Author: Szehon Ho <sz...@cloudera.com>
Authored: Tue Mar 1 12:07:48 2016 -0800
Committer: Szehon Ho <sz...@cloudera.com>
Committed: Tue Mar 1 12:07:48 2016 -0800

----------------------------------------------------------------------
 .../hive/common/metrics/MetricsTestUtils.java   |  9 +++++++
 .../metrics/metrics2/TestCodahaleMetrics.java   | 26 +++++---------------
 2 files changed, 15 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/14e92703/common/src/test/org/apache/hadoop/hive/common/metrics/MetricsTestUtils.java
----------------------------------------------------------------------
diff --git a/common/src/test/org/apache/hadoop/hive/common/metrics/MetricsTestUtils.java b/common/src/test/org/apache/hadoop/hive/common/metrics/MetricsTestUtils.java
index c90a614..4aaa808 100644
--- a/common/src/test/org/apache/hadoop/hive/common/metrics/MetricsTestUtils.java
+++ b/common/src/test/org/apache/hadoop/hive/common/metrics/MetricsTestUtils.java
@@ -57,4 +57,13 @@ public class MetricsTestUtils {
     JsonNode metricsNode = categoryNode.path(metricsName);
     return metricsNode.path(category.metricsHandle);
   }
+
+  public static byte[] getFileData(String path, int timeoutInterval, int tries) throws Exception {
+    File file = new File(path);
+    do {
+      Thread.sleep(timeoutInterval);
+      tries--;
+    } while (tries > 0 && !file.exists());
+    return Files.readAllBytes(Paths.get(path));
+  }
 }

http://git-wip-us.apache.org/repos/asf/hive/blob/14e92703/common/src/test/org/apache/hadoop/hive/common/metrics/metrics2/TestCodahaleMetrics.java
----------------------------------------------------------------------
diff --git a/common/src/test/org/apache/hadoop/hive/common/metrics/metrics2/TestCodahaleMetrics.java b/common/src/test/org/apache/hadoop/hive/common/metrics/metrics2/TestCodahaleMetrics.java
index 55bb2c2..6ee6245 100644
--- a/common/src/test/org/apache/hadoop/hive/common/metrics/metrics2/TestCodahaleMetrics.java
+++ b/common/src/test/org/apache/hadoop/hive/common/metrics/metrics2/TestCodahaleMetrics.java
@@ -23,6 +23,7 @@ import com.codahale.metrics.Timer;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
+import org.apache.hadoop.hive.common.metrics.MetricsTestUtils;
 import org.apache.hadoop.hive.common.metrics.common.MetricsFactory;
 import org.apache.hadoop.hive.common.metrics.common.MetricsVariable;
 import org.apache.hadoop.hive.conf.HiveConf;
@@ -122,11 +123,9 @@ public class TestCodahaleMetrics {
     int runs = 5;
     for (int i = 0; i < runs; i++) {
       MetricsFactory.getInstance().incrementCounter("count2");
-      Thread.sleep(100);
     }
 
-    Thread.sleep(2000);
-    byte[] jsonData = Files.readAllBytes(Paths.get(jsonReportFile.getAbsolutePath()));
+    byte[] jsonData = MetricsTestUtils.getFileData(jsonReportFile.getAbsolutePath(), 2000, 3);
     ObjectMapper objectMapper = new ObjectMapper();
 
     JsonNode rootNode = objectMapper.readTree(jsonData);
@@ -154,25 +153,12 @@ public class TestCodahaleMetrics {
     testVar.setValue(20);
 
     MetricsFactory.getInstance().addGauge("gauge1", testVar);
-    Thread.sleep(2000);
-    byte[] jsonData = Files.readAllBytes(Paths.get(jsonReportFile.getAbsolutePath()));
-    ObjectMapper objectMapper = new ObjectMapper();
+    String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson();
+    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "gauge1", testVar.getValue());
 
-    JsonNode rootNode = objectMapper.readTree(jsonData);
-    JsonNode gaugesNode = rootNode.path("gauges");
-    JsonNode methodGaugeNode = gaugesNode.path("gauge1");
-    JsonNode countNode = methodGaugeNode.path("value");
-    Assert.assertEquals(countNode.asInt(), testVar.getValue());
 
     testVar.setValue(40);
-    Thread.sleep(2000);
-
-    jsonData = Files.readAllBytes(Paths.get(jsonReportFile.getAbsolutePath()));
-
-    rootNode = objectMapper.readTree(jsonData);
-    gaugesNode = rootNode.path("gauges");
-    methodGaugeNode = gaugesNode.path("gauge1");
-    countNode = methodGaugeNode.path("value");
-    Assert.assertEquals(countNode.asInt(), testVar.getValue());
+    json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson();
+    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "gauge1", testVar.getValue());
   }
 }