Example usage for org.apache.solr.common.cloud ClusterState getCollectionsMap

List of usage examples for org.apache.solr.common.cloud ClusterState getCollectionsMap

Introduction

In this page you can find the example usage for org.apache.solr.common.cloud ClusterState getCollectionsMap.

Prototype

public Map<String, DocCollection> getCollectionsMap() 

Source Link

Document

Get a map of collection name vs DocCollection objects Implementation note: This method resolves the collection reference by calling CollectionRef#get() which can make a call to ZooKeeper.

Usage

From source file:com.shaie.solr.CollectionsStateHelper.java

License:Apache License

/** Returns all the replicas (of all shards and collections) that exist on the given node. */
public List<Replica> getAllNodeReplicas(String nodeName) {
    final List<Replica> replicas = Lists.newArrayList();
    final ClusterState clusterState = getClusterState();
    for (final DocCollection collection : clusterState.getCollectionsMap().values()) {
        for (final Slice slice : collection.getSlices()) {
            for (final Replica replica : slice.getReplicas()) {
                if (replica.getNodeName().equals(nodeName)) {
                    replicas.add(replica);
                }//ww  w .ja  va 2 s.co m
            }
        }
    }
    return replicas;
}

From source file:com.shaie.solr.CollectionsStateHelper.java

License:Apache License

/** Returns all replicas per node. */
private Map<String, List<ReplicaInfo>> getNodeReplicas() {
    final ClusterState clusterState = getClusterState();
    final Map<String, List<ReplicaInfo>> result = Maps.newHashMap();
    for (final DocCollection collection : clusterState.getCollectionsMap().values()) {
        for (final Slice slice : collection.getSlices()) {
            for (final Replica replica : slice.getReplicas()) {
                List<ReplicaInfo> nodeReplicas = result.get(replica.getNodeName());
                if (nodeReplicas == null) {
                    nodeReplicas = Lists.newArrayList();
                    result.put(replica.getNodeName(), nodeReplicas);
                }/*w ww.j av a  2s.c o m*/
                nodeReplicas.add(new ReplicaInfo(replica, collection.getName(), slice.getName()));
            }
        }
    }
    return result;
}

From source file:com.shaie.solr.SplitShardTest.java

License:Apache License

private void printClusterStatus() {
    final ClusterState clusterState = solrClient.getZkStateReader().getClusterState();
    System.out.println("live nodes: " + clusterState.getLiveNodes());
    for (final Entry<String, DocCollection> collection : clusterState.getCollectionsMap().entrySet()) {
        System.out.println(collection.getValue());
    }/* ww  w.  j a  va2  s. com*/
}

From source file:org.janusgraph.diskstorage.solr.SolrIndex.java

License:Apache License

@Override
public void clearStorage() throws BackendException {
    try {/*from w  w  w  .ja  v a2  s . c o  m*/
        if (mode != Mode.CLOUD) {
            logger.error(
                    "Operation only supported for SolrCloud. Cores must be deleted manually through the Solr API when using HTTP mode.");
            return;
        }
        logger.debug("Clearing storage from Solr: {}", solrClient);
        final ZkStateReader zkStateReader = ((CloudSolrClient) solrClient).getZkStateReader();
        zkStateReader.forciblyRefreshAllClusterStateSlow();
        final ClusterState clusterState = zkStateReader.getClusterState();
        for (final String collection : clusterState.getCollectionsMap().keySet()) {
            logger.debug("Clearing collection [{}] in Solr", collection);
            // Collection is not dropped because it may have been created externally
            final UpdateRequest deleteAll = newUpdateRequest();
            deleteAll.deleteByQuery("*:*");
            solrClient.request(deleteAll, collection);
        }

    } catch (final SolrServerException e) {
        logger.error("Unable to clear storage from index due to server error on Solr.", e);
        throw new PermanentBackendException(e);
    } catch (final IOException e) {
        logger.error("Unable to clear storage from index due to low-level I/O error.", e);
        throw new PermanentBackendException(e);
    } catch (final Exception e) {
        logger.error("Unable to clear storage from index due to general error.", e);
        throw new PermanentBackendException(e);
    }
}

From source file:org.janusgraph.diskstorage.solr.SolrIndex.java

License:Apache License

@Override
public boolean exists() throws BackendException {
    if (mode != Mode.CLOUD)
        throw new UnsupportedOperationException("Operation only supported for SolrCloud");
    final CloudSolrClient server = (CloudSolrClient) solrClient;
    try {/*from   w  ww . ja v a 2s . c o  m*/
        final ZkStateReader zkStateReader = server.getZkStateReader();
        zkStateReader.forciblyRefreshAllClusterStateSlow();
        final ClusterState clusterState = zkStateReader.getClusterState();
        final Map<String, DocCollection> collections = clusterState.getCollectionsMap();
        return collections != null && !collections.isEmpty();
    } catch (KeeperException | InterruptedException e) {
        throw new PermanentBackendException("Unable to check if index exists", e);
    }
}