From e5f31b56d4846eaa434f6dd406347475144d9e4b Mon Sep 17 00:00:00 2001 From: John Blum Date: Fri, 21 Sep 2018 15:07:28 -0700 Subject: [PATCH] Add configuration meta-data property for the number of members in the DistributedSystem. Guard against null/unset URLs for the DistributedSystem standard gemfire properties and security properties locations. --- .../boot/actuate/GeodeCacheHealthIndicator.java | 17 +++++++++++++++-- .../GeodeCacheHealthIndicatorUnitTests.java | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/spring-geode-actuator/src/main/java/org/springframework/geode/boot/actuate/GeodeCacheHealthIndicator.java b/spring-geode-actuator/src/main/java/org/springframework/geode/boot/actuate/GeodeCacheHealthIndicator.java index b15bea8e..c7ed4b0a 100644 --- a/spring-geode-actuator/src/main/java/org/springframework/geode/boot/actuate/GeodeCacheHealthIndicator.java +++ b/spring-geode-actuator/src/main/java/org/springframework/geode/boot/actuate/GeodeCacheHealthIndicator.java @@ -17,6 +17,7 @@ package org.springframework.geode.boot.actuate; import java.net.URL; +import java.util.Collection; import java.util.Optional; import java.util.function.Function; @@ -29,6 +30,7 @@ import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.boot.actuate.health.Status; import org.springframework.geode.boot.actuate.health.AbstractGeodeHealthIndicator; +import org.springframework.util.StringUtils; /** * The {@link GeodeCacheHealthIndicator} class is a Spring Boot {@link HealthIndicator} providing details about @@ -135,12 +137,23 @@ public class GeodeCacheHealthIndicator extends AbstractGeodeHealthIndicator { return healthBuilder -> getGemFireCache() .map(GemFireCache::getDistributedSystem) .map(distributedSystem -> healthBuilder + .withDetail("geode.distributed-system.member-count", Optional.of(distributedSystem) + .map(DistributedSystem::getAllOtherMembers) + .map(Collection::size) + .map(size -> size + 1) + .orElse(1)) .withDetail("geode.distributed-system.connection", toConnectedNoConnectedString(distributedSystem.isConnected())) .withDetail("geode.distributed-system.reconnecting", toYesNoString(distributedSystem.isReconnecting())) .withDetail("geode.distributed-system.properties-location", - Optional.ofNullable(DistributedSystem.getPropertiesFileURL()).map(URL::toExternalForm)) + Optional.ofNullable(DistributedSystem.getPropertiesFileURL()) + .map(URL::toExternalForm) + .filter(StringUtils::hasText) + .orElse("")) .withDetail("geode.distributed-system.security-properties-location", - Optional.ofNullable(DistributedSystem.getSecurityPropertiesFileURL()).map(URL::toExternalForm)) + Optional.ofNullable(DistributedSystem.getSecurityPropertiesFileURL()) + .map(URL::toExternalForm) + .filter(StringUtils::hasText) + .orElse("")) ) .orElse(healthBuilder); } diff --git a/spring-geode-actuator/src/test/java/org/springframework/geode/boot/actuate/GeodeCacheHealthIndicatorUnitTests.java b/spring-geode-actuator/src/test/java/org/springframework/geode/boot/actuate/GeodeCacheHealthIndicatorUnitTests.java index eb89f87f..ecdd3fad 100644 --- a/spring-geode-actuator/src/test/java/org/springframework/geode/boot/actuate/GeodeCacheHealthIndicatorUnitTests.java +++ b/spring-geode-actuator/src/test/java/org/springframework/geode/boot/actuate/GeodeCacheHealthIndicatorUnitTests.java @@ -24,6 +24,9 @@ import static org.mockito.Mockito.when; import java.util.Arrays; import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import org.apache.geode.CancelCriterion; import org.apache.geode.cache.GemFireCache; @@ -70,6 +73,15 @@ public class GeodeCacheHealthIndicatorUnitTests { public void setup() { this.cacheHealthIndicator = new GeodeCacheHealthIndicator(this.mockGemFireCache); } + + @SuppressWarnings("all") + private Set mockDistributedMembers(int size) { + + return IntStream.range(0, size) + .mapToObj(it -> mock(DistributedMember.class)) + .collect(Collectors.toSet()); + } + @Test public void healthCheckCapturesDetails() throws Exception { @@ -81,6 +93,7 @@ public class GeodeCacheHealthIndicatorUnitTests { DistributedSystem mockDistributedSystem = CacheMockObjects.mockDistributedSystem(mockDistributedMember); + when(mockDistributedSystem.getAllOtherMembers()).thenAnswer(invocation -> mockDistributedMembers(8)); when(mockDistributedSystem.isConnected()).thenReturn(true); when(mockDistributedSystem.isReconnecting()).thenReturn(false); @@ -118,6 +131,7 @@ public class GeodeCacheHealthIndicatorUnitTests { assertThat(healthDetails).containsEntry("geode.distributed-member.groups", Arrays.asList("TestGroup", "MockGroup")); assertThat(healthDetails).containsEntry("geode.distributed-member.host", "Skullbox"); assertThat(healthDetails).containsEntry("geode.distributed-member.process-id", 12345); + assertThat(healthDetails).containsEntry("geode.distributed-system.member-count", 9); assertThat(healthDetails).containsEntry("geode.distributed-system.connection", "Connected"); assertThat(healthDetails).containsEntry("geode.distributed-system.reconnecting", "No"); //assertThat(healthDetails).containsKey("geode.distributed-member.properties-location");