diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineV2ClientImpl.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineV2ClientImpl.java index e086e27cbc0..4ca26a77621 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineV2ClientImpl.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/client/api/impl/TimelineV2ClientImpl.java @@ -57,6 +57,8 @@ import com.google.common.annotations.VisibleForTesting; import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.ClientHandlerException; +import com.sun.jersey.api.client.UniformInterfaceException; import com.sun.jersey.core.util.MultivaluedMapImpl; /** @@ -313,14 +315,37 @@ public ClientResponse run() throws Exception { } catch (InterruptedException ie) { throw (IOException) new InterruptedIOException().initCause(ie); } - if (resp == null || resp.getStatusInfo() - .getStatusCode() != ClientResponse.Status.OK.getStatusCode()) { - String msg = - "Response from the timeline server is " + ((resp == null) ? "null" - : "not successful," + " HTTP error code: " + resp.getStatus() - + ", Server response:\n" + resp.getEntity(String.class)); + + //Close ClientResponse's input stream as we are done posting objects. + //ClientResponse#getEntity closes the input stream upon failure in processing HTTP response. + if (resp == null) { + String msg = "Error getting HTTP response from the timeline server."; LOG.error(msg); throw new YarnException(msg); + } else if (resp.getStatusInfo().getStatusCode() == ClientResponse.Status.OK.getStatusCode()) { + try { + resp.close(); + } catch(ClientHandlerException che) { + LOG.warn("Error closing the HTTP response's inputstream.", che); + } finally { + resp = null; + } + } else { + String msg = ""; + try { + String stringType = resp.getEntity(String.class); + msg = "Server response:\n" + stringType; + } catch (ClientHandlerException | UniformInterfaceException chuie) { + msg = "Error getting entity from the HTTP response." + chuie.getLocalizedMessage(); + } finally { + msg = "Response from the timeline server is not successful" + + ", HTTP error code: " + resp.getStatus() + + ", " + + msg; + resp = null; + LOG.error(msg); + throw new YarnException(msg); + } } }