Pārlūkot izejas kodu

AMBARI-18911 : Storm start is failing due to metrics initialization error (avijayan)

Aravindan Vijayan 8 gadi atpakaļ
vecāks
revīzija
280bb6ecfa

+ 9 - 0
ambari-metrics/ambari-metrics-common/pom.xml

@@ -76,6 +76,10 @@
                   <pattern>org.apache.commons.io</pattern>
                   <shadedPattern>org.apache.hadoop.metrics2.sink.relocated.commons.io</shadedPattern>
                 </relocation>
+                <relocation>
+                  <pattern>org.apache.commons.lang</pattern>
+                  <shadedPattern>org.apache.hadoop.metrics2.sink.relocated.commons.lang</shadedPattern>
+                </relocation>
                 <relocation>
                   <pattern>org.apache.curator</pattern>
                   <shadedPattern>org.apache.hadoop.metrics2.sink.relocated.curator</shadedPattern>
@@ -153,6 +157,11 @@
       <artifactId>jackson-mapper-asl</artifactId>
       <version>1.9.13</version>
     </dependency>
+    <dependency>
+      <groupId>commons-lang</groupId>
+      <artifactId>commons-lang</artifactId>
+      <version>2.5</version>
+    </dependency>
     <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-math3</artifactId>

+ 9 - 11
ambari-metrics/ambari-metrics-common/src/main/java/org/apache/hadoop/metrics2/sink/timeline/AbstractTimelineMetricsSink.java

@@ -23,6 +23,7 @@ import com.google.common.reflect.TypeToken;
 import com.google.gson.Gson;
 import com.google.gson.JsonSyntaxException;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.metrics2.sink.timeline.availability.MetricCollectorHAHelper;
@@ -477,24 +478,21 @@ public abstract class AbstractTimelineMetricsSink {
   /**
    * Parses input Sting of format "['host1', 'host2']" into Collection of hostnames
    */
-  protected Collection<String> parseHostsStringIntoCollection(String hostsString) {
+  public Collection<String> parseHostsStringIntoCollection(String hostsString) {
     Set<String> hosts = new HashSet<>();
 
-    if (hostsString == null) {
+    if (StringUtils.isEmpty(hostsString)) {
       LOG.error("No Metric collector configured.");
       return hosts;
     }
 
-    hostsString = hostsString.replace("[", "");
-    hostsString = hostsString.replace("]", "");
-    hostsString = hostsString.replace("'", "");
+    String[] untrimmedHosts = hostsString.split(",");
 
-    String [] hostNamesWithApostrophes  = hostsString.split(",");
-
-    for (String host : hostNamesWithApostrophes) {
-      host = host.trim();
-      if (host.equals("")) continue;
-      hosts.add(host);
+    for (String host : untrimmedHosts) {
+      host = StringUtils.substringBetween(host, "'");
+      if (StringUtils.isEmpty(host))
+        continue;
+      hosts.add(host.trim());
     }
 
     return hosts;

+ 98 - 0
ambari-metrics/ambari-metrics-common/src/test/java/org/apache/hadoop/metrics2/sink/timeline/availability/AbstractTimelineMetricSinkTest.java

@@ -0,0 +1,98 @@
+/**
+ * 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.hadoop.metrics2.sink.timeline.availability;
+
+import junit.framework.Assert;
+import org.apache.hadoop.metrics2.sink.timeline.AbstractTimelineMetricsSink;
+import org.apache.hadoop.metrics2.sink.timeline.TimelineMetrics;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+public class AbstractTimelineMetricSinkTest {
+
+  @Test
+  public void testParseHostsStringIntoCollection() {
+    AbstractTimelineMetricsSink sink = new TestTimelineMetricsSink();
+    Collection<String> hosts;
+
+    hosts = sink.parseHostsStringIntoCollection("[]");
+    Assert.assertTrue(hosts.isEmpty());
+
+    hosts = sink.parseHostsStringIntoCollection("[u'test1.123.abc.def.local']");
+    Assert.assertTrue(hosts.size() == 1);
+    Assert.assertTrue(hosts.contains("test1.123.abc.def.local"));
+
+    hosts = sink.parseHostsStringIntoCollection("['test1.123.abc.def.local']");
+    Assert.assertTrue(hosts.size() == 1);
+    Assert.assertTrue(hosts.contains("test1.123.abc.def.local"));
+
+    hosts = sink.parseHostsStringIntoCollection("[u'test1.123.abc.def.local', u'test1.456.abc.def.local']");
+    Assert.assertTrue(hosts.size() == 2);
+
+    hosts = sink.parseHostsStringIntoCollection("['test1.123.abc.def.local', 'test1.456.abc.def.local']");
+    Assert.assertTrue(hosts.size() == 2);
+    Assert.assertTrue(hosts.contains("test1.123.abc.def.local"));
+    Assert.assertTrue(hosts.contains("test1.456.abc.def.local"));
+
+  }
+
+  private class TestTimelineMetricsSink extends AbstractTimelineMetricsSink {
+    @Override
+    protected String getCollectorUri(String host) {
+      return "";
+    }
+
+    @Override
+    protected String getCollectorProtocol() {
+      return "http";
+    }
+
+    @Override
+    protected String getCollectorPort() {
+      return "2181";
+    }
+
+    @Override
+    protected int getTimeoutSeconds() {
+      return 10;
+    }
+
+    @Override
+    protected String getZookeeperQuorum() {
+      return "localhost:2181";
+    }
+
+    @Override
+    protected Collection<String> getConfiguredCollectorHosts() {
+      return Arrays.asList("localhost");
+    }
+
+    @Override
+    protected String getHostname() {
+      return "h1";
+    }
+
+    @Override
+    public boolean emitMetrics(TimelineMetrics metrics) {
+      super.init();
+      return super.emitMetrics(metrics);
+    }
+  }
+}

+ 1 - 0
ambari-metrics/ambari-metrics-storm-sink/src/main/java/org/apache/hadoop/metrics2/sink/storm/StormTimelineMetricsReporter.java

@@ -105,6 +105,7 @@ public class StormTimelineMetricsReporter extends AbstractTimelineMetricsSink
 
       collectorHosts = parseHostsStringIntoCollection(conf.getProperty(COLLECTOR_HOSTS_PROPERTY).toString());
       port = conf.getProperty(COLLECTOR_PORT) != null ? conf.getProperty(COLLECTOR_PORT) : "6188";
+      protocol = conf.getProperty(COLLECTOR_PROTOCOL, "http");
       zkQuorum = conf.getProperty(ZOOKEEPER_QUORUM) != null ? conf.getProperty(ZOOKEEPER_QUORUM) : null;
 
       timeoutSeconds = conf.getProperty(METRICS_POST_TIMEOUT_SECONDS) != null ?