From a1a64e7470a0989e83aee2fd2fd264b58c7ffe51 Mon Sep 17 00:00:00 2001 From: John Blum Date: Fri, 21 Sep 2018 15:08:56 -0700 Subject: [PATCH] Guard against accessing Pool.pendingEventCount when client is not durable. --- .../actuate/GeodePoolsHealthIndicator.java | 7 ++++++- .../GeodePoolsHealthIndicatorUnitTests.java | 21 ++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/spring-geode-actuator/src/main/java/org/springframework/geode/boot/actuate/GeodePoolsHealthIndicator.java b/spring-geode-actuator/src/main/java/org/springframework/geode/boot/actuate/GeodePoolsHealthIndicator.java index d585a5e1..2a1db705 100644 --- a/spring-geode-actuator/src/main/java/org/springframework/geode/boot/actuate/GeodePoolsHealthIndicator.java +++ b/spring-geode-actuator/src/main/java/org/springframework/geode/boot/actuate/GeodePoolsHealthIndicator.java @@ -26,6 +26,7 @@ import java.util.Objects; import java.util.stream.Collectors; import org.apache.geode.cache.GemFireCache; +import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.Pool; import org.apache.geode.cache.client.PoolManager; import org.springframework.boot.actuate.health.Health; @@ -94,7 +95,6 @@ public class GeodePoolsHealthIndicator extends AbstractGeodeHealthIndicator { .withDetail(poolKey(poolName, "min-connections"), pool.getMinConnections()) .withDetail(poolKey(poolName, "multi-user-authentication"), toYesNoString(pool.getMultiuserAuthentication())) .withDetail(poolKey(poolName, "online-locators"), toCommaDelimitedHostAndPortsString(pool.getOnlineLocators())) - .withDetail(poolKey(poolName, "pending-event-count"), pool.getPendingEventCount()) .withDetail(poolKey(poolName, "ping-interval"), pool.getPingInterval()) .withDetail(poolKey(poolName, "pr-single-hop-enabled"), toYesNoString(pool.getPRSingleHopEnabled())) .withDetail(poolKey(poolName, "read-timeout"), pool.getReadTimeout()) @@ -108,6 +108,11 @@ public class GeodePoolsHealthIndicator extends AbstractGeodeHealthIndicator { .withDetail(poolKey(poolName, "subscription-message-tracking-timeout"), pool.getSubscriptionMessageTrackingTimeout()) .withDetail(poolKey(poolName, "subscription-redundancy"), pool.getSubscriptionRedundancy()) .withDetail(poolKey(poolName, "thread-local-connections"), toYesNoString(pool.getThreadLocalConnections())); + + getGemFireCache() + .map(ClientCache.class::cast) + .filter(CacheUtils::isDurable) + .ifPresent(it -> builder.withDetail(poolKey(poolName, "pending-event-count"), pool.getPendingEventCount())); }); builder.up(); diff --git a/spring-geode-actuator/src/test/java/org/springframework/geode/boot/actuate/GeodePoolsHealthIndicatorUnitTests.java b/spring-geode-actuator/src/test/java/org/springframework/geode/boot/actuate/GeodePoolsHealthIndicatorUnitTests.java index 2b165793..2fc31c8a 100644 --- a/spring-geode-actuator/src/test/java/org/springframework/geode/boot/actuate/GeodePoolsHealthIndicatorUnitTests.java +++ b/spring-geode-actuator/src/test/java/org/springframework/geode/boot/actuate/GeodePoolsHealthIndicatorUnitTests.java @@ -28,11 +28,13 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Properties; import org.apache.geode.cache.Cache; import org.apache.geode.cache.GemFireCache; import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.Pool; +import org.apache.geode.distributed.DistributedSystem; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -41,6 +43,7 @@ import org.mockito.junit.MockitoJUnitRunner; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.Status; import org.springframework.data.gemfire.tests.mock.PoolMockObjects; +import org.springframework.data.gemfire.util.CacheUtils; /** * Unit tests for {@link GeodePoolsHealthIndicator}. @@ -54,6 +57,7 @@ import org.springframework.data.gemfire.tests.mock.PoolMockObjects; * @see org.apache.geode.cache.GemFireCache * @see org.apache.geode.cache.client.ClientCache * @see org.apache.geode.cache.client.Pool + * @see org.apache.geode.distributed.DistributedSystem * @see org.springframework.boot.actuate.health.Health * @see org.springframework.boot.actuate.health.HealthIndicator * @see org.springframework.data.gemfire.tests.mock.PoolMockObjects @@ -70,7 +74,22 @@ public class GeodePoolsHealthIndicatorUnitTests { @Before public void setup() { - this.poolsHealthIndicator = spy(new GeodePoolsHealthIndicator(this.mockClientCache)); + this.poolsHealthIndicator = spy(new GeodePoolsHealthIndicator(mockDurableClient(this.mockClientCache))); + } + + private ClientCache mockDurableClient(ClientCache mockClientCache) { + + Properties gemfireProperties = new Properties(); + + gemfireProperties.setProperty(CacheUtils.DURABLE_CLIENT_ID_PROPERTY_NAME, "test-durable-client"); + + DistributedSystem mockDistributedSystem = mock(DistributedSystem.class); + + when(mockDistributedSystem.isConnected()).thenReturn(true); + when(mockDistributedSystem.getProperties()).thenReturn(gemfireProperties); + when(mockClientCache.getDistributedSystem()).thenReturn(mockDistributedSystem); + + return mockClientCache; } private InetSocketAddress testSocketAddress(String hostname, int port) {