Search in sources :

Example 46 with AlreadyClosedException

use of org.apache.lucene.store.AlreadyClosedException in project lucene-solr by apache.

the class TestSearcherManager method testIntermediateClose.

public void testIntermediateClose() throws IOException, InterruptedException {
    Directory dir = newDirectory();
    // Test can deadlock if we use SMS:
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())).setMergeScheduler(new ConcurrentMergeScheduler()));
    writer.addDocument(new Document());
    writer.commit();
    final CountDownLatch awaitEnterWarm = new CountDownLatch(1);
    final CountDownLatch awaitClose = new CountDownLatch(1);
    final AtomicBoolean triedReopen = new AtomicBoolean(false);
    final ExecutorService es = random().nextBoolean() ? null : Executors.newCachedThreadPool(new NamedThreadFactory("testIntermediateClose"));
    final SearcherFactory factory = new SearcherFactory() {

        @Override
        public IndexSearcher newSearcher(IndexReader r, IndexReader previous) {
            try {
                if (triedReopen.get()) {
                    awaitEnterWarm.countDown();
                    awaitClose.await();
                }
            } catch (InterruptedException e) {
            //
            }
            return new IndexSearcher(r, es);
        }
    };
    final SearcherManager searcherManager = random().nextBoolean() ? new SearcherManager(dir, factory) : new SearcherManager(writer, random().nextBoolean(), false, factory);
    if (VERBOSE) {
        System.out.println("sm created");
    }
    IndexSearcher searcher = searcherManager.acquire();
    try {
        assertEquals(1, searcher.getIndexReader().numDocs());
    } finally {
        searcherManager.release(searcher);
    }
    writer.addDocument(new Document());
    writer.commit();
    final AtomicBoolean success = new AtomicBoolean(false);
    final Throwable[] exc = new Throwable[1];
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                triedReopen.set(true);
                if (VERBOSE) {
                    System.out.println("NOW call maybeRefresh");
                }
                searcherManager.maybeRefresh();
                success.set(true);
            } catch (AlreadyClosedException e) {
            // expected
            } catch (Throwable e) {
                if (VERBOSE) {
                    System.out.println("FAIL: unexpected exc");
                    e.printStackTrace(System.out);
                }
                exc[0] = e;
                // use success as the barrier here to make sure we see the write
                success.set(false);
            }
        }
    });
    thread.start();
    if (VERBOSE) {
        System.out.println("THREAD started");
    }
    awaitEnterWarm.await();
    if (VERBOSE) {
        System.out.println("NOW call close");
    }
    searcherManager.close();
    awaitClose.countDown();
    thread.join();
    expectThrows(AlreadyClosedException.class, () -> {
        searcherManager.acquire();
    });
    assertFalse(success.get());
    assertTrue(triedReopen.get());
    assertNull("" + exc[0], exc[0]);
    writer.close();
    dir.close();
    if (es != null) {
        es.shutdown();
        es.awaitTermination(1, TimeUnit.SECONDS);
    }
}
Also used : NamedThreadFactory(org.apache.lucene.util.NamedThreadFactory) ConcurrentMergeScheduler(org.apache.lucene.index.ConcurrentMergeScheduler) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) Document(org.apache.lucene.document.Document) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) ExecutorService(java.util.concurrent.ExecutorService) IndexReader(org.apache.lucene.index.IndexReader) Directory(org.apache.lucene.store.Directory)

Aggregations

AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)46 IOException (java.io.IOException)31 MockDirectoryWrapper (org.apache.lucene.store.MockDirectoryWrapper)13 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)12 Document (org.apache.lucene.document.Document)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 Directory (org.apache.lucene.store.Directory)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 EOFException (java.io.EOFException)6 IndexFormatTooOldException (org.apache.lucene.index.IndexFormatTooOldException)6 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)6 FileNotFoundException (java.io.FileNotFoundException)5 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)5 NoSuchFileException (java.nio.file.NoSuchFileException)5 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)4 ExecutionException (java.util.concurrent.ExecutionException)3 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3