Search in sources :

Example 16 with ReleasableLock

use of org.elasticsearch.common.util.concurrent.ReleasableLock in project elasticsearch by elastic.

the class Translog method commit.

@Override
public long commit() throws IOException {
    try (ReleasableLock lock = writeLock.acquire()) {
        ensureOpen();
        if (currentCommittingGeneration == NOT_SET_GENERATION) {
            prepareCommit();
        }
        assert currentCommittingGeneration != NOT_SET_GENERATION;
        assert readers.stream().filter(r -> r.getGeneration() == currentCommittingGeneration).findFirst().isPresent() : "reader list doesn't contain committing generation [" + currentCommittingGeneration + "]";
        // this is important - otherwise old files will not be cleaned up
        lastCommittedTranslogFileGeneration = current.getGeneration();
        currentCommittingGeneration = NOT_SET_GENERATION;
        trimUnreferencedReaders();
    }
    return 0;
}
Also used : ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock)

Example 17 with ReleasableLock

use of org.elasticsearch.common.util.concurrent.ReleasableLock in project elasticsearch by elastic.

the class Translog method newView.

/**
     * Returns a view into the current translog that is guaranteed to retain all current operations
     * while receiving future ones as well
     */
public Translog.View newView() {
    try (ReleasableLock lock = readLock.acquire()) {
        ensureOpen();
        View view = new View(lastCommittedTranslogFileGeneration);
        outstandingViews.add(view);
        return view;
    }
}
Also used : ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock)

Example 18 with ReleasableLock

use of org.elasticsearch.common.util.concurrent.ReleasableLock in project elasticsearch by elastic.

the class Translog method recoverFromFiles.

/** recover all translog files found on disk */
private ArrayList<TranslogReader> recoverFromFiles(TranslogGeneration translogGeneration, Checkpoint checkpoint) throws IOException {
    boolean success = false;
    ArrayList<TranslogReader> foundTranslogs = new ArrayList<>();
    // a temp file to copy checkpoint to - note it must be in on the same FS otherwise atomic move won't work
    final Path tempFile = Files.createTempFile(location, TRANSLOG_FILE_PREFIX, TRANSLOG_FILE_SUFFIX);
    boolean tempFileRenamed = false;
    try (ReleasableLock lock = writeLock.acquire()) {
        logger.debug("open uncommitted translog checkpoint {}", checkpoint);
        final String checkpointTranslogFile = getFilename(checkpoint.generation);
        for (long i = translogGeneration.translogFileGeneration; i < checkpoint.generation; i++) {
            Path committedTranslogFile = location.resolve(getFilename(i));
            if (Files.exists(committedTranslogFile) == false) {
                throw new IllegalStateException("translog file doesn't exist with generation: " + i + " lastCommitted: " + lastCommittedTranslogFileGeneration + " checkpoint: " + checkpoint.generation + " - translog ids must be consecutive");
            }
            final TranslogReader reader = openReader(committedTranslogFile, Checkpoint.read(location.resolve(getCommitCheckpointFileName(i))));
            foundTranslogs.add(reader);
            logger.debug("recovered local translog from checkpoint {}", checkpoint);
        }
        foundTranslogs.add(openReader(location.resolve(checkpointTranslogFile), checkpoint));
        Path commitCheckpoint = location.resolve(getCommitCheckpointFileName(checkpoint.generation));
        if (Files.exists(commitCheckpoint)) {
            Checkpoint checkpointFromDisk = Checkpoint.read(commitCheckpoint);
            if (checkpoint.equals(checkpointFromDisk) == false) {
                throw new IllegalStateException("Checkpoint file " + commitCheckpoint.getFileName() + " already exists but has corrupted content expected: " + checkpoint + " but got: " + checkpointFromDisk);
            }
        } else {
            // we first copy this into the temp-file and then fsync it followed by an atomic move into the target file
            // that way if we hit a disk-full here we are still in an consistent state.
            Files.copy(location.resolve(CHECKPOINT_FILE_NAME), tempFile, StandardCopyOption.REPLACE_EXISTING);
            IOUtils.fsync(tempFile, false);
            Files.move(tempFile, commitCheckpoint, StandardCopyOption.ATOMIC_MOVE);
            tempFileRenamed = true;
            // we only fsync the directory the tempFile was already fsynced
            IOUtils.fsync(commitCheckpoint.getParent(), true);
        }
        success = true;
    } finally {
        if (success == false) {
            IOUtils.closeWhileHandlingException(foundTranslogs);
        }
        if (tempFileRenamed == false) {
            try {
                Files.delete(tempFile);
            } catch (IOException ex) {
                logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to delete temp file {}", tempFile), ex);
            }
        }
    }
    return foundTranslogs;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock) LongSupplier(java.util.function.LongSupplier) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 19 with ReleasableLock

use of org.elasticsearch.common.util.concurrent.ReleasableLock in project crate by crate.

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();
        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) {
            // no version, get the version from the index, we know that we refresh on flush
            return getFromSearcher(get, searcherFactory, SearcherScope.INTERNAL);
        }
        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 (versionValue.getLocation() != null) {
            try {
                Translog.Operation operation = translog.readOperation(versionValue.getLocation());
                // in the case of a already pruned translog generation we might get null here - yet very unlikely
                if (operation == null) {
                    assert versionValue.seqNo >= 0 : versionValue;
                    refreshIfNeeded("realtime_get", versionValue.seqNo);
                    return getFromSearcher(get, searcherFactory, SearcherScope.INTERNAL);
                }
                final Translog.Index index = (Translog.Index) operation;
                TranslogLeafReader reader = new TranslogLeafReader(index);
                return new GetResult(new VersionsAndSeqNoResolver.DocIdAndVersion(0, index.version(), index.seqNo(), index.primaryTerm(), reader, 0), new Engine.Searcher("realtime_get", reader, null, IndexSearcher.getDefaultQueryCachingPolicy(), reader));
            } 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);
        // no version, get the version from the index, we know that we refresh on flush
        return getFromSearcher(get, searcherFactory, SearcherScope.INTERNAL);
    }
}
Also used : VersionsAndSeqNoResolver(org.elasticsearch.common.lucene.uid.VersionsAndSeqNoResolver) IOException(java.io.IOException) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock) Translog(org.elasticsearch.index.translog.Translog) Releasable(org.elasticsearch.common.lease.Releasable)

Example 20 with ReleasableLock

use of org.elasticsearch.common.util.concurrent.ReleasableLock in project crate by crate.

the class InternalEngine method noOp.

@Override
public NoOpResult noOp(final NoOp noOp) throws IOException {
    final NoOpResult noOpResult;
    try (ReleasableLock ignored = readLock.acquire()) {
        ensureOpen();
        noOpResult = innerNoOp(noOp);
    } catch (final Exception e) {
        try {
            maybeFailEngine("noop", e);
        } catch (Exception inner) {
            e.addSuppressed(inner);
        }
        throw e;
    }
    return noOpResult;
}
Also used : 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)

Aggregations

ReleasableLock (org.elasticsearch.common.util.concurrent.ReleasableLock)48 IOException (java.io.IOException)30 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)21 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)12 TranslogCorruptedException (org.elasticsearch.index.translog.TranslogCorruptedException)12 ArrayList (java.util.ArrayList)7 EOFException (java.io.EOFException)6 Path (java.nio.file.Path)6 Translog (org.elasticsearch.index.translog.Translog)6 IndexFormatTooOldException (org.apache.lucene.index.IndexFormatTooOldException)5 Releasable (org.elasticsearch.common.lease.Releasable)5 Closeable (java.io.Closeable)4 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)3 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)3 LongSupplier (java.util.function.LongSupplier)3 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)3 ReleasableBytesReference (org.elasticsearch.common.bytes.ReleasableBytesReference)3 ReleasableBytesStreamOutput (org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)3 FileChannel (java.nio.channels.FileChannel)2 Files (java.nio.file.Files)2