Search in sources :

Example 31 with Replica

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

the class ReplicaMutator method addReplicaProperty.

public ZkWriteCommand addReplicaProperty(ClusterState clusterState, ZkNodeProps message) {
    if (checkKeyExistence(message, ZkStateReader.COLLECTION_PROP) == false || checkKeyExistence(message, ZkStateReader.SHARD_ID_PROP) == false || checkKeyExistence(message, ZkStateReader.REPLICA_PROP) == false || checkKeyExistence(message, ZkStateReader.PROPERTY_PROP) == false || checkKeyExistence(message, ZkStateReader.PROPERTY_VALUE_PROP) == false) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Overseer ADDREPLICAPROP requires " + ZkStateReader.COLLECTION_PROP + " and " + ZkStateReader.SHARD_ID_PROP + " and " + ZkStateReader.REPLICA_PROP + " and " + ZkStateReader.PROPERTY_PROP + " and " + ZkStateReader.PROPERTY_VALUE_PROP + " no action taken.");
    }
    String collectionName = message.getStr(ZkStateReader.COLLECTION_PROP);
    String sliceName = message.getStr(ZkStateReader.SHARD_ID_PROP);
    String replicaName = message.getStr(ZkStateReader.REPLICA_PROP);
    String property = message.getStr(ZkStateReader.PROPERTY_PROP).toLowerCase(Locale.ROOT);
    if (StringUtils.startsWith(property, COLL_PROP_PREFIX) == false) {
        property = OverseerCollectionMessageHandler.COLL_PROP_PREFIX + property;
    }
    property = property.toLowerCase(Locale.ROOT);
    String propVal = message.getStr(ZkStateReader.PROPERTY_VALUE_PROP);
    String shardUnique = message.getStr(OverseerCollectionMessageHandler.SHARD_UNIQUE);
    boolean isUnique = false;
    if (SliceMutator.SLICE_UNIQUE_BOOLEAN_PROPERTIES.contains(property)) {
        if (StringUtils.isNotBlank(shardUnique) && Boolean.parseBoolean(shardUnique) == false) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Overseer ADDREPLICAPROP for " + property + " cannot have " + OverseerCollectionMessageHandler.SHARD_UNIQUE + " set to anything other than" + "'true'. No action taken");
        }
        isUnique = true;
    } else {
        isUnique = Boolean.parseBoolean(shardUnique);
    }
    DocCollection collection = clusterState.getCollection(collectionName);
    Replica replica = collection.getReplica(replicaName);
    if (replica == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Could not find collection/slice/replica " + collectionName + "/" + sliceName + "/" + replicaName + " no action taken.");
    }
    log.info("Setting property {} with value {} for collection {}", property, propVal, collectionName);
    log.debug("Full message: {}", message);
    // already the value we're going to set
    if (StringUtils.equalsIgnoreCase(replica.getStr(property), propVal))
        return ZkStateWriter.NO_OP;
    // OK, there's no way we won't change the cluster state now
    Map<String, Replica> replicas = collection.getSlice(sliceName).getReplicasCopy();
    if (isUnique == false) {
        replicas.get(replicaName).getProperties().put(property, propVal);
    } else {
        // Set prop for this replica, but remove it for all others.
        for (Replica rep : replicas.values()) {
            if (rep.getName().equalsIgnoreCase(replicaName)) {
                rep.getProperties().put(property, propVal);
            } else {
                rep.getProperties().remove(property);
            }
        }
    }
    Slice newSlice = new Slice(sliceName, replicas, collection.getSlice(sliceName).shallowCopy());
    DocCollection newCollection = CollectionMutator.updateSlice(collectionName, collection, newSlice);
    return new ZkWriteCommand(collectionName, newCollection);
}
Also used : Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection) Replica(org.apache.solr.common.cloud.Replica) SolrException(org.apache.solr.common.SolrException)

Example 32 with Replica

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

the class ReplicaMutator method deleteReplicaProperty.

public ZkWriteCommand deleteReplicaProperty(ClusterState clusterState, ZkNodeProps message) {
    if (checkKeyExistence(message, ZkStateReader.COLLECTION_PROP) == false || checkKeyExistence(message, ZkStateReader.SHARD_ID_PROP) == false || checkKeyExistence(message, ZkStateReader.REPLICA_PROP) == false || checkKeyExistence(message, ZkStateReader.PROPERTY_PROP) == false) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Overseer DELETEREPLICAPROP requires " + ZkStateReader.COLLECTION_PROP + " and " + ZkStateReader.SHARD_ID_PROP + " and " + ZkStateReader.REPLICA_PROP + " and " + ZkStateReader.PROPERTY_PROP + " no action taken.");
    }
    String collectionName = message.getStr(ZkStateReader.COLLECTION_PROP);
    String sliceName = message.getStr(ZkStateReader.SHARD_ID_PROP);
    String replicaName = message.getStr(ZkStateReader.REPLICA_PROP);
    String property = message.getStr(ZkStateReader.PROPERTY_PROP).toLowerCase(Locale.ROOT);
    if (StringUtils.startsWith(property, COLL_PROP_PREFIX) == false) {
        property = OverseerCollectionMessageHandler.COLL_PROP_PREFIX + property;
    }
    DocCollection collection = clusterState.getCollection(collectionName);
    Replica replica = collection.getReplica(replicaName);
    if (replica == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Could not find collection/slice/replica " + collectionName + "/" + sliceName + "/" + replicaName + " no action taken.");
    }
    log.info("Deleting property {} for collection: {} slice: {} replica: {}", property, collectionName, sliceName, replicaName);
    log.debug("Full message: {}", message);
    String curProp = replica.getStr(property);
    // not there anyway, nothing to do.
    if (curProp == null)
        return ZkStateWriter.NO_OP;
    Slice slice = collection.getSlice(sliceName);
    DocCollection newCollection = SliceMutator.updateReplica(collection, slice, replicaName, unsetProperty(replica, property));
    return new ZkWriteCommand(collectionName, newCollection);
}
Also used : Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection) Replica(org.apache.solr.common.cloud.Replica) SolrException(org.apache.solr.common.SolrException)

Example 33 with Replica

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

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

use of org.apache.solr.common.cloud.Replica 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)

Aggregations

Replica (org.apache.solr.common.cloud.Replica)232 Slice (org.apache.solr.common.cloud.Slice)140 DocCollection (org.apache.solr.common.cloud.DocCollection)86 ArrayList (java.util.ArrayList)81 ClusterState (org.apache.solr.common.cloud.ClusterState)67 HashMap (java.util.HashMap)60 SolrException (org.apache.solr.common.SolrException)53 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)50 Test (org.junit.Test)50 Map (java.util.Map)45 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)37 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)35 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)29 NamedList (org.apache.solr.common.util.NamedList)28 SolrQuery (org.apache.solr.client.solrj.SolrQuery)26 IOException (java.io.IOException)25 SolrInputDocument (org.apache.solr.common.SolrInputDocument)25 ZkCoreNodeProps (org.apache.solr.common.cloud.ZkCoreNodeProps)25 HashSet (java.util.HashSet)24 List (java.util.List)20