Skip to content

Commit

Permalink
Neo4j health check fix (#3084)
Browse files Browse the repository at this point in the history
* Apply fix for Neo4jHealthCheck: Unable to build native image. #3060

* Fix native image build using workaround

* Checkstyle fixes
  • Loading branch information
dalexandrov authored Jun 15, 2021
1 parent 2eb1222 commit 79a0931
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 57 deletions.
20 changes: 18 additions & 2 deletions examples/integrations/neo4j/neo4j-mp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.applications</groupId>
Expand Down Expand Up @@ -77,6 +77,22 @@
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
<exclusion>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server</artifactId>
<version>${neo4j-harness.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,9 @@
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
import org.eclipse.microprofile.health.Readiness;
import org.neo4j.driver.AccessMode;
import org.neo4j.driver.Driver;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.SessionConfig;
import org.neo4j.driver.exceptions.SessionExpiredException;
import org.neo4j.driver.summary.ResultSummary;
import org.neo4j.driver.summary.ServerInfo;


/**
* Health support module for Neo4j. Follows the standard MicroProfile HealthCheck pattern.
Expand All @@ -43,78 +38,63 @@ 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 SessionConfig DEFAULT_SESSION_CONFIG = SessionConfig.builder()
.withDefaultAccessMode(AccessMode.WRITE)
.build();
static final String CYPHER = "CALL dbms.components() YIELD name, edition WHERE name = 'Neo4j Kernel' RETURN edition";

private final Driver driver;

@Inject
//will be ignored outside of CDI
/**
* Constructor for Health checks.
*
* @param driver Neo4j.
*/
@Inject //will be ignored out of CDI
Neo4jHealthCheck(Driver driver) {
this.driver = driver;
}

/**
* To be used in SE context.
* Creates the Neo4j driver.
*
* @param driver create
* @return Driver
* @param driver Neo4j.
* @return Neo4jHealthCheck.
*/
public static Neo4jHealthCheck create(Driver driver) {
return new Neo4jHealthCheck(driver);
}

/**
* Applies the given ResultSummaryto the HealthCheckResponseBuilder builder and calls build
* afterwards.
*
* @param resultSummary the result summary returned by the server
* @param builder the health builder to be modified
* @return the final HealthCheckResponse health check response
*/
private static HealthCheckResponse buildStatusUp(ResultSummary resultSummary, HealthCheckResponseBuilder builder) {
ServerInfo serverInfo = resultSummary.server();
private HealthCheckResponse runHealthCheckQuery(HealthCheckResponseBuilder builder) {

builder.withData("server", serverInfo.version() + "@" + serverInfo.address());
try (Session session = this.driver.session()) {

String databaseName = resultSummary.database().name();
if (!(databaseName == null || databaseName.trim().isEmpty())) {
builder.withData("database", databaseName.trim());
}
return session.writeTransaction(tx -> {
var result = tx.run(CYPHER);

return builder.build();
}
var edition = result.single().get("edition").asString();
var resultSummary = result.consume();
var serverInfo = resultSummary.server();

@Override
public HealthCheckResponse call() {
var responseBuilder = builder
.withData("server", serverInfo.version() + "@" + serverInfo.address())
.withData("edition", edition);

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) {
resultSummary = runHealthCheckQuery();
}
return buildStatusUp(resultSummary, builder);
} catch (Exception e) {
return builder.down().withData("reason", e.getMessage()).build();
var databaseInfo = resultSummary.database();
if (!databaseInfo.name().trim().isBlank()) {
responseBuilder.withData("database", databaseInfo.name().trim());
}

return responseBuilder.up().build();
});
}
}

private ResultSummary runHealthCheckQuery() {
// We use WRITE here to make sure UP is returned for a server that supports
// all possible workloads
if (driver != null) {
Session session = this.driver.session(DEFAULT_SESSION_CONFIG);
@Override
public HealthCheckResponse call() {

Result run = session.run(CYPHER);
return run.consume();
var builder = HealthCheckResponse.named("Neo4j connection health check");
try {
return runHealthCheckQuery(builder);
} catch (Exception ex) {
return builder.down().withData("reason", ex.getMessage()).build();
}
return null;
}
}

0 comments on commit 79a0931

Please sign in to comment.