Search in sources :

Example 6 with Slice

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

the class SliceMutator method updateReplica.

public static DocCollection updateReplica(DocCollection collection, final Slice slice, String coreNodeName, final Replica replica) {
    Map<String, Replica> replicasCopy = slice.getReplicasCopy();
    if (replica == null) {
        replicasCopy.remove(coreNodeName);
    } else {
        replicasCopy.put(replica.getName(), replica);
    }
    Slice newSlice = new Slice(slice.getName(), replicasCopy, slice.getProperties());
    log.debug("Old Slice: {}", slice);
    log.debug("New Slice: {}", newSlice);
    return CollectionMutator.updateSlice(collection.getName(), collection, newSlice);
}
Also used : Slice(org.apache.solr.common.cloud.Slice) Replica(org.apache.solr.common.cloud.Replica)

Example 7 with Slice

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

the class SliceMutator method addReplica.

public ZkWriteCommand addReplica(ClusterState clusterState, ZkNodeProps message) {
    log.info("createReplica() {} ", message);
    String coll = message.getStr(ZkStateReader.COLLECTION_PROP);
    if (!checkCollectionKeyExistence(message))
        return ZkStateWriter.NO_OP;
    String slice = message.getStr(ZkStateReader.SHARD_ID_PROP);
    DocCollection collection = clusterState.getCollection(coll);
    Slice sl = collection.getSlice(slice);
    if (sl == null) {
        log.error("Invalid Collection/Slice {}/{} ", coll, slice);
        return ZkStateWriter.NO_OP;
    }
    String coreNodeName = Assign.assignNode(collection);
    Replica replica = new Replica(coreNodeName, makeMap(ZkStateReader.CORE_NAME_PROP, message.getStr(ZkStateReader.CORE_NAME_PROP), ZkStateReader.BASE_URL_PROP, message.getStr(ZkStateReader.BASE_URL_PROP), ZkStateReader.STATE_PROP, message.getStr(ZkStateReader.STATE_PROP), ZkStateReader.NODE_NAME_PROP, message.getStr(ZkStateReader.NODE_NAME_PROP), ZkStateReader.REPLICA_TYPE, message.get(ZkStateReader.REPLICA_TYPE)));
    return new ZkWriteCommand(coll, updateReplica(collection, sl, replica.getName(), replica));
}
Also used : Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection) Replica(org.apache.solr.common.cloud.Replica)

Example 8 with Slice

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

the class SliceMutator method updateShardState.

public ZkWriteCommand updateShardState(ClusterState clusterState, ZkNodeProps message) {
    String collectionName = message.getStr(ZkStateReader.COLLECTION_PROP);
    if (!checkCollectionKeyExistence(message))
        return ZkStateWriter.NO_OP;
    log.info("Update shard state invoked for collection: " + collectionName + " with message: " + message);
    DocCollection collection = clusterState.getCollection(collectionName);
    Map<String, Slice> slicesCopy = new LinkedHashMap<>(collection.getSlicesMap());
    for (String key : message.keySet()) {
        if (ZkStateReader.COLLECTION_PROP.equals(key))
            continue;
        if (Overseer.QUEUE_OPERATION.equals(key))
            continue;
        Slice slice = collection.getSlice(key);
        if (slice == null) {
            throw new RuntimeException("Overseer.updateShardState unknown collection: " + collectionName + " slice: " + key);
        }
        log.info("Update shard state " + key + " to " + message.getStr(key));
        Map<String, Object> props = slice.shallowCopy();
        if (Slice.State.getState(message.getStr(key)) == Slice.State.ACTIVE) {
            props.remove(Slice.PARENT);
            props.remove("shard_parent_node");
            props.remove("shard_parent_zk_session");
        }
        props.put(ZkStateReader.STATE_PROP, message.getStr(key));
        Slice newSlice = new Slice(slice.getName(), slice.getReplicasCopy(), props);
        slicesCopy.put(slice.getName(), newSlice);
    }
    return new ZkWriteCommand(collectionName, collection.copyWithSlices(slicesCopy));
}
Also used : Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with Slice

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

the class CollectionsHandler method forceLeaderElection.

private static void forceLeaderElection(SolrQueryRequest req, CollectionsHandler handler) {
    ClusterState clusterState = handler.coreContainer.getZkController().getClusterState();
    String collectionName = req.getParams().required().get(COLLECTION_PROP);
    String sliceId = req.getParams().required().get(SHARD_ID_PROP);
    log.info("Force leader invoked, state: {}", clusterState);
    DocCollection collection = clusterState.getCollection(collectionName);
    Slice slice = collection.getSlice(sliceId);
    if (slice == null) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "No shard with name " + sliceId + " exists for collection " + collectionName);
    }
    try {
        // if an active replica is the leader, then all is fine already
        Replica leader = slice.getLeader();
        if (leader != null && leader.getState() == State.ACTIVE) {
            throw new SolrException(ErrorCode.SERVER_ERROR, "The shard already has an active leader. Force leader is not applicable. State: " + slice);
        }
        // Clear out any LIR state
        String lirPath = handler.coreContainer.getZkController().getLeaderInitiatedRecoveryZnodePath(collectionName, sliceId);
        if (handler.coreContainer.getZkController().getZkClient().exists(lirPath, true)) {
            StringBuilder sb = new StringBuilder();
            handler.coreContainer.getZkController().getZkClient().printLayout(lirPath, 4, sb);
            log.info("Cleaning out LIR data, which was: {}", sb);
            handler.coreContainer.getZkController().getZkClient().clean(lirPath);
        }
        // state to active.
        for (Replica rep : slice.getReplicas()) {
            if (clusterState.getLiveNodes().contains(rep.getNodeName())) {
                ShardHandler shardHandler = handler.coreContainer.getShardHandlerFactory().getShardHandler();
                ModifiableSolrParams params = new ModifiableSolrParams();
                params.set(CoreAdminParams.ACTION, CoreAdminAction.FORCEPREPAREFORLEADERSHIP.toString());
                params.set(CoreAdminParams.CORE, rep.getStr("core"));
                String nodeName = rep.getNodeName();
                OverseerCollectionMessageHandler.sendShardRequest(nodeName, params, shardHandler, null, null, CommonParams.CORES_HANDLER_PATH, // synchronous request
                handler.coreContainer.getZkController().getZkStateReader());
            }
        }
        // Wait till we have an active leader
        boolean success = false;
        for (int i = 0; i < 9; i++) {
            Thread.sleep(5000);
            clusterState = handler.coreContainer.getZkController().getClusterState();
            collection = clusterState.getCollection(collectionName);
            slice = collection.getSlice(sliceId);
            if (slice.getLeader() != null && slice.getLeader().getState() == State.ACTIVE) {
                success = true;
                break;
            }
            log.warn("Force leader attempt {}. Waiting 5 secs for an active leader. State of the slice: {}", (i + 1), slice);
        }
        if (success) {
            log.info("Successfully issued FORCELEADER command for collection: {}, shard: {}", collectionName, sliceId);
        } else {
            log.info("Couldn't successfully force leader, collection: {}, shard: {}. Cluster state: {}", collectionName, sliceId, clusterState);
        }
    } catch (SolrException e) {
        throw e;
    } catch (Exception e) {
        throw new SolrException(ErrorCode.SERVER_ERROR, "Error executing FORCELEADER operation for collection: " + collectionName + " shard: " + sliceId, e);
    }
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) Slice(org.apache.solr.common.cloud.Slice) StrUtils.formatString(org.apache.solr.common.util.StrUtils.formatString) DocCollection(org.apache.solr.common.cloud.DocCollection) ShardHandler(org.apache.solr.handler.component.ShardHandler) Replica(org.apache.solr.common.cloud.Replica) SolrException(org.apache.solr.common.SolrException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException) KeeperException(org.apache.zookeeper.KeeperException)

Example 10 with Slice

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

the class AbstractCloudBackupRestoreTestCase method getShardToDocCountMap.

private Map<String, Integer> getShardToDocCountMap(CloudSolrClient client, DocCollection docCollection) throws SolrServerException, IOException {
    Map<String, Integer> shardToDocCount = new TreeMap<>();
    for (Slice slice : docCollection.getActiveSlices()) {
        String shardName = slice.getName();
        try (HttpSolrClient leaderClient = new HttpSolrClient.Builder(slice.getLeader().getCoreUrl()).withHttpClient(client.getHttpClient()).build()) {
            long docsInShard = leaderClient.query(new SolrQuery("*:*").setParam("distrib", "false")).getResults().getNumFound();
            shardToDocCount.put(shardName, (int) docsInShard);
        }
    }
    return shardToDocCount;
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) Slice(org.apache.solr.common.cloud.Slice) TreeMap(java.util.TreeMap) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

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