Java Code Examples for org.apache.solr.core.CoreContainer#getZkController()

The following examples show how to use org.apache.solr.core.CoreContainer#getZkController() . 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: DistributedZkUpdateProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public DistributedZkUpdateProcessor(SolrQueryRequest req,
                                    SolrQueryResponse rsp, UpdateRequestProcessor next) {
  super(req, rsp, next);
  CoreContainer cc = req.getCore().getCoreContainer();
  cloudDesc = req.getCore().getCoreDescriptor().getCloudDescriptor();
  zkController = cc.getZkController();
  cmdDistrib = new SolrCmdDistributor(cc.getUpdateShardHandler());
  cloneRequiredOnLeader = isCloneRequiredOnLeader(next);
  collection = cloudDesc.getCollectionName();
  clusterState = zkController.getClusterState();
  DocCollection coll = clusterState.getCollectionOrNull(collection);
  if (coll != null) {
    // check readOnly property in coll state
    readOnlyCollection = coll.isReadOnly();
  }
}
 
Example 2
Source File: SolrClusterReporter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void init(PluginInfo pluginInfo, CoreContainer cc) {
  super.init(pluginInfo, cc);
  if (reporter != null) {
    reporter.close();;
  }
  if (!enabled) {
    log.info("Reporter disabled for registry {}", registryName);
    return;
  }
  // start reporter only in cloud mode
  if (!cc.isZooKeeperAware()) {
    log.warn("Not ZK-aware, not starting...");
    return;
  }
  if (period < 1) { // don't start it
    log.info("Turning off node reporter, period={}", period);
    return;
  }
  HttpClient httpClient = cc.getUpdateShardHandler().getDefaultHttpClient();
  ZkController zk = cc.getZkController();
  String reporterId = zk.getNodeName();
  reporter = SolrReporter.Builder.forReports(metricManager, reports)
      .convertRatesTo(TimeUnit.SECONDS)
      .convertDurationsTo(TimeUnit.MILLISECONDS)
      .withHandler(handler)
      .withReporterId(reporterId)
      .setCompact(true)
      .cloudClient(false) // we want to send reports specifically to a selected leader instance
      .skipAggregateValues(true) // we don't want to transport details of aggregates
      .skipHistograms(true) // we don't want to transport histograms
      .build(httpClient, new OverseerUrlSupplier(zk));

  reporter.start(period, TimeUnit.SECONDS);
}
 
Example 3
Source File: MDCLoggingContext.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static void setNode(CoreContainer cc) {
  if (cc != null) {
    ZkController zk = cc.getZkController();
    if (zk != null) {
      setNode(zk.getNodeName());
    }
  }
}
 
Example 4
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 5
Source File: ScoreJoinQParserPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an String with the name of a core.
 * <p>
 * This method searches the core with fromIndex name in the core's container.
 * If fromIndex isn't name of collection or alias it's returns fromIndex without changes.
 * If fromIndex is name of alias but if the alias points to multiple collections it's throw
 * SolrException.ErrorCode.BAD_REQUEST because multiple shards not yet supported.
 *
 * @param  fromIndex name of the index
 * @param  container the core container for searching the core with fromIndex name or alias
 * @return      the string with name of core
 */
public static String getCoreName(final String fromIndex, CoreContainer container) {
  if (container.isZooKeeperAware()) {
    ZkController zkController = container.getZkController();
    final String resolved = resolveAlias(fromIndex, zkController);
    // TODO DWS: no need for this since later, clusterState.getCollection will throw a reasonable error
    if (!zkController.getClusterState().hasCollection(resolved)) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          "SolrCloud join: Collection '" + fromIndex + "' not found!");
    }
    return findLocalReplicaForFromIndex(zkController, resolved);
  }
  return fromIndex;
}
 
Example 6
Source File: SearchHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private ShardHandler getAndPrepShardHandler(SolrQueryRequest req, ResponseBuilder rb) {
  ShardHandler shardHandler = null;

  CoreContainer cc = req.getCore().getCoreContainer();
  boolean isZkAware = cc.isZooKeeperAware();
  rb.isDistrib = req.getParams().getBool(DISTRIB, isZkAware);
  if (!rb.isDistrib) {
    // for back compat, a shards param with URLs like localhost:8983/solr will mean that this
    // search is distributed.
    final String shards = req.getParams().get(ShardParams.SHARDS);
    rb.isDistrib = ((shards != null) && (shards.indexOf('/') > 0));
  }
  
  if (rb.isDistrib) {
    shardHandler = shardHandlerFactory.getShardHandler();
    shardHandler.prepDistributed(rb);
    if (!rb.isDistrib) {
      shardHandler = null; // request is not distributed after all and so the shard handler is not needed
    }
  }

  if (isZkAware) {
    String shardsTolerant = req.getParams().get(ShardParams.SHARDS_TOLERANT);
    boolean requireZkConnected = shardsTolerant != null && shardsTolerant.equals(ShardParams.REQUIRE_ZK_CONNECTED);
    ZkController zkController = cc.getZkController();
    boolean zkConnected = zkController != null && ! zkController.getZkClient().getConnectionManager().isLikelyExpired();
    if (requireZkConnected && false == zkConnected) {
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "ZooKeeper is not connected");
    } else {
      NamedList<Object> headers = rb.rsp.getResponseHeader();
      if (headers != null) {
        headers.add("zkConnected", zkConnected);
      }
    }
  }

  return shardHandler;
}
 
Example 7
Source File: RoutedAlias.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private CandidateCollection doSynchronous(AddUpdateCommand cmd, CandidateCollection targetCollectionDesc, CoreContainer coreContainer) {
  ensureCollection(targetCollectionDesc.getCreationCollection(), coreContainer); // *should* throw if fails for some reason but...
  ZkController zkController = coreContainer.getZkController();
  updateParsedCollectionAliases(zkController.zkStateReader, true);
  List<String> observedCols = zkController.zkStateReader.aliasesManager.getAliases().getCollectionAliasListMap().get(getAliasName());
  if (!observedCols.contains(targetCollectionDesc.creationCollection)) {
    // if collection creation did not occur we've failed. Bail out.
    throw new SolrException(SERVER_ERROR, "After we attempted to create " + targetCollectionDesc.creationCollection + " it did not exist");
  }
  // then recalculate the candiate, which may result in continuation or termination the loop calling this method
  targetCollectionDesc = findCandidateGivenValue(cmd);
  return targetCollectionDesc;
}
 
Example 8
Source File: RecoveryStrategy.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected RecoveryStrategy(CoreContainer cc, CoreDescriptor cd, RecoveryListener recoveryListener) {
  this.cc = cc;
  this.coreName = cd.getName();
  this.recoveryListener = recoveryListener;
  zkController = cc.getZkController();
  zkStateReader = zkController.getZkStateReader();
  baseUrl = zkController.getBaseUrl();
  coreZkNodeName = cd.getCloudDescriptor().getCoreNodeName();
  replicaType = cd.getCloudDescriptor().getReplicaType();
}
 
Example 9
Source File: ReSearcherHandler.java    From solr-researcher with Apache License 2.0 5 votes vote down vote up
private ShardHandler getAndPrepShardHandler(SolrQueryRequest req, ResponseBuilder rb, ShardHandlerFactory shardHandlerFactory) {
  ShardHandler shardHandler = null;

  boolean isZkAware = false;
  CoreContainer cc = null;
  if (req.getCore() != null) {
    cc = req.getCore().getCoreContainer();
    isZkAware = cc.isZooKeeperAware();
  } 
  
  rb.isDistrib = req.getParams().getBool("distrib", isZkAware);
  if (!rb.isDistrib) {
    // for back compat, a shards param with URLs like localhost:8983/solr will mean that this
    // search is distributed.
    final String shards = req.getParams().get(ShardParams.SHARDS);
    rb.isDistrib = ((shards != null) && (shards.indexOf('/') > 0));
  }
  
  if (rb.isDistrib) {
    shardHandler = shardHandlerFactory.getShardHandler();
    shardHandler.prepDistributed(rb);
    if (!rb.isDistrib) {
      shardHandler = null; // request is not distributed after all and so the shard handler is not needed
    }
  }

  if(isZkAware) {
    ZkController zkController = cc.getZkController();
    NamedList<Object> headers = rb.rsp.getResponseHeader();
    if(headers != null) {
      headers.add("zkConnected", 
          zkController != null 
        ? !zkController.getZkClient().getConnectionManager().isLikelyExpired() 
        : false);
    }
    
  }

  return shardHandler;
}
 
Example 10
Source File: CloudUtil.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * See if coreNodeName has been taken over by another baseUrl and unload core
 * + throw exception if it has been.
 */
public static void checkSharedFSFailoverReplaced(CoreContainer cc, CoreDescriptor desc) {
  if (!cc.isSharedFs(desc)) return;

  ZkController zkController = cc.getZkController();
  String thisCnn = zkController.getCoreNodeName(desc);
  String thisBaseUrl = zkController.getBaseUrl();

  log.debug("checkSharedFSFailoverReplaced running for coreNodeName={} baseUrl={}", thisCnn, thisBaseUrl);

  // if we see our core node name on a different base url, unload
  final DocCollection docCollection = zkController.getClusterState().getCollectionOrNull(desc.getCloudDescriptor().getCollectionName());
  if (docCollection != null && docCollection.getSlicesMap() != null) {
    Map<String,Slice> slicesMap = docCollection.getSlicesMap();
    for (Slice slice : slicesMap.values()) {
      for (Replica replica : slice.getReplicas()) {

        String cnn = replica.getName();
        String baseUrl = replica.getStr(ZkStateReader.BASE_URL_PROP);
        log.debug("compare against coreNodeName={} baseUrl={}", cnn, baseUrl);

        if (thisCnn != null && thisCnn.equals(cnn)
            && !thisBaseUrl.equals(baseUrl)) {
          if (cc.getLoadedCoreNames().contains(desc.getName())) {
            cc.unload(desc.getName());
          }

          try {
            FileUtils.deleteDirectory(desc.getInstanceDir().toFile());
          } catch (IOException e) {
            SolrException.log(log, "Failed to delete instance dir for core:"
                + desc.getName() + " dir:" + desc.getInstanceDir());
          }
          log.error("{}",
              new SolrException(ErrorCode.SERVER_ERROR, "Will not load SolrCore " + desc.getName()
                  + " because it has been replaced due to failover.")); // logOk
          throw new SolrException(ErrorCode.SERVER_ERROR,
              "Will not load SolrCore " + desc.getName()
                  + " because it has been replaced due to failover.");
        }
      }
    }
  }
}