Map<String, Replica> shards = entry.getValue().getReplicasMap(); for (Map.Entry<String, Replica> shard : shards.entrySet()) { String state = shard.getValue().getStr(ZkStateReader.STATE_PROP);
/** Constructs a slices map from a collection of slices and handles disambiguation if multiple collections are being queried simultaneously */ public static void addSlices(Map<String,Slice> target, String collectionName, Collection<Slice> slices, boolean multiCollection) { for (Slice slice : slices) { String key = slice.getName(); if (multiCollection) key = collectionName + "_" + key; target.put(key, slice); } } }
public List<Replica> getReplicas(EnumSet<Replica.Type> s) { List<Replica> replicas = new ArrayList<>(); for (Slice slice : this) { replicas.addAll(slice.getReplicas(s)); } return replicas; }
for (Replica replica : slice.getReplicas()) { int port = new URI(((HttpSolrClient) client).getBaseURL()) .getPort(); Set<Entry<String,Replica>> entries = slice.getReplicasMap().entrySet(); for (Entry<String,Replica> entry : entries) { Replica replica = entry.getValue(); if (replica.getStr(ZkStateReader.BASE_URL_PROP).contains(":" + port)) { List<CloudJettyRunner> list = shardToJetty.get(slice.getName()); if (list == null) { list = new ArrayList<>(); shardToJetty.put(slice.getName(), list); boolean isLeader = slice.getLeader() == replica; CloudJettyRunner cjr = new CloudJettyRunner(); cjr.jetty = jetty; list.add(cjr); if (isLeader) { shardToLeaderJetty.put(slice.getName(), cjr); List<CloudJettyRunner> jetties = shardToJetty.get(slice.getName()); if (!allowOverSharding) { assertNotNull("Test setup problem: We found no jetties for shard: " + slice.getName() + " just:" + shardToJetty.keySet(), jetties); assertEquals("slice:" + slice.getName(), slice.getReplicas().size(), jetties.size());
public String getShardId(String collectionName, String nodeName, String coreName) { Collection<CollectionRef> states = collectionStates.values(); if (collectionName != null) { CollectionRef c = collectionStates.get(collectionName); if (c != null) states = Collections.singletonList( c ); } for (CollectionRef ref : states) { DocCollection coll = ref.get(); if(coll == null) continue;// this collection go tremoved in between, skip for (Slice slice : coll.getSlices()) { for (Replica replica : slice.getReplicas()) { // TODO: for really large clusters, we could 'index' on this String rnodeName = replica.getStr(ZkStateReader.NODE_NAME_PROP); String rcore = replica.getStr(ZkStateReader.CORE_NAME_PROP); if (nodeName.equals(rnodeName) && coreName.equals(rcore)) { return slice.getName(); } } } } return null; }
for (Slice slice : slices) { if (slice.getState() == Slice.State.ACTIVE) { Collection<Replica> replicas = slice.getReplicas(); for (Replica replica : replicas) {
protected static void assertAllActive(String collection, ZkStateReader zkStateReader) throws KeeperException, InterruptedException { zkStateReader.forceUpdateCollection(collection); ClusterState clusterState = zkStateReader.getClusterState(); final DocCollection docCollection = clusterState.getCollectionOrNull(collection); if (docCollection == null || docCollection.getSlices() == null) { throw new IllegalArgumentException("Cannot find collection:" + collection); } Map<String,Slice> slices = docCollection.getSlicesMap(); for (Map.Entry<String,Slice> entry : slices.entrySet()) { Slice slice = entry.getValue(); if (slice.getState() != Slice.State.ACTIVE) { fail("Not all shards are ACTIVE - found a shard " + slice.getName() + " that is: " + slice.getState()); } Map<String,Replica> shards = slice.getReplicasMap(); for (Map.Entry<String,Replica> shard : shards.entrySet()) { Replica replica = shard.getValue(); if (replica.getState() != Replica.State.ACTIVE) { fail("Not all replicas are ACTIVE - found a replica " + replica.getName() + " that is: " + replica.getState()); } } } }
while (sliceIterator.hasNext()) { Slice slice = sliceIterator.next(); String name = slice.getName(); List<String> urls = new ArrayList<>(); Replica leader = slice.getLeader(); if (leader == null) { String url = zkProps.getCoreUrl(); urls.add(url); Collection<Replica> replicas = slice.getReplicas(); Iterator<Replica> replicaIterator = replicas.iterator(); while (replicaIterator.hasNext()) {
public List<List<String>> extractShardUrls(String zkHost, String collection) { DocCollection docCollection = extractDocCollection(zkHost, collection); List<Slice> slices = getSortedSlices(docCollection.getSlices()); List<List<String>> solrUrls = new ArrayList<List<String>>(slices.size()); for (Slice slice : slices) { if (slice.getLeader() == null) { throw new IllegalArgumentException("Cannot find SolrCloud slice leader. " + "It looks like not all of your shards are registered in ZooKeeper yet"); } Collection<Replica> replicas = slice.getReplicas(); List<String> urls = new ArrayList<String>(replicas.size()); for (Replica replica : replicas) { ZkCoreNodeProps props = new ZkCoreNodeProps(replica); urls.add(props.getCoreUrl()); } solrUrls.add(urls); } return solrUrls; }
for (Map.Entry<String,Slice> entry : slices.entrySet()) { Slice slice = entry.getValue(); if (slice.getState() == Slice.State.CONSTRUCTION) { // similar to replica recovering; pretend its the same thing if (verbose) System.out.println("Found a slice in construction state; will wait."); sawLiveRecovering = true; Map<String,Replica> shards = slice.getReplicasMap(); for (Map.Entry<String,Replica> shard : shards.entrySet()) { if (verbose) System.out.println("replica:" + shard.getValue().getName() + " rstate:"
Map<String,String> leaders = new HashMap<String,String>(); for (Slice slice : coll.getActiveSlicesArr()) { Replica leader = slice.getLeader(); if (leader != null) { ZkCoreNodeProps zkProps = new ZkCoreNodeProps(leader); String leaderUrl = zkProps.getBaseUrl() + "/" + zkProps.getCoreName(); leaders.put(leaderUrl, slice.getName()); String altLeaderUrl = zkProps.getBaseUrl() + "/" + collection; leaders.put(altLeaderUrl, slice.getName());
public Replica getLeader(String sliceName) { Slice slice = getSlice(sliceName); if (slice == null) return null; return slice.getLeader(); }
/** * Get a (reproducibly) random replica from a {@link Slice} */ protected static Replica getRandomReplica(Slice slice) { List<Replica> replicas = new ArrayList<>(slice.getReplicas()); if (replicas.size() == 0) fail("Couldn't get random replica from shard as it has no replicas!\n" + slice.toString()); Collections.shuffle(replicas, random()); return replicas.get(0); }
private static void addMissingReplicas(SolrCloudManager cloudManager, Suggestion.Ctx ctx) throws IOException { cloudManager.getClusterStateProvider().getClusterState().forEachCollection(coll -> coll.forEach(slice -> { ReplicaCount replicaCount = new ReplicaCount(); slice.forEach(replica -> { if (replica.getState() == Replica.State.ACTIVE || replica.getState() == Replica.State.RECOVERING) { replicaCount.increment(replica.getType()); } }); addMissingReplicas(replicaCount, coll, slice.getName(), Replica.Type.NRT, ctx); addMissingReplicas(replicaCount, coll, slice.getName(), Replica.Type.PULL, ctx); addMissingReplicas(replicaCount, coll, slice.getName(), Replica.Type.TLOG, ctx); } )); }
/** * @param name The name of the collection * @param slices The logical shards of the collection. This is used directly and a copy is not made. * @param props The properties of the slice. This is used directly and a copy is not made. */ public DocCollection(String name, Map<String, Slice> slices, Map<String, Object> props, DocRouter router, int zkVersion, String znode) { super(props==null ? props = new HashMap<String,Object>() : props); this.znodeVersion = zkVersion; this.name = name; this.slices = slices; this.activeSlices = new HashMap<>(); this.replicationFactor = (Integer) verifyProp(props, REPLICATION_FACTOR); this.maxShardsPerNode = (Integer) verifyProp(props, MAX_SHARDS_PER_NODE); Boolean autoAddReplicas = (Boolean) verifyProp(props, AUTO_ADD_REPLICAS); this.autoAddReplicas = autoAddReplicas == null ? false : autoAddReplicas; verifyProp(props, RULE); verifyProp(props, SNITCH); Iterator<Map.Entry<String, Slice>> iter = slices.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<String, Slice> slice = iter.next(); if (slice.getValue().getState() == Slice.State.ACTIVE) this.activeSlices.put(slice.getKey(), slice.getValue()); } this.router = router; this.znode = znode == null? ZkStateReader.CLUSTER_STATE : znode; assert name != null && slices != null; }
this.replicas = replicas != null ? replicas : makeReplicas((Map<String,Object>)propMap.get(REPLICAS)); propMap.put(REPLICAS, this.replicas); leader = findLeader();
/** Loads multiple slices into a Map from a generic Map that probably came from deserialized JSON. */ public static Map<String,Slice> loadAllFromMap(Map<String, Object> genericSlices) { if (genericSlices == null) return Collections.emptyMap(); Map<String, Slice> result = new LinkedHashMap<>(genericSlices.size()); for (Map.Entry<String, Object> entry : genericSlices.entrySet()) { String name = entry.getKey(); Object val = entry.getValue(); if (val instanceof Slice) { result.put(name, (Slice) val); } else if (val instanceof Map) { result.put(name, new Slice(name, null, (Map<String, Object>) val)); } } return result; }
public String getShardId(String collectionName, String nodeName, String coreName) { Collection<CollectionRef> states = collectionStates.values(); if (collectionName != null) { CollectionRef c = collectionStates.get(collectionName); if (c != null) states = Collections.singletonList( c ); } for (CollectionRef ref : states) { DocCollection coll = ref.get(); if(coll == null) continue;// this collection go tremoved in between, skip for (Slice slice : coll.getSlices()) { for (Replica replica : slice.getReplicas()) { // TODO: for really large clusters, we could 'index' on this String rnodeName = replica.getStr(ZkStateReader.NODE_NAME_PROP); String rcore = replica.getStr(ZkStateReader.CORE_NAME_PROP); if (nodeName.equals(rnodeName) && coreName.equals(rcore)) { return slice.getName(); } } } } return null; }
for (Slice slice : slices) { if (slice.getState() == Slice.State.ACTIVE) { Collection<Replica> replicas = slice.getReplicas(); for (Replica replica : replicas) {
Slice[] slices = col.getActiveSlicesArr(); for (Slice slice : slices) { String name = slice.getName(); List<String> urls = new ArrayList<>(); Replica leader = slice.getLeader(); if (directUpdatesToLeadersOnly && leader == null) { for (Replica replica : slice.getReplicas( replica -> replica.isActive(getClusterStateProvider().getLiveNodes()) && replica.getType() == Replica.Type.NRT)) { urls.add(url); if (!directUpdatesToLeadersOnly) { for (Replica replica : slice.getReplicas()) { if (!replica.getNodeName().equals(leader.getNodeName()) && !replica.getName().equals(leader.getName())) {