Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-85 Fix type of SolrShardingMetricsContainer#rawData #86

Merged
merged 3 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,7 +17,7 @@
public class SolrShardingMetricsContainer {

private ShardRegistry shardRegistry;
private Map<Floc, HashMap<Shard, HashSet<ShardState>>> rawData;
private Map<Floc, Map<Shard, Set<ShardState>>> rawData;
private long lastRefresh;
private long ttl; // how long before the raw data will be refreshed

Expand All @@ -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();
}
}

Expand Down Expand Up @@ -64,4 +63,19 @@ public Optional<ReplicaState> getReplicaState(ShardInstance shardInstance) {
.map(shardState -> ReplicaState.valueOf(shardState.getPropertyBag().get(ShardRegistryImpl.INSTANCE_STATE)));
}

/**
* The return type of {@link ShardRegistry#getFlocs()} changed from 'HashMap<Floc, HashMap<Shard,
* HashSet<ShardState>>>' to 'Map<Floc, Map<Shard, Set<ShardState>>>'. We invoke the method with reflection to catch
* this change.
*/
private Map<Floc, Map<Shard, Set<ShardState>>> getFlocsWithReflection() {
try {
// noinspection unchecked
return (Map<Floc, Map<Shard, Set<ShardState>>>)
shardRegistry.getClass().getMethod("getFlocs").invoke(shardRegistry);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new IllegalStateException("Failed to invoke ShardRegistry#getFlocs() using reflection", e);
}
}

}
Original file line number Diff line number Diff line change
@@ -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<Floc, HashMap<Shard, HashSet<ShardState>>> flocs = new HashMap<>();
flocs.put(floc, new HashMap<>());
when(shardRegistry.getFlocs()).thenReturn(flocs);

container.refresh();
assertThat(container.getFlocs(), contains(floc));
}

}