Search in sources :

Example 56 with ClusterState

use of org.apache.solr.common.cloud.ClusterState in project lucene-solr by apache.

the class ClusterStateMockUtil method buildClusterState.

/**
   * This method lets you construct a complex ClusterState object by using simple strings of letters.
   *
   * c = collection, s = slice, r = replica, \d = node number (r2 means the replica is on node 2),
   * state = [A,R,D,F], * = replica to replace, binds to the left.
   *
   * For example:
   * csrr2rD*sr2csr
   *
   * Creates:
   *
   * 'csrr2rD*'
   * A collection, a shard, a replica on node 1 (the default) that is active (the default), a replica on node 2, and a replica on node 1
   * that has a state of down and is the replica we will be looking to put somewhere else (the *).
   *
   * 'sr2'
   * Then, another shard that has a replica on node 2.
   *
   * 'csr'
   * Then, another collection that has a shard with a single active replica on node 1.
   *
   * Result:
   *        {
   *         "collection2":{
   *           "maxShardsPerNode":"1",
   *           "replicationFactor":"1",
   *           "shards":{"slice1":{
   *               "state":"active",
   *               "replicas":{"replica5":{
   *                   "state":"active",
   *                   "node_name":"baseUrl1_",
   *                   "base_url":"http://baseUrl1"}}}}},
   *         "collection1":{
   *           "maxShardsPerNode":"1",
   *           "replicationFactor":"1",
   *           "shards":{
   *             "slice1":{
   *               "state":"active",
   *               "replicas":{
   *                 "replica3 (bad)":{
   *                   "state":"down",
   *                   "node_name":"baseUrl1_",
   *                   "base_url":"http://baseUrl1"},
   *                 "replica2":{
   *                   "state":"active",
   *                   "node_name":"baseUrl2_",
   *                   "base_url":"http://baseUrl2"},
   *                 "replica1":{
   *                   "state":"active",
   *                   "node_name":"baseUrl1_",
   *                   "base_url":"http://baseUrl1"}}},
   *             "slice2":{
   *               "state":"active",
   *               "replicas":{"replica4":{
   *                   "state":"active",
   *                   "node_name":"baseUrl2_",
   *                   "base_url":"http://baseUrl2"}}}}}}
   *
   */
@SuppressWarnings("resource")
protected static ClusterStateMockUtil.Result buildClusterState(List<Result> results, String clusterDescription, int replicationFactor, int maxShardsPerNode, String... liveNodes) {
    ClusterStateMockUtil.Result result = new ClusterStateMockUtil.Result();
    Map<String, Slice> slices = null;
    Map<String, Replica> replicas = null;
    Map<String, Object> collectionProps = new HashMap<>();
    collectionProps.put(ZkStateReader.MAX_SHARDS_PER_NODE, Integer.toString(maxShardsPerNode));
    collectionProps.put(ZkStateReader.REPLICATION_FACTOR, Integer.toString(replicationFactor));
    Map<String, DocCollection> collectionStates = new HashMap<>();
    DocCollection docCollection = null;
    Slice slice = null;
    int replicaCount = 1;
    Matcher m = BLUEPRINT.matcher(clusterDescription);
    while (m.find()) {
        Replica replica;
        switch(m.group(1)) {
            case "c":
                slices = new HashMap<>();
                docCollection = new DocCollection("collection" + (collectionStates.size() + 1), slices, collectionProps, null);
                collectionStates.put(docCollection.getName(), docCollection);
                break;
            case "s":
                replicas = new HashMap<>();
                slice = new Slice("slice" + (slices.size() + 1), replicas, null);
                slices.put(slice.getName(), slice);
                break;
            case "r":
                Map<String, Object> replicaPropMap = new HashMap<>();
                String node;
                node = m.group(2);
                if (node == null || node.trim().length() == 0) {
                    node = "1";
                }
                Replica.State state = Replica.State.ACTIVE;
                String stateCode = m.group(3);
                if (stateCode != null) {
                    switch(stateCode.charAt(0)) {
                        case 'S':
                            state = Replica.State.ACTIVE;
                            break;
                        case 'R':
                            state = Replica.State.RECOVERING;
                            break;
                        case 'D':
                            state = Replica.State.DOWN;
                            break;
                        case 'F':
                            state = Replica.State.RECOVERY_FAILED;
                            break;
                        default:
                            throw new IllegalArgumentException("Unexpected state for replica: " + stateCode);
                    }
                }
                String nodeName = "baseUrl" + node + "_";
                String replicaName = "replica" + replicaCount++;
                if ("*".equals(m.group(4))) {
                    replicaName += " (bad)";
                }
                replicaPropMap.put(ZkStateReader.NODE_NAME_PROP, nodeName);
                replicaPropMap.put(ZkStateReader.BASE_URL_PROP, "http://baseUrl" + node);
                replicaPropMap.put(ZkStateReader.STATE_PROP, state.toString());
                replica = new Replica(replicaName, replicaPropMap);
                if ("*".equals(m.group(4))) {
                    result.badReplica = new OverseerAutoReplicaFailoverThread.DownReplica();
                    result.badReplica.replica = replica;
                    result.badReplica.slice = slice;
                    result.badReplica.collection = docCollection;
                }
                replicas.put(replica.getName(), replica);
                break;
            default:
                break;
        }
    }
    ClusterState clusterState = new ClusterState(1, new HashSet<>(Arrays.asList(liveNodes)), collectionStates);
    MockZkStateReader reader = new MockZkStateReader(clusterState, collectionStates.keySet());
    String json;
    try {
        json = new String(Utils.toJSON(clusterState), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Unexpected");
    }
    System.err.println(json);
    // todo remove the limitation of always having a bad replica
    assert result.badReplica != null : "Is there no bad replica?";
    assert result.badReplica.slice != null : "Is there no bad replica?";
    result.reader = reader;
    if (results != null) {
        results.add(result);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) DocCollection(org.apache.solr.common.cloud.DocCollection) ClusterState(org.apache.solr.common.cloud.ClusterState) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Replica(org.apache.solr.common.cloud.Replica) Slice(org.apache.solr.common.cloud.Slice)

Example 57 with ClusterState

use of org.apache.solr.common.cloud.ClusterState in project incubator-atlas by apache.

the class Solr5Index method clearStorage.

@Override
public void clearStorage() throws BackendException {
    try {
        if (mode != Mode.CLOUD)
            throw new UnsupportedOperationException("Operation only supported for SolrCloud");
        logger.debug("Clearing storage from Solr: {}", solrClient);
        ZkStateReader zkStateReader = ((CloudSolrClient) solrClient).getZkStateReader();
        zkStateReader.updateClusterState();
        ClusterState clusterState = zkStateReader.getClusterState();
        for (String collection : clusterState.getCollections()) {
            logger.debug("Clearing collection [{}] in Solr", collection);
            UpdateRequest deleteAll = newUpdateRequest();
            deleteAll.deleteByQuery("*:*");
            solrClient.request(deleteAll, collection);
        }
    } catch (SolrServerException e) {
        logger.error("Unable to clear storage from index due to server error on Solr.", e);
        throw new PermanentBackendException(e);
    } catch (IOException e) {
        logger.error("Unable to clear storage from index due to low-level I/O error.", e);
        throw new PermanentBackendException(e);
    } catch (Exception e) {
        logger.error("Unable to clear storage from index due to general error.", e);
        throw new PermanentBackendException(e);
    }
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) ClusterState(org.apache.solr.common.cloud.ClusterState) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) PermanentBackendException(com.thinkaurelius.titan.diskstorage.PermanentBackendException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) TemporaryBackendException(com.thinkaurelius.titan.diskstorage.TemporaryBackendException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) PermanentBackendException(com.thinkaurelius.titan.diskstorage.PermanentBackendException) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient)

Example 58 with ClusterState

use of org.apache.solr.common.cloud.ClusterState in project incubator-atlas by apache.

the class Solr5Index method checkIfCollectionExists.

/**
     * Checks if the collection has already been created in Solr.
     */
private static boolean checkIfCollectionExists(CloudSolrClient server, String collection) throws KeeperException, InterruptedException {
    ZkStateReader zkStateReader = server.getZkStateReader();
    zkStateReader.updateClusterState();
    ClusterState clusterState = zkStateReader.getClusterState();
    return clusterState.getCollectionOrNull(collection) != null;
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) ClusterState(org.apache.solr.common.cloud.ClusterState)

Example 59 with ClusterState

use of org.apache.solr.common.cloud.ClusterState in project incubator-atlas by apache.

the class Solr5Index method waitForRecoveriesToFinish.

/**
     * Wait for all the collection shards to be ready.
     */
private static void waitForRecoveriesToFinish(CloudSolrClient server, String collection) throws KeeperException, InterruptedException {
    ZkStateReader zkStateReader = server.getZkStateReader();
    try {
        boolean cont = true;
        while (cont) {
            boolean sawLiveRecovering = false;
            zkStateReader.updateClusterState();
            ClusterState clusterState = zkStateReader.getClusterState();
            Map<String, Slice> slices = clusterState.getSlicesMap(collection);
            Preconditions.checkNotNull("Could not find collection:" + collection, slices);
            for (Map.Entry<String, Slice> entry : slices.entrySet()) {
                Map<String, Replica> shards = entry.getValue().getReplicasMap();
                for (Map.Entry<String, Replica> shard : shards.entrySet()) {
                    String state = shard.getValue().getStr(ZkStateReader.STATE_PROP);
                    if ((state.equals(Replica.State.RECOVERING.toString()) || state.equals(Replica.State.DOWN.toString())) && clusterState.liveNodesContain(shard.getValue().getStr(ZkStateReader.NODE_NAME_PROP))) {
                        sawLiveRecovering = true;
                    }
                }
            }
            if (!sawLiveRecovering) {
                cont = false;
            } else {
                Thread.sleep(1000);
            }
        }
    } finally {
        logger.info("Exiting solr wait");
    }
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) ClusterState(org.apache.solr.common.cloud.ClusterState) Slice(org.apache.solr.common.cloud.Slice) Map(java.util.Map) HashMap(java.util.HashMap) Replica(org.apache.solr.common.cloud.Replica)

Example 60 with ClusterState

use of org.apache.solr.common.cloud.ClusterState in project janusgraph by JanusGraph.

the class SolrIndex method checkIfCollectionExists.

/**
 * Checks if the collection has already been created in Solr.
 */
private static boolean checkIfCollectionExists(CloudSolrClient server, String collection) throws KeeperException, InterruptedException {
    final ZkStateReader zkStateReader = server.getZkStateReader();
    zkStateReader.forceUpdateCollection(collection);
    final ClusterState clusterState = zkStateReader.getClusterState();
    return clusterState.getCollectionOrNull(collection) != null;
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) ClusterState(org.apache.solr.common.cloud.ClusterState)

Aggregations

ClusterState (org.apache.solr.common.cloud.ClusterState)122 Slice (org.apache.solr.common.cloud.Slice)78 Replica (org.apache.solr.common.cloud.Replica)65 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)56 DocCollection (org.apache.solr.common.cloud.DocCollection)49 HashMap (java.util.HashMap)42 ArrayList (java.util.ArrayList)36 Map (java.util.Map)25 IOException (java.io.IOException)20 Test (org.junit.Test)18 HashSet (java.util.HashSet)17 SolrException (org.apache.solr.common.SolrException)16 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)15 SolrQuery (org.apache.solr.client.solrj.SolrQuery)13 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)13 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)13 ZkCoreNodeProps (org.apache.solr.common.cloud.ZkCoreNodeProps)13 ZkNodeProps (org.apache.solr.common.cloud.ZkNodeProps)13 List (java.util.List)12 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)12