Example usage for org.apache.solr.common.cloud Replica getState

List of usage examples for org.apache.solr.common.cloud Replica getState

Introduction

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

Prototype

public State getState() 

Source Link

Document

Returns the State of this replica.

Usage

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

License:Apache License

/** Returns true if the replica is on a live node and active. */
public boolean isReplicaActive(Replica replica) {
    return getClusterState().liveNodesContain(replica.getNodeName())
            && replica.getState() == Replica.State.ACTIVE;
}

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

License:Apache License

/** Returns true if the replica is in a DOWN state. */
public boolean isReplicaDown(Replica replica) {
    return !getClusterState().liveNodesContain(replica.getNodeName())
            || replica.getState() == Replica.State.DOWN;
}

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

License:Apache License

@Test
public void kill_node_does_not_gracefully_shutdown() {
    solrCluster.startSolrNodes("node1", "node2");
    createCollectionAndWaitForRecoveries();
    indexDocumentAndWaitForSync("1");

    final String node2Name = SolrCloudUtils.baseUrlToNodeName(solrCluster.getBaseUrl("node2"));
    solrCluster.killSolr("node2");

    final boolean success = SolrCloudUtils.waitForNodeToDisappearFromLiveNodes(solrClient, node2Name,
            WAIT_TIMEOUT_SECONDS);
    assertThat(success)//from  w w w  . j a  va2s. c o m
            .overridingErrorMessage("node " + node2Name + " didn't disappear from cluster's live nodes")
            .isTrue();

    final CollectionsStateHelper collectionsStateHelper = new CollectionsStateHelper(
            solrClient.getZkStateReader());
    final List<Replica> replicas = collectionsStateHelper.getAllCollectionReplicas(COLLECTION_NAME);
    assertThat(replicas.size()).isEqualTo(2);
    for (final Replica replica : replicas) {
        assertThat(replica.getState()).isEqualTo(Replica.State.ACTIVE);
    }
}

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 ww  .  ja va2s.  com*/
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;
}