public static CloudState load(byte[] bytes, Set<String> liveNodes) throws KeeperException, InterruptedException { if (bytes == null || bytes.length == 0) { return new CloudState(liveNodes, Collections.<String, Map<String, Slice>>emptyMap()); } LinkedHashMap<String, Object> stateMap = (LinkedHashMap<String, Object>) ZkStateReader.fromJSON(bytes); HashMap<String, Map<String, Slice>> state = new HashMap<String, Map<String, Slice>>(); for (String collectionName : stateMap.keySet()) { Map<String, Object> collection = (Map<String, Object>) stateMap.get(collectionName); Map<String, Slice> slices = new LinkedHashMap<String, Slice>(); for (String sliceName : collection.keySet()) { Map<String, Map<String, String>> sliceMap = (Map<String, Map<String, String>>) collection.get(sliceName); Map<String, ZkNodeProps> shards = new LinkedHashMap<String, ZkNodeProps>(); for (String shardName : sliceMap.keySet()) { shards.put(shardName, new ZkNodeProps(sliceMap.get(shardName))); } Slice slice = new Slice(sliceName, shards); slices.put(sliceName, slice); } state.put(collectionName, slices); } return new CloudState(liveNodes, state); }
protected final synchronized DocCollection getDocCollection() { if (docCollection == null) { ZkStateReader zkStateReader = getCloudSolrServer().getZkStateReader(); docCollection = zkStateReader.getClusterState().getCollection(collection); // do basic checks once DocRouter docRouter = docCollection.getRouter(); if (docRouter instanceof ImplicitDocRouter) throw new IllegalStateException( "Implicit document routing not supported by this Partitioner!"); Collection<Slice> shards = getDocCollection().getSlices(); if (shards == null || shards.size() == 0) throw new IllegalStateException( "Collection '" + collection + "' does not have any shards!"); } return docCollection; }
protected List<String> buildShardList(CloudSolrClient cloudSolrServer) { ZkStateReader zkStateReader = cloudSolrServer.getZkStateReader(); ClusterState clusterState = zkStateReader.getClusterState(); String[] collections = null; if (clusterState.hasCollection(collection)) { collections = new String[] {collection}; } else { // might be a collection alias? Aliases aliases = zkStateReader.getAliases(); String aliasedCollections = aliases.getCollectionAlias(collection); if (aliasedCollections == null) throw new IllegalArgumentException("Collection " + collection + " not found!"); collections = aliasedCollections.split(","); } Set<String> liveNodes = clusterState.getLiveNodes(); Random random = new Random(5150); List<String> shards = new ArrayList<String>(); for (String coll : collections) { for (Slice slice : clusterState.getSlices(coll)) { List<String> replicas = new ArrayList<String>(); for (Replica r : slice.getReplicas()) { ZkCoreNodeProps replicaCoreProps = new ZkCoreNodeProps(r); if (liveNodes.contains(replicaCoreProps.getNodeName())) replicas.add(replicaCoreProps.getCoreUrl()); } int numReplicas = replicas.size(); if (numReplicas == 0) throw new IllegalStateException( "Shard " + slice.getName() + " does not have any active replicas!"); String replicaUrl = (numReplicas == 1) ? replicas.get(0) : replicas.get(random.nextInt(replicas.size())); shards.add(replicaUrl); } } return shards; }