Skip to content

Commit

Permalink
0005792: Better logging when ack fail
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Apr 17, 2023
1 parent ea014a8 commit 8c18a34
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
Expand Up @@ -365,19 +365,31 @@ protected void sendAck(Node remote, Node local, NodeSecurity localSecurity,
exception = e;
}
if (statusCode != WebConstants.SC_OK) {
String httpMessage = WebConstants.getHttpMessage(statusCode);
boolean retry = statusCode != WebConstants.REGISTRATION_REQUIRED && statusCode != WebConstants.REGISTRATION_PENDING &&
statusCode != WebConstants.SYNC_DISABLED && statusCode != WebConstants.SC_FORBIDDEN && statusCode != WebConstants.SC_AUTH_EXPIRED;
String message = String.format("Ack was not sent successfully on try number %s of %s.", i + 1, numberOfStatusSendRetries);
if (statusCode > 0) {
message += String.format(" statusCode=%s", statusCode);
message += " httpCode=" + statusCode;
}
if (httpMessage != null) {
message += " httpMessage=" + httpMessage;
}
if (exception != null) {
log.info(message + ": " + exception.getClass().getName() + ": " + exception.getMessage());
log.info(message + " " + exception.getClass().getName() + ": " + exception.getMessage());
} else {
log.info(message);
}
if (i < numberOfStatusSendRetries - 1) {
if (!retry) {
log.info("Leaving ack loop to recover from HTTP code that should not be retried");
}
if (retry && i < numberOfStatusSendRetries - 1) {
AppUtils.sleep(parameterService.getLong(ParameterConstants.DATA_LOADER_TIME_BETWEEN_ACK_RETRIES));
} else {
log.warn("Ack was not sent successfully.");
if (!retry) {
break;
}
}
}
}
Expand Down
Expand Up @@ -136,6 +136,7 @@ protected int sendMessage(String action, Node remote, Node local, String data,
}

protected int sendMessage(URL url, String nodeId, String securityToken, Map<String, String> requestProperties, String data) throws IOException {
int rc = 0;
try (HttpConnection conn = openConnection(url, nodeId, securityToken)) {
if (requestProperties != null) {
for (String key : requestProperties.keySet()) {
Expand All @@ -150,15 +151,18 @@ protected int sendMessage(URL url, String nodeId, String securityToken, Map<Stri
try (OutputStream os = conn.getOutputStream()) {
writeMessage(os, data);
checkForConnectionUpgrade(conn);
try (InputStream is = conn.getInputStream()) {
byte[] bytes = new byte[32];
while (is.read(bytes) != -1) {
log.debug("Read keep-alive");
rc = conn.getResponseCode();
if (rc == WebConstants.SC_OK) {
try (InputStream is = conn.getInputStream()) {
byte[] bytes = new byte[32];
while (is.read(bytes) != -1) {
log.debug("Read keep-alive");
}
}
}
return conn.getResponseCode();
}
}
return rc;
}

protected void checkForConnectionUpgrade(HttpConnection conn) {
Expand Down
Expand Up @@ -106,4 +106,42 @@ public class WebConstants {
public static final String REG_PASSWORD = "regPassword";
public static final String PUSH_REGISTRATION = "pushRegistration";
public static final String API_KEY_HEADER = "X-REST-API-KEY";

public static String getHttpMessage(int httpCode) {
String httpMessage = null;
if (httpCode == REGISTRATION_NOT_OPEN) {
httpMessage = "Registration is not open";
} else if (httpCode == REGISTRATION_REQUIRED) {
httpMessage = "Registration is required";
} else if (httpCode == REGISTRATION_PENDING) {
httpMessage = "Registration is pending";
} else if (httpCode == INITIAL_LOAD_PENDING) {
httpMessage = "Initial load is pending";
} else if (httpCode == SYNC_DISABLED) {
httpMessage = "Sync is disabled";
} else if (httpCode == SC_FORBIDDEN) {
httpMessage = "Bad node password";
} else if (httpCode == SC_AUTH_EXPIRED) {
httpMessage = "Session expired";
} else if (httpCode == SC_SERVICE_UNAVAILABLE) {
httpMessage = "Service is unavailable";
} else if (httpCode == SC_SERVICE_BUSY) {
httpMessage = "Service is busy";
} else if (httpCode == SC_SERVICE_ERROR) {
httpMessage = "Service internal error";
} else if (httpCode == SC_NO_RESERVATION) {
httpMessage = "Missing reservation";
} else if (httpCode == SC_ALREADY_CONNECTED) {
httpMessage = "Duplicate connection";
} else if (httpCode == SC_NO_ENGINE) {
httpMessage = "No engine found";
} else if (httpCode == SC_BAD_REQUEST) {
httpMessage = "URI handler not found";
} else if (httpCode == SC_INTERNAL_ERROR) {
httpMessage = "Server internal error";
} else if (httpCode == SC_NO_CONTENT) {
httpMessage = "No content";
}
return httpMessage;
}
}

0 comments on commit 8c18a34

Please sign in to comment.