Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of deprecated method in Neo4j extension #20272

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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