Skip to content

Commit

Permalink
gh-85 Invoke ShardRegistry#getFlocs() using reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
Daan Kerkhofs authored and anghelutar committed May 4, 2021
1 parent f21ce61 commit 44de253
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.xenit.alfred.telemetry.binder.solr.sharding;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -16,7 +17,7 @@
public class SolrShardingMetricsContainer {

private ShardRegistry shardRegistry;
private Map<Floc, ? extends Map<Shard, ? extends Set<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 @@ -33,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 @@ -62,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);
}
}

}

0 comments on commit 44de253

Please sign in to comment.