From 9f9c89a35777fc63bf672ff34c8f7897b79e2130 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 14 Jul 2021 17:40:43 +0100 Subject: [PATCH] Polish "Use server version from database in Neo4j health details" See gh-27294 --- .../actuate/neo4j/Neo4jHealthDetails.java | 50 +++++++++++++++++++ .../neo4j/Neo4jHealthDetailsHandler.java | 14 +++--- .../actuate/neo4j/Neo4jHealthIndicator.java | 6 +-- .../neo4j/Neo4jReactiveHealthIndicator.java | 18 +++---- .../neo4j/Neo4jHealthIndicatorTests.java | 2 +- 5 files changed, 66 insertions(+), 24 deletions(-) create mode 100644 spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jHealthDetails.java diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jHealthDetails.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jHealthDetails.java new file mode 100644 index 0000000000..a850dc8c78 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jHealthDetails.java @@ -0,0 +1,50 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.neo4j; + +import org.neo4j.driver.Record; +import org.neo4j.driver.summary.ResultSummary; + +/** + * Health details for a Neo4j server. + * + * @author Andy Wilkinson + */ +class Neo4jHealthDetails { + + private final Record record; + + private final ResultSummary summary; + + Neo4jHealthDetails(Record record, ResultSummary summary) { + this.record = record; + this.summary = summary; + } + + String getVersion() { + return this.record.get("version").asString(); + } + + String getEdition() { + return this.record.get("edition").asString(); + } + + ResultSummary getSummary() { + return this.summary; + } + +} diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jHealthDetailsHandler.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jHealthDetailsHandler.java index 4834bf2135..aec1250275 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jHealthDetailsHandler.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jHealthDetailsHandler.java @@ -33,14 +33,14 @@ class Neo4jHealthDetailsHandler { /** * Add health details for the specified {@link ResultSummary} and {@code edition}. * @param builder the {@link Builder} to use - * @param version the version of the server - * @param edition the edition of the server - * @param resultSummary server information + * @param healthDetails the health details of the server */ - void addHealthDetails(Builder builder, String version, String edition, ResultSummary resultSummary) { - ServerInfo serverInfo = resultSummary.server(); - builder.up().withDetail("server", version + "@" + serverInfo.address()).withDetail("edition", edition); - DatabaseInfo databaseInfo = resultSummary.database(); + void addHealthDetails(Builder builder, Neo4jHealthDetails healthDetails) { + ResultSummary summary = healthDetails.getSummary(); + ServerInfo serverInfo = summary.server(); + builder.up().withDetail("server", healthDetails.getVersion() + "@" + serverInfo.address()).withDetail("edition", + healthDetails.getEdition()); + DatabaseInfo databaseInfo = summary.database(); if (StringUtils.hasText(databaseInfo.name())) { builder.withDetail("database", databaseInfo.name()); } diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jHealthIndicator.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jHealthIndicator.java index bc6a69a5df..1cab0e246c 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jHealthIndicator.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jHealthIndicator.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -93,10 +93,8 @@ public class Neo4jHealthIndicator extends AbstractHealthIndicator { try (Session session = this.driver.session(DEFAULT_SESSION_CONFIG)) { Result result = session.run(CYPHER); Record record = result.single(); - String edition = record.get("edition").asString(); - String version = record.get("version").asString(); ResultSummary resultSummary = result.consume(); - this.healthDetailsHandler.addHealthDetails(builder, version, edition, resultSummary); + this.healthDetailsHandler.addHealthDetails(builder, new Neo4jHealthDetails(record, resultSummary)); } } diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jReactiveHealthIndicator.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jReactiveHealthIndicator.java index 7204091faa..2938d7c9b0 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jReactiveHealthIndicator.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jReactiveHealthIndicator.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,10 +22,7 @@ import org.neo4j.driver.Driver; import org.neo4j.driver.exceptions.SessionExpiredException; import org.neo4j.driver.reactive.RxResult; import org.neo4j.driver.reactive.RxSession; -import org.neo4j.driver.summary.ResultSummary; import reactor.core.publisher.Mono; -import reactor.util.function.Tuple2; -import reactor.util.function.Tuples; import reactor.util.retry.Retry; import org.springframework.boot.actuate.health.AbstractReactiveHealthIndicator; @@ -58,22 +55,19 @@ public final class Neo4jReactiveHealthIndicator extends AbstractReactiveHealthIn return runHealthCheckQuery() .doOnError(SessionExpiredException.class, (e) -> logger.warn(Neo4jHealthIndicator.MESSAGE_SESSION_EXPIRED)) - .retryWhen(Retry.max(1).filter(SessionExpiredException.class::isInstance)).map((result) -> { - this.healthDetailsHandler.addHealthDetails(builder, result.getT1().getT1(), result.getT1().getT2(), - result.getT2()); + .retryWhen(Retry.max(1).filter(SessionExpiredException.class::isInstance)).map((healthDetails) -> { + this.healthDetailsHandler.addHealthDetails(builder, healthDetails); return builder.build(); }); } - Mono, ResultSummary>> runHealthCheckQuery() { + Mono runHealthCheckQuery() { // We use WRITE here to make sure UP is returned for a server that supports // all possible workloads return Mono.using(() -> this.driver.rxSession(Neo4jHealthIndicator.DEFAULT_SESSION_CONFIG), (session) -> { RxResult result = session.run(Neo4jHealthIndicator.CYPHER); - return Mono.from(result.records()) - .flatMap((record) -> Mono - .just(Tuples.of(record.get("version").asString(), record.get("edition").asString())) - .zipWhen((edition) -> Mono.from(result.consume()))); + return Mono.from(result.records()).zipWhen((record) -> Mono.from(result.consume())) + .map((tuple) -> new Neo4jHealthDetails(tuple.getT1(), tuple.getT2())); }, RxSession::close); } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/neo4j/Neo4jHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/neo4j/Neo4jHealthIndicatorTests.java index 2806f49118..dd89d5b6cf 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/neo4j/Neo4jHealthIndicatorTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/neo4j/Neo4jHealthIndicatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.