Java Code Examples for org.apache.solr.cloud.ZkController

The following examples show how to use org.apache.solr.cloud.ZkController. These examples are extracted from open source projects. 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 Project: lucene-solr   Source File: DirectUpdateHandler2.java    License: Apache License 2.0 6 votes vote down vote up
public DirectUpdateHandler2(SolrCore core) {
  super(core);
 
  solrCoreState = core.getSolrCoreState();
  
  UpdateHandlerInfo updateHandlerInfo = core.getSolrConfig()
      .getUpdateHandlerInfo();
  int docsUpperBound = updateHandlerInfo.autoCommmitMaxDocs;
  int timeUpperBound = updateHandlerInfo.autoCommmitMaxTime;
  long fileSizeUpperBound = updateHandlerInfo.autoCommitMaxSizeBytes;
  commitTracker = new CommitTracker("Hard", core, docsUpperBound, timeUpperBound, fileSizeUpperBound, updateHandlerInfo.openSearcher, false);
  
  int softCommitDocsUpperBound = updateHandlerInfo.autoSoftCommmitMaxDocs;
  int softCommitTimeUpperBound = updateHandlerInfo.autoSoftCommmitMaxTime;
  softCommitTracker = new CommitTracker("Soft", core, softCommitDocsUpperBound, softCommitTimeUpperBound, NO_FILE_SIZE_UPPER_BOUND_PLACEHOLDER, true, true);
  
  commitWithinSoftCommit = updateHandlerInfo.commitWithinSoftCommit;
  indexWriterCloseWaitsForMerges = updateHandlerInfo.indexWriterCloseWaitsForMerges;

  ZkController zkController = core.getCoreContainer().getZkController();
  if (zkController != null && core.getCoreDescriptor().getCloudDescriptor().getReplicaType() == Replica.Type.TLOG) {
    commitWithinSoftCommit = false;
    commitTracker.setOpenSearcher(true);
  }

}
 
Example 2
Source Project: lucene-solr   Source File: HttpShardHandlerFactory.java    License: Apache License 2.0 6 votes vote down vote up
protected ReplicaListTransformer getReplicaListTransformer(final SolrQueryRequest req) {
  final SolrParams params = req.getParams();
  final SolrCore core = req.getCore(); // explicit check for null core (temporary?, for tests)
  @SuppressWarnings("resource")
  ZkController zkController = core == null ? null : core.getCoreContainer().getZkController();
  if (zkController != null) {
    return requestReplicaListTransformerGenerator.getReplicaListTransformer(
        params,
        zkController.getZkStateReader().getClusterProperties()
            .getOrDefault(ZkStateReader.DEFAULT_SHARD_PREFERENCES, "")
            .toString(),
        zkController.getNodeName(),
        zkController.getBaseUrl(),
        zkController.getSysPropsCacher()
    );
  } else {
    return requestReplicaListTransformerGenerator.getReplicaListTransformer(params);
  }
}
 
Example 3
Source Project: lucene-solr   Source File: SolrConfigHandler.java    License: Apache License 2.0 6 votes vote down vote up
public static List<String> getActiveReplicaCoreUrls(ZkController zkController,
                                                    String collection) {
  List<String> activeReplicaCoreUrls = new ArrayList<>();
  ClusterState clusterState = zkController.getZkStateReader().getClusterState();
  Set<String> liveNodes = clusterState.getLiveNodes();
  final DocCollection docCollection = clusterState.getCollectionOrNull(collection);
  if (docCollection != null && docCollection.getActiveSlices() != null && docCollection.getActiveSlices().size() > 0) {
    final Collection<Slice> activeSlices = docCollection.getActiveSlices();
    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 (replica.getState() == Replica.State.ACTIVE && liveNodes.contains(replica.getNodeName())) {
            activeReplicaCoreUrls.add(replica.getCoreUrl());
          }
        }
      }
    }
  }
  return activeReplicaCoreUrls;
}
 
Example 4
/**
 * This doesn't validate the config (path) itself and is just responsible for creating the confNode.
 * That check should be done before the config node is created.
 */
public static void createConfNode(DistribStateManager stateManager, String configName, String coll) throws IOException, AlreadyExistsException, BadVersionException, KeeperException, InterruptedException {

  if (configName != null) {
    String collDir = ZkStateReader.COLLECTIONS_ZKNODE + "/" + coll;
    log.debug("creating collections conf node {} ", collDir);
    byte[] data = Utils.toJSON(makeMap(ZkController.CONFIGNAME_PROP, configName));
    if (stateManager.hasData(collDir)) {
      stateManager.setData(collDir, data, -1);
    } else {
      stateManager.makePath(collDir, data, CreateMode.PERSISTENT, false);
    }
  } else {
    throw new SolrException(ErrorCode.BAD_REQUEST,"Unable to get config name");
  }
}
 
Example 5
Source Project: lucene-solr   Source File: ManagedIndexSchema.java    License: 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 6
Source Project: lucene-solr   Source File: TestManagedSchemaThreadSafety.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes"})
private Runnable indexSchemaLoader(String configsetName, final ZkController zkController) {
  return () -> {
    try {
      SolrResourceLoader loader = new ZkSolrResourceLoader(loaderPath, configsetName, null, zkController);
      SolrConfig solrConfig = SolrConfig.readFromResourceLoader(loader, "solrconfig.xml", true, null);

      ManagedIndexSchemaFactory factory = new ManagedIndexSchemaFactory();
      factory.init(new NamedList());
      factory.create("schema.xml", solrConfig);
    }
    catch (Exception e) {
      throw new RuntimeException(e);
    }
  };
}
 
Example 7
Source Project: lucene-solr   Source File: SolrClusterReporter.java    License: 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 8
Source Project: lucene-solr   Source File: MDCLoggingContext.java    License: 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 9
Source Project: lucene-solr   Source File: AtomicUpdateDocumentMerger.java    License: Apache License 2.0 5 votes vote down vote up
private static String getRouteField(AddUpdateCommand cmd) {
  String result = null;
  SolrCore core = cmd.getReq().getCore();
  CloudDescriptor cloudDescriptor = core.getCoreDescriptor().getCloudDescriptor();
  if (cloudDescriptor != null) {
    String collectionName = cloudDescriptor.getCollectionName();
    ZkController zkController = core.getCoreContainer().getZkController();
    DocCollection collection = zkController.getClusterState().getCollection(collectionName);
    result = collection.getRouter().getRouteField(collection);
  }
  return result;
}
 
Example 10
Source Project: lucene-solr   Source File: RoutedAliasUpdateProcessor.java    License: 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 11
Source Project: lucene-solr   Source File: RoutedAliasUpdateProcessor.java    License: Apache License 2.0 5 votes vote down vote up
private static Map<String, String> getAliasProps(SolrQueryRequest req, String aliasName) {
  ZkController zkController = req.getCore().getCoreContainer().getZkController();
  final Map<String, String> aliasProperties = zkController.getZkStateReader().getAliases().getCollectionAliasProperties(aliasName);
  if (aliasProperties.isEmpty()) {
    throw RoutedAlias.newAliasMustExistException(aliasName); // if it did exist, we'd have a non-null map
  }
  return aliasProperties;
}
 
Example 12
Source Project: lucene-solr   Source File: PeerSync.java    License: Apache License 2.0 5 votes vote down vote up
private String msg() {
  ZkController zkController = uhandler.core.getCoreContainer().getZkController();

  String myURL = "";

  if (zkController != null) {
    myURL = zkController.getBaseUrl();
  }

  // TODO: core name turns up blank in many tests - find URL if cloud enabled?
  return "PeerSync: core="+uhandler.core.getName()+ " url="+myURL +" ";
}
 
Example 13
Source Project: lucene-solr   Source File: PeerSyncWithLeader.java    License: Apache License 2.0 5 votes vote down vote up
private String msg() {
  ZkController zkController = uhandler.core.getCoreContainer().getZkController();
  String myURL = "";
  if (zkController != null) {
    myURL = zkController.getBaseUrl();
  }

  return "PeerSync: core="+uhandler.core.getName()+ " url="+myURL +" ";
}
 
Example 14
Source Project: lucene-solr   Source File: ScoreJoinQParserPlugin.java    License: 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 15
Source Project: lucene-solr   Source File: ScoreJoinQParserPlugin.java    License: Apache License 2.0 5 votes vote down vote up
private static String resolveAlias(String fromIndex, ZkController zkController) {
  final Aliases aliases = zkController.getZkStateReader().getAliases();
  try {
    return aliases.resolveSimpleAlias(fromIndex); // if not an alias, returns input
  } catch (IllegalArgumentException e) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        "SolrCloud join: Collection alias '" + fromIndex +
            "' maps to multiple collectiions, which is not currently supported for joins.", e);
  }
}
 
Example 16
Source Project: lucene-solr   Source File: ScoreJoinQParserPlugin.java    License: Apache License 2.0 5 votes vote down vote up
private static String findLocalReplicaForFromIndex(ZkController zkController, String fromIndex) {
  String fromReplica = null;

  String nodeName = zkController.getNodeName();
  for (Slice slice : zkController.getClusterState().getCollection(fromIndex).getActiveSlicesArr()) {
    if (fromReplica != null)
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          "SolrCloud join: To join with a sharded collection, use method=crossCollection.");

    for (Replica replica : slice.getReplicas()) {
      if (replica.getNodeName().equals(nodeName)) {
        fromReplica = replica.getStr(ZkStateReader.CORE_NAME_PROP);
        // found local replica, but is it Active?
        if (replica.getState() != Replica.State.ACTIVE)
          throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
              "SolrCloud join: "+fromIndex+" has a local replica ("+fromReplica+
                  ") on "+nodeName+", but it is "+replica.getState());

        break;
      }
    }
  }

  if (fromReplica == null)
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        "SolrCloud join: To join with a collection that might not be co-located, use method=crossCollection.");

  return fromReplica;
}
 
Example 17
Source Project: lucene-solr   Source File: RestoreCoreOp.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void execute(CoreAdminHandler.CallInfo it) throws Exception {
  final SolrParams params = it.req.getParams();
  String cname = params.required().get(CoreAdminParams.CORE);
  String name = params.required().get(NAME);

  ZkController zkController = it.handler.coreContainer.getZkController();
  if (zkController == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Only valid for SolrCloud");
  }

  String repoName = params.get(CoreAdminParams.BACKUP_REPOSITORY);
  BackupRepository repository = it.handler.coreContainer.newBackupRepository(Optional.ofNullable(repoName));

  String location = repository.getBackupLocation(params.get(CoreAdminParams.BACKUP_LOCATION));
  if (location == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'location' is not specified as a query"
        + " parameter or as a default repository property");
  }

  URI locationUri = repository.createURI(location);
  try (SolrCore core = it.handler.coreContainer.getCore(cname)) {
    CloudDescriptor cd = core.getCoreDescriptor().getCloudDescriptor();
    // this core must be the only replica in its shard otherwise
    // we cannot guarantee consistency between replicas because when we add data (or restore index) to this replica
    Slice slice = zkController.getClusterState().getCollection(cd.getCollectionName()).getSlice(cd.getShardId());
    if (slice.getReplicas().size() != 1) {
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
          "Failed to restore core=" + core.getName() + ", the core must be the only replica in its shard");
    }
    RestoreCore restoreCore = new RestoreCore(repository, core, locationUri, name);
    boolean success = restoreCore.doRestore();
    if (!success) {
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Failed to restore core=" + core.getName());
    }
    // other replicas to-be-created will know that they are out of date by
    // looking at their term : 0 compare to term of this core : 1
    zkController.getShardTerms(cd.getCollectionName(), cd.getShardId()).ensureHighestTermsAreNotZero();
  }
}
 
Example 18
Source Project: lucene-solr   Source File: AdminHandlersProxy.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Makes a remote request and returns a future and the solr client. The caller is responsible for closing the client 
 */
public static Pair<Future<NamedList<Object>>, SolrClient> callRemoteNode(String nodeName, String endpoint, 
                                                                         SolrParams params, ZkController zkController) 
    throws IOException, SolrServerException {
  log.debug("Proxying {} request to node {}", endpoint, nodeName);
  URL baseUrl = new URL(zkController.zkStateReader.getBaseUrlForNodeName(nodeName));
  HttpSolrClient solr = new HttpSolrClient.Builder(baseUrl.toString()).build();
  @SuppressWarnings({"rawtypes"})
  SolrRequest proxyReq = new GenericSolrRequest(SolrRequest.METHOD.GET, endpoint, params);
  HttpSolrClient.HttpUriRequestResponse proxyResp = solr.httpUriRequest(proxyReq);
  return new Pair<>(proxyResp.future, solr);
}
 
Example 19
Source Project: lucene-solr   Source File: CoreAdminHandler.java    License: Apache License 2.0 5 votes vote down vote up
protected static Map<String, String> buildCoreParams(SolrParams params) {

    Map<String, String> coreParams = new HashMap<>();

    // standard core create parameters
    for (Map.Entry<String, String> entry : paramToProp.entrySet()) {
      String value = params.get(entry.getKey(), null);
      if (StringUtils.isNotEmpty(value)) {
        coreParams.put(entry.getValue(), value);
      }
    }

    // extra properties
    Iterator<String> paramsIt = params.getParameterNamesIterator();
    while (paramsIt.hasNext()) {
      String param = paramsIt.next();
      if (param.startsWith(CoreAdminParams.PROPERTY_PREFIX)) {
        String propName = param.substring(CoreAdminParams.PROPERTY_PREFIX.length());
        String propValue = params.get(param);
        coreParams.put(propName, propValue);
      }
      if (param.startsWith(ZkController.COLLECTION_PARAM_PREFIX)) {
        coreParams.put(param, params.get(param));
      }
    }

    return coreParams;
  }
 
Example 20
Source Project: lucene-solr   Source File: CdcrUpdateLogSynchronizer.java    License: Apache License 2.0 5 votes vote down vote up
private String getLeaderUrl() {
  ZkController zkController = core.getCoreContainer().getZkController();
  ClusterState cstate = zkController.getClusterState();
  DocCollection docCollection = cstate.getCollection(collection);
  ZkNodeProps leaderProps = docCollection.getLeader(shardId);
  if (leaderProps == null) { // we might not have a leader yet, returns null
    return null;
  }
  ZkCoreNodeProps nodeProps = new ZkCoreNodeProps(leaderProps);
  return nodeProps.getCoreUrl();
}
 
Example 21
Source Project: lucene-solr   Source File: IndexFetcher.java    License: Apache License 2.0 5 votes vote down vote up
private Replica getLeaderReplica() throws InterruptedException {
  ZkController zkController = solrCore.getCoreContainer().getZkController();
  CloudDescriptor cd = solrCore.getCoreDescriptor().getCloudDescriptor();
  Replica leaderReplica = zkController.getZkStateReader().getLeaderRetry(
      cd.getCollectionName(), cd.getShardId());
  return leaderReplica;
}
 
Example 22
Source Project: lucene-solr   Source File: SearchHandler.java    License: 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 23
Source Project: lucene-solr   Source File: RoutedAlias.java    License: 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 24
Source Project: lucene-solr   Source File: SolrLogLayout.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
private Map<String, Object> getReplicaProps(ZkController zkController, SolrCore core) {
  final String collectionName = core.getCoreDescriptor().getCloudDescriptor().getCollectionName();
  DocCollection collection = zkController.getClusterState().getCollectionOrNull(collectionName);
  Replica replica = collection.getReplica(zkController.getCoreNodeName(core.getCoreDescriptor()));
  if (replica != null) {
    return replica.getProperties();
  }
  return Collections.EMPTY_MAP;
}
 
Example 25
Source Project: lucene-solr   Source File: HdfsDirectoryFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String getDataHome(CoreDescriptor cd) throws IOException {
  if (hdfsDataDir == null) {
    throw new SolrException(ErrorCode.SERVER_ERROR, "You must set the "
        + this.getClass().getSimpleName() + " param " + HDFS_HOME
        + " for relative dataDir paths to work");
  }
  
  // by default, we go off the instance directory
  String path;
  if (cd.getCloudDescriptor() != null) {
    path = URLEncoder.encode(cd.getCloudDescriptor().getCollectionName(),
        "UTF-8")
        + "/"
        + URLEncoder.encode(cd.getCloudDescriptor().getCoreNodeName(),
            "UTF-8");
  } else {
    path = cd.getName();
  }
  
  return normalize(SolrPaths.normalizeDir(ZkController
      .trimLeadingAndTrailingSlashes(hdfsDataDir)
      + "/"
      + path
      + "/"
      + cd.getDataDir()));
}
 
Example 26
Source Project: lucene-solr   Source File: CoreDescriptor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a new CoreDescriptor.
 * @param name            the CoreDescriptor's name
 * @param instanceDir     a Path resolving to the instanceDir. Must be absolute.
 * @param coreProps       a Map of the properties for this core
 * @param containerProperties the properties from the enclosing container.
 * @param zkController    the ZkController in SolrCloud mode, otherwise null.
 */
public CoreDescriptor(String name, Path instanceDir, Map<String, String> coreProps,
                      Properties containerProperties, ZkController zkController) {
  this.instanceDir = instanceDir;
  assert instanceDir.isAbsolute();

  originalCoreProperties.setProperty(CORE_NAME, name);

  name = PropertiesUtil.substituteProperty(checkPropertyIsNotEmpty(name, CORE_NAME),
                                           containerProperties);

  coreProperties.putAll(defaultProperties);
  coreProperties.put(CORE_NAME, name);

  for (Map.Entry<String, String> entry : coreProps.entrySet()) {
    String propname = entry.getKey();
    String propvalue = entry.getValue();

    if (isUserDefinedProperty(propname))
      originalExtraProperties.put(propname, propvalue);
    else
      originalCoreProperties.put(propname, propvalue);

    if (!requiredProperties.contains(propname))   // Required props are already dealt with
      coreProperties.setProperty(propname,
          PropertiesUtil.substituteProperty(propvalue, containerProperties));
  }

  loadExtraProperties();
  buildSubstitutableProperties();

  // TODO maybe make this a CloudCoreDescriptor subclass?
  if (zkController != null) {
    cloudDesc = new CloudDescriptor(this, name, coreProperties);
  } else {
    cloudDesc = null;
  }
  log.debug("Created CoreDescriptor: {}", coreProperties);
}
 
Example 27
Source Project: lucene-solr   Source File: ConfigSetService.java    License: Apache License 2.0 5 votes vote down vote up
public static ConfigSetService createConfigSetService(NodeConfig nodeConfig, SolrResourceLoader loader, ZkController zkController) {
  if (zkController == null) {
    return new Standalone(loader, nodeConfig.hasSchemaCache(), nodeConfig.getConfigSetBaseDirectory());
  } else {
    return new CloudConfigSetService(loader, nodeConfig.hasSchemaCache(), zkController);
  }
}
 
Example 28
@Test
public void testSpecificConfigsets() throws Exception {
  CollectionAdminRequest.createCollection("withconfigset2", "conf2", 1, 1).process(cluster.getSolrClient());
  byte[] data = zkClient().getData(ZkStateReader.COLLECTIONS_ZKNODE + "/" + "withconfigset2", null, null, true);
  assertNotNull(data);
  ZkNodeProps props = ZkNodeProps.load(data);
  String configName = props.getStr(ZkController.CONFIGNAME_PROP);
  assertEquals("conf2", configName);
}
 
Example 29
Source Project: lucene-solr   Source File: TestManagedSchemaThreadSafety.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@LogLevel("org.apache.solr.common.cloud.SolrZkClient=debug")
public void testThreadSafety() throws Exception {

  final String configsetName = "managed-config";//

  try (SolrZkClient client = new SuspendingZkClient(zkServer.getZkHost(), 30000)) {
    // we can pick any to load configs, I suppose, but here we check
    client.upConfig(configset("cloud-managed-upgrade"), configsetName);
  }

  ExecutorService executor = ExecutorUtil.newMDCAwareCachedThreadPool("threadpool");
  
  try (SolrZkClient raceJudge = new SuspendingZkClient(zkServer.getZkHost(), 30000)) {

    ZkController zkController = createZkController(raceJudge);

    List<Future<?>> futures = new ArrayList<>();
    for (int i = 0; i < 2; i++) {
      futures.add(executor.submit(indexSchemaLoader(configsetName, zkController)));
    }

    for (Future<?> future : futures) {
      future.get();
    }
  }
  finally {
    ExecutorUtil.shutdownAndAwaitTermination(executor);
  }
}
 
Example 30
Source Project: lucene-solr   Source File: TestManagedSchemaThreadSafety.java    License: Apache License 2.0 5 votes vote down vote up
private ZkController createZkController(SolrZkClient client) throws KeeperException, InterruptedException {
  assumeWorkingMockito();
  
  CoreContainer mockAlwaysUpCoreContainer = mock(CoreContainer.class, 
      Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));
  when(mockAlwaysUpCoreContainer.isShutDown()).thenReturn(Boolean.FALSE);  // Allow retry on session expiry
  
  
  ZkController zkController = mock(ZkController.class,
      Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));

  when(zkController.getCoreContainer()).thenReturn(mockAlwaysUpCoreContainer);

  when(zkController.getZkClient()).thenReturn(client);
  Mockito.doAnswer(new Answer<Boolean>() {
    volatile boolean sessionExpired=false;
    
    @Override
    public Boolean answer(InvocationOnMock invocation) throws Throwable {
      String path = (String) invocation.getArguments()[0];
      perhapsExpired();
      Boolean exists = client.exists(path, true);
      perhapsExpired();
      return exists;
    }

    private void perhapsExpired() throws SessionExpiredException {
      if (!sessionExpired && rarely()) {
        sessionExpired = true;
        throw new KeeperException.SessionExpiredException();
      }
    }
  }).when(zkController).pathExists(Mockito.anyString());
  return zkController;
}