diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/routing/AffinityReplicaListTransformer.java b/solr/solrj/src/java/org/apache/solr/client/solrj/routing/AffinityReplicaListTransformer.java index f6dbf6101804..864105a24005 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/routing/AffinityReplicaListTransformer.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/routing/AffinityReplicaListTransformer.java @@ -16,6 +16,7 @@ */ package org.apache.solr.client.solrj.routing; +import java.io.Closeable; import java.util.Arrays; import java.util.Comparator; import java.util.List; @@ -30,7 +31,7 @@ * either explicitly configured hash routing parameter, or the hash of a query parameter (configurable, usually related * to the main query). */ -public class AffinityReplicaListTransformer implements ReplicaListTransformer { +public class AffinityReplicaListTransformer implements ReplicaListTransformer, Closeable { private final int routingDividend; @@ -83,6 +84,11 @@ public void transform(List choices) { } } + @Override + public void close() { + + } + private static final class SortableChoice { private final Object choice; diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/routing/ReplicaListTransformer.java b/solr/solrj/src/java/org/apache/solr/client/solrj/routing/ReplicaListTransformer.java index e2344c62fe82..d8003ca85b1d 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/routing/ReplicaListTransformer.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/routing/ReplicaListTransformer.java @@ -32,4 +32,7 @@ public interface ReplicaListTransformer { */ public void transform(List choices); + default void close() { + + } } diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/routing/RequestReplicaListTransformerGenerator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/routing/RequestReplicaListTransformerGenerator.java index 4853787783c2..9ca7d0db0f59 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/routing/RequestReplicaListTransformerGenerator.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/routing/RequestReplicaListTransformerGenerator.java @@ -16,6 +16,7 @@ */ package org.apache.solr.client.solrj.routing; +import java.io.Closeable; import java.lang.invoke.MethodHandles; import java.util.Arrays; import java.util.Iterator; @@ -124,7 +125,7 @@ public ReplicaListTransformer getReplicaListTransformer(final SolrParams request * and subsequently reordering any equivalent replica sets according to behavior specified * by the baseReplicaListTransformer. */ - private static final class TopLevelReplicaListTransformer implements ReplicaListTransformer { + private static final class TopLevelReplicaListTransformer implements ReplicaListTransformer, Closeable { private final NodePreferenceRulesComparator replicaComp; private final ReplicaListTransformer baseReplicaListTransformer; @@ -185,6 +186,11 @@ public void transform(List choices) { } } } + + @Override + public void close() { + + } } } diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/routing/ShufflingReplicaListTransformer.java b/solr/solrj/src/java/org/apache/solr/client/solrj/routing/ShufflingReplicaListTransformer.java index 7c37129c3425..7d529a694e59 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/routing/ShufflingReplicaListTransformer.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/routing/ShufflingReplicaListTransformer.java @@ -16,11 +16,12 @@ */ package org.apache.solr.client.solrj.routing; +import java.io.Closeable; import java.util.Collections; import java.util.List; import java.util.Random; -public class ShufflingReplicaListTransformer implements ReplicaListTransformer { +public class ShufflingReplicaListTransformer implements ReplicaListTransformer, Closeable { private final Random r; @@ -36,4 +37,9 @@ public void transform(List choices) } } + @Override + public void close() { + + } + } diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/routing/ReplicaListTransformerTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/routing/ReplicaListTransformerTest.java index 0331ef5e740f..75afe702bdb5 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/routing/ReplicaListTransformerTest.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/routing/ReplicaListTransformerTest.java @@ -16,6 +16,7 @@ */ package org.apache.solr.client.solrj.routing; +import java.io.Closeable; import java.lang.invoke.MethodHandles; import java.util.ArrayList; @@ -41,7 +42,7 @@ public class ReplicaListTransformerTest extends SolrTestCase { private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); // A transformer that keeps only matching choices - private static class ToyMatchingReplicaListTransformer implements ReplicaListTransformer { + private static class ToyMatchingReplicaListTransformer implements ReplicaListTransformer, Closeable { private final String regex; @@ -73,10 +74,15 @@ else if (choice instanceof Replica) { } } + @Override + public void close() { + + } + } // A transformer that makes no transformation - private static class ToyNoOpReplicaListTransformer implements ReplicaListTransformer { + private static class ToyNoOpReplicaListTransformer implements ReplicaListTransformer, Closeable { public ToyNoOpReplicaListTransformer() { @@ -88,6 +94,11 @@ public void transform(List choices) log.info("No-Op transform ignoring input: {}", choices); } + @Override + public void close() { + + } + } @Test @@ -95,72 +106,74 @@ public void testTransform() throws Exception { final String regex = ".*" + random().nextInt(10) + ".*"; - final ReplicaListTransformer transformer; - if (random().nextBoolean()) { - log.info("Using ToyMatching Transfomer"); - transformer = new ToyMatchingReplicaListTransformer(regex); + ReplicaListTransformer transformer = null; + try { + if (random().nextBoolean()) { + log.info("Using ToyMatching Transfomer"); + transformer = new ToyMatchingReplicaListTransformer(regex); - } else { - log.info("Using conditional Transfomer"); - transformer = new HttpShardHandlerFactory() { + } else { + log.info("Using conditional Transfomer"); + transformer = new HttpShardHandlerFactory() { - @Override - protected ReplicaListTransformer getReplicaListTransformer(final SolrQueryRequest req) - { - final SolrParams params = req.getParams(); + @Override + protected ReplicaListTransformer getReplicaListTransformer(final SolrQueryRequest req) { + final SolrParams params = req.getParams(); - if (params.getBool("toyNoTransform", false)) { - return new ToyNoOpReplicaListTransformer(); - } + if (params.getBool("toyNoTransform", false)) { + return new ToyNoOpReplicaListTransformer(); + } - final String regex = params.get("toyRegEx"); - if (regex != null) { - return new ToyMatchingReplicaListTransformer(regex); + final String regex = params.get("toyRegEx"); + if (regex != null) { + return new ToyMatchingReplicaListTransformer(regex); + } + + return super.getReplicaListTransformer(req); } - return super.getReplicaListTransformer(req); - } + }.getReplicaListTransformer( + new LocalSolrQueryRequest(null, + new ModifiableSolrParams().add("toyRegEx", regex))); + } - }.getReplicaListTransformer( - new LocalSolrQueryRequest(null, - new ModifiableSolrParams().add("toyRegEx", regex))); - } + final List inputs = new ArrayList<>(); + final List expectedTransformed = new ArrayList<>(); + + final List urls = createRandomUrls(); + for (int ii = 0; ii < urls.size(); ++ii) { + + final String name = "replica" + (ii + 1); + final String url = urls.get(ii); + final Map propMap = new HashMap(); + propMap.put("base_url", url); + propMap.put("core", "test_core"); + propMap.put("node_name", "test_node"); + propMap.put("type", "NRT"); + // a skeleton replica, good enough for this test's purposes + final Replica replica = new Replica(name, propMap, "c1", "s1"); + + inputs.add(replica); + final String coreUrl = replica.getCoreUrl(); + if (coreUrl.matches(regex)) { + log.info("adding replica=[{}] to expected due to core url ({}) regex match on {} ", + replica, coreUrl, regex); + expectedTransformed.add(replica); + } else { + log.info("NOT expecting replica=[{}] due to core url ({}) regex mismatch ({})", + replica, coreUrl, regex); + } - final List inputs = new ArrayList<>(); - final List expectedTransformed = new ArrayList<>(); - - final List urls = createRandomUrls(); - for (int ii=0; ii propMap = new HashMap(); - propMap.put("base_url", url); - propMap.put("core", "test_core"); - propMap.put("node_name", "test_node"); - propMap.put("type", "NRT"); - // a skeleton replica, good enough for this test's purposes - final Replica replica = new Replica(name, propMap,"c1","s1"); - - inputs.add(replica); - final String coreUrl = replica.getCoreUrl(); - if (coreUrl.matches(regex)) { - log.info("adding replica=[{}] to expected due to core url ({}) regex match on {} ", - replica, coreUrl, regex); - expectedTransformed.add(replica); - } else { - log.info("NOT expecting replica=[{}] due to core url ({}) regex mismatch ({})", - replica, coreUrl, regex); } - - } - final List actualTransformed = new ArrayList<>(inputs); - transformer.transform(actualTransformed); + final List actualTransformed = new ArrayList<>(inputs); + transformer.transform(actualTransformed); - assertEquals(expectedTransformed, actualTransformed); - if (transformer instanceof HttpShardHandlerFactory) { - ((HttpShardHandlerFactory) transformer).close(); + assertEquals(expectedTransformed, actualTransformed); + } finally { + if (transformer != null) { + transformer.close(); + } } }