Java Code Examples for org.apache.solr.common.cloud.ClusterState#getCollectionsMap()

The following examples show how to use org.apache.solr.common.cloud.ClusterState#getCollectionsMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Solr6Index.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Override
public boolean exists() throws BackendException {
    if (mode!= Mode.CLOUD) throw new UnsupportedOperationException("Operation only supported for SolrCloud");
    final CloudSolrClient server = (CloudSolrClient) solrClient;
    try {
        final ZkStateReader zkStateReader = server.getZkStateReader();
        zkStateReader.forciblyRefreshAllClusterStateSlow();
        final ClusterState clusterState = zkStateReader.getClusterState();
        final Map<String, DocCollection> collections = clusterState.getCollectionsMap();
        return collections != null && !collections.isEmpty();
    } catch (KeeperException | InterruptedException e) {
        throw new PermanentBackendException("Unable to check if index exists", e);
    }
}
 
Example 2
Source File: CloudSolrStream.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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> allCollections = new ArrayList<>();
  String[] collectionNames = collectionName.split(",");
  for(String col : collectionNames) {
    List<String> collections = checkAlias
        ? zkStateReader.getAliases().resolveAliases(col)  // if not an alias, returns collectionName
        : Collections.singletonList(collectionName);
    allCollections.addAll(collections);
  }

  // Lookup all actives slices for these collections
  List<Slice> slices = allCollections.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(Entry<String, DocCollection> entry : collectionsMap.entrySet()) {
    if(entry.getKey().equalsIgnoreCase(collectionName)) {
      return entry.getValue().getActiveSlicesArr();
    }
  }

  throw new IOException("Slices not found for " + collectionName);
}
 
Example 3
Source File: DeepRandomStream.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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> allCollections = new ArrayList<>();
  String[] collectionNames = collectionName.split(",");
  for(String col : collectionNames) {
    List<String> collections = checkAlias
        ? zkStateReader.getAliases().resolveAliases(col)  // if not an alias, returns collectionName
        : Collections.singletonList(collectionName);
    allCollections.addAll(collections);
  }

  // Lookup all actives slices for these collections
  List<Slice> slices = allCollections.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(Entry<String, DocCollection> entry : collectionsMap.entrySet()) {
    if(entry.getKey().equalsIgnoreCase(collectionName)) {
      return entry.getValue().getActiveSlicesArr();
    }
  }

  throw new IOException("Slices not found for " + collectionName);
}
 
Example 4
Source File: AbstractFullDistribZkTestBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected String printClusterStateInfo(String collection) throws Exception {
  cloudClient.getZkStateReader().forceUpdateCollection(collection);
  String cs = null;
  ClusterState clusterState = cloudClient.getZkStateReader().getClusterState();
  if (collection != null) {
    cs = clusterState.getCollection(collection).toString();
  } else {
    Map<String,DocCollection> map = clusterState.getCollectionsMap();
    CharArr out = new CharArr();
    new JSONWriter(out, 2).write(map);
    cs = out.toString();
  }
  return cs;
}
 
Example 5
Source File: Assign.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
static HashMap<String, ReplicaCount> getNodeNameVsShardCount(String collectionName,
                                                                     ClusterState clusterState, List<String> createNodeList) {
  HashMap<String, ReplicaCount> nodeNameVsShardCount = new HashMap<>();
  List<String> liveNodes = createNodeList == null || createNodeList.isEmpty() ?
      new ArrayList<>(clusterState.getLiveNodes()) :
      checkLiveNodes(createNodeList, clusterState);

  for (String s : liveNodes) {
    nodeNameVsShardCount.put(s, new ReplicaCount(s));
  }

  // if we were given a list, just use that, don't worry about counts
  if (createNodeList != null) { // Overrides petty considerations about maxShardsPerNode
    return nodeNameVsShardCount;
  }

  // if we get here we were not given a createNodeList, build a map with real counts.
  DocCollection coll = clusterState.getCollection(collectionName);
  int maxShardsPerNode = coll.getMaxShardsPerNode() == -1 ? Integer.MAX_VALUE : coll.getMaxShardsPerNode();
  Map<String, DocCollection> collections = clusterState.getCollectionsMap();
  for (Map.Entry<String, DocCollection> entry : collections.entrySet()) {
    DocCollection c = entry.getValue();
    //identify suitable nodes  by checking the no:of cores in each of them
    for (Slice slice : c.getSlices()) {
      Collection<Replica> replicas = slice.getReplicas();
      for (Replica replica : replicas) {
        ReplicaCount count = nodeNameVsShardCount.get(replica.getNodeName());
        if (count != null) {
          count.totalNodes++; // Used to "weigh" whether this node should be used later.
          if (entry.getKey().equals(collectionName)) {
            count.thisCollectionNodes++;
            if (count.thisCollectionNodes >= maxShardsPerNode) nodeNameVsShardCount.remove(replica.getNodeName());
          }
        }
      }
    }
  }

  return nodeNameVsShardCount;
}
 
Example 6
Source File: ReplicaAssigner.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * @param shardVsReplicaCount shard names vs no:of replicas required for each of those shards
 * @param snitches            snitches details
 * @param shardVsNodes        The current state of the system. can be an empty map if no nodes
 *                            are created in this collection till now
 */
@SuppressWarnings({"unchecked"})
public ReplicaAssigner(List<Rule> rules,
                       Map<String, Integer> shardVsReplicaCount,
                       @SuppressWarnings({"rawtypes"})List snitches,
                       Map<String, Map<String, Integer>> shardVsNodes,
                       List<String> participatingLiveNodes,
                       SolrCloudManager cloudManager, ClusterState clusterState) {
  this.rules = rules;
  for (Rule rule : rules) tagNames.add(rule.tag.name);
  this.shardVsReplicaCount = shardVsReplicaCount;
  this.participatingLiveNodes = new ArrayList<>(participatingLiveNodes);
  this.nodeVsTags = getTagsForNodes(cloudManager, snitches);
  this.shardVsNodes = getDeepCopy(shardVsNodes, 2);

  if (clusterState != null) {
    Map<String, DocCollection> collections = clusterState.getCollectionsMap();
    for (Map.Entry<String, DocCollection> entry : collections.entrySet()) {
      DocCollection coll = entry.getValue();
      for (Slice slice : coll.getSlices()) {
        for (Replica replica : slice.getReplicas()) {
          AtomicInteger count = nodeVsCores.get(replica.getNodeName());
          if (count == null) nodeVsCores.put(replica.getNodeName(), count = new AtomicInteger());
          count.incrementAndGet();
        }
      }
    }
  }
}
 
Example 7
Source File: NodeMutator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public List<ZkWriteCommand> downNode(ClusterState clusterState, ZkNodeProps message) {
  List<ZkWriteCommand> zkWriteCommands = new ArrayList<>();
  String nodeName = message.getStr(ZkStateReader.NODE_NAME_PROP);

  log.debug("DownNode state invoked for node: {}", nodeName);

  Map<String, DocCollection> collections = clusterState.getCollectionsMap();
  for (Map.Entry<String, DocCollection> entry : collections.entrySet()) {
    String collection = entry.getKey();
    DocCollection docCollection = entry.getValue();

    Map<String,Slice> slicesCopy = new LinkedHashMap<>(docCollection.getSlicesMap());

    boolean needToUpdateCollection = false;
    for (Entry<String, Slice> sliceEntry : slicesCopy.entrySet()) {
      Slice slice = sliceEntry.getValue();
      Map<String, Replica> newReplicas = slice.getReplicasCopy();

      Collection<Replica> replicas = slice.getReplicas();
      for (Replica replica : replicas) {
        String rNodeName = replica.getNodeName();
        if (rNodeName == null) {
          throw new RuntimeException("Replica without node name! " + replica);
        }
        if (rNodeName.equals(nodeName)) {
          log.debug("Update replica state for {} to {}", replica, Replica.State.DOWN);
          Map<String, Object> props = replica.shallowCopy();
          props.put(ZkStateReader.STATE_PROP, Replica.State.DOWN.toString());
          Replica newReplica = new Replica(replica.getName(), props, collection, slice.getName());
          newReplicas.put(replica.getName(), newReplica);
          needToUpdateCollection = true;
        }
      }

      Slice newSlice = new Slice(slice.getName(), newReplicas, slice.shallowCopy(),collection);
      slicesCopy.put(slice.getName(), newSlice);
    }

    if (needToUpdateCollection) {
      zkWriteCommands.add(new ZkWriteCommand(collection, docCollection.copyWithSlices(slicesCopy)));
    }
  }

  return zkWriteCommands;
}
 
Example 8
Source File: SnapshotClusterStateProvider.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public SnapshotClusterStateProvider(ClusterStateProvider other) throws Exception {
  liveNodes = Set.copyOf(other.getLiveNodes());
  ClusterState otherState = other.getClusterState();
  clusterState = new ClusterState(liveNodes, otherState.getCollectionsMap());
  clusterProperties = new HashMap<>(other.getClusterProperties());
}
 
Example 9
Source File: SharedFSAutoReplicaFailoverTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private boolean waitingForReplicasNotLive(ZkStateReader zkStateReader, int timeoutInMs, List<JettySolrRunner> jetties) {
  Set<String> nodeNames = jetties.stream()
      .filter(jetty -> jetty.getCoreContainer() != null)
      .map(JettySolrRunner::getNodeName)
      .collect(Collectors.toSet());
  long timeout = System.nanoTime()
      + TimeUnit.NANOSECONDS.convert(timeoutInMs, TimeUnit.MILLISECONDS);
  boolean success = false;
  while (!success && System.nanoTime() < timeout) {
    success = true;
    ClusterState clusterState = zkStateReader.getClusterState();
    if (clusterState != null) {
      Map<String, DocCollection> collections = clusterState.getCollectionsMap();
      for (Map.Entry<String, DocCollection> entry : collections.entrySet()) {
        DocCollection docCollection = entry.getValue();
        Collection<Slice> slices = docCollection.getSlices();
        for (Slice slice : slices) {
          // only look at active shards
          if (slice.getState() == Slice.State.ACTIVE) {
            Collection<Replica> replicas = slice.getReplicas();
            for (Replica replica : replicas) {
              if (nodeNames.contains(replica.getNodeName())) {
                boolean live = clusterState.liveNodesContain(replica
                    .getNodeName());
                if (live) {
                  success = false;
                }
              }
            }
          }
        }
      }
      if (!success) {
        try {
          Thread.sleep(500);
        } catch (InterruptedException e) {
          Thread.currentThread().interrupt();
          throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Interrupted");
        }
      }
    }
  }

  return success;
}