public static Slice[] getSlices(String collectionName, ZkStateReader zkStateReader, boolean checkAlias) throws IOException { ClusterState clusterState = zkStateReader.getClusterState(); Map<String, DocCollection> collectionsMap = clusterState.getCollectionsMap(); //TODO we should probably split collection by comma to query more than one // which is something already supported in other parts of Solr // check for alias or collection List<String> collections = checkAlias ? zkStateReader.getAliases().resolveAliases(collectionName) // if not an alias, returns collectionName : Collections.singletonList(collectionName); // Lookup all actives slices for these collections List<Slice> slices = collections.stream() .map(collectionsMap::get) .filter(Objects::nonNull) .flatMap(docCol -> Arrays.stream(docCol.getActiveSlicesArr())) .collect(Collectors.toList()); if (!slices.isEmpty()) { return slices.toArray(new Slice[slices.size()]); } // Check collection case insensitive for(String collectionMapKey : collectionsMap.keySet()) { if(collectionMapKey.equalsIgnoreCase(collectionName)) { return collectionsMap.get(collectionMapKey).getActiveSlicesArr(); } } throw new IOException("Slices not found for " + collectionName); }
protected Slice hashToSlice(int hash, DocCollection collection) { final Slice[] slices = collection.getActiveSlicesArr(); for (Slice slice : slices) { Range range = slice.getRange(); if (range != null && range.includes(hash)) return slice; } throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No active slice servicing hash code " + Integer.toHexString(hash) + " in " + collection); }
@Override public Collection<Slice> getSearchSlicesSingle(String shardKey, SolrParams params, DocCollection collection) { if (shardKey == null) { // search across whole collection // TODO: this may need modification in the future when shard splitting could cause an overlap return collection.getActiveSlices(); } String id = shardKey; if (shardKey.indexOf(SEPARATOR) < 0) { // shardKey is a simple id, so don't do a range return Collections.singletonList(hashToSlice(Hash.murmurhash3_x86_32(id, 0, id.length(), 0), collection)); } Range completeRange = new KeyParser(id).getRange(); List<Slice> targetSlices = new ArrayList<>(1); for (Slice slice : collection.getActiveSlicesArr()) { Range range = slice.getRange(); if (range != null && range.overlaps(completeRange)) { targetSlices.add(slice); } } return targetSlices; }
DocCollection coll = getDocCollection(collection, null); Map<String,String> leaders = new HashMap<String,String>(); for (Slice slice : coll.getActiveSlicesArr()) { Replica leader = slice.getLeader(); if (leader != null) {
private Map<String,List<String>> buildUrlMap(DocCollection col) { Map<String, List<String>> urlMap = new HashMap<>(); Slice[] slices = col.getActiveSlicesArr(); for (Slice slice : slices) { String name = slice.getName();
public static int getLiveAndActiveReplicaCount(ZkStateReader zkStateReader, String collection) { Slice[] slices; slices = zkStateReader.getClusterState().getCollection(collection).getActiveSlicesArr(); int liveAndActive = 0; for (Slice slice : slices) { for (Replica replica : slice.getReplicas()) { boolean live = zkStateReader.getClusterState().liveNodesContain(replica.getNodeName()); boolean active = replica.getState() == Replica.State.ACTIVE; if (live && active) { liveAndActive++; } } } return liveAndActive; }