diff --git a/alfred-telemetry-platform/src/main/java/eu/xenit/alfred/telemetry/binder/solr/sharding/SolrShardingMetricsContainer.java b/alfred-telemetry-platform/src/main/java/eu/xenit/alfred/telemetry/binder/solr/sharding/SolrShardingMetricsContainer.java index bb27f856..ce5fbadb 100644 --- a/alfred-telemetry-platform/src/main/java/eu/xenit/alfred/telemetry/binder/solr/sharding/SolrShardingMetricsContainer.java +++ b/alfred-telemetry-platform/src/main/java/eu/xenit/alfred/telemetry/binder/solr/sharding/SolrShardingMetricsContainer.java @@ -1,7 +1,6 @@ package eu.xenit.alfred.telemetry.binder.solr.sharding; -import java.util.HashMap; -import java.util.HashSet; +import java.lang.reflect.InvocationTargetException; import java.util.Map; import java.util.Objects; import java.util.Optional; @@ -18,7 +17,7 @@ public class SolrShardingMetricsContainer { private ShardRegistry shardRegistry; - private Map>> rawData; + private Map>> rawData; private long lastRefresh; private long ttl; // how long before the raw data will be refreshed @@ -35,7 +34,7 @@ public SolrShardingMetricsContainer(ShardRegistry shardRegistry, long ttl) { public void refresh() { long now = System.currentTimeMillis(); if (rawData == null || now - lastRefresh > ttl) { - rawData = shardRegistry.getFlocs(); + rawData = getFlocsWithReflection(); } } @@ -64,4 +63,19 @@ public Optional getReplicaState(ShardInstance shardInstance) { .map(shardState -> ReplicaState.valueOf(shardState.getPropertyBag().get(ShardRegistryImpl.INSTANCE_STATE))); } + /** + * The return type of {@link ShardRegistry#getFlocs()} changed from 'HashMap>>' to 'Map>>'. We invoke the method with reflection to catch + * this change. + */ + private Map>> getFlocsWithReflection() { + try { + // noinspection unchecked + return (Map>>) + shardRegistry.getClass().getMethod("getFlocs").invoke(shardRegistry); + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + throw new IllegalStateException("Failed to invoke ShardRegistry#getFlocs() using reflection", e); + } + } + } diff --git a/alfred-telemetry-platform/src/test/java/eu/xenit/alfred/telemetry/binder/solr/sharding/SolrShardingMetricsContainerTest.java b/alfred-telemetry-platform/src/test/java/eu/xenit/alfred/telemetry/binder/solr/sharding/SolrShardingMetricsContainerTest.java new file mode 100644 index 00000000..4e209bfb --- /dev/null +++ b/alfred-telemetry-platform/src/test/java/eu/xenit/alfred/telemetry/binder/solr/sharding/SolrShardingMetricsContainerTest.java @@ -0,0 +1,43 @@ +package eu.xenit.alfred.telemetry.binder.solr.sharding; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.HashSet; +import org.alfresco.repo.index.shard.Floc; +import org.alfresco.repo.index.shard.Shard; +import org.alfresco.repo.index.shard.ShardRegistry; +import org.alfresco.repo.index.shard.ShardState; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class SolrShardingMetricsContainerTest { + + @Mock + private ShardRegistry shardRegistry; + + private SolrShardingMetricsContainer container; + + @BeforeEach + void setup() { + container = new SolrShardingMetricsContainer(shardRegistry); + } + + @Test + void refresh() { + Floc floc = new Floc(); + HashMap>> flocs = new HashMap<>(); + flocs.put(floc, new HashMap<>()); + when(shardRegistry.getFlocs()).thenReturn(flocs); + + container.refresh(); + assertThat(container.getFlocs(), contains(floc)); + } + +} \ No newline at end of file