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

APOLLO-23097: Support querying aliases #285

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Expand Up @@ -7,10 +7,7 @@
import org.apache.solr.client.solrj.io.stream.SolrStream;
import org.apache.solr.client.solrj.io.stream.StreamContext;
import org.apache.solr.client.solrj.io.stream.TupleStream;
import org.apache.solr.common.cloud.Replica;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.cloud.ZkCoreNodeProps;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.cloud.*;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
Expand Down Expand Up @@ -98,7 +95,8 @@ protected StreamContext getStreamContext() {

protected Replica getRandomReplica() {
ZkStateReader zkStateReader = cloudSolrClient.getZkStateReader();
Collection<Slice> slices = zkStateReader.getClusterState().getCollection(collection.split(",")[0]).getActiveSlices();
final String concreteCollection = findAnyConcreteBackingCollection(zkStateReader);
Collection<Slice> slices = zkStateReader.getClusterState().getCollection(concreteCollection).getActiveSlices();
if (slices == null || slices.size() == 0)
throw new IllegalStateException("No active shards found "+collection);

Expand All @@ -108,4 +106,19 @@ protected Replica getRandomReplica() {
}
return shuffler.get(random.nextInt(shuffler.size()));
}

private String findAnyConcreteBackingCollection(ZkStateReader zkStateReader) {
final String[] collectionsOrAliases = collection.split(",");

for (String collectionOrAlias : collectionsOrAliases) {
if (zkStateReader.getClusterState().hasCollection(collectionOrAlias)) {
return collectionOrAlias;
} else if (zkStateReader.getAliases().hasAlias(collectionOrAlias)) {
final List<String> backingCollections = zkStateReader.getAliases().resolveAliases(collectionOrAlias);
return backingCollections.get(random.nextInt(backingCollections.size()));
}
}

throw new IllegalStateException("Could not find any concrete (non-alias) collection underlying [" + collection + "]");
}
}