Java Code Examples for org.apache.solr.cloud.ZkController#getZkStateReader()

The following examples show how to use org.apache.solr.cloud.ZkController#getZkStateReader() . 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: ManagedIndexSchema.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected static List<String> getActiveReplicaCoreUrls(ZkController zkController, String collection, String localCoreNodeName) {
  List<String> activeReplicaCoreUrls = new ArrayList<>();
  ZkStateReader zkStateReader = zkController.getZkStateReader();
  ClusterState clusterState = zkStateReader.getClusterState();
  Set<String> liveNodes = clusterState.getLiveNodes();
  final DocCollection docCollection = clusterState.getCollectionOrNull(collection);
  if (docCollection != null && docCollection.getActiveSlicesArr().length > 0) {
    final Slice[] activeSlices = docCollection.getActiveSlicesArr();
    for (Slice next : activeSlices) {
      Map<String, Replica> replicasMap = next.getReplicasMap();
      if (replicasMap != null) {
        for (Map.Entry<String, Replica> entry : replicasMap.entrySet()) {
          Replica replica = entry.getValue();
          if (!localCoreNodeName.equals(replica.getName()) &&
              replica.getState() == Replica.State.ACTIVE &&
              liveNodes.contains(replica.getNodeName())) {
            ZkCoreNodeProps replicaCoreProps = new ZkCoreNodeProps(replica);
            activeReplicaCoreUrls.add(replicaCoreProps.getCoreUrl());
          }
        }
      }
    }
  }
  return activeReplicaCoreUrls;
}
 
Example 2
Source File: RoutedAliasUpdateProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static UpdateRequestProcessor wrap(SolrQueryRequest req, UpdateRequestProcessor next) {
  String aliasName = null;
  // Demeter please don't arrest us... hide your eyes :(
  // todo: a core should have a more direct way of finding a collection name, and the collection properties
  SolrCore core = req.getCore();
  CoreDescriptor coreDescriptor = core.getCoreDescriptor();
  CloudDescriptor cloudDescriptor = coreDescriptor.getCloudDescriptor();
  if (cloudDescriptor != null) {
    String collectionName = cloudDescriptor.getCollectionName();
    CoreContainer coreContainer = core.getCoreContainer();
    ZkController zkController = coreContainer.getZkController();
    ZkStateReader zkStateReader = zkController.getZkStateReader();
    Map<String, String> collectionProperties = zkStateReader.getCollectionProperties(collectionName, CACHE_FOR_MILLIS);
    aliasName = collectionProperties.get(RoutedAlias.ROUTED_ALIAS_NAME_CORE_PROP);
  }
  // fall back on core properties (legacy)
  if (StringUtils.isBlank(aliasName)) {
    aliasName = coreDescriptor.getCoreProperty(RoutedAlias.ROUTED_ALIAS_NAME_CORE_PROP, null);
  }
  final DistribPhase shardDistribPhase =
      DistribPhase.parseParam(req.getParams().get(DISTRIB_UPDATE_PARAM));
  final DistribPhase aliasDistribPhase =
      DistribPhase.parseParam(req.getParams().get(ALIAS_DISTRIB_UPDATE_PARAM));
  if (aliasName == null || aliasDistribPhase != DistribPhase.NONE || shardDistribPhase != DistribPhase.NONE) {
    // if aliasDistribPhase is not NONE, then there is no further collection routing to be done here.
    //    TODO this may eventually not be true but at the moment it is
    // if shardDistribPhase is not NONE, then the phase is after the scope of this URP
    return next;
  } else {
    try {
      RoutedAlias alias = RoutedAlias.fromProps(aliasName, getAliasProps(req, aliasName));
      return new RoutedAliasUpdateProcessor(req, next, aliasDistribPhase, alias);
    } catch (Exception e) { // ensure we throw SERVER_ERROR not BAD_REQUEST at this stage
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Routed alias has invalid properties: " + e, e);
    }

  }
}