org.apache.solr.cloud.ZkController Java Examples

The following examples show how to use org.apache.solr.cloud.ZkController. 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: HttpShardHandlerFactory.java    From lucene-solr with 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 #2
Source File: SolrConfigHandler.java    From lucene-solr with 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 #3
Source File: TestManagedSchemaThreadSafety.java    From lucene-solr with 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 #4
Source File: OverseerCollectionMessageHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * 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 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 #6
Source File: DirectUpdateHandler2.java    From lucene-solr with 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 #7
Source File: IndexFetcher.java    From lucene-solr with 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 #8
Source File: ZooKeeperDownloader.java    From kite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns config value given collection name
 * Borrowed heavily from Solr's ZKController.
 */
public String readConfigName(SolrZkClient zkClient, String collection)
throws KeeperException, InterruptedException {
  if (collection == null) {
    throw new IllegalArgumentException("collection must not be null");
  }
  String configName = null;

  // first check for alias
  byte[] aliasData = zkClient.getData(ZkStateReader.ALIASES, null, null, true);
  Aliases aliases = ClusterState.load(aliasData);
  String alias = aliases.getCollectionAlias(collection);
  if (alias != null) {
    List<String> aliasList = StrUtils.splitSmart(alias, ",", true);
    if (aliasList.size() > 1) {
      throw new IllegalArgumentException("collection cannot be an alias that maps to multiple collections");
    }
    collection = aliasList.get(0);
  }
  
  String path = ZkStateReader.COLLECTIONS_ZKNODE + "/" + collection;
  if (LOG.isInfoEnabled()) {
    LOG.info("Load collection config from:" + path);
  }
  byte[] data = zkClient.getData(path, null, null, true);
  
  if(data != null) {
    ZkNodeProps props = ZkNodeProps.load(data);
    configName = props.getStr(ZkController.CONFIGNAME_PROP);
  }
  
  if (configName != null && !zkClient.exists(ZkConfigManager.CONFIGS_ZKNODE + "/" + configName, true)) {
    LOG.error("Specified config does not exist in ZooKeeper:" + configName);
    throw new IllegalArgumentException("Specified config does not exist in ZooKeeper:"
      + configName);
  }

  return configName;
}
 
Example #9
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 #10
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 #11
Source File: SolrLogLayout.java    From lucene-solr with 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 #12
Source File: HdfsDirectoryFactory.java    From lucene-solr with 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 #13
Source File: CoreDescriptor.java    From lucene-solr with 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 #14
Source File: ConfigSetService.java    From lucene-solr with 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 #15
Source File: CollectionsAPIDistributedZkTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@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 #16
Source File: TestManagedSchemaThreadSafety.java    From lucene-solr with 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 #17
Source File: TestManagedSchemaThreadSafety.java    From lucene-solr with 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;
}
 
Example #18
Source File: CoreSorterTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private CoreDescriptor newCoreDescriptor(Replica r) {
  @SuppressWarnings({"unchecked"})
  Map<String,String> props = map(
      CoreDescriptor.CORE_SHARD, r.getSlice(),
      CoreDescriptor.CORE_COLLECTION, r.getCollection(),
      CoreDescriptor.CORE_NODE_NAME, r.getNodeName()
  );
  return new CoreDescriptor(r.getCoreName(), TEST_PATH(), props , null, mock(ZkController.class));
}
 
Example #19
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 #20
Source File: ZooKeeperInspector.java    From examples with Apache License 2.0 5 votes vote down vote up
/**
 * Returns config value given collection name Borrowed heavily from Solr's ZKController.
 */
public String readConfigName(SolrZkClient zkClient, String collection) throws KeeperException,
    InterruptedException {
  if (collection == null) {
    throw new IllegalArgumentException("collection must not be null");
  }
  String configName = null;

  // first check for alias
  collection = checkForAlias(zkClient, collection);

  String path = ZkStateReader.COLLECTIONS_ZKNODE + "/" + collection;
  if (LOG.isInfoEnabled()) {
    LOG.info("Load collection config from:" + path);
  }
  byte[] data = zkClient.getData(path, null, null, true);

  if (data != null) {
    ZkNodeProps props = ZkNodeProps.load(data);
    configName = props.getStr(ZkController.CONFIGNAME_PROP);
  }

  if (configName != null
      && !zkClient.exists(ZkController.CONFIGS_ZKNODE + "/" + configName, true)) {
    LOG.error("Specified config does not exist in ZooKeeper:" + configName);
    throw new IllegalArgumentException("Specified config does not exist in ZooKeeper:"
        + configName);
  }

  return configName;
}
 
Example #21
Source File: SolrConfigLoader.java    From hbase-indexer with Apache License 2.0 5 votes vote down vote up
private String getCollectionConfigPath(String collectionName) {
    try {
        byte[] data = zk.getData("/collections/" + collectionName, false, null);
        ObjectMapper objectMapper = new ObjectMapper();
        JsonParser jsonParser = objectMapper.getJsonFactory().createJsonParser(data);
        JsonNode collectionNode = objectMapper.readTree(jsonParser);
        return ZkConfigManager.CONFIGS_ZKNODE + "/" + collectionNode.get(ZkController.CONFIGNAME_PROP).getValueAsText();
    } catch (Exception e) {
        // TODO Better exception handling here
        throw new RuntimeException(e);
    }
}
 
Example #22
Source File: ScoreJoinQParserPlugin.java    From lucene-solr with 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 #23
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 #24
Source File: RoutedAliasUpdateProcessor.java    From lucene-solr with 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 #25
Source File: AtomicUpdateDocumentMerger.java    From lucene-solr with 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 #26
Source File: PeerSync.java    From lucene-solr with 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 #27
Source File: PeerSyncWithLeader.java    From lucene-solr with 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 #28
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 #29
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 #30
Source File: ScoreJoinQParserPlugin.java    From lucene-solr with 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);
  }
}