public AssertingSearcher( IndexSearcher indexSearcher, Searcher wrappedSearcher, ShardId shardId) { // we only use the given index searcher here instead of the IS of the wrapped searcher. the IS // might be a wrapped searcher // with a wrapped reader. this.wrappedSearcher = wrappedSearcher; this.shardId = shardId; initialRefCount = wrappedSearcher.reader().getRefCount(); this.indexSearcher = indexSearcher; assert initialRefCount > 0 : "IndexReader#getRefCount() was [" + initialRefCount + "] expected a value > [0] - reader is already closed"; INFLIGHT_ENGINE_SEARCHERS.put( this, new RuntimeException("Unreleased Searcher, source [" + wrappedSearcher.source() + "]")); }
@Override public void close() throws ElasticsearchException { RuntimeException remove = INFLIGHT_ENGINE_SEARCHERS.remove(this); synchronized (lock) { // make sure we only get this once and store the stack of the first caller! if (remove == null) { assert firstReleaseStack != null; AssertionError error = new AssertionError( "Released Searcher more than once, source [" + wrappedSearcher.source() + "]"); error.initCause(firstReleaseStack); throw error; } else { assert firstReleaseStack == null; firstReleaseStack = new RuntimeException( "Searcher Released first here, source [" + wrappedSearcher.source() + "]"); } } final int refCount = wrappedSearcher.reader().getRefCount(); // this assert seems to be paranoid but given LUCENE-5362 we better add some assertions here // to make sure we catch any potential // problems. assert refCount > 0 : "IndexReader#getRefCount() was [" + refCount + "] expected a value > [0] - reader is already closed. Initial refCount was: [" + initialRefCount + "]"; try { wrappedSearcher.close(); } catch (RuntimeException ex) { logger.debug("Failed to release searcher", ex); throw ex; } }
private long loadCurrentVersionFromIndex(Term uid) throws IOException { try (final Searcher searcher = acquireSearcher("load_version")) { return Versions.loadVersion(searcher.reader(), uid); } }