Java Code Examples for org.apache.solr.core.CoreDescriptor#getCloudDescriptor()

The following examples show how to use org.apache.solr.core.CoreDescriptor#getCloudDescriptor() . 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: 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);
    }

  }
}
 
Example 2
Source File: ZkController.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void checkStateInZk(CoreDescriptor cd) throws InterruptedException, NotInClusterStateException {
  CloudDescriptor cloudDesc = cd.getCloudDescriptor();
  String nodeName = cloudDesc.getCoreNodeName();
  if (nodeName == null) {
    throw new SolrException(ErrorCode.SERVER_ERROR, "No coreNodeName for " + cd);
  }
  final String coreNodeName = nodeName;

  if (cloudDesc.getShardId() == null) {
    throw new SolrException(ErrorCode.SERVER_ERROR, "No shard id for " + cd);
  }

  AtomicReference<String> errorMessage = new AtomicReference<>();
  AtomicReference<DocCollection> collectionState = new AtomicReference<>();
  try {
    zkStateReader.waitForState(cd.getCollectionName(), 10, TimeUnit.SECONDS, (c) -> {
      collectionState.set(c);
      if (c == null)
        return false;
      Slice slice = c.getSlice(cloudDesc.getShardId());
      if (slice == null) {
        errorMessage.set("Invalid shard: " + cloudDesc.getShardId());
        return false;
      }
      Replica replica = slice.getReplica(coreNodeName);
      if (replica == null) {
        errorMessage.set("coreNodeName " + coreNodeName + " does not exist in shard " + cloudDesc.getShardId() +
            ", ignore the exception if the replica was deleted");
        return false;
      }
      return true;
    });
  } catch (TimeoutException e) {
    String error = errorMessage.get();
    if (error == null)
      error = "coreNodeName " + coreNodeName + " does not exist in shard " + cloudDesc.getShardId() +
          ", ignore the exception if the replica was deleted";
    throw new NotInClusterStateException(ErrorCode.SERVER_ERROR, error);
  }
}
 
Example 3
Source File: HttpShardHandler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void prepDistributed(ResponseBuilder rb) {
  final SolrQueryRequest req = rb.req;
  final SolrParams params = req.getParams();
  final String shards = params.get(ShardParams.SHARDS);

  CoreDescriptor coreDescriptor = req.getCore().getCoreDescriptor();
  CloudDescriptor cloudDescriptor = coreDescriptor.getCloudDescriptor();
  ZkController zkController = req.getCore().getCoreContainer().getZkController();

  final ReplicaListTransformer replicaListTransformer = httpShardHandlerFactory.getReplicaListTransformer(req);

  HttpShardHandlerFactory.WhitelistHostChecker hostChecker = httpShardHandlerFactory.getWhitelistHostChecker();
  if (shards != null && zkController == null && hostChecker.isWhitelistHostCheckingEnabled() && !hostChecker.hasExplicitWhitelist()) {
    throw new SolrException(SolrException.ErrorCode.FORBIDDEN, "HttpShardHandlerFactory " + HttpShardHandlerFactory.INIT_SHARDS_WHITELIST
        + " not configured but required (in lieu of ZkController and ClusterState) when using the '" + ShardParams.SHARDS + "' parameter."
        + HttpShardHandlerFactory.SET_SOLR_DISABLE_SHARDS_WHITELIST_CLUE);
  }

  ReplicaSource replicaSource;
  if (zkController != null) {
    boolean onlyNrt = Boolean.TRUE == req.getContext().get(ONLY_NRT_REPLICAS);

    replicaSource = new CloudReplicaSource.Builder()
        .params(params)
        .zkStateReader(zkController.getZkStateReader())
        .whitelistHostChecker(hostChecker)
        .replicaListTransformer(replicaListTransformer)
        .collection(cloudDescriptor.getCollectionName())
        .onlyNrt(onlyNrt)
        .build();
    rb.slices = replicaSource.getSliceNames().toArray(new String[replicaSource.getSliceCount()]);

    if (canShortCircuit(rb.slices, onlyNrt, params, cloudDescriptor)) {
      rb.isDistrib = false;
      rb.shortCircuitedURL = ZkCoreNodeProps.getCoreUrl(zkController.getBaseUrl(), coreDescriptor.getName());
      return;
      // We shouldn't need to do anything to handle "shard.rows" since it was previously meant to be an optimization?
    }

    for (int i = 0; i < rb.slices.length; i++) {
      if (!ShardParams.getShardsTolerantAsBool(params) && replicaSource.getReplicasBySlice(i).isEmpty()) {
        // stop the check when there are no replicas available for a shard
        // todo fix use of slices[i] which can be null if user specified urls in shards param
        throw new SolrException(SolrException.ErrorCode.SERVICE_UNAVAILABLE,
            "no servers hosting shard: " + rb.slices[i]);
      }
    }
  } else {
    replicaSource = new StandaloneReplicaSource.Builder()
        .whitelistHostChecker(hostChecker)
        .shards(shards)
        .build();
    rb.slices = new String[replicaSource.getSliceCount()];
  }

  rb.shards = new String[rb.slices.length];
  for (int i = 0; i < rb.slices.length; i++) {
    rb.shards[i] = createSliceShardsStr(replicaSource.getReplicasBySlice(i));
  }

  String shards_rows = params.get(ShardParams.SHARDS_ROWS);
  if (shards_rows != null) {
    rb.shards_rows = Integer.parseInt(shards_rows);
  }
  String shards_start = params.get(ShardParams.SHARDS_START);
  if (shards_start != null) {
    rb.shards_start = Integer.parseInt(shards_start);
  }
}