Search in sources :

Example 51 with ClusterState

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

the class RebalanceLeaders method execute.

void execute() throws KeeperException, InterruptedException {
    req.getParams().required().check(COLLECTION_PROP);
    String collectionName = req.getParams().get(COLLECTION_PROP);
    if (StringUtils.isBlank(collectionName)) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, String.format(Locale.ROOT, "The " + COLLECTION_PROP + " is required for the Rebalance Leaders command."));
    }
    coreContainer.getZkController().getZkStateReader().forceUpdateCollection(collectionName);
    ClusterState clusterState = coreContainer.getZkController().getClusterState();
    DocCollection dc = clusterState.getCollection(collectionName);
    if (dc == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Collection '" + collectionName + "' does not exist, no action taken.");
    }
    Map<String, String> currentRequests = new HashMap<>();
    int max = req.getParams().getInt(MAX_AT_ONCE_PROP, Integer.MAX_VALUE);
    if (max <= 0)
        max = Integer.MAX_VALUE;
    int maxWaitSecs = req.getParams().getInt(MAX_WAIT_SECONDS_PROP, 60);
    NamedList<Object> results = new NamedList<>();
    boolean keepGoing = true;
    for (Slice slice : dc.getSlices()) {
        ensurePreferredIsLeader(results, slice, currentRequests);
        if (currentRequests.size() == max) {
            log.info("Queued " + max + " leader reassignments, waiting for some to complete.");
            keepGoing = waitForLeaderChange(currentRequests, maxWaitSecs, false, results);
            if (keepGoing == false) {
                // If we've waited longer than specified, don't continue to wait!
                break;
            }
        }
    }
    if (keepGoing == true) {
        keepGoing = waitForLeaderChange(currentRequests, maxWaitSecs, true, results);
    }
    if (keepGoing == true) {
        log.info("All leader reassignments completed.");
    } else {
        log.warn("Exceeded specified timeout of ." + maxWaitSecs + "' all leaders may not have been reassigned");
    }
    rsp.getValues().addAll(results);
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) HashMap(java.util.HashMap) NamedList(org.apache.solr.common.util.NamedList) Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection) SolrException(org.apache.solr.common.SolrException)

Example 52 with ClusterState

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

the class ManagedIndexSchema method getActiveReplicaCoreUrls.

protected static List<String> getActiveReplicaCoreUrls(ZkController zkController, String collection, String localCoreNodeName) {
    List<String> activeReplicaCoreUrls = new ArrayList<>();
    ZkStateReader zkStateReader = zkController.getZkStateReader();
    ClusterState clusterState = zkStateReader.getClusterState();
    Set<String> liveNodes = clusterState.getLiveNodes();
    Collection<Slice> activeSlices = clusterState.getActiveSlices(collection);
    if (activeSlices != null && activeSlices.size() > 0) {
        for (Slice next : activeSlices) {
            Map<String, Replica> replicasMap = next.getReplicasMap();
            if (replicasMap != null) {
                for (Map.Entry<String, Replica> entry : replicasMap.entrySet()) {
                    Replica replica = entry.getValue();
                    if (!localCoreNodeName.equals(replica.getName()) && replica.getState() == Replica.State.ACTIVE && liveNodes.contains(replica.getNodeName())) {
                        ZkCoreNodeProps replicaCoreProps = new ZkCoreNodeProps(replica);
                        activeReplicaCoreUrls.add(replicaCoreProps.getCoreUrl());
                    }
                }
            }
        }
    }
    return activeReplicaCoreUrls;
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) ZkCoreNodeProps(org.apache.solr.common.cloud.ZkCoreNodeProps) ArrayList(java.util.ArrayList) Replica(org.apache.solr.common.cloud.Replica) ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) Slice(org.apache.solr.common.cloud.Slice) Map(java.util.Map) HashMap(java.util.HashMap)

Example 53 with ClusterState

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

the class HttpSolrCall method getCoreByCollection.

protected SolrCore getCoreByCollection(String collectionName, boolean isPreferLeader) {
    ZkStateReader zkStateReader = cores.getZkController().getZkStateReader();
    ClusterState clusterState = zkStateReader.getClusterState();
    DocCollection collection = clusterState.getCollectionOrNull(collectionName);
    if (collection == null) {
        return null;
    }
    Set<String> liveNodes = clusterState.getLiveNodes();
    if (isPreferLeader) {
        List<Replica> leaderReplicas = collection.getLeaderReplicas(cores.getZkController().getNodeName());
        SolrCore core = randomlyGetSolrCore(liveNodes, leaderReplicas);
        if (core != null)
            return core;
    }
    List<Replica> replicas = collection.getReplicas(cores.getZkController().getNodeName());
    return randomlyGetSolrCore(liveNodes, replicas);
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) ClusterState(org.apache.solr.common.cloud.ClusterState) SolrCore(org.apache.solr.core.SolrCore) DocCollection(org.apache.solr.common.cloud.DocCollection) Replica(org.apache.solr.common.cloud.Replica)

Example 54 with ClusterState

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

the class HttpSolrCall method getRemotCoreUrl.

private String getRemotCoreUrl(String collectionName, String origCorename) {
    ClusterState clusterState = cores.getZkController().getClusterState();
    Collection<Slice> slices = clusterState.getActiveSlices(collectionName);
    boolean byCoreName = false;
    if (slices == null) {
        slices = new ArrayList<>();
        // look by core name
        byCoreName = true;
        getSlicesForCollections(clusterState, slices, true);
        if (slices.isEmpty()) {
            getSlicesForCollections(clusterState, slices, false);
        }
    }
    if (slices.isEmpty()) {
        return null;
    }
    if (collectionsList == null)
        collectionsList = new ArrayList<>();
    collectionsList.add(collectionName);
    String coreUrl = getCoreUrl(collectionName, origCorename, clusterState, slices, byCoreName, true);
    if (coreUrl == null) {
        coreUrl = getCoreUrl(collectionName, origCorename, clusterState, slices, byCoreName, false);
    }
    return coreUrl;
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) Slice(org.apache.solr.common.cloud.Slice) ArrayList(java.util.ArrayList)

Example 55 with ClusterState

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

the class SolrCore method bufferUpdatesIfConstructing.

/** Set UpdateLog to buffer updates if the slice is in construction. */
private void bufferUpdatesIfConstructing(CoreDescriptor coreDescriptor) {
    if (coreContainer != null && coreContainer.isZooKeeperAware()) {
        if (reqHandlers.get("/get") == null) {
            log.warn("WARNING: RealTimeGetHandler is not registered at /get. " + "SolrCloud will always use full index replication instead of the more efficient PeerSync method.");
        }
        // ZK pre-register would have already happened so we read slice properties now
        final ClusterState clusterState = coreContainer.getZkController().getClusterState();
        final DocCollection collection = clusterState.getCollection(coreDescriptor.getCloudDescriptor().getCollectionName());
        final Slice slice = collection.getSlice(coreDescriptor.getCloudDescriptor().getShardId());
        if (slice.getState() == Slice.State.CONSTRUCTION) {
            // set update log to buffer before publishing the core
            getUpdateHandler().getUpdateLog().bufferUpdates();
        }
    }
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection)

Aggregations

ClusterState (org.apache.solr.common.cloud.ClusterState)118 Slice (org.apache.solr.common.cloud.Slice)77 Replica (org.apache.solr.common.cloud.Replica)64 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)52 DocCollection (org.apache.solr.common.cloud.DocCollection)48 HashMap (java.util.HashMap)41 ArrayList (java.util.ArrayList)36 Map (java.util.Map)24 IOException (java.io.IOException)19 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 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 NamedList (org.apache.solr.common.util.NamedList)12