Search in sources :

Example 1 with AlreadyClosedException

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

the class Engine method acquireSearcher.

/**
     * Returns a new searcher instance. The consumer of this
     * API is responsible for releasing the returned searcher in a
     * safe manner, preferably in a try/finally block.
     *
     * @see Searcher#close()
     */
public final Searcher acquireSearcher(String source) throws EngineException {
    boolean success = false;
    /* Acquire order here is store -> manager since we need
          * to make sure that the store is not closed before
          * the searcher is acquired. */
    store.incRef();
    try {
        // can never be null
        final SearcherManager manager = getSearcherManager();
        /* This might throw NPE but that's fine we will run ensureOpen()
            *  in the catch block and throw the right exception */
        final IndexSearcher searcher = manager.acquire();
        try {
            final Searcher retVal = newSearcher(source, searcher, manager);
            success = true;
            return retVal;
        } finally {
            if (!success) {
                manager.release(searcher);
            }
        }
    } catch (AlreadyClosedException ex) {
        throw ex;
    } catch (Exception ex) {
        // throw EngineCloseException here if we are already closed
        ensureOpen();
        logger.error((Supplier<?>) () -> new ParameterizedMessage("failed to acquire searcher, source {}", source), ex);
        throw new EngineException(shardId, "failed to acquire searcher, source " + source, ex);
    } finally {
        if (!success) {
            // release the ref in the case of an error...
            store.decRef();
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) SearcherManager(org.apache.lucene.search.SearcherManager) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) NoSuchFileException(java.nio.file.NoSuchFileException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 2 with AlreadyClosedException

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

the class InternalEngine method refresh.

@Override
public void refresh(String source) throws EngineException {
    // since it flushes the index as well (though, in terms of concurrency, we are allowed to do it)
    try (ReleasableLock lock = readLock.acquire()) {
        ensureOpen();
        searcherManager.maybeRefreshBlocking();
    } catch (AlreadyClosedException e) {
        failOnTragicEvent(e);
        throw e;
    } catch (Exception e) {
        try {
            failEngine("refresh failed", e);
        } catch (Exception inner) {
            e.addSuppressed(inner);
        }
        throw new RefreshFailedEngineException(shardId, e);
    }
    // TODO: maybe we should just put a scheduled job in threadPool?
    // We check for pruning in each delete request, but we also prune here e.g. in case a delete burst comes in and then no more deletes
    // for a long time:
    maybePruneDeletedTombstones();
    versionMapRefreshPending.set(false);
    mergeScheduler.refreshConfig();
}
Also used : AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) TranslogCorruptedException(org.elasticsearch.index.translog.TranslogCorruptedException) IOException(java.io.IOException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException)

Example 3 with AlreadyClosedException

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

the class InternalEngine method forceMerge.

@Override
public void forceMerge(final boolean flush, int maxNumSegments, boolean onlyExpungeDeletes, final boolean upgrade, final boolean upgradeOnlyAncientSegments) throws EngineException, IOException {
    /*
         * We do NOT acquire the readlock here since we are waiting on the merges to finish
         * that's fine since the IW.rollback should stop all the threads and trigger an IOException
         * causing us to fail the forceMerge
         *
         * The way we implement upgrades is a bit hackish in the sense that we set an instance
         * variable and that this setting will thus apply to the next forced merge that will be run.
         * This is ok because (1) this is the only place we call forceMerge, (2) we have a single
         * thread for optimize, and the 'optimizeLock' guarding this code, and (3) ConcurrentMergeScheduler
         * syncs calls to findForcedMerges.
         */
    assert indexWriter.getConfig().getMergePolicy() instanceof ElasticsearchMergePolicy : "MergePolicy is " + indexWriter.getConfig().getMergePolicy().getClass().getName();
    ElasticsearchMergePolicy mp = (ElasticsearchMergePolicy) indexWriter.getConfig().getMergePolicy();
    optimizeLock.lock();
    try {
        ensureOpen();
        if (upgrade) {
            logger.info("starting segment upgrade upgradeOnlyAncientSegments={}", upgradeOnlyAncientSegments);
            mp.setUpgradeInProgress(true, upgradeOnlyAncientSegments);
        }
        // increment the ref just to ensure nobody closes the store while we optimize
        store.incRef();
        try {
            if (onlyExpungeDeletes) {
                assert upgrade == false;
                indexWriter.forceMergeDeletes(true);
            } else if (maxNumSegments <= 0) {
                assert upgrade == false;
                indexWriter.maybeMerge();
            } else {
                indexWriter.forceMerge(maxNumSegments, true);
            }
            if (flush) {
                if (tryRenewSyncCommit() == false) {
                    flush(false, true);
                }
            }
            if (upgrade) {
                logger.info("finished segment upgrade");
            }
        } finally {
            store.decRef();
        }
    } catch (AlreadyClosedException ex) {
        /* in this case we first check if the engine is still open. If so this exception is just fine
             * and expected. We don't hold any locks while we block on forceMerge otherwise it would block
             * closing the engine as well. If we are not closed we pass it on to failOnTragicEvent which ensures
             * we are handling a tragic even exception here */
        ensureOpen();
        failOnTragicEvent(ex);
        throw ex;
    } catch (Exception e) {
        try {
            maybeFailEngine("force merge", e);
        } catch (Exception inner) {
            e.addSuppressed(inner);
        }
        throw e;
    } finally {
        try {
            // reset it just to make sure we reset it in a case of an error
            mp.setUpgradeInProgress(false, false);
        } finally {
            optimizeLock.unlock();
        }
    }
}
Also used : AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) TranslogCorruptedException(org.elasticsearch.index.translog.TranslogCorruptedException) IOException(java.io.IOException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) ElasticsearchMergePolicy(org.elasticsearch.index.shard.ElasticsearchMergePolicy)

Example 4 with AlreadyClosedException

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

the class InternalEngine method writeIndexingBuffer.

@Override
public void writeIndexingBuffer() throws EngineException {
    // since it flushes the index as well (though, in terms of concurrency, we are allowed to do it)
    try (ReleasableLock lock = readLock.acquire()) {
        ensureOpen();
        // TODO: it's not great that we secretly tie searcher visibility to "freeing up heap" here... really we should keep two
        // searcher managers, one for searching which is only refreshed by the schedule the user requested (refresh_interval, or invoking
        // refresh API), and another for version map interactions.  See #15768.
        final long versionMapBytes = versionMap.ramBytesUsedForRefresh();
        final long indexingBufferBytes = indexWriter.ramBytesUsed();
        final boolean useRefresh = versionMapRefreshPending.get() || (indexingBufferBytes / 4 < versionMapBytes);
        if (useRefresh) {
            // The version map is using > 25% of the indexing buffer, so we do a refresh so the version map also clears
            logger.debug("use refresh to write indexing buffer (heap size=[{}]), to also clear version map (heap size=[{}])", new ByteSizeValue(indexingBufferBytes), new ByteSizeValue(versionMapBytes));
            refresh("write indexing buffer");
        } else {
            // Most of our heap is used by the indexing buffer, so we do a cheaper (just writes segments, doesn't open a new searcher) IW.flush:
            logger.debug("use IndexWriter.flush to write indexing buffer (heap size=[{}]) since version map is small (heap size=[{}])", new ByteSizeValue(indexingBufferBytes), new ByteSizeValue(versionMapBytes));
            indexWriter.flush();
        }
    } catch (AlreadyClosedException e) {
        failOnTragicEvent(e);
        throw e;
    } catch (Exception e) {
        try {
            failEngine("writeIndexingBuffer failed", e);
        } catch (Exception inner) {
            e.addSuppressed(inner);
        }
        throw new RefreshFailedEngineException(shardId, e);
    }
}
Also used : ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) TranslogCorruptedException(org.elasticsearch.index.translog.TranslogCorruptedException) IOException(java.io.IOException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException)

Example 5 with AlreadyClosedException

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

the class ShadowEngine method refresh.

@Override
public void refresh(String source) throws EngineException {
    // since it flushes the index as well (though, in terms of concurrency, we are allowed to do it)
    try (ReleasableLock lock = readLock.acquire()) {
        ensureOpen();
        searcherManager.maybeRefreshBlocking();
    } catch (AlreadyClosedException e) {
        throw e;
    } catch (Exception e) {
        try {
            failEngine("refresh failed", e);
        } catch (Exception inner) {
            e.addSuppressed(inner);
        }
        throw new RefreshFailedEngineException(shardId, e);
    }
}
Also used : AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException)

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