Search in sources :

Example 1 with Slice

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

Example 2 with Slice

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

the class SolrSnapshotsTool method getIndexFilesPathForSnapshot.

public Map<String, List<String>> getIndexFilesPathForSnapshot(String collectionName, String snapshotName, Optional<String> pathPrefix) throws SolrServerException, IOException {
    Map<String, List<String>> result = new HashMap<>();
    Collection<CollectionSnapshotMetaData> snaps = listCollectionSnapshots(collectionName);
    Optional<CollectionSnapshotMetaData> meta = Optional.empty();
    for (CollectionSnapshotMetaData m : snaps) {
        if (snapshotName.equals(m.getName())) {
            meta = Optional.of(m);
        }
    }
    if (!meta.isPresent()) {
        throw new IllegalArgumentException("The snapshot named " + snapshotName + " is not found for collection " + collectionName);
    }
    DocCollection collectionState = solrClient.getZkStateReader().getClusterState().getCollection(collectionName);
    for (Slice s : collectionState.getSlices()) {
        List<CoreSnapshotMetaData> replicaSnaps = meta.get().getReplicaSnapshotsForShard(s.getName());
        // Prepare a list of *existing* replicas (since one or more replicas could have been deleted after the snapshot creation).
        List<CoreSnapshotMetaData> availableReplicas = new ArrayList<>();
        for (CoreSnapshotMetaData m : replicaSnaps) {
            if (isReplicaAvailable(s, m.getCoreName())) {
                availableReplicas.add(m);
            }
        }
        if (availableReplicas.isEmpty()) {
            throw new IllegalArgumentException("The snapshot named " + snapshotName + " not found for shard " + s.getName() + " of collection " + collectionName);
        }
        // Prefer a leader replica (at the time when the snapshot was created).
        CoreSnapshotMetaData coreSnap = availableReplicas.get(0);
        for (CoreSnapshotMetaData m : availableReplicas) {
            if (m.isLeader()) {
                coreSnap = m;
            }
        }
        String indexDirPath = coreSnap.getIndexDirPath();
        if (pathPrefix.isPresent()) {
            // If the path prefix is specified, rebuild the path to the index directory.
            Path t = new Path(coreSnap.getIndexDirPath());
            indexDirPath = (new Path(pathPrefix.get(), t.toUri().getPath())).toString();
        }
        List<String> paths = new ArrayList<>();
        for (String fileName : coreSnap.getFiles()) {
            Path p = new Path(indexDirPath, fileName);
            paths.add(p.toString());
        }
        result.put(s.getName(), paths);
    }
    return result;
}
Also used : Path(org.apache.hadoop.fs.Path) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Slice(org.apache.solr.common.cloud.Slice) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) DocCollection(org.apache.solr.common.cloud.DocCollection) CoreSnapshotMetaData(org.apache.solr.core.snapshots.CollectionSnapshotMetaData.CoreSnapshotMetaData)

Example 3 with Slice

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

the class CoreSorter method getReplicas.

/**Return all replicas for a given collection+slice combo
   */
Collection<Replica> getReplicas(ClusterState cs, String coll, String slice) {
    DocCollection c = cs.getCollectionOrNull(coll);
    if (c == null)
        return emptyList();
    Slice s = c.getSlice(slice);
    if (s == null)
        return emptyList();
    return s.getReplicas();
}
Also used : Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection)

Example 4 with Slice

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

the class SliceMutator method addRoutingRule.

public ZkWriteCommand addRoutingRule(final ClusterState clusterState, ZkNodeProps message) {
    String collectionName = message.getStr(ZkStateReader.COLLECTION_PROP);
    if (!checkCollectionKeyExistence(message))
        return ZkStateWriter.NO_OP;
    String shard = message.getStr(ZkStateReader.SHARD_ID_PROP);
    String routeKey = message.getStr("routeKey");
    String range = message.getStr("range");
    String targetCollection = message.getStr("targetCollection");
    String expireAt = message.getStr("expireAt");
    DocCollection collection = clusterState.getCollection(collectionName);
    Slice slice = collection.getSlice(shard);
    if (slice == null) {
        throw new RuntimeException("Overseer.addRoutingRule unknown collection: " + collectionName + " slice:" + shard);
    }
    Map<String, RoutingRule> routingRules = slice.getRoutingRules();
    if (routingRules == null)
        routingRules = new HashMap<>();
    RoutingRule r = routingRules.get(routeKey);
    if (r == null) {
        Map<String, Object> map = new HashMap<>();
        map.put("routeRanges", range);
        map.put("targetCollection", targetCollection);
        map.put("expireAt", expireAt);
        RoutingRule rule = new RoutingRule(routeKey, map);
        routingRules.put(routeKey, rule);
    } else {
        // add this range
        Map<String, Object> map = r.shallowCopy();
        map.put("routeRanges", map.get("routeRanges") + "," + range);
        map.put("expireAt", expireAt);
        routingRules.put(routeKey, new RoutingRule(routeKey, map));
    }
    Map<String, Object> props = slice.shallowCopy();
    props.put("routingRules", routingRules);
    Slice newSlice = new Slice(slice.getName(), slice.getReplicasCopy(), props);
    return new ZkWriteCommand(collectionName, CollectionMutator.updateSlice(collectionName, collection, newSlice));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection) RoutingRule(org.apache.solr.common.cloud.RoutingRule)

Example 5 with Slice

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

the class SliceMutator method removeRoutingRule.

public ZkWriteCommand removeRoutingRule(final ClusterState clusterState, ZkNodeProps message) {
    String collectionName = message.getStr(ZkStateReader.COLLECTION_PROP);
    if (!checkCollectionKeyExistence(message))
        return ZkStateWriter.NO_OP;
    String shard = message.getStr(ZkStateReader.SHARD_ID_PROP);
    String routeKeyStr = message.getStr("routeKey");
    log.info("Overseer.removeRoutingRule invoked for collection: " + collectionName + " shard: " + shard + " routeKey: " + routeKeyStr);
    DocCollection collection = clusterState.getCollection(collectionName);
    Slice slice = collection.getSlice(shard);
    if (slice == null) {
        log.warn("Unknown collection: " + collectionName + " shard: " + shard);
        return ZkStateWriter.NO_OP;
    }
    Map<String, RoutingRule> routingRules = slice.getRoutingRules();
    if (routingRules != null) {
        // no rules left
        routingRules.remove(routeKeyStr);
        Map<String, Object> props = slice.shallowCopy();
        props.put("routingRules", routingRules);
        Slice newSlice = new Slice(slice.getName(), slice.getReplicasCopy(), props);
        return new ZkWriteCommand(collectionName, CollectionMutator.updateSlice(collectionName, collection, newSlice));
    }
    return ZkStateWriter.NO_OP;
}
Also used : Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection) RoutingRule(org.apache.solr.common.cloud.RoutingRule)

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