@Test public void testFetchTimelineMetrics() throws Exception { EasyMockSupport easyMockSupport = new EasyMockSupport(); final long now = System.currentTimeMillis(); TimelineMetrics metrics = new TimelineMetrics(); TimelineMetric timelineMetric = new TimelineMetric(); timelineMetric.setMetricName("cpu_user"); timelineMetric.setAppId("app1"); TreeMap<Long, Double> metricValues = new TreeMap<Long, Double>(); metricValues.put(now + 100, 1.0); metricValues.put(now + 200, 2.0); metricValues.put(now + 300, 3.0); timelineMetric.setMetricValues(metricValues); metrics.getMetrics().add(timelineMetric); ObjectMapper mapper = new ObjectMapper(); AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(); mapper.setAnnotationIntrospector(introspector); ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter(); String metricsResponse = writer.writeValueAsString(metrics); InputStream inputStream = IOUtils.toInputStream(metricsResponse); HttpURLConnection httpURLConnectionMock = createMock(HttpURLConnection.class); expect(httpURLConnectionMock.getInputStream()).andReturn(inputStream).once(); expect(httpURLConnectionMock.getResponseCode()).andReturn(HttpStatus.SC_OK).once(); URLStreamProvider urlStreamProviderMock = createMock(URLStreamProvider.class); expect(urlStreamProviderMock.processURL(EasyMock.isA(String.class), EasyMock.isA(String.class), isNull(String.class), EasyMock.<Map<String, List<String>>>anyObject())).andReturn(httpURLConnectionMock).once(); replay(httpURLConnectionMock, urlStreamProviderMock); String randomSpec = "http: "&startTime=1447912834&endTime=1447990034&precision=SECONDS"; MetricsRequestHelper metricsRequestHelper = new MetricsRequestHelper(urlStreamProviderMock); metricsRequestHelper.fetchTimelineMetrics(new URIBuilder(randomSpec), now, now + 300); easyMockSupport.verifyAll(); String metricsPrecisionErrorResponse = "{\"exception\": \"PrecisionLimitExceededException\",\n" + "\"message\": \"Requested precision (SECONDS) for given time range causes result set size of 169840, " + "which exceeds the limit - 15840. Please request higher precision.\",\n" + "\"javaClassName\": \"org.apache.hadoop.metrics2.sink.timeline.PrecisionLimitExceededException\"\n" + "}"; InputStream errorStream = IOUtils.toInputStream(metricsPrecisionErrorResponse); inputStream = IOUtils.toInputStream(metricsResponse); httpURLConnectionMock = createMock(HttpURLConnection.class); expect(httpURLConnectionMock.getErrorStream()).andReturn(errorStream).once(); expect(httpURLConnectionMock.getInputStream()).andReturn(inputStream).once(); expect(httpURLConnectionMock.getResponseCode()) .andReturn(HttpStatus.SC_BAD_REQUEST).once() .andReturn(HttpStatus.SC_OK).once(); urlStreamProviderMock = createMock(URLStreamProvider.class); expect(urlStreamProviderMock.processURL(EasyMock.isA(String.class), EasyMock.isA(String.class), isNull(String.class), EasyMock.<Map<String, List<String>>>anyObject())).andReturn(httpURLConnectionMock).times(2); replay(httpURLConnectionMock, urlStreamProviderMock); metricsRequestHelper = new MetricsRequestHelper(urlStreamProviderMock); metricsRequestHelper.fetchTimelineMetrics(new URIBuilder(randomSpec), now, now + 300); easyMockSupport.verifyAll(); }
public TimelineMetrics fetchTimelineMetrics(URIBuilder uriBuilder, Long startTime, Long endTime) throws IOException { LOG.debug("Metrics request url = " + uriBuilder.toString()); BufferedReader reader = null; TimelineMetrics timelineMetrics = null; try { HttpURLConnection connection = streamProvider.processURL(uriBuilder.toString(), HttpMethod.GET, (String) null, Collections.<String, List<String>>emptyMap()); if (!checkConnectionForPrecisionException(connection)) { String higherPrecision = getHigherPrecision(uriBuilder, startTime, endTime); if (higherPrecision != null) { LOG.debug("Requesting metrics with higher precision : " + higherPrecision); uriBuilder.setParameter("precision", higherPrecision); String newSpec = uriBuilder.toString(); connection = streamProvider.processURL(newSpec, HttpMethod.GET, (String) null, Collections.<String, List<String>>emptyMap()); if (!checkConnectionForPrecisionException(connection)) { throw new IOException("Encountered Precision exception : Higher precision request also failed."); } } else { throw new IOException("Encountered Precision exception : Unable to request higher precision"); } } InputStream inputStream = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(inputStream)); timelineMetrics = timelineObjectReader.readValue(reader); if (LOG.isTraceEnabled()) { for (TimelineMetric metric : timelineMetrics.getMetrics()) { LOG.trace("metric: " + metric.getMetricName() + ", size = " + metric.getMetricValues().size() + ", host = " + metric.getHostName() + ", app = " + metric.getAppId() + ", instance = " + metric.getInstanceId() + ", time = " + metric.getTimestamp() + ", startTime = " + new Date(metric.getStartTime())); } } } catch (IOException io) { String errorMsg = "Error getting timeline metrics : " + io.getMessage(); LOG.error(errorMsg); if (LOG.isDebugEnabled()) { LOG.debug(errorMsg, io); } if (io instanceof SocketTimeoutException || io instanceof ConnectException) { errorMsg = "Cannot connect to collector: SocketTimeoutException for " + uriBuilder.getHost(); LOG.error(errorMsg); throw io; } } catch (URISyntaxException e) { String errorMsg = "Error getting timeline metrics : " + e.getMessage(); LOG.error(errorMsg); if (LOG.isDebugEnabled()) { LOG.debug(errorMsg, e); } } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { if (LOG.isWarnEnabled()) { if (LOG.isDebugEnabled()) { LOG.warn("Unable to close http input stream : spec=" + uriBuilder.toString(), e); } else { LOG.warn("Unable to close http input stream : spec=" + uriBuilder.toString()); } } } } } return timelineMetrics; }
MetricsRequestHelper { public TimelineMetrics fetchTimelineMetrics(URIBuilder uriBuilder, Long startTime, Long endTime) throws IOException { LOG.debug("Metrics request url = " + uriBuilder.toString()); BufferedReader reader = null; TimelineMetrics timelineMetrics = null; try { HttpURLConnection connection = streamProvider.processURL(uriBuilder.toString(), HttpMethod.GET, (String) null, Collections.<String, List<String>>emptyMap()); if (!checkConnectionForPrecisionException(connection)) { String higherPrecision = getHigherPrecision(uriBuilder, startTime, endTime); if (higherPrecision != null) { LOG.debug("Requesting metrics with higher precision : " + higherPrecision); uriBuilder.setParameter("precision", higherPrecision); String newSpec = uriBuilder.toString(); connection = streamProvider.processURL(newSpec, HttpMethod.GET, (String) null, Collections.<String, List<String>>emptyMap()); if (!checkConnectionForPrecisionException(connection)) { throw new IOException("Encountered Precision exception : Higher precision request also failed."); } } else { throw new IOException("Encountered Precision exception : Unable to request higher precision"); } } InputStream inputStream = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(inputStream)); timelineMetrics = timelineObjectReader.readValue(reader); if (LOG.isTraceEnabled()) { for (TimelineMetric metric : timelineMetrics.getMetrics()) { LOG.trace("metric: " + metric.getMetricName() + ", size = " + metric.getMetricValues().size() + ", host = " + metric.getHostName() + ", app = " + metric.getAppId() + ", instance = " + metric.getInstanceId() + ", time = " + metric.getTimestamp() + ", startTime = " + new Date(metric.getStartTime())); } } } catch (IOException io) { String errorMsg = "Error getting timeline metrics : " + io.getMessage(); LOG.error(errorMsg); if (LOG.isDebugEnabled()) { LOG.debug(errorMsg, io); } if (io instanceof SocketTimeoutException || io instanceof ConnectException) { errorMsg = "Cannot connect to collector: SocketTimeoutException for " + uriBuilder.getHost(); LOG.error(errorMsg); throw io; } } catch (URISyntaxException e) { String errorMsg = "Error getting timeline metrics : " + e.getMessage(); LOG.error(errorMsg); if (LOG.isDebugEnabled()) { LOG.debug(errorMsg, e); } } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { if (LOG.isWarnEnabled()) { if (LOG.isDebugEnabled()) { LOG.warn("Unable to close http input stream : spec=" + uriBuilder.toString(), e); } else { LOG.warn("Unable to close http input stream : spec=" + uriBuilder.toString()); } } } } } return timelineMetrics; } }
MetricsRequestHelper { public TimelineMetrics fetchTimelineMetrics(URIBuilder uriBuilder, Long startTime, Long endTime) throws IOException { LOG.debug("Metrics request url = " + uriBuilder.toString()); BufferedReader reader = null; TimelineMetrics timelineMetrics = null; try { HttpURLConnection connection = streamProvider.processURL(uriBuilder.toString(), HttpMethod.GET, (String) null, Collections.<String, List<String>>emptyMap()); if (!checkConnectionForPrecisionException(connection)) { String higherPrecision = getHigherPrecision(uriBuilder, startTime, endTime); if (higherPrecision != null) { LOG.debug("Requesting metrics with higher precision : " + higherPrecision); uriBuilder.setParameter("precision", higherPrecision); String newSpec = uriBuilder.toString(); connection = streamProvider.processURL(newSpec, HttpMethod.GET, (String) null, Collections.<String, List<String>>emptyMap()); if (!checkConnectionForPrecisionException(connection)) { throw new IOException("Encountered Precision exception : Higher precision request also failed."); } } else { throw new IOException("Encountered Precision exception : Unable to request higher precision"); } } InputStream inputStream = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(inputStream)); timelineMetrics = timelineObjectReader.readValue(reader); if (LOG.isTraceEnabled()) { for (TimelineMetric metric : timelineMetrics.getMetrics()) { LOG.trace("metric: " + metric.getMetricName() + ", size = " + metric.getMetricValues().size() + ", host = " + metric.getHostName() + ", app = " + metric.getAppId() + ", instance = " + metric.getInstanceId() + ", time = " + metric.getTimestamp() + ", startTime = " + new Date(metric.getStartTime())); } } } catch (IOException io) { String errorMsg = "Error getting timeline metrics : " + io.getMessage(); LOG.error(errorMsg); if (LOG.isDebugEnabled()) { LOG.debug(errorMsg, io); } if (io instanceof SocketTimeoutException || io instanceof ConnectException) { errorMsg = "Cannot connect to collector: SocketTimeoutException for " + uriBuilder.getHost(); LOG.error(errorMsg); throw io; } } catch (URISyntaxException e) { String errorMsg = "Error getting timeline metrics : " + e.getMessage(); LOG.error(errorMsg); if (LOG.isDebugEnabled()) { LOG.debug(errorMsg, e); } } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { if (LOG.isWarnEnabled()) { if (LOG.isDebugEnabled()) { LOG.warn("Unable to close http input stream : spec=" + uriBuilder.toString(), e); } else { LOG.warn("Unable to close http input stream : spec=" + uriBuilder.toString()); } } } } } return timelineMetrics; } MetricsRequestHelper(URLStreamProvider streamProvider); }
MetricsRequestHelper { public TimelineMetrics fetchTimelineMetrics(URIBuilder uriBuilder, Long startTime, Long endTime) throws IOException { LOG.debug("Metrics request url = " + uriBuilder.toString()); BufferedReader reader = null; TimelineMetrics timelineMetrics = null; try { HttpURLConnection connection = streamProvider.processURL(uriBuilder.toString(), HttpMethod.GET, (String) null, Collections.<String, List<String>>emptyMap()); if (!checkConnectionForPrecisionException(connection)) { String higherPrecision = getHigherPrecision(uriBuilder, startTime, endTime); if (higherPrecision != null) { LOG.debug("Requesting metrics with higher precision : " + higherPrecision); uriBuilder.setParameter("precision", higherPrecision); String newSpec = uriBuilder.toString(); connection = streamProvider.processURL(newSpec, HttpMethod.GET, (String) null, Collections.<String, List<String>>emptyMap()); if (!checkConnectionForPrecisionException(connection)) { throw new IOException("Encountered Precision exception : Higher precision request also failed."); } } else { throw new IOException("Encountered Precision exception : Unable to request higher precision"); } } InputStream inputStream = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(inputStream)); timelineMetrics = timelineObjectReader.readValue(reader); if (LOG.isTraceEnabled()) { for (TimelineMetric metric : timelineMetrics.getMetrics()) { LOG.trace("metric: " + metric.getMetricName() + ", size = " + metric.getMetricValues().size() + ", host = " + metric.getHostName() + ", app = " + metric.getAppId() + ", instance = " + metric.getInstanceId() + ", time = " + metric.getTimestamp() + ", startTime = " + new Date(metric.getStartTime())); } } } catch (IOException io) { String errorMsg = "Error getting timeline metrics : " + io.getMessage(); LOG.error(errorMsg); if (LOG.isDebugEnabled()) { LOG.debug(errorMsg, io); } if (io instanceof SocketTimeoutException || io instanceof ConnectException) { errorMsg = "Cannot connect to collector: SocketTimeoutException for " + uriBuilder.getHost(); LOG.error(errorMsg); throw io; } } catch (URISyntaxException e) { String errorMsg = "Error getting timeline metrics : " + e.getMessage(); LOG.error(errorMsg); if (LOG.isDebugEnabled()) { LOG.debug(errorMsg, e); } } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { if (LOG.isWarnEnabled()) { if (LOG.isDebugEnabled()) { LOG.warn("Unable to close http input stream : spec=" + uriBuilder.toString(), e); } else { LOG.warn("Unable to close http input stream : spec=" + uriBuilder.toString()); } } } } } return timelineMetrics; } MetricsRequestHelper(URLStreamProvider streamProvider); TimelineMetrics fetchTimelineMetrics(URIBuilder uriBuilder, Long startTime, Long endTime); }
MetricsRequestHelper { public TimelineMetrics fetchTimelineMetrics(URIBuilder uriBuilder, Long startTime, Long endTime) throws IOException { LOG.debug("Metrics request url = " + uriBuilder.toString()); BufferedReader reader = null; TimelineMetrics timelineMetrics = null; try { HttpURLConnection connection = streamProvider.processURL(uriBuilder.toString(), HttpMethod.GET, (String) null, Collections.<String, List<String>>emptyMap()); if (!checkConnectionForPrecisionException(connection)) { String higherPrecision = getHigherPrecision(uriBuilder, startTime, endTime); if (higherPrecision != null) { LOG.debug("Requesting metrics with higher precision : " + higherPrecision); uriBuilder.setParameter("precision", higherPrecision); String newSpec = uriBuilder.toString(); connection = streamProvider.processURL(newSpec, HttpMethod.GET, (String) null, Collections.<String, List<String>>emptyMap()); if (!checkConnectionForPrecisionException(connection)) { throw new IOException("Encountered Precision exception : Higher precision request also failed."); } } else { throw new IOException("Encountered Precision exception : Unable to request higher precision"); } } InputStream inputStream = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(inputStream)); timelineMetrics = timelineObjectReader.readValue(reader); if (LOG.isTraceEnabled()) { for (TimelineMetric metric : timelineMetrics.getMetrics()) { LOG.trace("metric: " + metric.getMetricName() + ", size = " + metric.getMetricValues().size() + ", host = " + metric.getHostName() + ", app = " + metric.getAppId() + ", instance = " + metric.getInstanceId() + ", time = " + metric.getTimestamp() + ", startTime = " + new Date(metric.getStartTime())); } } } catch (IOException io) { String errorMsg = "Error getting timeline metrics : " + io.getMessage(); LOG.error(errorMsg); if (LOG.isDebugEnabled()) { LOG.debug(errorMsg, io); } if (io instanceof SocketTimeoutException || io instanceof ConnectException) { errorMsg = "Cannot connect to collector: SocketTimeoutException for " + uriBuilder.getHost(); LOG.error(errorMsg); throw io; } } catch (URISyntaxException e) { String errorMsg = "Error getting timeline metrics : " + e.getMessage(); LOG.error(errorMsg); if (LOG.isDebugEnabled()) { LOG.debug(errorMsg, e); } } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { if (LOG.isWarnEnabled()) { if (LOG.isDebugEnabled()) { LOG.warn("Unable to close http input stream : spec=" + uriBuilder.toString(), e); } else { LOG.warn("Unable to close http input stream : spec=" + uriBuilder.toString()); } } } } } return timelineMetrics; } MetricsRequestHelper(URLStreamProvider streamProvider); TimelineMetrics fetchTimelineMetrics(URIBuilder uriBuilder, Long startTime, Long endTime);  }