Search in sources :

Example 26 with ReleasableLock

use of org.opensearch.common.util.concurrent.ReleasableLock in project OpenSearch by opensearch-project.

the class NRTReplicationEngine method trimUnreferencedTranslogFiles.

@Override
public void trimUnreferencedTranslogFiles() throws EngineException {
    try (ReleasableLock lock = readLock.acquire()) {
        ensureOpen();
        translog.trimUnreferencedReaders();
    } catch (Exception e) {
        try {
            failEngine("translog trimming failed", e);
        } catch (Exception inner) {
            e.addSuppressed(inner);
        }
        throw new EngineException(shardId, "failed to trim translog", e);
    }
}
Also used : ReleasableLock(org.opensearch.common.util.concurrent.ReleasableLock) IOException(java.io.IOException)

Example 27 with ReleasableLock

use of org.opensearch.common.util.concurrent.ReleasableLock in project OpenSearch by opensearch-project.

the class NoOpEngine method trimUnreferencedTranslogFiles.

/**
 * This implementation will trim existing translog files using a {@link TranslogDeletionPolicy}
 * that retains nothing but the last translog generation from safe commit.
 */
@Override
public void trimUnreferencedTranslogFiles() {
    final Store store = this.engineConfig.getStore();
    store.incRef();
    try (ReleasableLock lock = readLock.acquire()) {
        ensureOpen();
        final List<IndexCommit> commits = DirectoryReader.listCommits(store.directory());
        if (commits.size() == 1 && translogStats.getTranslogSizeInBytes() > translogStats.getUncommittedSizeInBytes()) {
            final Map<String, String> commitUserData = getLastCommittedSegmentInfos().getUserData();
            final String translogUuid = commitUserData.get(Translog.TRANSLOG_UUID_KEY);
            if (translogUuid == null) {
                throw new IllegalStateException("commit doesn't contain translog unique id");
            }
            final TranslogConfig translogConfig = engineConfig.getTranslogConfig();
            final long localCheckpoint = Long.parseLong(commitUserData.get(SequenceNumbers.LOCAL_CHECKPOINT_KEY));
            final TranslogDeletionPolicy translogDeletionPolicy = new DefaultTranslogDeletionPolicy(-1, -1, 0);
            translogDeletionPolicy.setLocalCheckpointOfSafeCommit(localCheckpoint);
            try (Translog translog = new Translog(translogConfig, translogUuid, translogDeletionPolicy, engineConfig.getGlobalCheckpointSupplier(), engineConfig.getPrimaryTermSupplier(), seqNo -> {
            })) {
                translog.trimUnreferencedReaders();
                // refresh the translog stats
                this.translogStats = translog.stats();
                assert translog.currentFileGeneration() == translog.getMinFileGeneration() : "translog was not trimmed " + " current gen " + translog.currentFileGeneration() + " != min gen " + translog.getMinFileGeneration();
            }
        }
    } catch (final Exception e) {
        try {
            failEngine("translog trimming failed", e);
        } catch (Exception inner) {
            e.addSuppressed(inner);
        }
        throw new EngineException(shardId, "failed to trim translog", e);
    } finally {
        store.decRef();
    }
}
Also used : TranslogConfig(org.opensearch.index.translog.TranslogConfig) Store(org.opensearch.index.store.Store) ReleasableLock(org.opensearch.common.util.concurrent.ReleasableLock) IndexCommit(org.apache.lucene.index.IndexCommit) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Translog(org.opensearch.index.translog.Translog) DefaultTranslogDeletionPolicy(org.opensearch.index.translog.DefaultTranslogDeletionPolicy) TranslogDeletionPolicy(org.opensearch.index.translog.TranslogDeletionPolicy) DefaultTranslogDeletionPolicy(org.opensearch.index.translog.DefaultTranslogDeletionPolicy)

Example 28 with ReleasableLock

use of org.opensearch.common.util.concurrent.ReleasableLock in project OpenSearch by opensearch-project.

the class Translog method newSnapshot.

/**
 * Creates a new translog snapshot containing operations from the given range.
 *
 * @param fromSeqNo the lower bound of the range (inclusive)
 * @param toSeqNo   the upper bound of the range (inclusive)
 * @return the new snapshot
 */
public Snapshot newSnapshot(long fromSeqNo, long toSeqNo, boolean requiredFullRange) throws IOException {
    assert fromSeqNo <= toSeqNo : fromSeqNo + " > " + toSeqNo;
    assert fromSeqNo >= 0 : "from_seq_no must be non-negative " + fromSeqNo;
    try (ReleasableLock ignored = readLock.acquire()) {
        ensureOpen();
        TranslogSnapshot[] snapshots = Stream.concat(readers.stream(), Stream.of(current)).filter(reader -> reader.getCheckpoint().minSeqNo <= toSeqNo && fromSeqNo <= reader.getCheckpoint().maxEffectiveSeqNo()).map(BaseTranslogReader::newSnapshot).toArray(TranslogSnapshot[]::new);
        final Snapshot snapshot = newMultiSnapshot(snapshots);
        return new SeqNoFilterSnapshot(snapshot, fromSeqNo, toSeqNo, requiredFullRange);
    }
}
Also used : ReleasableLock(org.opensearch.common.util.concurrent.ReleasableLock)

Example 29 with ReleasableLock

use of org.opensearch.common.util.concurrent.ReleasableLock in project OpenSearch by opensearch-project.

the class Translog method closeFilesIfNoPendingRetentionLocks.

void closeFilesIfNoPendingRetentionLocks() throws IOException {
    try (ReleasableLock ignored = writeLock.acquire()) {
        if (closed.get() && deletionPolicy.pendingTranslogRefCount() == 0) {
            logger.trace("closing files. translog is closed and there are no pending retention locks");
            ArrayList<Closeable> toClose = new ArrayList<>(readers);
            toClose.add(current);
            IOUtils.close(toClose);
        }
    }
}
Also used : Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) ReleasableLock(org.opensearch.common.util.concurrent.ReleasableLock)

Example 30 with ReleasableLock

use of org.opensearch.common.util.concurrent.ReleasableLock in project OpenSearch by opensearch-project.

the class Translog method trimUnreferencedReaders.

/**
 * Trims unreferenced translog generations by asking {@link TranslogDeletionPolicy} for the minimum
 * required generation
 */
public void trimUnreferencedReaders() throws IOException {
    // first check under read lock if any readers can be trimmed
    try (ReleasableLock ignored = readLock.acquire()) {
        if (closed.get()) {
            // we're shutdown potentially on some tragic event, don't delete anything
            return;
        }
        if (getMinReferencedGen() == getMinFileGeneration()) {
            return;
        }
    }
    // move most of the data to disk to reduce the time the write lock is held
    sync();
    try (ReleasableLock ignored = writeLock.acquire()) {
        if (closed.get()) {
            // we're shutdown potentially on some tragic event, don't delete anything
            return;
        }
        final long minReferencedGen = getMinReferencedGen();
        for (Iterator<TranslogReader> iterator = readers.iterator(); iterator.hasNext(); ) {
            TranslogReader reader = iterator.next();
            if (reader.getGeneration() >= minReferencedGen) {
                break;
            }
            iterator.remove();
            IOUtils.closeWhileHandlingException(reader);
            final Path translogPath = reader.path();
            logger.trace("delete translog file [{}], not referenced and not current anymore", translogPath);
            // The checkpoint is used when opening the translog to know which files should be recovered from.
            // We now update the checkpoint to ignore the file we are going to remove.
            // Note that there is a provision in recoverFromFiles to allow for the case where we synced the checkpoint
            // but crashed before we could delete the file.
            // sync at once to make sure that there's at most one unreferenced generation.
            current.sync();
            deleteReaderFiles(reader);
        }
        assert readers.isEmpty() == false || current.generation == minReferencedGen : "all readers were cleaned but the minReferenceGen [" + minReferencedGen + "] is not the current writer's gen [" + current.generation + "]";
    } catch (final Exception ex) {
        closeOnTragicEvent(ex);
        throw ex;
    }
}
Also used : Path(java.nio.file.Path) ReleasableLock(org.opensearch.common.util.concurrent.ReleasableLock) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) EOFException(java.io.EOFException) MissingHistoryOperationsException(org.opensearch.index.engine.MissingHistoryOperationsException) IOException(java.io.IOException)

Aggregations

ReleasableLock (org.opensearch.common.util.concurrent.ReleasableLock)30 IOException (java.io.IOException)18 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)12 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)7 TranslogCorruptedException (org.opensearch.index.translog.TranslogCorruptedException)7 ArrayList (java.util.ArrayList)5 EOFException (java.io.EOFException)4 Path (java.nio.file.Path)4 HashMap (java.util.HashMap)4 Releasable (org.opensearch.common.lease.Releasable)4 MissingHistoryOperationsException (org.opensearch.index.engine.MissingHistoryOperationsException)4 Translog (org.opensearch.index.translog.Translog)4 Closeable (java.io.Closeable)3 Arrays (java.util.Arrays)3 Iterator (java.util.Iterator)3 LongPoint (org.apache.lucene.document.LongPoint)3 FileChannel (java.nio.channels.FileChannel)2 Files (java.nio.file.Files)2 StandardOpenOption (java.nio.file.StandardOpenOption)2 Collections (java.util.Collections)2