Search in sources :

Example 16 with AlreadyClosedException

use of org.apache.lucene.store.AlreadyClosedException in project elasticsearch by elastic.

the class SearchServiceTests method testSearchWhileIndexDeleted.

public void testSearchWhileIndexDeleted() throws IOException, InterruptedException {
    createIndex("index");
    client().prepareIndex("index", "type", "1").setSource("field", "value").setRefreshPolicy(IMMEDIATE).get();
    SearchService service = getInstanceFromNode(SearchService.class);
    IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    IndexService indexService = indicesService.indexServiceSafe(resolveIndex("index"));
    IndexShard indexShard = indexService.getShard(0);
    AtomicBoolean running = new AtomicBoolean(true);
    CountDownLatch startGun = new CountDownLatch(1);
    Semaphore semaphore = new Semaphore(Integer.MAX_VALUE);
    final Thread thread = new Thread() {

        @Override
        public void run() {
            startGun.countDown();
            while (running.get()) {
                service.afterIndexRemoved(indexService.index(), indexService.getIndexSettings(), DELETED);
                if (randomBoolean()) {
                    // context in a non-sane way.
                    try {
                        semaphore.acquire();
                    } catch (InterruptedException e) {
                        throw new AssertionError(e);
                    }
                    client().prepareIndex("index", "type").setSource("field", "value").setRefreshPolicy(randomFrom(WriteRequest.RefreshPolicy.values())).execute(new ActionListener<IndexResponse>() {

                        @Override
                        public void onResponse(IndexResponse indexResponse) {
                            semaphore.release();
                        }

                        @Override
                        public void onFailure(Exception e) {
                            semaphore.release();
                        }
                    });
                }
            }
        }
    };
    thread.start();
    startGun.await();
    try {
        final int rounds = scaledRandomIntBetween(100, 10000);
        for (int i = 0; i < rounds; i++) {
            try {
                QuerySearchResultProvider querySearchResultProvider = service.executeQueryPhase(new ShardSearchLocalRequest(indexShard.shardId(), 1, SearchType.DEFAULT, new SearchSourceBuilder(), new String[0], false, new AliasFilter(null, Strings.EMPTY_ARRAY), 1.0f), new SearchTask(123L, "", "", "", null));
                IntArrayList intCursors = new IntArrayList(1);
                intCursors.add(0);
                ShardFetchRequest req = new ShardFetchRequest(querySearchResultProvider.id(), intCursors, null);
                service.executeFetchPhase(req, new SearchTask(123L, "", "", "", null));
            } catch (AlreadyClosedException ex) {
                throw ex;
            } catch (IllegalStateException ex) {
                assertEquals("search context is already closed can't increment refCount current count [0]", ex.getMessage());
            } catch (SearchContextMissingException ex) {
            // that's fine
            }
        }
    } finally {
        running.set(false);
        thread.join();
        semaphore.acquire(Integer.MAX_VALUE);
    }
}
Also used : AliasFilter(org.elasticsearch.search.internal.AliasFilter) ShardSearchLocalRequest(org.elasticsearch.search.internal.ShardSearchLocalRequest) IndexService(org.elasticsearch.index.IndexService) Semaphore(java.util.concurrent.Semaphore) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchTask(org.elasticsearch.action.search.SearchTask) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) ShardFetchRequest(org.elasticsearch.search.fetch.ShardFetchRequest) CountDownLatch(java.util.concurrent.CountDownLatch) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) ExecutionException(java.util.concurrent.ExecutionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexResponse(org.elasticsearch.action.index.IndexResponse) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 17 with AlreadyClosedException

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

the class CachingDirectoryFactory method get.

/*
   * (non-Javadoc)
   * 
   * @see org.apache.solr.core.DirectoryFactory#get(java.lang.String,
   * java.lang.String, boolean)
   */
@Override
public final Directory get(String path, DirContext dirContext, String rawLockType) throws IOException {
    String fullPath = normalize(path);
    synchronized (this) {
        if (closed) {
            throw new AlreadyClosedException("Already closed");
        }
        final CacheValue cacheValue = byPathCache.get(fullPath);
        Directory directory = null;
        if (cacheValue != null) {
            directory = cacheValue.directory;
        }
        if (directory == null) {
            directory = create(fullPath, createLockFactory(rawLockType), dirContext);
            assert ObjectReleaseTracker.track(directory);
            boolean success = false;
            try {
                CacheValue newCacheValue = new CacheValue(fullPath, directory);
                byDirectoryCache.put(directory, newCacheValue);
                byPathCache.put(fullPath, newCacheValue);
                log.debug("return new directory for {}", fullPath);
                success = true;
            } finally {
                if (!success) {
                    IOUtils.closeWhileHandlingException(directory);
                }
            }
        } else {
            cacheValue.refCnt++;
            log.debug("Reusing cached directory: {}", cacheValue);
        }
        return directory;
    }
}
Also used : AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) Directory(org.apache.lucene.store.Directory)

Example 18 with AlreadyClosedException

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

the class MetricsMap method getMBeanInfo.

@Override
public MBeanInfo getMBeanInfo() {
    ArrayList<MBeanAttributeInfo> attrInfoList = new ArrayList<>();
    Map<String, Object> stats = getValue(true);
    if (useCachedStatsBetweenGetMBeanInfoCalls) {
        cachedValue = stats;
    }
    try {
        stats.forEach((k, v) -> {
            Class type = v.getClass();
            OpenType typeBox = determineType(type);
            if (type.equals(String.class) || typeBox == null) {
                attrInfoList.add(new MBeanAttributeInfo(k, String.class.getName(), null, true, false, false));
            } else {
                attrInfoList.add(new OpenMBeanAttributeInfoSupport(k, k, typeBox, true, false, false));
            }
        });
    } catch (Exception e) {
        // don't log issue if the core is closing
        if (!(SolrException.getRootCause(e) instanceof AlreadyClosedException))
            log.warn("Could not get attributes of MetricsMap: {}", this, e);
    }
    MBeanAttributeInfo[] attrInfoArr = attrInfoList.toArray(new MBeanAttributeInfo[attrInfoList.size()]);
    return new MBeanInfo(getClass().getName(), "MetricsMap", attrInfoArr, null, null, null);
}
Also used : OpenType(javax.management.openmbean.OpenType) MBeanInfo(javax.management.MBeanInfo) ArrayList(java.util.ArrayList) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) AttributeNotFoundException(javax.management.AttributeNotFoundException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) SolrException(org.apache.solr.common.SolrException) ReflectionException(javax.management.ReflectionException) OpenMBeanAttributeInfoSupport(javax.management.openmbean.OpenMBeanAttributeInfoSupport)

Example 19 with AlreadyClosedException

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

the class TestIndexFileDeleter method testExcInDecRef.

// LUCENE-5919
public void testExcInDecRef() throws Throwable {
    MockDirectoryWrapper dir = newMockDirectory();
    // disable slow things: we don't rely upon sleeps here.
    dir.setThrottling(MockDirectoryWrapper.Throttling.NEVER);
    dir.setUseSlowOpenClosers(false);
    final AtomicBoolean doFailExc = new AtomicBoolean();
    dir.failOn(new MockDirectoryWrapper.Failure() {

        @Override
        public void eval(MockDirectoryWrapper dir) throws IOException {
            if (doFailExc.get() && random().nextInt(4) == 1) {
                Exception e = new Exception();
                StackTraceElement[] stack = e.getStackTrace();
                for (int i = 0; i < stack.length; i++) {
                    if (stack[i].getClassName().equals(IndexFileDeleter.class.getName()) && stack[i].getMethodName().equals("decRef")) {
                        throw new RuntimeException("fake fail");
                    }
                }
            }
        }
    });
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    //iwc.setMergeScheduler(new SerialMergeScheduler());
    MergeScheduler ms = iwc.getMergeScheduler();
    if (ms instanceof ConcurrentMergeScheduler) {
        final ConcurrentMergeScheduler suppressFakeFail = new ConcurrentMergeScheduler() {

            @Override
            protected void handleMergeException(Directory dir, Throwable exc) {
                // suppress only FakeIOException:
                if (exc instanceof RuntimeException && exc.getMessage().equals("fake fail")) {
                // ok to ignore
                } else if ((exc instanceof AlreadyClosedException || exc instanceof IllegalStateException) && exc.getCause() != null && "fake fail".equals(exc.getCause().getMessage())) {
                // also ok to ignore
                } else {
                    super.handleMergeException(dir, exc);
                }
            }
        };
        final ConcurrentMergeScheduler cms = (ConcurrentMergeScheduler) ms;
        suppressFakeFail.setMaxMergesAndThreads(cms.getMaxMergeCount(), cms.getMaxThreadCount());
        iwc.setMergeScheduler(suppressFakeFail);
    }
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
    // Since we hit exc during merging, a partial
    // forceMerge can easily return when there are still
    // too many segments in the index:
    w.setDoRandomForceMergeAssert(false);
    doFailExc.set(true);
    int ITERS = atLeast(1000);
    for (int iter = 0; iter < ITERS; iter++) {
        try {
            if (random().nextInt(10) == 5) {
                w.commit();
            } else if (random().nextInt(10) == 7) {
                w.getReader().close();
            } else {
                Document doc = new Document();
                doc.add(newTextField("field", "some text", Field.Store.NO));
                w.addDocument(doc);
            }
        } catch (Throwable t) {
            if (t.toString().contains("fake fail") || (t.getCause() != null && t.getCause().toString().contains("fake fail"))) {
            // ok
            } else {
                throw t;
            }
        }
    }
    doFailExc.set(false);
    w.close();
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) Document(org.apache.lucene.document.Document) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Directory(org.apache.lucene.store.Directory)

Example 20 with AlreadyClosedException

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

the class TestConcurrentMergeScheduler method testFlushExceptions.

// Make sure running BG merges still work fine even when
// we are hitting exceptions during flushing.
public void testFlushExceptions() throws IOException {
    MockDirectoryWrapper directory = newMockDirectory();
    FailOnlyOnFlush failure = new FailOnlyOnFlush();
    directory.failOn(failure);
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random())).setMaxBufferedDocs(2);
    if (iwc.getMergeScheduler() instanceof ConcurrentMergeScheduler) {
        iwc.setMergeScheduler(new SuppressingConcurrentMergeScheduler() {

            @Override
            protected boolean isOK(Throwable th) {
                return th instanceof AlreadyClosedException || (th instanceof IllegalStateException && th.getMessage().contains("this writer hit an unrecoverable error"));
            }
        });
    }
    IndexWriter writer = new IndexWriter(directory, iwc);
    Document doc = new Document();
    Field idField = newStringField("id", "", Field.Store.YES);
    doc.add(idField);
    outer: for (int i = 0; i < 10; i++) {
        if (VERBOSE) {
            System.out.println("TEST: iter=" + i);
        }
        for (int j = 0; j < 20; j++) {
            idField.setStringValue(Integer.toString(i * 20 + j));
            writer.addDocument(doc);
        }
        // flush, and we don't hit the exception
        while (true) {
            writer.addDocument(doc);
            failure.setDoFail();
            try {
                writer.flush(true, true);
                if (failure.hitExc) {
                    fail("failed to hit IOException");
                }
            } catch (IOException ioe) {
                if (VERBOSE) {
                    ioe.printStackTrace(System.out);
                }
                failure.clearDoFail();
                assertTrue(writer.isClosed());
                // Abort should have closed the deleter:
                assertTrue(writer.deleter.isClosed());
                break outer;
            }
        }
    }
    assertFalse(DirectoryReader.indexExists(directory));
    directory.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) StringField(org.apache.lucene.document.StringField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer)

Aggregations

AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)79 IOException (java.io.IOException)53 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)16 CountDownLatch (java.util.concurrent.CountDownLatch)15 MockDirectoryWrapper (org.apache.lucene.store.MockDirectoryWrapper)14 TranslogCorruptedException (org.elasticsearch.index.translog.TranslogCorruptedException)13 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 Document (org.apache.lucene.document.Document)11 ElasticsearchException (org.elasticsearch.ElasticsearchException)11 ReleasableLock (org.elasticsearch.common.util.concurrent.ReleasableLock)10 UncheckedIOException (java.io.UncheckedIOException)9 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)9 EOFException (java.io.EOFException)8 ArrayList (java.util.ArrayList)7 FileNotFoundException (java.io.FileNotFoundException)6 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)6 NoSuchFileException (java.nio.file.NoSuchFileException)6 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)6 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)6