use switch for status codes and add comments

This commit is contained in:
javanna 2016-06-09 16:51:08 +02:00 committed by Luca Cavanna
parent cbdffc7965
commit de8b9fd579
1 changed files with 23 additions and 21 deletions

View File

@ -171,7 +171,7 @@ public final class RestClient implements Closeable {
RequestLogger.log(logger, "request succeeded", request, host, httpResponse); RequestLogger.log(logger, "request succeeded", request, host, httpResponse);
onSuccess(host); onSuccess(host);
return response; return response;
} else { }
RequestLogger.log(logger, "request failed", request, host, httpResponse); RequestLogger.log(logger, "request failed", request, host, httpResponse);
String responseBody; String responseBody;
try { try {
@ -183,19 +183,21 @@ public final class RestClient implements Closeable {
} finally { } finally {
response.close(); response.close();
} }
ResponseException responseException = new ResponseException( lastSeenException = addSuppressedException(lastSeenException, new ResponseException(response, responseBody));
response, responseBody); switch(statusCode) {
lastSeenException = addSuppressedException(lastSeenException, responseException); case 502:
//clients don't retry on 500 because elasticsearch still misuses it instead of 400 in some places case 503:
if (statusCode == 502 || statusCode == 503 || statusCode == 504) { case 504:
//mark host dead and retry against next one
onFailure(host); onFailure(host);
} else {
//don't retry and call onSuccess as the error should be a request problem, the node is alive
onSuccess(host);
break; break;
default:
//mark host alive and don't retry, as the error should be a request problem
onSuccess(host);
throw lastSeenException;
} }
} }
} //we get here only when we tried all nodes and they all failed
assert lastSeenException != null; assert lastSeenException != null;
throw lastSeenException; throw lastSeenException;
} }