Skip to content

Commit

Permalink
Remove usage of deprecated method in Neo4j extension
Browse files Browse the repository at this point in the history
`dbms.components` will be used to retrieve the correct kernel version instead of `ServerInfo#version`.
  • Loading branch information
michael-simons committed Sep 20, 2021
1 parent 879c866 commit 62d4c9d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import org.jboss.logging.Logger;
import org.neo4j.driver.AccessMode;
import org.neo4j.driver.Driver;
import org.neo4j.driver.Record;
import org.neo4j.driver.Session;
import org.neo4j.driver.SessionConfig;
import org.neo4j.driver.exceptions.SessionExpiredException;
import org.neo4j.driver.internal.retry.ExponentialBackoffRetryLogic;
import org.neo4j.driver.summary.ResultSummary;
import org.neo4j.driver.summary.ServerInfo;

Expand All @@ -25,7 +26,7 @@ public class Neo4jHealthCheck implements HealthCheck {
/**
* The Cypher statement used to verify Neo4j is up.
*/
private static final String CYPHER = "RETURN 1 AS result";
private static final String CYPHER = "CALL dbms.components() YIELD versions, name, edition WHERE name = 'Neo4j Kernel' RETURN edition, versions[0] as version";
/**
* Message indicating that the health check failed.
*/
Expand All @@ -49,15 +50,16 @@ public HealthCheckResponse call() {

HealthCheckResponseBuilder builder = HealthCheckResponse.named("Neo4j connection health check").up();
try {
ResultSummary resultSummary;
// Retry one time when the session has been expired
try {
resultSummary = runHealthCheckQuery();
} catch (SessionExpiredException sessionExpiredException) {
log.warn(MESSAGE_SESSION_EXPIRED);
resultSummary = runHealthCheckQuery();
return runHealthCheckQuery(builder);
} catch (Throwable exception) {
if (ExponentialBackoffRetryLogic.isRetryable(exception)) {
log.warn(MESSAGE_SESSION_EXPIRED);
return runHealthCheckQuery(builder);
}
throw exception;
}
return buildStatusUp(resultSummary, builder);
} catch (Exception e) {
return builder.down().withData("reason", e.getMessage()).build();
}
Expand All @@ -67,29 +69,34 @@ public HealthCheckResponse call() {
* Applies the given {@link ResultSummary} to the {@link HealthCheckResponseBuilder builder} and calls {@code build}
* afterwards.
*
* @param editionAndVersion a single record containing edition and version
* @param resultSummary the result summary returned by the server
* @param builder the health builder to be modified
* @return the final {@link HealthCheckResponse health check response}
*/
private static HealthCheckResponse buildStatusUp(ResultSummary resultSummary, HealthCheckResponseBuilder builder) {
private static HealthCheckResponse buildStatusUp(Record editionAndVersion, ResultSummary resultSummary,
HealthCheckResponseBuilder builder) {
ServerInfo serverInfo = resultSummary.server();

builder.withData("server", serverInfo.version() + "@" + serverInfo.address());
builder.withData("server", "Neo4j/" + editionAndVersion.get("version").asString() + "@" + serverInfo.address());

String databaseName = resultSummary.database().name();
if (!(databaseName == null || databaseName.trim().isEmpty())) {
builder.withData("database", databaseName.trim());
}
builder.withData("edition", editionAndVersion.get("edition").asString());

return builder.build();
}

private ResultSummary runHealthCheckQuery() {
private HealthCheckResponse runHealthCheckQuery(HealthCheckResponseBuilder builder) {
// We use WRITE here to make sure UP is returned for a server that supports
// all possible workloads
try (Session session = this.driver.session(DEFAULT_SESSION_CONFIG)) {
ResultSummary resultSummary = session.run(CYPHER).consume();
return resultSummary;
var result = session.run(CYPHER);
var editionAndVersion = result.single();
ResultSummary resultSummary = result.consume();
return buildStatusUp(editionAndVersion, resultSummary, builder);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public void health() {
.body("status", is("UP"),
"checks.status", containsInAnyOrder("UP"),
"checks.name", containsInAnyOrder("Neo4j connection health check"),
"checks.data.server", containsInAnyOrder(matchesRegex("Neo4j/.*@.*:\\d*")));
"checks.data.server", containsInAnyOrder(matchesRegex("Neo4j/.*@.*:\\d*")),
"checks.data.edition", containsInAnyOrder(is(notNullValue())));
}

@Test
Expand Down

0 comments on commit 62d4c9d

Please sign in to comment.