Skip to content

Commit

Permalink
Merge pull request #86 from xenit-eu/gh-85
Browse files Browse the repository at this point in the history
gh-85 Fix type of SolrShardingMetricsContainer#rawData
Merge confirmed by @kerkhofsd
  • Loading branch information
anghelutar authored Apr 29, 2021
2 parents 6bc7638 + a531688 commit e4239bf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
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));
}

}

0 comments on commit e4239bf

Please sign in to comment.