Search in sources :

Example 26 with ReleasableLock

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

the class Engine method close.

@Override
public void close() throws IOException {
    if (isClosed.get() == false) {
        // don't acquire the write lock if we are already closed
        logger.debug("close now acquiring writeLock");
        try (ReleasableLock lock = writeLock.acquire()) {
            logger.debug("close acquired writeLock");
            closeNoLock("api", closedLatch);
        }
    }
    awaitPendingClose();
}
Also used : ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock)

Example 27 with ReleasableLock

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

the class Translog method recoverFromFiles.

/**
 * recover all translog files found on disk
 */
private ArrayList<TranslogReader> recoverFromFiles(Checkpoint checkpoint) throws IOException {
    boolean success = false;
    ArrayList<TranslogReader> foundTranslogs = new ArrayList<>();
    try (ReleasableLock ignored = writeLock.acquire()) {
        logger.debug("open uncommitted translog checkpoint {}", checkpoint);
        final long minGenerationToRecoverFrom = checkpoint.minTranslogGeneration;
        // translog was found.
        for (long i = checkpoint.generation; i >= minGenerationToRecoverFrom; i--) {
            Path committedTranslogFile = location.resolve(getFilename(i));
            if (Files.exists(committedTranslogFile) == false) {
                throw new TranslogCorruptedException(committedTranslogFile.toString(), "translog file doesn't exist with generation: " + i + " recovering from: " + minGenerationToRecoverFrom + " checkpoint: " + checkpoint.generation + " - translog ids must be consecutive");
            }
            final Checkpoint readerCheckpoint = i == checkpoint.generation ? checkpoint : Checkpoint.read(location.resolve(getCommitCheckpointFileName(i)));
            final TranslogReader reader = openReader(committedTranslogFile, readerCheckpoint);
            assert reader.getPrimaryTerm() <= primaryTermSupplier.getAsLong() : "Primary terms go backwards; current term [" + primaryTermSupplier.getAsLong() + "] translog path [ " + committedTranslogFile + ", existing term [" + reader.getPrimaryTerm() + "]";
            foundTranslogs.add(reader);
            logger.debug("recovered local translog from checkpoint {}", checkpoint);
        }
        Collections.reverse(foundTranslogs);
        // when we clean up files, we first update the checkpoint with a new minReferencedTranslog and then delete them;
        // if we crash just at the wrong moment, it may be that we leave one unreferenced file behind so we delete it if there
        IOUtils.deleteFilesIgnoringExceptions(location.resolve(getFilename(minGenerationToRecoverFrom - 1)), location.resolve(getCommitCheckpointFileName(minGenerationToRecoverFrom - 1)));
        Path commitCheckpoint = location.resolve(getCommitCheckpointFileName(checkpoint.generation));
        if (Files.exists(commitCheckpoint)) {
            Checkpoint checkpointFromDisk = Checkpoint.read(commitCheckpoint);
            if (checkpoint.equals(checkpointFromDisk) == false) {
                throw new TranslogCorruptedException(commitCheckpoint.toString(), "checkpoint file " + commitCheckpoint.getFileName() + " already exists but has corrupted content: expected " + checkpoint + " but got " + checkpointFromDisk);
            }
        } else {
            copyCheckpointTo(commitCheckpoint);
        }
        success = true;
    } finally {
        if (success == false) {
            IOUtils.closeWhileHandlingException(foundTranslogs);
        }
    }
    return foundTranslogs;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock)

Example 28 with ReleasableLock

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

the class Translog method acquireRetentionLock.

/**
 * Acquires a lock on the translog files, preventing them from being trimmed
 */
public Closeable acquireRetentionLock() {
    try (ReleasableLock lock = readLock.acquire()) {
        ensureOpen();
        final long viewGen = getMinFileGeneration();
        return acquireTranslogGenFromDeletionPolicy(viewGen);
    }
}
Also used : ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock)

Example 29 with ReleasableLock

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

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) 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);
    }
}
Also used : ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock)

Example 30 with ReleasableLock

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

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.elasticsearch.common.util.concurrent.ReleasableLock)

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