Java Code Examples for org.apache.solr.core.SolrCore#close()

The following examples show how to use org.apache.solr.core.SolrCore#close() . 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: TestPullReplica.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts that Update logs don't exist for replicas of type {@link org.apache.solr.common.cloud.Replica.Type#PULL}
 */
private void assertUlogPresence(DocCollection collection) {
  for (Slice s:collection.getSlices()) {
    for (Replica r:s.getReplicas()) {
      if (r.getType() == Replica.Type.NRT) {
        continue;
      }
      SolrCore core = null;
      try {
        core = cluster.getReplicaJetty(r).getCoreContainer().getCore(r.getCoreName());
        assertNotNull(core);
        assertFalse("Update log should not exist for replicas of type Passive but file is present: " + core.getUlogDir(),
            new java.io.File(core.getUlogDir()).exists());
      } finally {
        core.close();
      }
    }
  }
}
 
Example 4
Source File: TestTlogReplica.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts that Update logs exist for replicas of type {@link org.apache.solr.common.cloud.Replica.Type#NRT}, but not
 * for replicas of type {@link org.apache.solr.common.cloud.Replica.Type#PULL}
 */
private void assertUlogPresence(DocCollection collection) {
  for (Slice s:collection.getSlices()) {
    for (Replica r:s.getReplicas()) {
      SolrCore core = null;
      try {
        core = cluster.getReplicaJetty(r).getCoreContainer().getCore(r.getCoreName());
        assertNotNull(core);
        assertTrue("Update log should exist for replicas of type Append",
            new java.io.File(core.getUlogDir()).exists());
      } finally {
        core.close();
      }
    }
  }
}
 
Example 5
Source File: SecureCoreAdminHandler.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
private String getCollectionFromCoreName(String coreName) {
  SolrCore solrCore = null;
  try {
    if (coreName != null && !coreName.equals("")) {
      solrCore = coreContainer.getCore(coreName);
      if (solrCore != null) {
        return solrCore.getCoreDescriptor().getCloudDescriptor().getCollectionName();
      }
    }
  } finally {
    if (solrCore != null) {
      solrCore.close();
    }
  }
  return null;
}
 
Example 6
Source File: TestHarness.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Gets a core that does not have its refcount incremented (i.e. there is no need to
 * close when done).  This is not MT safe in conjunction with reloads!
 */
public SolrCore getCore() {
  // get the core & decrease its refcount:
  // the container holds the core for the harness lifetime
  SolrCore core = container.getCore(coreName);
  if (core != null)
    core.close();
  return core;
}
 
Example 7
Source File: IndexFetcher.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void openNewSearcherAndUpdateCommitPoint() throws IOException {
  RefCounted<SolrIndexSearcher> searcher = null;
  IndexCommit commitPoint;
  // must get the latest solrCore object because the one we have might be closed because of a reload
  // todo stop keeping solrCore around
  SolrCore core = solrCore.getCoreContainer().getCore(solrCore.getName());
  try {
    @SuppressWarnings({"rawtypes"})
    Future[] waitSearcher = new Future[1];
    searcher = core.getSearcher(true, true, waitSearcher, true);
    if (waitSearcher[0] != null) {
      try {
        waitSearcher[0].get();
      } catch (InterruptedException | ExecutionException e) {
        SolrException.log(log, e);
      }
    }
    commitPoint = searcher.get().getIndexReader().getIndexCommit();
  } finally {
    if (searcher != null) {
      searcher.decref();
    }
    core.close();
  }

  // update the commit point in replication handler
  replicationHandler.indexCommitPoint = commitPoint;

}
 
Example 8
Source File: SolrEntityIndexerMixin.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
public void inflateSolrSchema()
{
    server = solr.solrServer();
    SolrCore solrCore = solr.solrCore();
    try
    {
        indexedFields = solrCore.getSchema().getFields();
    }
    finally
    {
        solrCore.close();
    }
}
 
Example 9
Source File: SentryTestBase.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
public static void closeCore(SolrCore coreToClose, CloudDescriptor cloudDescriptor)
    throws Exception {
  if (cloudDescriptor != null) {
    CoreDescriptor coreDescriptor = coreToClose.getCoreDescriptor();
    Field cloudDescField = CoreDescriptor.class.getDeclaredField("cloudDesc");
    cloudDescField.setAccessible(true);
    cloudDescField.set(coreDescriptor, cloudDescriptor);
  }
  coreToClose.close();
}
 
Example 10
Source File: AlfrescoCoreAdminHandler.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Unload an existing Core from SOLR
 *
 * Synchronous execution
 *
 * @param req Query Request including following parameters:
 * - coreName, mandatory, the name of the core to be unloaded
 * - storeRef, mandatory, the storeRef for the SOLR Core (workspace://SpacesStore, archive://SpacesStore)
 *
 * @return Response including the action result:
 * - status: success, when the core has been unloaded
 * - status: error, when the core has NOT been unloaded
 * - errorMessage: message, if action status is "error" an error message node is included
 */
private NamedList<Object> removeCore(SolrQueryRequest req)
{
    String store = "";
    SolrParams params = req.getParams();
    NamedList<Object> response = new SimpleOrderedMap<>();

    if (params.get("storeRef") != null)
    {
        store = params.get("storeRef");
    }

    if ((store == null) || (store.length() == 0))
    {
        response.add(ACTION_STATUS_LABEL, ACTION_STATUS_ERROR);
        response.add(ACTION_ERROR_MESSAGE_LABEL, "Core " + params.get("coreName") + " has NOT been removed as storeRef param is required");
        return response;
    }

    StoreRef storeRef = new StoreRef(store);

    String coreName = ofNullable(coreName(req.getParams())).orElse(storeRef.getProtocol() + "-" + storeRef.getIdentifier());
    SolrCore core = coreContainer.getCore(coreName);

    if (core == null)
    {
        response.add(ACTION_STATUS_LABEL, ACTION_STATUS_ERROR);
        response.add(ACTION_ERROR_MESSAGE_LABEL, "Core " + params.get("coreName") + " has NOT been removed as it doesn't exist");
        return response;
    }

    // Close the references to the core to unload before actually unloading it,
    // otherwise this operation gets into and endless loop
    while (core.getOpenCount() > 1)
    {
        core.close();
    }

    // remove core
    coreContainer.unload(coreName, true, true, true);

    response.add(ACTION_STATUS_LABEL, ACTION_STATUS_SUCCESS);
    return response;

}
 
Example 11
Source File: TestZKPropertiesWriter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressForbidden(reason = "Needs currentTimeMillis to construct date stamps")
@Test
@SuppressWarnings({"unchecked"})
public void testZKPropertiesWriter() throws Exception {
  CollectionAdminRequest.createCollectionWithImplicitRouter("collection1", "conf", "1", 1)
      .setMaxShardsPerNode(1)
      .process(cluster.getSolrClient());

  // DIH talks core, SolrCloud talks collection.
  DocCollection coll = getCollectionState("collection1");
  Replica replica = coll.getReplicas().iterator().next();
  JettySolrRunner jetty = minicluster.getReplicaJetty(replica);
  SolrCore core = jetty.getCoreContainer().getCore(replica.getCoreName());

  localAssertQ("test query on empty index", request(core, "qlkciyopsbgzyvkylsjhchghjrdf"), "//result[@numFound='0']");

  SimpleDateFormat errMsgFormat = new SimpleDateFormat(dateFormat, Locale.ROOT);

  // These two calls are from SolrTestCaseJ4 and end up in TestHarness... That's ok they are static and do not reference
  // the various variables that were not initialized (so not copying them to this test class as some other methods at the bottom).
  delQ("*:*");
  commit();
  SimpleDateFormat df = new SimpleDateFormat(dateFormat, Locale.ROOT);
  Date oneSecondAgo = new Date(System.currentTimeMillis() - 1000);

  Map<String, String> init = new HashMap<>();
  init.put("dateFormat", dateFormat);
  ZKPropertiesWriter spw = new ZKPropertiesWriter();
  spw.init(new DataImporter(core, "dataimport"), init);
  Map<String, Object> props = new HashMap<>();
  props.put("SomeDates.last_index_time", oneSecondAgo);
  props.put("last_index_time", oneSecondAgo);
  spw.persist(props);

  @SuppressWarnings({"rawtypes"})
  List rows = new ArrayList();
  rows.add(AbstractDataImportHandlerTestCase.createMap("id", "1", "year_s", "2013"));
  MockDataSource.setIterator("select " + df.format(oneSecondAgo) + " from dummy", rows.iterator());

  localQuery("/dataimport", localMakeRequest(core, "command", "full-import", "dataConfig",
      generateConfig(), "clean", "true", "commit", "true", "synchronous",
      "true", "indent", "true"));
  props = spw.readIndexerProperties();
  Date entityDate = df.parse((String) props.get("SomeDates.last_index_time"));
  Date docDate = df.parse((String) props.get("last_index_time"));

  Assert.assertTrue("This date: " + errMsgFormat.format(oneSecondAgo) + " should be prior to the document date: " + errMsgFormat.format(docDate), docDate.getTime() - oneSecondAgo.getTime() > 0);
  Assert.assertTrue("This date: " + errMsgFormat.format(oneSecondAgo) + " should be prior to the entity date: " + errMsgFormat.format(entityDate), entityDate.getTime() - oneSecondAgo.getTime() > 0);
  localAssertQ("Should have found 1 doc, year 2013", request(core, "*:*"), "//*[@numFound='1']", "//doc/str[@name=\"year_s\"]=\"2013\"");

  core.close();
}
 
Example 12
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 13
Source File: SplitOp.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 *   This is called when splitByPrefix is used.
 *   The overseer called us to get recommended splits taking into
 *   account actual document distribution over the hash space.
 */
private void handleGetRanges(CoreAdminHandler.CallInfo it, String coreName) throws Exception {

  SolrCore parentCore = it.handler.coreContainer.getCore(coreName);
  if (parentCore == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown core " + coreName);
  }

  RefCounted<SolrIndexSearcher> searcherHolder = parentCore.getRealtimeSearcher();

  try {
    if (!it.handler.coreContainer.isZooKeeperAware()) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Shard splitByPrefix requires SolrCloud mode.");
    } else {
      SolrIndexSearcher searcher = searcherHolder.get();

      String routeFieldName = null;
      String prefixField = "id_prefix";

      ClusterState clusterState = it.handler.coreContainer.getZkController().getClusterState();
      String collectionName = parentCore.getCoreDescriptor().getCloudDescriptor().getCollectionName();
      DocCollection collection = clusterState.getCollection(collectionName);
      String sliceName = parentCore.getCoreDescriptor().getCloudDescriptor().getShardId();
      Slice slice = collection.getSlice(sliceName);
      DocRouter router = collection.getRouter() != null ? collection.getRouter() : DocRouter.DEFAULT;
      DocRouter.Range currentRange = slice.getRange();

      Object routerObj = collection.get(DOC_ROUTER); // for back-compat with Solr 4.4
      if (routerObj instanceof Map) {
        @SuppressWarnings({"rawtypes"})
        Map routerProps = (Map) routerObj;
        routeFieldName = (String) routerProps.get("field");
      }
      if (routeFieldName == null) {
        routeFieldName = searcher.getSchema().getUniqueKeyField().getName();
      }

      Collection<RangeCount> counts = getHashHistogram(searcher, prefixField, router, collection);

      if (counts.size() == 0) {
        // How to determine if we should look at the id field to figure out the prefix buckets?
        // There may legitimately be no indexed terms in id_prefix if no ids have a prefix yet.
        // For now, avoid using splitByPrefix unless you are actually using prefixes.
        counts = getHashHistogramFromId(searcher, searcher.getSchema().getUniqueKeyField().getName(), router, collection);
      }

      Collection<DocRouter.Range> splits = getSplits(counts, currentRange);
      String splitString = toSplitString(splits);

      if (splitString == null) {
        return;
      }

      it.rsp.add(CoreAdminParams.RANGES, splitString);
    }
  } finally {
    if (searcherHolder != null) searcherHolder.decref();
    if (parentCore != null) parentCore.close();
  }
}
 
Example 14
Source File: IndexSizeEstimatorTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testEstimator() throws Exception {
  JettySolrRunner jetty = cluster.getRandomJetty(random());
  String randomCoreName = jetty.getCoreContainer().getAllCoreNames().iterator().next();
  SolrCore core = jetty.getCoreContainer().getCore(randomCoreName);
  RefCounted<SolrIndexSearcher> searcherRef = core.getSearcher();
  try {
    SolrIndexSearcher searcher = searcherRef.get();
    // limit the max length
    IndexSizeEstimator estimator = new IndexSizeEstimator(searcher.getRawReader(), 20, 50, true, true);
    IndexSizeEstimator.Estimate estimate = estimator.estimate();
    Map<String, Long> fieldsBySize = estimate.getFieldsBySize();
    assertFalse("empty fieldsBySize", fieldsBySize.isEmpty());
    assertEquals(fieldsBySize.toString(), fields.size(), fieldsBySize.size());
    fieldsBySize.forEach((k, v) -> assertTrue("unexpected size of " + k + ": " + v, v > 0));
    Map<String, Long> typesBySize = estimate.getTypesBySize();
    assertFalse("empty typesBySize", typesBySize.isEmpty());
    assertTrue("expected at least 8 types: " + typesBySize.toString(), typesBySize.size() >= 8);
    typesBySize.forEach((k, v) -> assertTrue("unexpected size of " + k + ": " + v, v > 0));
    Map<String, Object> summary = estimate.getSummary();
    assertNotNull("summary", summary);
    assertFalse("empty summary", summary.isEmpty());
    assertEquals(summary.keySet().toString(), fields.size(), summary.keySet().size());
    Map<String, Object> details = estimate.getDetails();
    assertNotNull("details", details);
    assertFalse("empty details", details.isEmpty());
    // by type
    assertEquals(details.keySet().toString(), 6, details.keySet().size());

    // check sampling
    estimator.setSamplingThreshold(searcher.getRawReader().maxDoc() / 2);
    IndexSizeEstimator.Estimate sampledEstimate = estimator.estimate();
    Map<String, Long> sampledFieldsBySize = sampledEstimate.getFieldsBySize();
    assertFalse("empty fieldsBySize", sampledFieldsBySize.isEmpty());
    // verify that the sampled values are within 50% of the original values
    fieldsBySize.forEach((field, size) -> {
      Long sampledSize = sampledFieldsBySize.get(field);
      assertNotNull("sampled size for " + field + " is missing in " + sampledFieldsBySize, sampledSize);
      double delta = (double) size * 0.5;
      assertEquals("sampled size of " + field + " is wildly off", (double)size, (double)sampledSize, delta);
    });
    // verify the reader is still usable - SOLR-13694
    IndexReader reader = searcher.getRawReader();
    for (LeafReaderContext context : reader.leaves()) {
      LeafReader leafReader = context.reader();
      assertTrue("unexpected LeafReader class: " + leafReader.getClass().getName(), leafReader instanceof CodecReader);
      Bits liveDocs = leafReader.getLiveDocs();
      CodecReader codecReader = (CodecReader) leafReader;
      StoredFieldsReader storedFieldsReader = codecReader.getFieldsReader();
      StoredFieldVisitor visitor = new DocumentStoredFieldVisitor();
      assertNotNull(storedFieldsReader);
      for (int docId = 0; docId < leafReader.maxDoc(); docId++) {
        if (liveDocs != null && !liveDocs.get(docId)) {
          continue;
        }
        storedFieldsReader.visitDocument(docId, visitor);
      }
    }
  } finally {
    searcherRef.decref();
    core.close();
  }
}