/** * Checks if the collection has already been created in Solr. */ private static boolean checkIfCollectionExists(CloudSolrClient server, String collection) throws KeeperException, InterruptedException { ZkStateReader zkStateReader = server.getZkStateReader(); zkStateReader.updateClusterState(true); ClusterState clusterState = zkStateReader.getClusterState(); return clusterState.getCollectionOrNull(collection) != null; }
boolean sawLiveRecovering = false; zkStateReader.updateClusterState(true); ClusterState clusterState = zkStateReader.getClusterState(); Map<String, Slice> slices = clusterState.getSlicesMap(collection); Preconditions.checkNotNull("Could not find collection:" + collection, slices);
@Override public void clearStorage() throws BackendException { try { if (mode!=Mode.CLOUD) throw new UnsupportedOperationException("Operation only supported for SolrCloud"); logger.debug("Clearing storage from Solr: {}", solrClient); ZkStateReader zkStateReader = ((CloudSolrClient) solrClient).getZkStateReader(); zkStateReader.updateClusterState(true); ClusterState clusterState = zkStateReader.getClusterState(); for (String collection : clusterState.getCollections()) { logger.debug("Clearing collection [{}] in Solr",collection); UpdateRequest deleteAll = newUpdateRequest(); deleteAll.deleteByQuery("*:*"); solrClient.request(deleteAll, collection); } } catch (SolrServerException e) { logger.error("Unable to clear storage from index due to server error on Solr.", e); throw new PermanentBackendException(e); } catch (IOException e) { logger.error("Unable to clear storage from index due to low-level I/O error.", e); throw new PermanentBackendException(e); } catch (Exception e) { logger.error("Unable to clear storage from index due to general error.", e); throw new PermanentBackendException(e); } }
protected void createCollectionIfNotExist(CloudSolrClient client, String collectionName) { Set<String> collectionNames = client.getZkStateReader().getClusterState().getCollections(); if (!collectionNames.contains(collectionName)) { try { new CollectionAdminRequest.Create().setCollectionName(collectionName).setNumShards(getSolrCloudNumShards()) .setMaxShardsPerNode(getSolrCloudNumShards()).setConfigName(getSolrCloudConfigName()).process(client); } catch (SolrServerException e) { throw ExceptionHelper.refineException(e); } catch (IOException e) { throw ExceptionHelper.refineException(e); } } }
@Override public ClusterState getClusterState() throws IOException { return zkStateReader.getClusterState(); }
@Override public Set<String> getLiveNodes() { ClusterState clusterState = zkStateReader.getClusterState(); if (clusterState != null) { return clusterState.getLiveNodes(); } else { return Collections.emptySet(); } }
@Override public ClusterState.CollectionRef getState(String collection) { ClusterState clusterState = zkStateReader.getClusterState(); if (clusterState != null) { return clusterState.getCollectionRef(collection); } else { return null; } } public ZkStateReader getZkStateReader(){
private String getRandomSlice() { Map<String,Slice> slices = zkStateReader.getClusterState().getCollection(collection).getSlicesMap(); List<String> sliceKeyList = new ArrayList<>(slices.size()); sliceKeyList.addAll(slices.keySet()); String sliceName = sliceKeyList.get(chaosRandom.nextInt(sliceKeyList.size())); return sliceName; }
final ZkStateReader zkStateReader = ((CloudSolrClient) solrClient).getZkStateReader(); final Collection<Slice> activeSlices = zkStateReader.getClusterState().getCollection(this.solrCollection).getActiveSlices(); solrBackupResult.setTotalShards(activeSlices.size()); String bckupName = ""; for (final Slice slice : activeSlices) { // here you can get leader of each shard final Replica leader = slice.getLeader(); final Map<String, Object> prop = leader.getProperties(); final String url = (String) prop.get("base_url"); final String coreName = (String) prop.get("core"); }
static ClusterState getClusterState(AuthorizedSolrClient<CloudSolrClient> authorizedSolrClient) { authorizedSolrClient.solrClient.connect(); return authorizedSolrClient.solrClient.getZkStateReader().getClusterState(); }
public static boolean isAutoAddReplicas(ZkStateReader reader, String collection) { ClusterState clusterState = reader.getClusterState(); if (clusterState != null) { DocCollection docCollection = clusterState.getCollectionOrNull(collection); if (docCollection != null) { return docCollection.getAutoAddReplicas(); } } return false; }
protected int getTotalReplicas(String collection) { ZkStateReader zkStateReader = cloudClient.getZkStateReader(); DocCollection coll = zkStateReader.getClusterState().getCollectionOrNull(collection); if (coll == null) return 0; // support for when collection hasn't been created yet int cnt = 0; for (Slice slices : coll.getSlices()) { cnt += slices.getReplicas().size(); } return cnt; }
public Replica getLeader(String collection, String shard) throws InterruptedException { if (clusterState != null) { Replica replica = clusterState.getLeader(collection, shard); if (replica != null && getClusterState().liveNodesContain(replica.getNodeName())) { return replica; } } return null; }
private final Map<String,String> getShardLeaders(CloudSolrServer solr, String collection) throws Exception { Map<String,String> leaders = new TreeMap<String,String>(); ZkStateReader zkStateReader = solr.getZkStateReader(); for (Slice slice : zkStateReader.getClusterState().getSlices(collection)) { leaders.put(slice.getName(), zkStateReader.getLeaderUrl(collection, slice.getName(), ZK_CLIENT_TIMEOUT)); } return leaders; } }
/** * Get the collection state for a particular collection */ protected static DocCollection getCollectionState(String collectionName) { return cluster.getSolrClient().getZkStateReader().getClusterState().getCollection(collectionName); }
private Type getTypeForJetty(String sliceName, CloudJettyRunner cjetty) { DocCollection docCollection = zkStateReader.getClusterState().getCollection(collection); Slice slice = docCollection.getSlice(sliceName); ZkNodeProps props = slice.getReplicasMap().get(cjetty.coreNodeName); if (props == null) { throw new RuntimeException("shard name " + cjetty.coreNodeName + " not found in " + slice.getReplicasMap().keySet()); } return Replica.Type.valueOf(props.getStr(ZkStateReader.REPLICA_TYPE)); }
/** * Checks if the collection has already been created in Solr. */ private static boolean checkIfCollectionExists(CloudSolrClient server, String collection) throws KeeperException, InterruptedException { ZkStateReader zkStateReader = server.getZkStateReader(); zkStateReader.updateClusterState(); ClusterState clusterState = zkStateReader.getClusterState(); return clusterState.getCollectionOrNull(collection) != null; }
public Replica getLeader(String collection, String shard) { if (clusterState != null) { DocCollection docCollection = clusterState.getCollectionOrNull(collection); Replica replica = docCollection != null ? docCollection.getLeader(shard) : null; if (replica != null && getClusterState().liveNodesContain(replica.getNodeName())) { return replica; } } return null; }
protected ZkCoreNodeProps getLeaderUrlFromZk(String collection, String slice) { ClusterState clusterState = getCommonCloudSolrClient().getZkStateReader().getClusterState(); final DocCollection docCollection = clusterState.getCollectionOrNull(collection); if (docCollection != null && docCollection.getLeader(slice) != null) { return new ZkCoreNodeProps(docCollection.getLeader(slice)); } throw new RuntimeException("Could not find leader:" + collection + " " + slice); }
/** Delete all collections (and aliases) */ public void deleteAllCollections() throws Exception { try (ZkStateReader reader = new ZkStateReader(solrClient.getZkStateReader().getZkClient())) { reader.createClusterStateWatchersAndUpdate(); // up to date aliases & collections reader.aliasesManager.applyModificationAndExportToZk(aliases -> Aliases.EMPTY); for (String collection : reader.getClusterState().getCollectionStates().keySet()) { CollectionAdminRequest.deleteCollection(collection).process(solrClient); } } }