Search in sources :

Example 31 with Slice

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

the class ReplicaMutator method updateState.

private ZkWriteCommand updateState(final ClusterState prevState, ZkNodeProps message, String collectionName, Integer numShards, boolean collectionExists) {
    String sliceName = message.getStr(ZkStateReader.SHARD_ID_PROP);
    String coreNodeName = message.getStr(ZkStateReader.CORE_NODE_NAME_PROP);
    DocCollection collection = prevState.getCollectionOrNull(collectionName);
    if (coreNodeName == null) {
        coreNodeName = ClusterStateMutator.getAssignedCoreNodeName(collection, message.getStr(ZkStateReader.NODE_NAME_PROP), message.getStr(ZkStateReader.CORE_NAME_PROP));
        if (coreNodeName != null) {
            log.debug("node=" + coreNodeName + " is already registered");
        } else {
            // if coreNodeName is null, auto assign one
            coreNodeName = Assign.assignNode(collection);
        }
        message.getProperties().put(ZkStateReader.CORE_NODE_NAME_PROP, coreNodeName);
    }
    // use the provided non null shardId
    if (sliceName == null) {
        //get shardId from ClusterState
        sliceName = ClusterStateMutator.getAssignedId(collection, coreNodeName);
        if (sliceName != null) {
            log.debug("shard=" + sliceName + " is already registered");
        }
    }
    if (sliceName == null) {
        //request new shardId
        if (collectionExists) {
            // use existing numShards
            numShards = collection.getSlices().size();
            log.debug("Collection already exists with " + ZkStateReader.NUM_SHARDS_PROP + "=" + numShards);
        }
        sliceName = Assign.assignShard(collection, numShards);
        log.info("Assigning new node to shard shard=" + sliceName);
    }
    Slice slice = collection != null ? collection.getSlice(sliceName) : null;
    Map<String, Object> replicaProps = new LinkedHashMap<>();
    replicaProps.putAll(message.getProperties());
    if (slice != null) {
        Replica oldReplica = slice.getReplica(coreNodeName);
        if (oldReplica != null) {
            if (oldReplica.containsKey(ZkStateReader.LEADER_PROP)) {
                replicaProps.put(ZkStateReader.LEADER_PROP, oldReplica.get(ZkStateReader.LEADER_PROP));
            }
            replicaProps.put(ZkStateReader.REPLICA_TYPE, oldReplica.getType().toString());
            // Move custom props over.
            for (Map.Entry<String, Object> ent : oldReplica.getProperties().entrySet()) {
                if (ent.getKey().startsWith(COLL_PROP_PREFIX)) {
                    replicaProps.put(ent.getKey(), ent.getValue());
                }
            }
        }
    }
    // we don't put these in the clusterstate
    replicaProps.remove(ZkStateReader.NUM_SHARDS_PROP);
    replicaProps.remove(ZkStateReader.CORE_NODE_NAME_PROP);
    replicaProps.remove(ZkStateReader.SHARD_ID_PROP);
    replicaProps.remove(ZkStateReader.COLLECTION_PROP);
    replicaProps.remove(Overseer.QUEUE_OPERATION);
    // remove any props with null values
    Set<Map.Entry<String, Object>> entrySet = replicaProps.entrySet();
    List<String> removeKeys = new ArrayList<>();
    for (Map.Entry<String, Object> entry : entrySet) {
        if (entry.getValue() == null) {
            removeKeys.add(entry.getKey());
        }
    }
    for (String removeKey : removeKeys) {
        replicaProps.remove(removeKey);
    }
    replicaProps.remove(ZkStateReader.CORE_NODE_NAME_PROP);
    // remove shard specific properties
    String shardRange = (String) replicaProps.remove(ZkStateReader.SHARD_RANGE_PROP);
    String shardState = (String) replicaProps.remove(ZkStateReader.SHARD_STATE_PROP);
    String shardParent = (String) replicaProps.remove(ZkStateReader.SHARD_PARENT_PROP);
    Replica replica = new Replica(coreNodeName, replicaProps);
    log.debug("Will update state for replica: {}", replica);
    Map<String, Object> sliceProps = null;
    Map<String, Replica> replicas;
    if (slice != null) {
        collection = checkAndCompleteShardSplit(prevState, collection, coreNodeName, sliceName, replica);
        // get the current slice again because it may have been updated due to checkAndCompleteShardSplit method
        slice = collection.getSlice(sliceName);
        sliceProps = slice.getProperties();
        replicas = slice.getReplicasCopy();
    } else {
        replicas = new HashMap<>(1);
        sliceProps = new HashMap<>();
        sliceProps.put(Slice.RANGE, shardRange);
        sliceProps.put(ZkStateReader.STATE_PROP, shardState);
        sliceProps.put(Slice.PARENT, shardParent);
    }
    replicas.put(replica.getName(), replica);
    slice = new Slice(sliceName, replicas, sliceProps);
    DocCollection newCollection = CollectionMutator.updateSlice(collectionName, collection, slice);
    log.debug("Collection is now: {}", newCollection);
    return new ZkWriteCommand(collectionName, newCollection);
}
Also used : ArrayList(java.util.ArrayList) Replica(org.apache.solr.common.cloud.Replica) LinkedHashMap(java.util.LinkedHashMap) Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 32 with Slice

use of org.apache.solr.common.cloud.Slice 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 33 with Slice

use of org.apache.solr.common.cloud.Slice 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 34 with Slice

use of org.apache.solr.common.cloud.Slice 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 35 with Slice

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

the class ClusterStateMutator method getAssignedCoreNodeName.

public static String getAssignedCoreNodeName(DocCollection collection, String forNodeName, String forCoreName) {
    Collection<Slice> slices = collection != null ? collection.getSlices() : null;
    if (slices != null) {
        for (Slice slice : slices) {
            for (Replica replica : slice.getReplicas()) {
                String nodeName = replica.getStr(ZkStateReader.NODE_NAME_PROP);
                String core = replica.getStr(ZkStateReader.CORE_NAME_PROP);
                if (nodeName.equals(forNodeName) && core.equals(forCoreName)) {
                    return replica.getName();
                }
            }
        }
    }
    return null;
}
Also used : Slice(org.apache.solr.common.cloud.Slice) Replica(org.apache.solr.common.cloud.Replica)

Aggregations

Slice (org.apache.solr.common.cloud.Slice)221 Replica (org.apache.solr.common.cloud.Replica)143 DocCollection (org.apache.solr.common.cloud.DocCollection)121 ClusterState (org.apache.solr.common.cloud.ClusterState)82 ArrayList (java.util.ArrayList)79 HashMap (java.util.HashMap)68 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)51 SolrException (org.apache.solr.common.SolrException)49 Map (java.util.Map)47 Test (org.junit.Test)37 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)28 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)25 HashSet (java.util.HashSet)24 SolrQuery (org.apache.solr.client.solrj.SolrQuery)24 IOException (java.io.IOException)23 NamedList (org.apache.solr.common.util.NamedList)23 List (java.util.List)22 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)22 DocRouter (org.apache.solr.common.cloud.DocRouter)20 ZkCoreNodeProps (org.apache.solr.common.cloud.ZkCoreNodeProps)20