This is an automated email from the ASF dual-hosted git repository. wuzhiguo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/ambari-metrics.git
The following commit(s) were added to refs/heads/master by this push: new 0b3f3c3 AMBARI-25400 Issue while determining live collector in case of HA (#51) 0b3f3c3 is described below commit 0b3f3c313a0b8ecb16e845d97fe5e9a5b0b27a48 Author: lucasbak <lucas.bakal...@gmail.com> AuthorDate: Mon Nov 7 20:19:40 2022 +0100 AMBARI-25400 Issue while determining live collector in case of HA (#51) --- .../sink/timeline/AbstractTimelineMetricsSink.java | 31 ++++++++++++++-------- .../timeline/AbstractTimelineMetricSinkTest.java | 18 +++++++++++++ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/ambari-metrics-common/src/main/java/org/apache/hadoop/metrics2/sink/timeline/AbstractTimelineMetricsSink.java b/ambari-metrics-common/src/main/java/org/apache/hadoop/metrics2/sink/timeline/AbstractTimelineMetricsSink.java index 739e9dc..d77f60c 100644 --- a/ambari-metrics-common/src/main/java/org/apache/hadoop/metrics2/sink/timeline/AbstractTimelineMetricsSink.java +++ b/ambari-metrics-common/src/main/java/org/apache/hadoop/metrics2/sink/timeline/AbstractTimelineMetricsSink.java @@ -626,18 +626,27 @@ public abstract class AbstractTimelineMetricsSink { connection.setReadTimeout(2000); int responseCode = connection.getResponseCode(); - if (responseCode == 200) { - try (InputStream in = connection.getInputStream()) { - StringWriter writer = new StringWriter(); - IOUtils.copy(in, writer); - try { - collectors = gson.fromJson(writer.toString(), new TypeToken<List<String>>(){}.getType()); - } catch (JsonSyntaxException jse) { - // Swallow this at the behest of still trying to POST - LOG.debug("Exception deserializing the json data on live " + - "collector nodes.", jse); + + switch (responseCode) { + case 200 : + try (InputStream in = connection.getInputStream()) { + StringWriter writer = new StringWriter(); + IOUtils.copy(in, writer); + try { + collectors = gson.fromJson(writer.toString(), new TypeToken<List<String>>(){}.getType()); + } catch (JsonSyntaxException jse) { + // Swallow this at the behest of still trying to POST + LOG.debug("Exception deserializing the json data on live " + + "collector nodes.", jse); + } } - } + break; + case 500 : + String warnMsg = "Unable to connect to collector to find live nodes, Internal server error"; + throw new MetricCollectorUnavailableException(warnMsg); + default : + String msg = String.format("Unhandled response code (%d) at requesting live collector nodes!", responseCode); + LOG.warn(msg); } } catch (IOException ioe) { diff --git a/ambari-metrics-common/src/test/java/org/apache/hadoop/metrics2/sink/timeline/AbstractTimelineMetricSinkTest.java b/ambari-metrics-common/src/test/java/org/apache/hadoop/metrics2/sink/timeline/AbstractTimelineMetricSinkTest.java index 634d18c..f3f8a62 100644 --- a/ambari-metrics-common/src/test/java/org/apache/hadoop/metrics2/sink/timeline/AbstractTimelineMetricSinkTest.java +++ b/ambari-metrics-common/src/test/java/org/apache/hadoop/metrics2/sink/timeline/AbstractTimelineMetricSinkTest.java @@ -18,6 +18,7 @@ package org.apache.hadoop.metrics2.sink.timeline; import junit.framework.Assert; +import org.apache.hadoop.metrics2.sink.timeline.availability.MetricCollectorUnavailableException; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.easymock.PowerMock; @@ -35,6 +36,7 @@ import static org.easymock.EasyMock.anyString; import static org.easymock.EasyMock.expect; import static org.powermock.api.easymock.PowerMock.expectNew; import static org.powermock.api.easymock.PowerMock.replayAll; +import static org.powermock.api.easymock.PowerMock.verifyAll; @RunWith(PowerMockRunner.class) @PrepareForTest({AbstractTimelineMetricsSink.class, HttpURLConnection.class}) @@ -186,6 +188,22 @@ public class AbstractTimelineMetricSinkTest { Assert.assertEquals(0, sink.getMetricsPostCache().size()); } + @Test(expected = MetricCollectorUnavailableException.class) + @PrepareForTest({URL.class, AbstractTimelineMetricsSink.class, HttpURLConnection.class, TimelineMetric.class}) + public void testFindLiveCollectorHostsFromKnownCollector() throws Exception { + HttpURLConnection connection = PowerMock.createNiceMock(HttpURLConnection.class); + URL url = PowerMock.createNiceMock(URL.class); + expectNew(URL.class, anyString()).andReturn(url).anyTimes(); + expect(url.openConnection()).andReturn(connection).anyTimes(); + expect(connection.getResponseCode()).andReturn(500).anyTimes(); + replayAll(); + + TestTimelineMetricsSink sink = new TestTimelineMetricsSink(); + sink.findLiveCollectorHostsFromKnownCollector("host", "1234"); + + verifyAll(); + } + private class TestTimelineMetricsSink extends AbstractTimelineMetricsSink { @Override protected String getCollectorUri(String host) { --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@ambari.apache.org For additional commands, e-mail: commits-h...@ambari.apache.org