List of usage examples for org.apache.solr.common.cloud ClusterState hasCollection
public boolean hasCollection(String collectionName)
From source file:de.qaware.chronix.storage.solr.ChronixSolrCloudStorage.java
License:Apache License
/** * Returns the list of shards of the default collection. * * @param zkHost ZooKeeper URL * @param chronixCollection Solr collection name for chronix time series data * @return the list of shards of the default collection *//*from w w w. j a v a 2s .c o m*/ public List<String> getShardList(String zkHost, String chronixCollection) throws IOException { CloudSolrClient cloudSolrClient = new CloudSolrClient(zkHost); List<String> shards = new ArrayList<>(); try { cloudSolrClient.connect(); ZkStateReader zkStateReader = cloudSolrClient.getZkStateReader(); ClusterState clusterState = zkStateReader.getClusterState(); String[] collections; if (clusterState.hasCollection(chronixCollection)) { collections = new String[] { chronixCollection }; } else { // might be a collection alias? Aliases aliases = zkStateReader.getAliases(); String aliasedCollections = aliases.getCollectionAlias(chronixCollection); if (aliasedCollections == null) throw new IllegalArgumentException("Collection " + chronixCollection + " not found!"); collections = aliasedCollections.split(","); } Set<String> liveNodes = clusterState.getLiveNodes(); Random random = new Random(5150); for (String coll : collections) { for (Slice slice : clusterState.getSlices(coll)) { List<String> replicas = new ArrayList<>(); for (Replica r : slice.getReplicas()) { if (r.getState().equals(Replica.State.ACTIVE)) { ZkCoreNodeProps replicaCoreProps = new ZkCoreNodeProps(r); if (liveNodes.contains(replicaCoreProps.getNodeName())) replicas.add(replicaCoreProps.getCoreUrl()); } } int numReplicas = replicas.size(); if (numReplicas == 0) throw new IllegalStateException("Shard " + slice.getName() + " in collection " + coll + " does not have any active replicas!"); String replicaUrl = (numReplicas == 1) ? replicas.get(0) : replicas.get(random.nextInt(replicas.size())); shards.add(replicaUrl); } } } finally { cloudSolrClient.close(); } return shards; }