org.apache.solr.util.RefCounted Java Examples

The following examples show how to use org.apache.solr.util.RefCounted. 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: FacetTreeGenerator.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
/**
 * Get a reference to the searcher for the required collection. If the collection is
 * not the same as the search collection, we assume it is under the same Solr instance.
 * @param rb the response builder holding the facets.
 * @return a counted reference to the searcher.
 * @throws SolrException if the collection cannot be found.
 */
private RefCounted<SolrIndexSearcher> getSearcherReference(ResponseBuilder rb) throws SolrException {
	RefCounted<SolrIndexSearcher> searcherRef;
	
	SolrCore currentCore = rb.req.getCore();
	if (StringUtils.isBlank(collection)) {
		searcherRef = currentCore.getSearcher();
	} else {
		// Using an alternative core - find it
		SolrCore reqCore = currentCore.getCoreDescriptor().getCoreContainer().getCore(collection);
		if (reqCore == null) {
			throw new SolrException(ErrorCode.BAD_REQUEST, "Collection \"" + collection
					+ "\" cannot be found");
		}
		searcherRef = reqCore.getSearcher();
	}
	
	return searcherRef;
}
 
Example #2
Source File: DefaultSolrCoreState.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void initRefCntWriter() {
  // TODO: since we moved to a read-write lock, and don't rely on the count to close the writer, we don't really
  // need this class any more.  It could also be a singleton created at the same time as SolrCoreState
  // or we could change the API of SolrCoreState to just return the writer and then add a releaseWriter() call.
  if (refCntWriter == null && indexWriter != null) {
    refCntWriter = new RefCounted<IndexWriter>(indexWriter) {

      @Override
      public void decref() {
        iwLock.readLock().unlock();
        super.decref();  // This is now redundant (since we switched to read-write locks), we don't really need to maintain our own reference count.
      }

      @Override
      public void close() {
        //  We rely on other code to actually close the IndexWriter, and there's nothing special to do when the ref count hits 0
      }
    };
  }
}
 
Example #3
Source File: SegmentsInfoRequestHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private SimpleOrderedMap<Object> getMergeInformation(SolrQueryRequest req, SegmentInfos infos, List<String> mergeCandidates) throws IOException {
  SimpleOrderedMap<Object> result = new SimpleOrderedMap<>();
  RefCounted<IndexWriter> refCounted = req.getCore().getSolrCoreState().getIndexWriter(req.getCore());
  try {
    IndexWriter indexWriter = refCounted.get();
    if (indexWriter instanceof SolrIndexWriter) {
      result.addAll(((SolrIndexWriter)indexWriter).getRunningMerges());
    }
    //get chosen merge policy
    MergePolicy mp = indexWriter.getConfig().getMergePolicy();
    //Find merges
    MergeSpecification findMerges = mp.findMerges(MergeTrigger.EXPLICIT, infos, indexWriter);
    if (findMerges != null && findMerges.merges != null && findMerges.merges.size() > 0) {
      for (OneMerge merge : findMerges.merges) {
        //TODO: add merge grouping
        for (SegmentCommitInfo mergeSegmentInfo : merge.segments) {
          mergeCandidates.add(mergeSegmentInfo.info.name);
        }
      }
    }

    return result;
  } finally {
    refCounted.decref();
  }
}
 
Example #4
Source File: UpdateLog.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Opens a new realtime searcher and clears the id caches.
 * This may also be called when we updates are being buffered (from PeerSync/IndexFingerprint)
 */
public void openRealtimeSearcher() {
  synchronized (this) {
    // We must cause a new IndexReader to be opened before anything looks at these caches again
    // so that a cache miss will read fresh data.
    try {
      RefCounted<SolrIndexSearcher> holder = uhandler.core.openNewSearcher(true, true);
      holder.decref();
    } catch (Exception e) {
      SolrException.log(log, "Error opening realtime searcher", e);
      return;
    }

    if (map != null) map.clear();
    if (prevMap != null) prevMap.clear();
    if (prevMap2 != null) prevMap2.clear();
  }
}
 
Example #5
Source File: UpdateLog.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** currently for testing only */
public void deleteAll() {
  synchronized (this) {

    try {
      RefCounted<SolrIndexSearcher> holder = uhandler.core.openNewSearcher(true, true);
      holder.decref();
    } catch (Exception e) {
      SolrException.log(log, "Error opening realtime searcher for deleteByQuery", e);
    }

    if (map != null) map.clear();
    if (prevMap != null) prevMap.clear();
    if (prevMap2 != null) prevMap2.clear();

    oldDeletes.clear();
    deleteByQueries.clear();
  }
}
 
Example #6
Source File: IndexFingerprint.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Opens a new realtime searcher and returns it's (possibly cached) fingerprint */
public static IndexFingerprint getFingerprint(SolrCore core, long maxVersion) throws IOException {
  RTimer timer = new RTimer();
  core.getUpdateHandler().getUpdateLog().openRealtimeSearcher();
  RefCounted<SolrIndexSearcher> newestSearcher = core.getUpdateHandler().getUpdateLog().uhandler.core.getRealtimeSearcher();
  try {
    IndexFingerprint f = newestSearcher.get().getIndexFingerprint(maxVersion);
    final double duration = timer.stop();
    log.info("IndexFingerprint millis:{} result:{}",duration, f);
    return f;
  } finally {
    if (newestSearcher != null) {
      newestSearcher.decref();
    }
  }
}
 
Example #7
Source File: DirectUpdateHandler2.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void allowDuplicateUpdate(AddUpdateCommand cmd) throws IOException {
  RefCounted<IndexWriter> iw = solrCoreState.getIndexWriter(core);
  try {
    IndexWriter writer = iw.get();
    Iterable<Document> nestedDocs = cmd.getLuceneDocsIfNested();
    if (nestedDocs != null) {
      writer.addDocuments(nestedDocs);
    } else {
      writer.addDocument(cmd.getLuceneDocument());
    }
    if (ulog != null) ulog.add(cmd);

  } finally {
    iw.decref();
  }

}
 
Example #8
Source File: DirectUpdateHandler2.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void doNormalUpdate(AddUpdateCommand cmd) throws IOException {
  RefCounted<IndexWriter> iw = solrCoreState.getIndexWriter(core);
  try {
    IndexWriter writer = iw.get();

    updateDocOrDocValues(cmd, writer);

    // Add to the transaction log *after* successfully adding to the
    // index, if there was no error.
    // This ordering ensures that if we log it, it's definitely been
    // added to the the index.
    // This also ensures that if a commit sneaks in-between, that we
    // know everything in a particular
    // log version was definitely committed.
    if (ulog != null) ulog.add(cmd);

  } finally {
    iw.decref();
  }
}
 
Example #9
Source File: DirectUpdateHandler2.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void delete(DeleteUpdateCommand cmd) throws IOException {
  TestInjection.injectDirectUpdateLatch();
  deleteByIdCommands.increment();
  deleteByIdCommandsCumulative.mark();

  if ((cmd.getFlags() & UpdateCommand.IGNORE_INDEXWRITER) != 0 ) {
    if (ulog != null) ulog.delete(cmd);
    return;
  }

  Term deleteTerm = getIdTerm(cmd.getIndexedId(), false);
  // SolrCore.verbose("deleteDocuments",deleteTerm,writer);
  RefCounted<IndexWriter> iw = solrCoreState.getIndexWriter(core);
  try {
    iw.get().deleteDocuments(deleteTerm);
  } finally {
    iw.decref();
  }
  // SolrCore.verbose("deleteDocuments",deleteTerm,"DONE");

  if (ulog != null) ulog.delete(cmd);

  updateDeleteTrackers(cmd);
}
 
Example #10
Source File: DirectUpdateHandler2.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void prepareCommit(CommitUpdateCommand cmd) throws IOException {

    boolean error=true;

    try {
      log.debug("start {}", cmd);
      RefCounted<IndexWriter> iw = solrCoreState.getIndexWriter(core);
      try {
        SolrIndexWriter.setCommitData(iw.get(), cmd.getVersion());
        iw.get().prepareCommit();
      } finally {
        iw.decref();
      }

      log.debug("end_prepareCommit");

      error=false;
    }
    finally {
      if (error) {
        numErrors.increment();
        numErrorsCumulative.mark();
      }
    }
  }
 
Example #11
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 #12
Source File: RecoveryStrategy.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
final private void cloudDebugLog(SolrCore core, String op) {
  if (!log.isDebugEnabled()) {
    return;
  }
  try {
    RefCounted<SolrIndexSearcher> searchHolder = core.getNewestSearcher(false);
    SolrIndexSearcher searcher = searchHolder.get();
    try {
      final int totalHits = searcher.count(new MatchAllDocsQuery());
      final String nodeName = core.getCoreContainer().getZkController().getNodeName();
      log.debug("[{}] {} [{} total hits]", nodeName, op, totalHits);
    } finally {
      searchHolder.decref();
    }
  } catch (Exception e) {
    log.debug("Error in solrcloud_debug block", e);
  }
}
 
Example #13
Source File: SolrCore.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void initSearcher(SolrCore prev) throws IOException {
  // use the (old) writer to open the first searcher
  RefCounted<IndexWriter> iwRef = null;
  if (prev != null) {
    iwRef = prev.getUpdateHandler().getSolrCoreState().getIndexWriter(null);
    if (iwRef != null) {
      final IndexWriter iw = iwRef.get();
      final SolrCore core = this;
      newReaderCreator = () -> indexReaderFactory.newReader(iw, core);
    }
  }

  try {
    getSearcher(false, false, null, true);
  } finally {
    newReaderCreator = null;
    if (iwRef != null) {
      iwRef.decref();
    }
  }
}
 
Example #14
Source File: SolrCore.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private RefCounted<SolrIndexSearcher> newHolder(SolrIndexSearcher newSearcher, final List<RefCounted<SolrIndexSearcher>> searcherList) {
  RefCounted<SolrIndexSearcher> holder = new RefCounted<SolrIndexSearcher>(newSearcher) {
    @Override
    public void close() {
      try {
        synchronized (searcherLock) {
          // it's possible for someone to get a reference via the _searchers queue
          // and increment the refcount while RefCounted.close() is being called.
          // we check the refcount again to see if this has happened and abort the close.
          // This relies on the RefCounted class allowing close() to be called every
          // time the counter hits zero.
          if (refcount.get() > 0) return;
          searcherList.remove(this);
        }
        resource.close();
      } catch (Exception e) {
        // do not allow decref() operations to fail since they are typically called in finally blocks
        // and throwing another exception would be very unexpected.
        SolrException.log(log, "Error closing searcher:" + this, e);
      }
    }
  };
  holder.incref();  // set ref count to 1 to account for this._searcher
  return holder;
}
 
Example #15
Source File: FacetTreeGenerator.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
public List<SimpleOrderedMap<Object>> generateTree(ResponseBuilder rb, NamedList<Integer> facetValues) throws IOException {
	List<SimpleOrderedMap<Object>> retVal = null;
	
	// First get the searcher for the required collection
	RefCounted<SolrIndexSearcher> searcherRef = getSearcherReference(rb);
	
	try {
		// Build the facet tree(s)
		Collection<TreeFacetField> fTrees = treeBuilder.processFacetTree(searcherRef.get(), extractFacetValues(facetValues));
		LOGGER.debug("Extracted {} facet trees", fTrees.size());
		
		if (pruner != null) {
			// Prune the trees
			fTrees = pruner.prune(fTrees);
		}

		// Convert the trees into a SimpleOrderedMap
		retVal = convertTreeFacetFields(fTrees);
	} finally {
		// Make sure the search ref count is decreased
		searcherRef.decref();
	}
	
	return retVal;
}
 
Example #16
Source File: UninvertDocValuesMergePolicyTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static void withNewRawReader(TestHarness h, DirectoryReaderConsumer consumer) {
  try (SolrCore core = h.getCoreInc()) {
    final RefCounted<SolrIndexSearcher> searcherRef = core.openNewSearcher(true, true);
    final SolrIndexSearcher searcher = searcherRef.get();
    try {
      try {
        consumer.accept(searcher.getRawReader());
      } catch (Exception e) {
        fail(e.toString());
      }
    } finally {
      searcherRef.decref();
    }
  }
}
 
Example #17
Source File: DocValuesMultiTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testDocValues() throws IOException {

  final DocValuesType expectedNumericDvType = Boolean.getBoolean(NUMERIC_POINTS_SYSPROP) ?
    DocValuesType.SORTED_NUMERIC : DocValuesType.SORTED_SET;
  
  assertU(adoc("id", "1", "floatdv", "4.5", "intdv", "-1", "intdv", "3",
      "stringdv", "value1", "stringdv", "value2",
      "booldv", "false", "booldv", "true"));
  assertU(commit());
  try (SolrCore core = h.getCoreInc()) {
    final RefCounted<SolrIndexSearcher> searcherRef = core.openNewSearcher(true, true);
    final SolrIndexSearcher searcher = searcherRef.get();
    try {
      final LeafReader reader = searcher.getSlowAtomicReader();
      assertEquals(1, reader.numDocs());
      final FieldInfos infos = reader.getFieldInfos();
      assertEquals(DocValuesType.SORTED_SET, infos.fieldInfo("stringdv").getDocValuesType());
      assertEquals(DocValuesType.SORTED_SET, infos.fieldInfo("booldv").getDocValuesType());
      assertEquals(expectedNumericDvType, infos.fieldInfo("floatdv").getDocValuesType());
      assertEquals(expectedNumericDvType, infos.fieldInfo("intdv").getDocValuesType());

      SortedSetDocValues dv = reader.getSortedSetDocValues("stringdv");
      assertEquals(0, dv.nextDoc());
      assertEquals(0, dv.nextOrd());
      assertEquals(1, dv.nextOrd());
      assertEquals(SortedSetDocValues.NO_MORE_ORDS, dv.nextOrd());

      dv = reader.getSortedSetDocValues("booldv");
      assertEquals(0, dv.nextDoc());
      assertEquals(0, dv.nextOrd());
      assertEquals(1, dv.nextOrd());
      assertEquals(SortedSetDocValues.NO_MORE_ORDS, dv.nextOrd());


    } finally {
      searcherRef.decref();
    }
  }
}
 
Example #18
Source File: SolrCore.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void seedVersionBuckets() {
  UpdateHandler uh = getUpdateHandler();
  if (uh != null && uh.getUpdateLog() != null) {
    RefCounted<SolrIndexSearcher> newestSearcher = getRealtimeSearcher();
    if (newestSearcher != null) {
      try {
        uh.getUpdateLog().seedBucketsWithHighestVersion(newestSearcher.get());
      } finally {
        newestSearcher.decref();
      }
    } else {
      log.warn("No searcher available! Cannot seed version buckets with max from index.");
    }
  }
}
 
Example #19
Source File: TestMergePolicyConfig.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Given an SolrCore, asserts that each segment in the (searchable) index 
 * has a compound file status that matches the expected input.
 */
public static void assertCompoundSegments(SolrCore core, boolean compound) {
  RefCounted<SolrIndexSearcher> searcherRef = core.getRegisteredSearcher();
  try {
    assertCompoundSegments(searcherRef.get().getRawReader(), compound);
  } finally {
    searcherRef.decref();
  }
}
 
Example #20
Source File: TestSimpleTextCodec.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void test() throws Exception {
  SolrConfig config = h.getCore().getSolrConfig();
  String codecFactory =  config.get("codecFactory/@class");
  assertEquals("Unexpected solrconfig codec factory", "solr.SimpleTextCodecFactory", codecFactory);

  assertEquals("Unexpected core codec", "SimpleText", h.getCore().getCodec().getName());

  RefCounted<IndexWriter> writerRef = h.getCore().getSolrCoreState().getIndexWriter(h.getCore());
  try {
    IndexWriter writer = writerRef.get();
    assertEquals("Unexpected codec in IndexWriter config", 
        "SimpleText", writer.getConfig().getCodec().getName()); 
  } finally {
    writerRef.decref();
  }

  assertU(add(doc("id","1", "text","textual content goes here")));
  assertU(commit());

  h.getCore().withSearcher(searcher -> {
    SegmentInfos infos = SegmentInfos.readLatestCommit(searcher.getIndexReader().directory());
    SegmentInfo info = infos.info(infos.size() - 1).info;
    assertEquals("Unexpected segment codec", "SimpleText", info.getCodec().getName());
    return null;
  });

  assertQ(req("q", "id:1"),
      "*[count(//doc)=1]");
}
 
Example #21
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Set<Long> getErrorDocIds() throws IOException
{
    Set<Long> errorDocIds = new HashSet<>();
    RefCounted<SolrIndexSearcher> refCounted = null;
    try
    {
        refCounted = this.core.getSearcher();
        SolrIndexSearcher searcher = refCounted.get();
        TermQuery errorQuery = new TermQuery(new Term(FIELD_DOC_TYPE, DOC_TYPE_ERROR_NODE));
        DocListCollector docListCollector = new DocListCollector();
        searcher.search(errorQuery, docListCollector);
        IntArrayList docList = docListCollector.getDocs();
        int size = docList.size();

        for (int i = 0; i < size; ++i)
        {
            int doc = docList.get(i);
            Document document = searcher.doc(doc, REQUEST_ONLY_ID_FIELD);
            IndexableField id = document.getField(FIELD_SOLR4_ID);
            String idString = id.stringValue();

            if (idString.startsWith(PREFIX_ERROR))
            {
                idString = idString.substring(PREFIX_ERROR.length());
            }

            errorDocIds.add(Long.valueOf(idString));
        }
    }
    finally
    {
        ofNullable(refCounted).ifPresent(RefCounted::decref);
    }
    return errorDocIds;
}
 
Example #22
Source File: TestMergePolicyConfig.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Given an SolrCore, asserts that the number of leave segments in 
 * the index reader matches the expected value.
 */
public static void assertNumSegments(SolrCore core, int expected) {
  RefCounted<SolrIndexSearcher> searcherRef = core.getRegisteredSearcher();
  try {
    assertEquals(expected, searcherRef.get().getIndexReader().leaves().size());
  } finally {
    searcherRef.decref();
  }
}
 
Example #23
Source File: SegmentsInfoRequestHandlerTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {

  // we need a consistent segmentation to ensure we don't get a random
  // merge that reduces the total num docs in all segments, or the number of deletes
  //
  systemSetPropertySolrTestsMergePolicyFactory(NoMergePolicyFactory.class.getName());
  // Also prevent flushes
  System.setProperty("solr.tests.maxBufferedDocs", "1000");
  System.setProperty("solr.tests.ramBufferSizeMB", "5000");
  
  System.setProperty("enable.update.log", "false"); // no _version_ in our schema
  initCore("solrconfig.xml", "schema12.xml"); // segments API shouldn't depend on _version_ or ulog
  
  // build up an index with at least 2 segments and some deletes
  for (int i = 0; i < DOC_COUNT; i++) {
    assertU(adoc("id","SOLR100" + i, "name","Apache Solr:" + i));
  }
  for (int i = 0; i < DEL_COUNT; i++) {
    assertU(delI("SOLR100" + i));
  }
  assertU(commit());
  for (int i = 0; i < DOC_COUNT; i++) {
    assertU(adoc("id","SOLR200" + i, "name","Apache Solr:" + i));
  }
  assertU(commit());
  h.getCore().withSearcher((searcher) -> {
    int numSegments = SegmentInfos.readLatestCommit(searcher.getIndexReader().directory()).size();
    // if this is not NUM_SEGMENTS, there was some unexpected flush or merge
    assertEquals("Unexpected number of segment in the index: " + numSegments, 
        NUM_SEGMENTS, numSegments);
    return null;
  });
  // see SOLR-14431
  RefCounted<IndexWriter> iwRef = h.getCore().getSolrCoreState().getIndexWriter(h.getCore());
  initialRefCount = iwRef.getRefcount();
  iwRef.decref();
}
 
Example #24
Source File: TestInPlaceUpdatesDistrib.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeSuperClass() throws Exception {
  schemaString = "schema-inplace-updates.xml";
  configString = "solrconfig-tlog.xml";

  // we need consistent segments that aren't re-ordered on merge because we're
  // asserting inplace updates happen by checking the internal [docid]
  systemSetPropertySolrTestsMergePolicyFactory(NoMergePolicyFactory.class.getName());

  randomizeUpdateLogImpl();

  initCore(configString, schemaString);
  
  // sanity check that autocommits are disabled
  assertEquals(-1, h.getCore().getSolrConfig().getUpdateHandlerInfo().autoCommmitMaxTime);
  assertEquals(-1, h.getCore().getSolrConfig().getUpdateHandlerInfo().autoSoftCommmitMaxTime);
  assertEquals(-1, h.getCore().getSolrConfig().getUpdateHandlerInfo().autoCommmitMaxDocs);
  assertEquals(-1, h.getCore().getSolrConfig().getUpdateHandlerInfo().autoSoftCommmitMaxDocs);
  
  // assert that NoMergePolicy was chosen
  RefCounted<IndexWriter> iw = h.getCore().getSolrCoreState().getIndexWriter(h.getCore());
  try {
    IndexWriter writer = iw.get();
    assertTrue("Actual merge policy is: " + writer.getConfig().getMergePolicy(),
        writer.getConfig().getMergePolicy() instanceof NoMergePolicy); 
  } finally {
    iw.decref();
  }
}
 
Example #25
Source File: SolrCore.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the latest real-time searcher w/o forcing open a new searcher if one already exists.
 * The reference count will be incremented.
 */
public RefCounted<SolrIndexSearcher> getRealtimeSearcher() {
  synchronized (searcherLock) {
    if (realtimeSearcher != null) {
      realtimeSearcher.incref();
      return realtimeSearcher;
    }
  }

  // use the searcher lock to prevent multiple people from trying to open at once
  openSearcherLock.lock();
  try {

    // try again
    synchronized (searcherLock) {
      if (realtimeSearcher != null) {
        realtimeSearcher.incref();
        return realtimeSearcher;
      }
    }

    // force a new searcher open
    return openNewSearcher(true, true);
  } finally {
    openSearcherLock.unlock();
  }
}
 
Example #26
Source File: SolrCore.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Return the newest normal {@link RefCounted}&lt;{@link SolrIndexSearcher}&gt; with
 * the reference count incremented.  It <b>must</b> be decremented when no longer needed.
 * If no searcher is currently open, then if openNew==true a new searcher will be opened,
 * or null is returned if openNew==false.
 */
public RefCounted<SolrIndexSearcher> getNewestSearcher(boolean openNew) {
  synchronized (searcherLock) {
    if (!_searchers.isEmpty()) {
      RefCounted<SolrIndexSearcher> newest = _searchers.getLast();
      newest.incref();
      return newest;
    }
  }

  return openNew ? getRealtimeSearcher() : null;
}
 
Example #27
Source File: SolrCore.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the current registered searcher with its reference count incremented, or null if none are registered.
 */
public RefCounted<SolrIndexSearcher> getRegisteredSearcher() {
  synchronized (searcherLock) {
    if (_searcher != null) {
      _searcher.incref();
    }
    return _searcher;
  }
}
 
Example #28
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean isInIndex(long id, LRU cache, String fieldName, boolean populateCache, SolrCore core) throws IOException
{
    if(cache.containsKey(id))
    {
        return true;
    }
    else
    {
        RefCounted<SolrIndexSearcher> refCounted = null;
        try
        {
            if(populateCache)
            {
                cache.put(id, null); // Safe to add this here because we reset this on rollback.
            }
            refCounted = core.getSearcher();
            SolrIndexSearcher searcher = refCounted.get();
            FieldType fieldType = searcher.getSchema().getField(fieldName).getType();
            TermQuery q = new TermQuery(new Term(fieldName, fieldType.readableToIndexed(Long.toString(id))));
            TopDocs topDocs = searcher.search(q, 1);
            return topDocs.totalHits > 0;
        }
        finally
        {
            ofNullable(refCounted).ifPresent(RefCounted::decref);
        }
    }
}
 
Example #29
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 #30
Source File: SegmentsInfoRequestHandlerTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void afterClass() throws Exception {
  RefCounted<IndexWriter> iwRef = h.getCore().getSolrCoreState().getIndexWriter(h.getCore());
  int finalRefCount = iwRef.getRefcount();
  iwRef.decref();
  assertEquals("IW refcount mismatch", initialRefCount, finalRefCount);
  systemClearPropertySolrTestsMergePolicyFactory();
  System.clearProperty("solr.tests.maxBufferedDocs");
  System.clearProperty("solr.tests.ramBufferSizeMB");
}