org.apache.lucene.store.AlreadyClosedException Java Examples

The following examples show how to use org.apache.lucene.store.AlreadyClosedException. 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: InternalEngine.java    From crate with Apache License 2.0 6 votes vote down vote up
private boolean failOnTragicEvent(AlreadyClosedException ex) {
    final boolean engineFailed;
    // if we are already closed due to some tragic exception
    // we need to fail the engine. it might have already been failed before
    // but we are double-checking it's failed and closed
    if (indexWriter.isOpen() == false && indexWriter.getTragicException() != null) {
        final Exception tragicException;
        if (indexWriter.getTragicException() instanceof Exception) {
            tragicException = (Exception) indexWriter.getTragicException();
        } else {
            tragicException = new RuntimeException(indexWriter.getTragicException());
        }
        failEngine("already closed by tragic event on the index writer", tragicException);
        engineFailed = true;
    } else if (translog.isOpen() == false && translog.getTragicException() != null) {
        failEngine("already closed by tragic event on the translog", translog.getTragicException());
        engineFailed = true;
    } else if (failedEngine.get() == null && isClosed.get() == false) { // we are closed but the engine is not failed yet?
        // this smells like a bug - we only expect ACE if we are in a fatal case ie. either translog or IW is closed by
        // a tragic event or has closed itself. if that is not the case we are in a buggy state and raise an assertion error
        throw new AssertionError("Unexpected AlreadyClosedException", ex);
    } else {
        engineFailed = false;
    }
    return engineFailed;
}
 
Example #2
Source File: CommonStats.java    From crate with Apache License 2.0 6 votes vote down vote up
public CommonStats(IndexShard indexShard, CommonStatsFlags flags) {
    CommonStatsFlags.Flag[] setFlags = flags.getFlags();
    for (CommonStatsFlags.Flag flag : setFlags) {
        try {
            switch (flag) {
                case Docs:
                    docs = indexShard.docStats();
                    break;
                case Store:
                    store = indexShard.storeStats();
                    break;
                default:
                    throw new IllegalStateException("Unknown Flag: " + flag);
            }
        } catch (AlreadyClosedException e) {
            // shard is closed - no stats is fine
        }
    }
}
 
Example #3
Source File: TestLuceneIndexer.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
  int i = 0;
  while (i < 10000) {
    try {
      if (data.size() <= i) {
        sleep(1);
        continue;
      }
      final String key = "key" + i;
      final String val = "value" + i;
      final List<Document> documents = index.searchForDocuments(new TermQuery(new Term(key, val)), 10, new Sort(new SortField(key, SortField.Type.STRING)));
      if (documents.size() != 1) {
        throw new RuntimeException("Invalid number of matching documents for " + key + ", found " + documents);
      }
      ++i;
    } catch (IOException ioe) {
      error = ioe;
      break;
    } catch (InterruptedException e) {
    } catch (AlreadyClosedException ace) {
      error = ace;
      break;
    }
  }
}
 
Example #4
Source File: AutoCompleter.java    From webdsl with Apache License 2.0 6 votes vote down vote up
private void swapSearcher(final Directory dir) throws IOException {
  /*
   * opening a searcher is possibly very expensive.
   * We rather close it again if the Autocompleter was closed during
   * this operation than block access to the current searcher while opening.
   */
  final IndexSearcher indexSearcher = createSearcher(dir);
  synchronized (searcherLock) {
    if(closed){
      indexSearcher.close();
      throw new AlreadyClosedException("Autocompleter has been closed");
    }
    if (searcher != null) {
      searcher.close();
    }
    // set the autocomplete index in the sync block - ensure consistency.
    searcher = indexSearcher;
    this.autoCompleteIndex = dir;
  }
}
 
Example #5
Source File: LumongoSegment.java    From lumongo with Apache License 2.0 6 votes vote down vote up
private void reopenIndexWritersIfNecessary() throws Exception {
	if (!indexWriter.isOpen()) {
		synchronized (this) {
			if (!indexWriter.isOpen()) {
				this.indexWriter = this.indexSegmentInterface.getIndexWriter(segmentNumber);
				this.directoryReader = DirectoryReader.open(indexWriter, indexConfig.getIndexSettings().getApplyUncommittedDeletes(), false);
			}
		}
	}

	//TODO: is this a real use case?
	try {
		taxoWriter.getSize();
	}
	catch (AlreadyClosedException e) {
		synchronized (this) {
			this.taxoWriter = this.indexSegmentInterface.getTaxoWriter(segmentNumber);
			this.taxoReader = new DirectoryTaxonomyReader(taxoWriter);
		}
	}

}
 
Example #6
Source File: TestIDVersionPostingsFormat.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testInvalidVersions2() throws IOException {
  Directory dir = newDirectory();
  IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
  iwc.setCodec(TestUtil.alwaysPostingsFormat(new IDVersionPostingsFormat()));
  RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc, false);
  Document doc = new Document();
  // Long.MAX_VALUE:
  doc.add(new StringAndPayloadField("id", "id", new BytesRef(new byte[] {(byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff})));
  expectThrows(IllegalArgumentException.class, () -> {
    w.addDocument(doc);
    w.commit(false);
  });
  expectThrows(AlreadyClosedException.class, () -> {
    w.addDocument(doc);
  });

  dir.close();
}
 
Example #7
Source File: IndexReader.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Expert: decreases the refCount of this IndexReader
 * instance.  If the refCount drops to 0, then this
 * reader is closed.  If an exception is hit, the refCount
 * is unchanged.
 *
 * @throws IOException in case an IOException occurs in  doClose()
 *
 * @see #incRef
 */
@SuppressWarnings("try")
public final void decRef() throws IOException {
  // only check refcount here (don't call ensureOpen()), so we can
  // still close the reader if it was made invalid by a child:
  if (refCount.get() <= 0) {
    throw new AlreadyClosedException("this IndexReader is closed");
  }
  
  final int rc = refCount.decrementAndGet();
  if (rc == 0) {
    closed = true;
    try (Closeable finalizer = this::reportCloseToParentReaders;
          Closeable finalizer1 = this::notifyReaderClosedListeners) {
      doClose();
    }
  } else if (rc < 0) {
    throw new IllegalStateException("too many decRef calls: refCount is " + rc + " after decrement");
  }
}
 
Example #8
Source File: InternalEngine.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean maybeFailEngine(String source, Exception e) {
    boolean shouldFail = super.maybeFailEngine(source, e);
    if (shouldFail) {
        return true;
    }
    // Check for AlreadyClosedException -- ACE is a very special
    // exception that should only be thrown in a tragic event. we pass on the checks to failOnTragicEvent which will
    // throw and AssertionError if the tragic event condition is not met.
    if (e instanceof AlreadyClosedException) {
        return failOnTragicEvent((AlreadyClosedException)e);
    } else if (e != null &&
            ((indexWriter.isOpen() == false && indexWriter.getTragicException() == e)
                    || (translog.isOpen() == false && translog.getTragicException() == e))) {
        // this spot on - we are handling the tragic event exception here so we have to fail the engine
        // right away
        failEngine(source, e);
        return true;
    }
    return false;
}
 
Example #9
Source File: TestIndexWriterReader.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testAfterClose() throws Exception {
  Directory dir1 = getAssertNoDeletesDirectory(newDirectory());
  IndexWriter writer = new IndexWriter(dir1, newIndexWriterConfig(new MockAnalyzer(random())));

  // create the index
  createIndexNoClose(false, "test", writer);

  DirectoryReader r = writer.getReader();
  writer.close();

  TestUtil.checkIndex(dir1);

  // reader should remain usable even after IndexWriter is closed:
  assertEquals(100, r.numDocs());
  Query q = new TermQuery(new Term("indexname", "test"));
  IndexSearcher searcher = newSearcher(r);
  assertEquals(100, searcher.count(q));

  expectThrows(AlreadyClosedException.class, () -> {
    DirectoryReader.openIfChanged(r);
  });

  r.close();
  dir1.close();
}
 
Example #10
Source File: TestParallelLeafReader.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testCloseInnerReader() throws Exception {
  Directory dir1 = getDir1(random());
  LeafReader ir1 = getOnlyLeafReader(DirectoryReader.open(dir1));
  
  // with overlapping
  ParallelLeafReader pr = new ParallelLeafReader(true,
   new LeafReader[] {ir1},
   new LeafReader[] {ir1});

  ir1.close();
  
  // should already be closed because inner reader is closed!
  expectThrows(AlreadyClosedException.class, () -> {
    pr.document(0);
  });
  
  // noop:
  pr.close();
  dir1.close();
}
 
Example #11
Source File: TestIndexReaderClose.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testRegisterListenerOnClosedReader() throws IOException {
  Directory dir = newDirectory();
  IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
  w.addDocument(new Document());
  DirectoryReader r = DirectoryReader.open(w);
  w.close();

  // The reader is open, everything should work
  r.getReaderCacheHelper().addClosedListener(key -> {});
  r.leaves().get(0).reader().getReaderCacheHelper().addClosedListener(key -> {});
  r.leaves().get(0).reader().getCoreCacheHelper().addClosedListener(key -> {});

  // But now we close
  r.close();
  expectThrows(AlreadyClosedException.class, () -> r.getReaderCacheHelper().addClosedListener(key -> {}));
  expectThrows(AlreadyClosedException.class, () -> r.leaves().get(0).reader().getReaderCacheHelper().addClosedListener(key -> {}));
  expectThrows(AlreadyClosedException.class, () -> r.leaves().get(0).reader().getCoreCacheHelper().addClosedListener(key -> {}));

  dir.close();
}
 
Example #12
Source File: TestAddIndexes.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
void handle(Throwable t) {
  boolean report = true;

  if (t instanceof AlreadyClosedException || t instanceof MergePolicy.MergeAbortedException || t instanceof NullPointerException) {
    report = !didClose;
  } else if (t instanceof FileNotFoundException || t instanceof NoSuchFileException)  {
    report = !didClose;
  } else if (t instanceof IOException)  {
    Throwable t2 = t.getCause();
    if (t2 instanceof MergePolicy.MergeAbortedException) {
      report = !didClose;
    }
  }
  if (report) {
    t.printStackTrace(System.out);
    synchronized(failures) {
      failures.add(t);
    }
  }
}
 
Example #13
Source File: LucenePerUserWaveViewHandlerImpl.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
/**
 * Closes the handler, releases resources and flushes the recent index changes
 * to persistent storage.
 */
@Override
public synchronized void close() {
  if (isClosed) {
    throw new AlreadyClosedException("Already closed");
  }
  isClosed = true;
  try {
    nrtManager.close();
    if (analyzer != null) {
      analyzer.close();
    }
    nrtManagerReopenThread.close();
    indexWriter.close();
  } catch (IOException ex) {
    LOG.log(Level.SEVERE, "Failed to close the Lucene index", ex);
  }
  LOG.info("Successfully closed the Lucene index...");
}
 
Example #14
Source File: TestSearcherManager.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testEnsureOpen() throws Exception {
  Directory dir = newDirectory();
  new IndexWriter(dir, new IndexWriterConfig(null)).close();
  SearcherManager sm = new SearcherManager(dir, null);
  IndexSearcher s = sm.acquire();
  sm.close();
  
  // this should succeed;
  sm.release(s);
  
  // this should fail
  expectThrows(AlreadyClosedException.class, () -> {
    sm.acquire();
  });
  
  // this should fail
  expectThrows(AlreadyClosedException.class, () -> {
    sm.maybeRefresh();
  });

  dir.close();
}
 
Example #15
Source File: test.java    From vscode-extension with MIT License 6 votes vote down vote up
@Override
protected boolean maybeFailEngine(String source, Exception e) {
    boolean shouldFail = super.maybeFailEngine(source, e);
    if (shouldFail) {
        return true;
    }
    // Check for AlreadyClosedException -- ACE is a very special
    // exception that should only be thrown in a tragic event. we pass on the checks to failOnTragicEvent which will
    // throw and AssertionError if the tragic event condition is not met.
    if (e instanceof AlreadyClosedException) {
        return failOnTragicEvent((AlreadyClosedException)e);
    } else if (e != null &&
            ((indexWriter.isOpen() == false && indexWriter.getTragicException() == e)
                    || (translog.isOpen() == false && translog.getTragicException() == e))) {
        // this spot on - we are handling the tragic event exception here so we have to fail the engine
        // right away
        failEngine(source, e);
        return true;
    }
    return false;
}
 
Example #16
Source File: IndexShard.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public StoreStats storeStats() {
    try {
        return store.stats();
    } catch (IOException e) {
        logger.debug("io exception when get store stats", e);
        throw new ElasticsearchException("io exception while building 'store stats'", e);
    } catch (AlreadyClosedException ex) {
        logger.debug("shard already closed exception", ex);
        return null; // already closed
    }
}
 
Example #17
Source File: Analyzer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the stored value.
 *
 * @param storedValue Value to store
 * @throws AlreadyClosedException if the Analyzer is closed.
 */
protected final void setStoredValue(Analyzer analyzer, Object storedValue) {
  if (analyzer.storedValue == null) {
    throw new AlreadyClosedException("this Analyzer is closed");
  }
  analyzer.storedValue.set(storedValue);
}
 
Example #18
Source File: TestReaderClosed.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void test() throws Exception {
  assertTrue(reader.getRefCount() > 0);
  IndexSearcher searcher = newSearcher(reader);
  TermRangeQuery query = TermRangeQuery.newStringRange("field", "a", "z", true, true);
  searcher.search(query, 5);
  reader.close();
  try {
    searcher.search(query, 5);
  } catch (AlreadyClosedException ace) {
    // expected
  } catch (RejectedExecutionException ree) {
    // expected if the searcher has been created with threads since LuceneTestCase
    // closes the thread-pool in a reader close listener
  }
}
 
Example #19
Source File: InternalEngine.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void updateIndexWriterSettings() {
    try {
        final LiveIndexWriterConfig iwc = indexWriter.getConfig();
        iwc.setRAMBufferSizeMB(engineConfig.getIndexingBufferSize().mbFrac());
        iwc.setUseCompoundFile(engineConfig.isCompoundOnFlush());
    } catch (AlreadyClosedException ex) {
        // ignore
    }
}
 
Example #20
Source File: IndexShard.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether or not the translog generation should be rolled to a new generation. This test is based on the size of the current
 * generation compared to the configured generation threshold size.
 *
 * @return {@code true} if the current generation should be rolled to a new generation
 */
boolean shouldRollTranslogGeneration() {
    final Engine engine = getEngineOrNull();
    if (engine != null) {
        try {
            return engine.shouldRollTranslogGeneration();
        } catch (final AlreadyClosedException e) {
            // we are already closed, no need to flush or roll
        }
    }
    return false;
}
 
Example #21
Source File: IndexShard.java    From crate with Apache License 2.0 5 votes vote down vote up
public void activateThrottling() {
    try {
        getEngine().activateThrottling();
    } catch (AlreadyClosedException ex) {
        // ignore
    }
}
 
Example #22
Source File: ShardRowContext.java    From crate with Apache License 2.0 5 votes vote down vote up
@Nullable
public Long globalSeqNoCheckpoint() {
    try {
        var stats = indexShard.seqNoStats();
        return stats == null ? null : stats.getGlobalCheckpoint();
    } catch (AlreadyClosedException e) {
        return 0L;
    }
}
 
Example #23
Source File: TestAddIndexes.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
void handle(Throwable t) {
  if (!(t instanceof AlreadyClosedException) && !(t instanceof NullPointerException)) {
    t.printStackTrace(System.out);
    synchronized(failures) {
      failures.add(t);
    }
  }
}
 
Example #24
Source File: ShardRowContext.java    From crate with Apache License 2.0 5 votes vote down vote up
public ShardRowContext(IndexShard indexShard, ClusterService clusterService) {
    this(indexShard, null, clusterService, Suppliers.memoizeWithExpiration(() -> {
        try {
            StoreStats storeStats = indexShard.storeStats();
            return storeStats.getSizeInBytes();
        } catch (AlreadyClosedException e) {
            return 0L;
        }
    }, 10, TimeUnit.SECONDS));
}
 
Example #25
Source File: TestParallelCompositeReader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testCloseInnerReader() throws Exception {
  Directory dir1 = getDir1(random());
  CompositeReader ir1 = DirectoryReader.open(dir1);
  assertEquals(1, ir1.getSequentialSubReaders().get(0).getRefCount());
  
  // with overlapping
  ParallelCompositeReader pr = new ParallelCompositeReader(true,
   new CompositeReader[] {ir1},
   new CompositeReader[] {ir1});

  IndexReader psub = pr.getSequentialSubReaders().get(0);
  assertEquals(1, psub.getRefCount());

  ir1.close();

  assertEquals("refCount of synthetic subreader should be unchanged", 1, psub.getRefCount());
  expectThrows(AlreadyClosedException.class, () -> {
    psub.document(0);
  });
  
  expectThrows(AlreadyClosedException.class, () -> {
    pr.document(0);
  });
  
  // noop:
  pr.close();
  assertEquals(0, psub.getRefCount());
  dir1.close();
}
 
Example #26
Source File: Engine.java    From crate with Apache License 2.0 5 votes vote down vote up
protected final void ensureOpen(Exception suppressed) {
    if (isClosed.get()) {
        AlreadyClosedException ace = new AlreadyClosedException(shardId + " engine is closed", failedEngine.get());
        if (suppressed != null) {
            ace.addSuppressed(suppressed);
        }
        throw ace;
    }
}
 
Example #27
Source File: ShardRowContext.java    From crate with Apache License 2.0 5 votes vote down vote up
@Nullable
public Integer translogUncommittedOperations() {
    try {
        var stats = indexShard.translogStats();
        return stats == null ? null : stats.getUncommittedOperations();
    } catch (AlreadyClosedException e) {
        return 0;
    }
}
 
Example #28
Source File: HdfsBlobStore.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the provided operation against this store
 */
<V> V execute(Operation<V> operation) throws IOException {
    if (closed) {
        throw new AlreadyClosedException("HdfsBlobStore is closed: " + this);
    }
    return securityContext.doPrivilegedOrThrow(() -> {
        securityContext.ensureLogin();
        return operation.run(fileContext);
    });
}
 
Example #29
Source File: InternalEngine.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean maybeFailEngine(String source, Throwable t) {
    boolean shouldFail = super.maybeFailEngine(source, t);
    if (shouldFail) {
        return true;
    }

    // Check for AlreadyClosedException
    if (t instanceof AlreadyClosedException) {
        // if we are already closed due to some tragic exception
        // we need to fail the engine. it might have already been failed before
        // but we are double-checking it's failed and closed
        if (indexWriter.isOpen() == false && indexWriter.getTragicException() != null) {
            failEngine("already closed by tragic event on the index writer", indexWriter.getTragicException());
        } else if (translog.isOpen() == false && translog.getTragicException() != null) {
            failEngine("already closed by tragic event on the translog", translog.getTragicException());
        }
        return true;
    } else if (t != null &&
            ((indexWriter.isOpen() == false && indexWriter.getTragicException() == t)
                    || (translog.isOpen() == false && translog.getTragicException() == t))) {
        // this spot on - we are handling the tragic event exception here so we have to fail the engine
        // right away
        failEngine(source, t);
        return true;
    }
    return false;
}
 
Example #30
Source File: TestDirectoryTaxonomyReader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testAlreadyClosed() throws Exception {
  Directory dir = newDirectory();
  DirectoryTaxonomyWriter ltw = new DirectoryTaxonomyWriter(dir);
  ltw.addCategory(new FacetLabel("a"));
  ltw.close();
  
  DirectoryTaxonomyReader ltr = new DirectoryTaxonomyReader(dir);
  ltr.close();
  expectThrows(AlreadyClosedException.class, () -> {
    ltr.getSize();
  });

  dir.close();
}