Search in sources :

Example 26 with DocCollection

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

the class ReplicaMutator method updateStateNew.

/**
   * Handles non-legacy state updates
   */
protected ZkWriteCommand updateStateNew(ClusterState clusterState, final ZkNodeProps message) {
    String collectionName = message.getStr(ZkStateReader.COLLECTION_PROP);
    if (!checkCollectionKeyExistence(message))
        return ZkStateWriter.NO_OP;
    String sliceName = message.getStr(ZkStateReader.SHARD_ID_PROP);
    if (collectionName == null || sliceName == null) {
        log.error("Invalid collection and slice {}", message);
        return ZkStateWriter.NO_OP;
    }
    DocCollection collection = clusterState.getCollectionOrNull(collectionName);
    Slice slice = collection != null ? collection.getSlice(sliceName) : null;
    if (slice == null) {
        log.error("No such slice exists {}", message);
        return ZkStateWriter.NO_OP;
    }
    return updateState(clusterState, message);
}
Also used : Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection)

Example 27 with DocCollection

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

the class SliceMutator method setShardLeader.

public ZkWriteCommand setShardLeader(ClusterState clusterState, ZkNodeProps message) {
    StringBuilder sb = new StringBuilder();
    String baseUrl = message.getStr(ZkStateReader.BASE_URL_PROP);
    String coreName = message.getStr(ZkStateReader.CORE_NAME_PROP);
    sb.append(baseUrl);
    if (baseUrl != null && !baseUrl.endsWith("/"))
        sb.append("/");
    sb.append(coreName == null ? "" : coreName);
    if (!(sb.substring(sb.length() - 1).equals("/")))
        sb.append("/");
    String leaderUrl = sb.length() > 0 ? sb.toString() : null;
    String collectionName = message.getStr(ZkStateReader.COLLECTION_PROP);
    String sliceName = message.getStr(ZkStateReader.SHARD_ID_PROP);
    DocCollection coll = clusterState.getCollectionOrNull(collectionName);
    if (coll == null) {
        log.error("Could not mark shard leader for non existing collection:" + collectionName);
        return ZkStateWriter.NO_OP;
    }
    Map<String, Slice> slices = coll.getSlicesMap();
    Slice slice = slices.get(sliceName);
    Replica oldLeader = slice.getLeader();
    final Map<String, Replica> newReplicas = new LinkedHashMap<>();
    for (Replica replica : slice.getReplicas()) {
        // TODO: this should only be calculated once and cached somewhere?
        String coreURL = ZkCoreNodeProps.getCoreUrl(replica.getStr(ZkStateReader.BASE_URL_PROP), replica.getStr(ZkStateReader.CORE_NAME_PROP));
        if (replica == oldLeader && !coreURL.equals(leaderUrl)) {
            replica = new ReplicaMutator(zkStateReader).unsetLeader(replica);
        } else if (coreURL.equals(leaderUrl)) {
            replica = new ReplicaMutator(zkStateReader).setLeader(replica);
        }
        newReplicas.put(replica.getName(), replica);
    }
    Map<String, Object> newSliceProps = slice.shallowCopy();
    newSliceProps.put(Slice.REPLICAS, newReplicas);
    slice = new Slice(slice.getName(), newReplicas, slice.getProperties());
    return new ZkWriteCommand(collectionName, CollectionMutator.updateSlice(collectionName, coll, slice));
}
Also used : Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection) Replica(org.apache.solr.common.cloud.Replica) LinkedHashMap(java.util.LinkedHashMap)

Example 28 with DocCollection

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

the class SliceMutator method removeReplica.

public ZkWriteCommand removeReplica(ClusterState clusterState, ZkNodeProps message) {
    final String cnn = message.getStr(ZkStateReader.CORE_NODE_NAME_PROP);
    final String collection = message.getStr(ZkStateReader.COLLECTION_PROP);
    final String baseUrl = message.getStr(ZkStateReader.BASE_URL_PROP);
    if (!checkCollectionKeyExistence(message))
        return ZkStateWriter.NO_OP;
    DocCollection coll = clusterState.getCollectionOrNull(collection);
    if (coll == null) {
        // make sure we delete the zk nodes for this collection just to be safe
        return new ZkWriteCommand(collection, null);
    }
    Map<String, Slice> newSlices = new LinkedHashMap<>();
    for (Slice slice : coll.getSlices()) {
        Replica replica = slice.getReplica(cnn);
        if (replica != null && (baseUrl == null || baseUrl.equals(replica.getBaseUrl()))) {
            Map<String, Replica> newReplicas = slice.getReplicasCopy();
            newReplicas.remove(cnn);
            slice = new Slice(slice.getName(), newReplicas, slice.getProperties());
        }
        newSlices.put(slice.getName(), slice);
    }
    return new ZkWriteCommand(collection, coll.copyWithSlices(newSlices));
}
Also used : Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection) Replica(org.apache.solr.common.cloud.Replica) LinkedHashMap(java.util.LinkedHashMap)

Example 29 with DocCollection

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

the class ClusterStateMutator method migrateStateFormat.

public ZkWriteCommand migrateStateFormat(ClusterState clusterState, ZkNodeProps message) {
    final String collection = message.getStr(ZkStateReader.COLLECTION_PROP);
    if (!CollectionMutator.checkKeyExistence(message, ZkStateReader.COLLECTION_PROP))
        return ZkStateWriter.NO_OP;
    DocCollection coll = clusterState.getCollectionOrNull(collection);
    if (coll == null || coll.getStateFormat() == 2)
        return ZkStateWriter.NO_OP;
    return new ZkWriteCommand(coll.getName(), new DocCollection(coll.getName(), coll.getSlicesMap(), coll.getProperties(), coll.getRouter(), 0, ZkStateReader.getCollectionPath(collection)));
}
Also used : DocCollection(org.apache.solr.common.cloud.DocCollection)

Example 30 with DocCollection

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

the class ReplaceNodeCmd method getReplicasOfNode.

static List<ZkNodeProps> getReplicasOfNode(String source, ClusterState state) {
    List<ZkNodeProps> sourceReplicas = new ArrayList<>();
    for (Map.Entry<String, DocCollection> e : state.getCollectionsMap().entrySet()) {
        for (Slice slice : e.getValue().getSlices()) {
            for (Replica replica : slice.getReplicas()) {
                if (source.equals(replica.getNodeName())) {
                    ZkNodeProps props = new ZkNodeProps(COLLECTION_PROP, e.getKey(), SHARD_ID_PROP, slice.getName(), ZkStateReader.CORE_NAME_PROP, replica.getCoreName(), ZkStateReader.REPLICA_PROP, replica.getName(), ZkStateReader.REPLICA_TYPE, replica.getType().name(), CoreAdminParams.NODE, source);
                    sourceReplicas.add(props);
                }
            }
        }
    }
    return sourceReplicas;
}
Also used : Slice(org.apache.solr.common.cloud.Slice) ZkNodeProps(org.apache.solr.common.cloud.ZkNodeProps) ArrayList(java.util.ArrayList) DocCollection(org.apache.solr.common.cloud.DocCollection) Map(java.util.Map) Replica(org.apache.solr.common.cloud.Replica)

Aggregations

DocCollection (org.apache.solr.common.cloud.DocCollection)187 Slice (org.apache.solr.common.cloud.Slice)120 Replica (org.apache.solr.common.cloud.Replica)86 HashMap (java.util.HashMap)55 ClusterState (org.apache.solr.common.cloud.ClusterState)52 ArrayList (java.util.ArrayList)50 Map (java.util.Map)42 SolrException (org.apache.solr.common.SolrException)41 Test (org.junit.Test)39 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)32 List (java.util.List)23 NamedList (org.apache.solr.common.util.NamedList)23 HashSet (java.util.HashSet)21 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)19 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)19 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)19 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)17 SolrQuery (org.apache.solr.client.solrj.SolrQuery)16 SolrInputDocument (org.apache.solr.common.SolrInputDocument)16 ZkNodeProps (org.apache.solr.common.cloud.ZkNodeProps)15