Skip to content

Commit

Permalink
fix: fixed broken HA test, reduced errors in console on HA replicatio…
Browse files Browse the repository at this point in the history
…n issues (#1795)
  • Loading branch information
lvca authored Oct 29, 2024
1 parent 79633a4 commit 9db7062
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ protected String parseRequestPayload(final HttpServerExchange e) {
e.startBlocking();

if (!mustExecuteOnWorkerThread())
LogManager.instance().log(this, Level.SEVERE, "Error: handler must return true at mustExecuteOnWorkerThread() to read payload from request");
LogManager.instance()
.log(this, Level.SEVERE, "Error: handler must return true at mustExecuteOnWorkerThread() to read payload from request");

final AtomicReference<String> result = new AtomicReference<>();
e.getRequestReceiver().receiveFullBytes(
Expand Down Expand Up @@ -90,7 +91,7 @@ public void handleRequest(final HttpServerExchange exchange) {
final HeaderValues authorization = exchange.getRequestHeaders().get("Authorization");
if (isRequireAuthentication() && (authorization == null || authorization.isEmpty())) {
exchange.setStatusCode(401);
exchange.getResponseHeaders().put(Headers.WWW_AUTHENTICATE,"Basic");
exchange.getResponseHeaders().put(Headers.WWW_AUTHENTICATE, "Basic");
sendErrorResponse(exchange, 401, "", null, null);
return;
}
Expand Down Expand Up @@ -130,39 +131,56 @@ public void handleRequest(final HttpServerExchange exchange) {

} catch (final ServerSecurityException e) {
// PASS SecurityException TO THE CLIENT
LogManager.instance().log(this, getUserSevereErrorLogLevel(), "Security error on command execution (%s)", e, SecurityException.class.getSimpleName());
LogManager.instance().log(this, getUserSevereErrorLogLevel(), "Security error on command execution (%s): %s",
SecurityException.class.getSimpleName(), e.getMessage());
sendErrorResponse(exchange, 403, "Security error", e, null);
} catch (final ServerIsNotTheLeaderException e) {
LogManager.instance().log(this, getUserSevereErrorLogLevel(), "Error on command execution (%s)", e, getClass().getSimpleName());
LogManager.instance()
.log(this, getUserSevereErrorLogLevel(), "Error on command execution (%s): %s", getClass().getSimpleName(),
e.getMessage());
sendErrorResponse(exchange, 400, "Cannot execute command", e, e.getLeaderAddress());
} catch (final NeedRetryException e) {
LogManager.instance().log(this, getUserSevereErrorLogLevel(), "Error on command execution (%s)", e, getClass().getSimpleName());
LogManager.instance()
.log(this, getUserSevereErrorLogLevel(), "Error on command execution (%s): %s", getClass().getSimpleName(),
e.getMessage());
sendErrorResponse(exchange, 503, "Cannot execute command", e, null);
} catch (final DuplicatedKeyException e) {
LogManager.instance().log(this, getUserSevereErrorLogLevel(), "Error on command execution (%s)", e, getClass().getSimpleName());
sendErrorResponse(exchange, 503, "Found duplicate key in index", e, e.getIndexName() + "|" + e.getKeys() + "|" + e.getCurrentIndexedRID());
LogManager.instance()
.log(this, getUserSevereErrorLogLevel(), "Error on command execution (%s): %s", getClass().getSimpleName(),
e.getMessage());
sendErrorResponse(exchange, 503, "Found duplicate key in index", e,
e.getIndexName() + "|" + e.getKeys() + "|" + e.getCurrentIndexedRID());
} catch (final RecordNotFoundException e) {
LogManager.instance().log(this, getUserSevereErrorLogLevel(), "Error on command execution (%s)", e, getClass().getSimpleName());
LogManager.instance()
.log(this, getUserSevereErrorLogLevel(), "Error on command execution (%s): %s", getClass().getSimpleName(),
e.getMessage());
sendErrorResponse(exchange, 404, "Record not found", e, null);
} catch (final IllegalArgumentException e) {
LogManager.instance().log(this, getUserSevereErrorLogLevel(), "Error on command execution (%s)", e, getClass().getSimpleName());
LogManager.instance()
.log(this, getUserSevereErrorLogLevel(), "Error on command execution (%s): %s", getClass().getSimpleName(),
e.getMessage());
sendErrorResponse(exchange, 400, "Cannot execute command", e, null);
} catch (final CommandExecutionException | CommandParsingException e) {
Throwable realException = e;
if (e.getCause() != null)
realException = e.getCause();

LogManager.instance().log(this, getUserSevereErrorLogLevel(), "Error on command execution (%s)", e, getClass().getSimpleName());
LogManager.instance()
.log(this, getUserSevereErrorLogLevel(), "Error on command execution (%s): %s", getClass().getSimpleName(),
e.getMessage());
sendErrorResponse(exchange, 500, "Cannot execute command", realException, null);
} catch (final TransactionException e) {
Throwable realException = e;
if (e.getCause() != null)
realException = e.getCause();

LogManager.instance().log(this, getUserSevereErrorLogLevel(), "Error on transaction execution (%s)", e, getClass().getSimpleName());
LogManager.instance()
.log(this, getUserSevereErrorLogLevel(), "Error on transaction execution (%s): %s", getClass().getSimpleName(),
e.getMessage());
sendErrorResponse(exchange, 500, "Error on transaction commit", realException, null);
} catch (final Throwable e) {
LogManager.instance().log(this, getErrorLogLevel(), "Error on command execution (%s)", e, getClass().getSimpleName());
LogManager.instance()
.log(this, getErrorLogLevel(), "Error on command execution (%s): %s", getClass().getSimpleName(), e.getMessage());
sendErrorResponse(exchange, 500, "Internal error", e, null);
} finally {
LogManager.instance().setContext(null);
Expand All @@ -189,15 +207,17 @@ protected JSONObject createResult(final SecurityUser user, final Database databa
final JSONObject json = new JSONObject();
if (database != null)
json.setDateFormat(database.getSchema().getDateTimeFormat());
json.put("user", user.getName()).put("version", Constants.getVersion()).put("serverName", httpServer.getServer().getServerName());
json.put("user", user.getName()).put("version", Constants.getVersion())
.put("serverName", httpServer.getServer().getServerName());
return json;
}

protected String decode(final String command) {
return command.replace("&amp;", " ").replace("&lt;", "<").replace("&gt;", ">").replace("&quot;", "\"").replace("&#039;", "'");
}

protected String error2json(final String error, final String detail, final Throwable exception, final String exceptionArgs, final String help) {
protected String error2json(final String error, final String detail, final Throwable exception, final String exceptionArgs,
final String help) {
final JSONObject json = new JSONObject();
json.put("error", error);
if (detail != null)
Expand Down Expand Up @@ -232,14 +252,19 @@ protected String getQueryParameter(final HttpServerExchange exchange, final Stri
}

private Level getErrorLogLevel() {
return "development".equals(httpServer.getServer().getConfiguration().getValueAsString(GlobalConfiguration.SERVER_MODE)) ? Level.SEVERE : Level.FINE;
return "development".equals(httpServer.getServer().getConfiguration().getValueAsString(GlobalConfiguration.SERVER_MODE)) ?
Level.SEVERE :
Level.FINE;
}

private Level getUserSevereErrorLogLevel() {
return "development".equals(httpServer.getServer().getConfiguration().getValueAsString(GlobalConfiguration.SERVER_MODE)) ? Level.INFO : Level.FINE;
return "development".equals(httpServer.getServer().getConfiguration().getValueAsString(GlobalConfiguration.SERVER_MODE)) ?
Level.INFO :
Level.FINE;
}

private void sendErrorResponse(final HttpServerExchange exchange, final int code, final String errorMessage, final Throwable e, final String exceptionArgs) {
private void sendErrorResponse(final HttpServerExchange exchange, final int code, final String errorMessage, final Throwable e,
final String exceptionArgs) {
if (!exchange.isResponseStarted())
exchange.setStatusCode(code);
exchange.getResponseSender().send(error2json(errorMessage, e != null ? e.getMessage() : "", e, exceptionArgs, null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public void testReplication() {
}

}
break;

} catch (final NeedRetryException | TimeoutException | TransactionException e) {
if (e instanceof TimeoutException) {
Expand All @@ -108,18 +109,17 @@ public void testReplication() {
}
// IGNORE IT
LogManager.instance()
.log(this, Level.SEVERE, "Error on creating vertex %d, retrying (retry=%d/%d)...", e, counter, retry, maxRetry);
.log(this, Level.SEVERE, "Error on creating vertex %d, retrying (retry=%d/%d): %s", counter, retry, maxRetry,
e.getMessage());
CodeUtils.sleep(500);

} catch (final DuplicatedKeyException e) {
// THIS MEANS THE ENTRY WAS INSERTED BEFORE THE CRASH
LogManager.instance().log(this, Level.SEVERE, "Error: %s (IGNORE IT)", null, e.toString());
LogManager.instance().log(this, Level.SEVERE, "Error: %s (IGNORE IT)", e.getMessage());
} catch (final Exception e) {
// IGNORE IT
LogManager.instance().log(this, Level.SEVERE, "Generic Exception: %s", e, e.getMessage());
LogManager.instance().log(this, Level.SEVERE, "Generic Exception: %s", e.getMessage());
}

break;
}
}

Expand Down

0 comments on commit 9db7062

Please sign in to comment.