Search in sources :

Example 21 with ReleasableLock

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

the class InternalEngine method restoreLocalHistoryFromTranslog.

@Override
public int restoreLocalHistoryFromTranslog(TranslogRecoveryRunner translogRecoveryRunner) throws IOException {
    try (ReleasableLock ignored = readLock.acquire()) {
        ensureOpen();
        final long localCheckpoint = localCheckpointTracker.getProcessedCheckpoint();
        try (Translog.Snapshot snapshot = getTranslog().newSnapshot(localCheckpoint + 1, Long.MAX_VALUE)) {
            return translogRecoveryRunner.run(this, snapshot);
        }
    }
}
Also used : ReleasableLock(org.opensearch.common.util.concurrent.ReleasableLock) Translog(org.opensearch.index.translog.Translog)

Example 22 with ReleasableLock

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

the class InternalEngine method get.

@Override
public GetResult get(Get get, BiFunction<String, SearcherScope, Engine.Searcher> searcherFactory) throws EngineException {
    assert Objects.equals(get.uid().field(), IdFieldMapper.NAME) : get.uid().field();
    try (ReleasableLock ignored = readLock.acquire()) {
        ensureOpen();
        SearcherScope scope;
        if (get.realtime()) {
            VersionValue versionValue = null;
            try (Releasable ignore = versionMap.acquireLock(get.uid().bytes())) {
                // we need to lock here to access the version map to do this truly in RT
                versionValue = getVersionFromMap(get.uid().bytes());
            }
            if (versionValue != null) {
                if (versionValue.isDelete()) {
                    return GetResult.NOT_EXISTS;
                }
                if (get.versionType().isVersionConflictForReads(versionValue.version, get.version())) {
                    throw new VersionConflictEngineException(shardId, get.id(), get.versionType().explainConflictForReads(versionValue.version, get.version()));
                }
                if (get.getIfSeqNo() != SequenceNumbers.UNASSIGNED_SEQ_NO && (get.getIfSeqNo() != versionValue.seqNo || get.getIfPrimaryTerm() != versionValue.term)) {
                    throw new VersionConflictEngineException(shardId, get.id(), get.getIfSeqNo(), get.getIfPrimaryTerm(), versionValue.seqNo, versionValue.term);
                }
                if (get.isReadFromTranslog()) {
                    // the update call doesn't need the consistency since it's source only + _parent but parent can go away in 7.0
                    if (versionValue.getLocation() != null) {
                        try {
                            Translog.Operation operation = translog.readOperation(versionValue.getLocation());
                            if (operation != null) {
                                // in the case of a already pruned translog generation we might get null here - yet very unlikely
                                final Translog.Index index = (Translog.Index) operation;
                                TranslogLeafReader reader = new TranslogLeafReader(index);
                                return new GetResult(new Engine.Searcher("realtime_get", reader, IndexSearcher.getDefaultSimilarity(), null, IndexSearcher.getDefaultQueryCachingPolicy(), reader), new VersionsAndSeqNoResolver.DocIdAndVersion(0, index.version(), index.seqNo(), index.primaryTerm(), reader, 0), true);
                            }
                        } catch (IOException e) {
                            // lets check if the translog has failed with a tragic event
                            maybeFailEngine("realtime_get", e);
                            throw new EngineException(shardId, "failed to read operation from translog", e);
                        }
                    } else {
                        trackTranslogLocation.set(true);
                    }
                }
                assert versionValue.seqNo >= 0 : versionValue;
                refreshIfNeeded("realtime_get", versionValue.seqNo);
            }
            scope = SearcherScope.INTERNAL;
        } else {
            // we expose what has been externally expose in a point in time snapshot via an explicit refresh
            scope = SearcherScope.EXTERNAL;
        }
        // no version, get the version from the index, we know that we refresh on flush
        return getFromSearcher(get, searcherFactory, scope);
    }
}
Also used : VersionsAndSeqNoResolver(org.opensearch.common.lucene.uid.VersionsAndSeqNoResolver) IOException(java.io.IOException) ReleasableLock(org.opensearch.common.util.concurrent.ReleasableLock) Translog(org.opensearch.index.translog.Translog) Releasable(org.opensearch.common.lease.Releasable)

Example 23 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 24 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)

Example 25 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)

Aggregations

ReleasableLock (org.opensearch.common.util.concurrent.ReleasableLock)27 IOException (java.io.IOException)15 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