private String getCoreUrl( String collectionName, String origCorename, ClusterState clusterState, Collection<Slice> slices, boolean byCoreName, boolean activeReplicas) { String coreUrl; Set<String> liveNodes = clusterState.getLiveNodes(); List<Slice> randomizedSlices = new ArrayList<>(slices.size()); randomizedSlices.addAll(slices); Collections.shuffle(randomizedSlices, random); for (Slice slice : randomizedSlices) { List<Replica> randomizedReplicas = new ArrayList<>(); randomizedReplicas.addAll(slice.getReplicas()); Collections.shuffle(randomizedReplicas, random); for (Replica replica : randomizedReplicas) { if (!activeReplicas || (liveNodes.contains(replica.getNodeName()) && replica.getState() == Replica.State.ACTIVE)) { if (byCoreName && !collectionName.equals(replica.getStr(CORE_NAME_PROP))) { // if it's by core name, make sure they match continue; } if (replica.getStr(BASE_URL_PROP).equals(cores.getZkController().getBaseUrl())) { // don't count a local core continue; } if (origCorename != null) { coreUrl = replica.getStr(BASE_URL_PROP) + "/" + origCorename; } else { coreUrl = replica.getCoreUrl(); if (coreUrl.endsWith("/")) { coreUrl = coreUrl.substring(0, coreUrl.length() - 1); } } return coreUrl; } } } return null; }
private static String findLocalReplicaForFromIndex(ZkController zkController, String fromIndex) { String fromReplica = null; String nodeName = zkController.getNodeName(); for (Slice slice : zkController.getClusterState().getActiveSlices(fromIndex)) { if (fromReplica != null) throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "SolrCloud join: multiple shards not yet supported " + fromIndex); for (Replica replica : slice.getReplicas()) { if (replica.getNodeName().equals(nodeName)) { fromReplica = replica.getStr(ZkStateReader.CORE_NAME_PROP); // found local replica, but is it Active? if (replica.getState() != Replica.State.ACTIVE) throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "SolrCloud join: " + fromIndex + " has a local replica (" + fromReplica + ") on " + nodeName + ", but it is " + replica.getState()); break; } } } if (fromReplica == null) throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "SolrCloud join: No active replicas for " + fromIndex + " found in node " + nodeName); return fromReplica; }
@Test public void testStoreAndRead() throws Exception { Map<String, DocCollection> collectionStates = new HashMap<>(); Set<String> liveNodes = new HashSet<>(); liveNodes.add("node1"); liveNodes.add("node2"); Map<String, Slice> slices = new HashMap<>(); Map<String, Replica> sliceToProps = new HashMap<>(); Map<String, Object> props = new HashMap<>(); props.put("prop1", "value"); props.put("prop2", "value2"); Replica replica = new Replica("node1", props); sliceToProps.put("node1", replica); Slice slice = new Slice("shard1", sliceToProps, null); slices.put("shard1", slice); Slice slice2 = new Slice("shard2", sliceToProps, null); slices.put("shard2", slice2); collectionStates.put( "collection1", new DocCollection("collection1", slices, null, DocRouter.DEFAULT)); collectionStates.put( "collection2", new DocCollection("collection2", slices, null, DocRouter.DEFAULT)); ZkStateReader zkStateReaderMock = getMockZkStateReader(collectionStates.keySet()); ClusterState clusterState = new ClusterState(-1, liveNodes, collectionStates); byte[] bytes = Utils.toJSON(clusterState); // System.out.println("#################### " + new String(bytes)); ClusterState loadedClusterState = ClusterState.load(-1, bytes, liveNodes); assertEquals( "Provided liveNodes not used properly", 2, loadedClusterState.getLiveNodes().size()); assertEquals("No collections found", 2, loadedClusterState.getCollectionsMap().size()); assertEquals( "Properties not copied properly", replica.getStr("prop1"), loadedClusterState .getSlice("collection1", "shard1") .getReplicasMap() .get("node1") .getStr("prop1")); assertEquals( "Properties not copied properly", replica.getStr("prop2"), loadedClusterState .getSlice("collection1", "shard1") .getReplicasMap() .get("node1") .getStr("prop2")); loadedClusterState = ClusterState.load(-1, new byte[0], liveNodes); assertEquals( "Provided liveNodes not used properly", 2, loadedClusterState.getLiveNodes().size()); assertEquals("Should not have collections", 0, loadedClusterState.getCollectionsMap().size()); loadedClusterState = ClusterState.load(-1, (byte[]) null, liveNodes); assertEquals( "Provided liveNodes not used properly", 2, loadedClusterState.getLiveNodes().size()); assertEquals("Should not have collections", 0, loadedClusterState.getCollectionsMap().size()); }