Example usage for org.apache.solr.common.cloud Slice getReplicas

List of usage examples for org.apache.solr.common.cloud Slice getReplicas

Introduction

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

Prototype

public Collection<Replica> getReplicas() 

Source Link

Document

Gets the list of all replicas for this slice.

Usage

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

License:Apache License

/** Returns the active replicas of a collection. */
public Collection<Replica> getActiveReplicas(String collection) {
    final List<Replica> activeReplicas = Lists.newArrayList();
    for (final Slice slice : getSlices(collection)) {
        if (!isSliceActive(slice)) {
            continue;
        }//w  w  w  . ja  v  a 2s  . com
        for (final Replica replica : slice.getReplicas()) {
            if (isReplicaActive(replica)) {
                activeReplicas.add(replica);
            }
        }
    }
    return activeReplicas;
}

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

License:Apache License

/** Returns the inactive replicas of a collection. */
public Collection<Replica> getInactiveReplicas(String collection) {
    final List<Replica> inactiveReplicas = Lists.newArrayList();
    for (final Slice slice : getSlices(collection)) {
        if (!isSliceActive(slice)) {
            inactiveReplicas.addAll(slice.getReplicas());
        } else {//from w  ww.j  av  a  2  s .c  o  m
            for (final Replica replica : slice.getReplicas()) {
                if (!isReplicaActive(replica)) {
                    inactiveReplicas.add(replica);
                }
            }
        }
    }
    return inactiveReplicas;
}

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

License:Apache License

/** Returns true if the slice and all its replicas are active. */
public boolean isSliceAndAllReplicasActive(Slice slice) {
    if (!isSliceActive(slice)) {
        return false;
    }/*  w w  w.j  a va 2s.  c om*/
    for (final Replica replica : slice.getReplicas()) {
        if (!isReplicaActive(replica)) {
            return false;
        }
    }
    return true;
}

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);
                }/*w w w . j a v  a2s .  c o  m*/
            }
        }
    }
    return replicas;
}

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

License:Apache License

/** Returns all the replicas of all shards of the specified collection. */
public List<Replica> getAllCollectionReplicas(String collection) {
    final List<Replica> replicas = Lists.newArrayList();
    for (final Slice slice : getSlices(collection)) {
        replicas.addAll(slice.getReplicas());
    }//from  ww  w.  j a v  a  2s  .  com
    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);
                }/*from  w ww. jav a  2  s  . co  m*/
                nodeReplicas.add(new ReplicaInfo(replica, collection.getName(), slice.getName()));
            }
        }
    }
    return result;
}

From source file:com.shaie.solr.solrj.ClusterStatusResponseTest.java

License:Apache License

private static void assertResponseCollection1(final ClusterStatusResponse.Collection collection1) {
    assertThat(collection1.getName()).isEqualTo("collection1");
    assertThat(collection1.getAliases()).containsOnly("both");
    final List<Slice> collection1Slices = collection1.getSlices();
    assertThat(collection1Slices.size()).isEqualTo(2);
    for (final Slice slice : collection1Slices) {
        assertThat(slice.getReplicas().size()).isEqualTo(2);
    }/*from w w  w.jav  a2s. c  o m*/
}

From source file:com.shaie.solr.solrj.ClusterStatusResponseTest.java

License:Apache License

private static void assertResponseCollection2(ClusterStatusResponse.Collection collection2) {
    assertThat(collection2.getName()).isEqualTo("collection2");
    assertThat(collection2.getAliases()).containsOnly("both");
    final List<Slice> collection1Slices = collection2.getSlices();
    assertThat(collection1Slices.size()).isEqualTo(3);
    for (final Slice slice : collection1Slices) {
        assertThat(slice.getReplicas().size()).isEqualTo(1);
    }//from  w w w. j  a  v a 2  s . c  o m
}

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  2  s. 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;
}

From source file:org.opencommercesearch.CloudSearchServer.java

License:Apache License

/**
 * Reloads the core/*from   w  w  w  .j ava2  s  . c o m*/
 *
 * @param collectionName
 *            the cored to be reloaded
 *
 * @throws SearchServerException
 *          if an error occurs while reloading the core
 *
 */
public void reloadCollection(String collectionName, Locale locale) throws SearchServerException {
    CoreAdminRequest adminRequest = new CoreAdminRequest();
    adminRequest.setCoreName(collectionName);
    adminRequest.setAction(CoreAdminAction.RELOAD);

    CloudSolrServer server = getSolrServer(collectionName, locale);
    ZkStateReader zkStateReader = server.getZkStateReader();
    if (zkStateReader == null) {
        //if the zkStateReader is null it means we haven't connect to this collection
        server.connect();
        zkStateReader = server.getZkStateReader();
    }

    ClusterState clusterState = zkStateReader.getClusterState();
    Set<String> liveNodes = clusterState.getLiveNodes();

    if (liveNodes == null || liveNodes.size() == 0) {
        if (isLoggingInfo()) {
            logInfo("No live nodes found, 0 cores were reloaded");
        }
        return;
    }

    Map<String, Slice> slices = clusterState.getSlicesMap(collectionName);
    if (slices.size() == 0) {
        if (isLoggingInfo()) {
            logInfo("No slices found, 0 cores were reloaded");
        }
    }

    for (Slice slice : slices.values()) {
        for (ZkNodeProps nodeProps : slice.getReplicas()) {
            ZkCoreNodeProps coreNodeProps = new ZkCoreNodeProps(nodeProps);
            String node = coreNodeProps.getNodeName();
            if (!liveNodes.contains(coreNodeProps.getNodeName())
                    || !coreNodeProps.getState().equals(ZkStateReader.ACTIVE)) {
                if (isLoggingInfo()) {
                    logInfo("Node " + node + " is not live, unable to reload core " + collectionName);
                }
                continue;
            }

            if (isLoggingInfo()) {
                logInfo("Reloading core " + collectionName + " on " + node);
            }
            HttpClient httpClient = server.getLbServer().getHttpClient();
            HttpSolrServer nodeServer = new HttpSolrServer(coreNodeProps.getBaseUrl(), httpClient,
                    getResponseParser());
            try {
                CoreAdminResponse adminResponse = adminRequest.process(nodeServer);
                if (isLoggingInfo()) {
                    logInfo("Reladed core " + collectionName + ", current status is "
                            + adminResponse.getCoreStatus());
                }
            } catch (SolrServerException ex) {
                if (ex.getCause() instanceof SocketTimeoutException) {
                    //if we experience a socket timeout out don't kill the entire process. Try to reload the other nodes
                    if (isLoggingError()) {
                        logError("Reloading core failed due to socket timeout for node [" + node
                                + "] and collection [" + collectionName + "]");
                    }
                } else {
                    throw create(CORE_RELOAD_EXCEPTION, ex);
                }
            } catch (IOException ex) {
                throw create(CORE_RELOAD_EXCEPTION, ex);
            }
        }
    }
}