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

The following examples show how to use org.apache.solr.core.CoreContainer#getCore() . 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: ScoreJoinQParserPlugin.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, org.apache.lucene.search.ScoreMode scoreMode, float boost) throws IOException {
  SolrRequestInfo info = SolrRequestInfo.getRequestInfo();

  CoreContainer container = info.getReq().getCore().getCoreContainer();

  final SolrCore fromCore = container.getCore(fromIndex);

  if (fromCore == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Cross-core join: no such core " + fromIndex);
  }
  RefCounted<SolrIndexSearcher> fromHolder = null;
  fromHolder = fromCore.getRegisteredSearcher();
  final Query joinQuery;
  try {
    joinQuery = JoinUtil.createJoinQuery(fromField, true,
        toField, fromQuery, fromHolder.get(), this.scoreMode);
  } finally {
    fromCore.close();
    fromHolder.decref();
  }
  return joinQuery.rewrite(searcher.getIndexReader()).createWeight(searcher, scoreMode, boost);
}
 
Example 2
Source File: DeleteSnapshotOp.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(CoreAdminHandler.CallInfo it) throws Exception {
  final SolrParams params = it.req.getParams();
  String commitName = params.required().get(CoreAdminParams.COMMIT_NAME);
  String cname = params.required().get(CoreAdminParams.CORE);

  CoreContainer cc = it.handler.getCoreContainer();
  SolrCore core = cc.getCore(cname);
  if (core == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unable to locate core " + cname);
  }

  try {
    core.deleteNamedSnapshot(commitName);
    // Ideally we shouldn't need this. This is added since the RPC logic in
    // OverseerCollectionMessageHandler can not provide the coreName as part of the result.
    it.rsp.add(CoreAdminParams.CORE, core.getName());
    it.rsp.add(CoreAdminParams.COMMIT_NAME, commitName);
  } finally {
    core.close();
  }
}
 
Example 3
Source File: TestBulkSchemaAPI.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * whitebox checks the Similarity for the specified field according to {@link SolrCore#getLatestSchema}
 * 
 * Executes each of the specified Similarity-accepting validators.
 */
@SafeVarargs
@SuppressWarnings({"unchecked", "varargs"})
private static <T extends Similarity> void assertFieldSimilarity(String fieldname, Class<T> expected, Consumer<T>... validators) {
  CoreContainer cc = jetty.getCoreContainer();
  try (SolrCore core = cc.getCore("collection1")) {
    SimilarityFactory simfac = core.getLatestSchema().getSimilarityFactory();
    assertNotNull(simfac);
    assertTrue("test only works with SchemaSimilarityFactory",
               simfac instanceof SchemaSimilarityFactory);
    
    Similarity mainSim = core.getLatestSchema().getSimilarity();
    assertNotNull(mainSim);
    
    // sanity check simfac vs sim in use - also verify infom called on simfac, otherwise exception
    assertEquals(mainSim, simfac.getSimilarity());
    
    assertTrue("test only works with PerFieldSimilarityWrapper, SchemaSimilarityFactory redefined?",
               mainSim instanceof PerFieldSimilarityWrapper);
    Similarity fieldSim = ((PerFieldSimilarityWrapper)mainSim).get(fieldname);
    assertEquals("wrong sim for field=" + fieldname, expected, fieldSim.getClass());
    Arrays.asList(validators).forEach(v -> v.accept((T)fieldSim));
  }
}
 
Example 4
Source File: CreateSnapshotOp.java    From lucene-solr with 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 commitName = params.required().get(CoreAdminParams.COMMIT_NAME);
  String cname = params.required().get(CoreAdminParams.CORE);

  CoreContainer cc = it.handler.getCoreContainer();

  try (SolrCore core = cc.getCore(cname)) {
    if (core == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unable to locate core " + cname);
    }

    final String indexDirPath = core.getIndexDir();
    final IndexDeletionPolicyWrapper delPol = core.getDeletionPolicy();
    final IndexCommit ic = delPol.getAndSaveLatestCommit();
    try {
      if (null == ic) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                                "No index commits to snapshot in core " + cname);
      }
      final SolrSnapshotMetaDataManager mgr = core.getSnapshotMetaDataManager();
      mgr.snapshot(commitName, indexDirPath, ic.getGeneration());

      it.rsp.add(CoreAdminParams.CORE, core.getName());
      it.rsp.add(CoreAdminParams.COMMIT_NAME, commitName);
      it.rsp.add(SolrSnapshotManager.INDEX_DIR_PATH, indexDirPath);
      it.rsp.add(SolrSnapshotManager.GENERATION_NUM, ic.getGeneration());
      it.rsp.add(SolrSnapshotManager.FILE_LIST, ic.getFileNames());
    } finally {
      delPol.releaseCommitPoint(ic);
    }
  }
}
 
Example 5
Source File: CoreMergeIndexesAdminHandlerTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testMergeIndexesCoreAdminHandler() throws Exception {
  final File workDir = createTempDir().toFile();

  final CoreContainer cores = h.getCoreContainer();
  cores.getAllowPaths().add(workDir.toPath());

  try (final CoreAdminHandler admin = new CoreAdminHandler(cores);
       SolrCore core = cores.getCore("collection1")) {
    DirectoryFactory df = core.getDirectoryFactory();
    FailingDirectoryFactory dirFactory = (FailingDirectoryFactory) df;

    try {
      dirFactory.fail = true;
      ignoreException(WRAPPED_FAILING_MSG);
      SolrException e = expectThrows(SolrException.class, () -> {
        admin.handleRequestBody
            (req(CoreAdminParams.ACTION,
                CoreAdminParams.CoreAdminAction.MERGEINDEXES.toString(),
                CoreAdminParams.CORE, "collection1",
                CoreAdminParams.INDEX_DIR, workDir.getAbsolutePath()),
                new SolrQueryResponse());
      });
      assertEquals(FailingDirectoryFactory.FailingDirectoryFactoryException.class, e.getCause().getClass());
    } finally {
      unIgnoreException(WRAPPED_FAILING_MSG);
    }
    dirFactory.fail = false;
  }
}
 
Example 6
Source File: ConfigSetsAPITest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({"unchecked"})
public void testSharedSchema() throws Exception {
  CollectionAdminRequest.createCollection("col1", "cShare", 1, 1)
      .processAndWait(cluster.getSolrClient(), DEFAULT_TIMEOUT);
  CollectionAdminRequest.createCollection("col2", "cShare", 1, 1)
      .processAndWait(cluster.getSolrClient(), DEFAULT_TIMEOUT);
  CollectionAdminRequest.createCollection("col3", "conf1", 1, 1)
      .processAndWait(cluster.getSolrClient(), DEFAULT_TIMEOUT);

  CoreContainer coreContainer = cluster.getJettySolrRunner(0).getCoreContainer();

  try (SolrCore coreCol1 = coreContainer.getCore("col1_shard1_replica_n1");
       SolrCore coreCol2 = coreContainer.getCore("col2_shard1_replica_n1");
       SolrCore coreCol3 = coreContainer.getCore("col3_shard1_replica_n1")) {
    assertSame(coreCol1.getLatestSchema(), coreCol2.getLatestSchema());
    assertNotSame(coreCol1.getLatestSchema(), coreCol3.getLatestSchema());
  }

  // change col1's configSet
  CollectionAdminRequest.modifyCollection("col1",
    map("collection.configName", "conf1")  // from cShare
  ).processAndWait(cluster.getSolrClient(), DEFAULT_TIMEOUT);

  try (SolrCore coreCol1 = coreContainer.getCore("col1_shard1_replica_n1");
       SolrCore coreCol2 = coreContainer.getCore("col2_shard1_replica_n1")) {
    assertNotSame(coreCol1.getLatestSchema(), coreCol2.getLatestSchema());
  }

}
 
Example 7
Source File: ChangedSchemaMergeTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testOptimizeDiffSchemas() throws Exception {
  // load up a core (why not put it on disk?)
  CoreContainer cc = init();
  try (SolrCore changed = cc.getCore("changed")) {

    assertSimilarity(changed, simfac1);
                     
    // add some documents
    addDoc(changed, "id", "1", "which", "15", "text", "some stuff with which");
    addDoc(changed, "id", "2", "which", "15", "text", "some stuff with which");
    addDoc(changed, "id", "3", "which", "15", "text", "some stuff with which");
    addDoc(changed, "id", "4", "which", "15", "text", "some stuff with which");
    SolrQueryRequest req = new LocalSolrQueryRequest(changed, new NamedList<>());
    changed.getUpdateHandler().commit(new CommitUpdateCommand(req, false));

    // write the new schema out and make it current
    FileUtils.writeStringToFile(schemaFile, withoutWhich, StandardCharsets.UTF_8);

    IndexSchema iSchema = IndexSchemaFactory.buildIndexSchema("schema.xml", changed.getSolrConfig());
    changed.setLatestSchema(iSchema);
    
    assertSimilarity(changed, simfac2);
    // sanity check our sanity check
    assertFalse("test is broken: both simfacs are the same", simfac1.equals(simfac2)); 

    addDoc(changed, "id", "1", "text", "some stuff without which");
    addDoc(changed, "id", "5", "text", "some stuff without which");

    changed.getUpdateHandler().commit(new CommitUpdateCommand(req, false));
    changed.getUpdateHandler().commit(new CommitUpdateCommand(req, true));
  } catch (Throwable e) {
    log.error("Test exception, logging so not swallowed if there is a (finally) shutdown exception: {}"
        , e.getMessage(), e);
    throw e;
  } finally {
    if (cc != null) cc.shutdown();
  }
}
 
Example 8
Source File: JoinQParserPlugin.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
JoinParams parseJoin(QParser qparser) throws SyntaxError {
  final String fromField = qparser.getParam("from");
  final String fromIndex = qparser.getParam("fromIndex");
  final String toField = qparser.getParam("to");
  final String v = qparser.localParams.get(QueryParsing.V);
  final String coreName;

  Query fromQuery;
  long fromCoreOpenTime = 0;

  if (fromIndex != null && !fromIndex.equals(qparser.req.getCore().getCoreDescriptor().getName()) ) {
    CoreContainer container = qparser.req.getCore().getCoreContainer();

    // if in SolrCloud mode, fromIndex should be the name of a single-sharded collection
    coreName = ScoreJoinQParserPlugin.getCoreName(fromIndex, container);

    final SolrCore fromCore = container.getCore(coreName);
    if (fromCore == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          "Cross-core join: no such core " + coreName);
    }

    RefCounted<SolrIndexSearcher> fromHolder = null;
    LocalSolrQueryRequest otherReq = new LocalSolrQueryRequest(fromCore, qparser.params);
    try {
      QParser parser = QParser.getParser(v, otherReq);
      fromQuery = parser.getQuery();
      fromHolder = fromCore.getRegisteredSearcher();
      if (fromHolder != null) fromCoreOpenTime = fromHolder.get().getOpenNanoTime();
    } finally {
      otherReq.close();
      fromCore.close();
      if (fromHolder != null) fromHolder.decref();
    }
  } else {
    coreName = null;
    QParser fromQueryParser = qparser.subQuery(v, null);
    fromQueryParser.setIsFilter(true);
    fromQuery = fromQueryParser.getQuery();
  }

  final String indexToUse = coreName == null ? fromIndex : coreName;
  return new JoinParams(fromField, indexToUse, fromQuery, fromCoreOpenTime, toField);
}
 
Example 9
Source File: JoinQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public JoinQueryWeight(SolrIndexSearcher searcher, ScoreMode scoreMode, float boost) {
  super(JoinQuery.this, boost);
  this.scoreMode = scoreMode;
  this.fromSearcher = searcher;
  SolrRequestInfo info = SolrRequestInfo.getRequestInfo();
  if (info != null) {
    rb = info.getResponseBuilder();
  }

  if (fromIndex == null) {
    this.fromSearcher = searcher;
  } else {
    if (info == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Cross-core join must have SolrRequestInfo");
    }

    CoreContainer container = searcher.getCore().getCoreContainer();
    final SolrCore fromCore = container.getCore(fromIndex);

    if (fromCore == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Cross-core join: no such core " + fromIndex);
    }

    if (info.getReq().getCore() == fromCore) {
      // if this is the same core, use the searcher passed in... otherwise we could be warming and
      // get an older searcher from the core.
      fromSearcher = searcher;
    } else {
      // This could block if there is a static warming query with a join in it, and if useColdSearcher is true.
      // Deadlock could result if two cores both had useColdSearcher and had joins that used eachother.
      // This would be very predictable though (should happen every time if misconfigured)
      fromRef = fromCore.getSearcher(false, true, null);

      // be careful not to do anything with this searcher that requires the thread local
      // SolrRequestInfo in a manner that requires the core in the request to match
      fromSearcher = fromRef.get();
    }

    if (fromRef != null) {
      final RefCounted<SolrIndexSearcher> ref = fromRef;
      info.addCloseHook(new Closeable() {
        @Override
        public void close() {
          ref.decref();
        }
      });
    }

    info.addCloseHook(new Closeable() {
      @Override
      public void close() {
        fromCore.close();
      }
    });

  }
  this.toSearcher = searcher;
}
 
Example 10
Source File: HttpPartitionTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected void testRf2() throws Exception {
  // create a collection that has 1 shard but 2 replicas
  String testCollectionName = "c8n_1x2";
  createCollectionRetry(testCollectionName, "conf1", 1, 2, 1);
  cloudClient.setDefaultCollection(testCollectionName);
  
  sendDoc(1);
  
  Replica notLeader = 
      ensureAllReplicasAreActive(testCollectionName, "shard1", 1, 2, maxWaitSecsToSeeAllActive).get(0);
  JettySolrRunner leaderJetty = getJettyOnPort(getReplicaPort(getShardLeader(testCollectionName, "shard1", 1000)));

  // ok, now introduce a network partition between the leader and the replica
  SocketProxy proxy = getProxyForReplica(notLeader);
  SocketProxy leaderProxy = getProxyForReplica(getShardLeader(testCollectionName, "shard1", 1000));
  
  proxy.close();
  leaderProxy.close();

  // indexing during a partition
  sendDoc(2, null, leaderJetty);
  // replica should publish itself as DOWN if the network is not healed after some amount time
  waitForState(testCollectionName, notLeader.getName(), DOWN, 10000);
  
  proxy.reopen();
  leaderProxy.reopen();
  
  List<Replica> notLeaders = 
      ensureAllReplicasAreActive(testCollectionName, "shard1", 1, 2, maxWaitSecsToSeeAllActive);
  
  int achievedRf = sendDoc(3);
  if (achievedRf == 1) {
    // this case can happen when leader reuse an connection get established before network partition
    // TODO: Remove when SOLR-11776 get committed
    ensureAllReplicasAreActive(testCollectionName, "shard1", 1, 2, maxWaitSecsToSeeAllActive);
  }
  
  // sent 3 docs in so far, verify they are on the leader and replica
  assertDocsExistInAllReplicas(notLeaders, testCollectionName, 1, 3);

  // Get the max version from the replica core to make sure it gets updated after recovery (see SOLR-7625)
  JettySolrRunner replicaJetty = getJettyOnPort(getReplicaPort(notLeader));
  CoreContainer coreContainer = replicaJetty.getCoreContainer();
  ZkCoreNodeProps replicaCoreNodeProps = new ZkCoreNodeProps(notLeader);
  String coreName = replicaCoreNodeProps.getCoreName();
  Long maxVersionBefore = null;
  try (SolrCore core = coreContainer.getCore(coreName)) {
    assertNotNull("Core '"+coreName+"' not found for replica: "+notLeader.getName(), core);
    UpdateLog ulog = core.getUpdateHandler().getUpdateLog();
    maxVersionBefore = ulog.getCurrentMaxVersion();
  }
  assertNotNull("max version bucket seed not set for core " + coreName, maxVersionBefore);
  log.info("Looked up max version bucket seed {} for core {}", maxVersionBefore, coreName);

  // now up the stakes and do more docs
  int numDocs = TEST_NIGHTLY ? 1000 : 105;
  boolean hasPartition = false;
  for (int d = 0; d < numDocs; d++) {
    // create / restore partition every 100 docs
    if (d % 10 == 0) {
      if (hasPartition) {
        proxy.reopen();
        leaderProxy.reopen();
        hasPartition = false;
      } else {
        if (d >= 10) {
          proxy.close();
          leaderProxy.close();
          hasPartition = true;
          Thread.sleep(sleepMsBeforeHealPartition);
        }
      }
    }
    // always send doc directly to leader without going through proxy
    sendDoc(d + 4, null, leaderJetty); // 4 is offset as we've already indexed 1-3
  }
  
  // restore connectivity if lost
  if (hasPartition) {
    proxy.reopen();
    leaderProxy.reopen();
  }
  
  notLeaders = ensureAllReplicasAreActive(testCollectionName, "shard1", 1, 2, maxWaitSecsToSeeAllActive);

  try (SolrCore core = coreContainer.getCore(coreName)) {
    assertNotNull("Core '" + coreName + "' not found for replica: " + notLeader.getName(), core);
    Long currentMaxVersion = core.getUpdateHandler().getUpdateLog().getCurrentMaxVersion();
    log.info("After recovery, looked up NEW max version bucket seed {} for core {}, was: {}"
        , currentMaxVersion, coreName, maxVersionBefore);
    assertTrue("max version bucket seed not updated after recovery!", currentMaxVersion > maxVersionBefore);
  }

  // verify all docs received
  assertDocsExistInAllReplicas(notLeaders, testCollectionName, 1, numDocs + 3);

  log.info("testRf2 succeeded ... deleting the {} collection", testCollectionName);

  // try to clean up
  attemptCollectionDelete(cloudClient, testCollectionName);
}