Search in sources :

Example 46 with ReleasableLock

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

the class Translog method getMaxSeqNo.

/**
 * Returns the max seq_no of translog operations found in this translog. Since this value is calculated based on the current
 * existing readers, this value is not necessary to be the max seq_no of all operations have been stored in this translog.
 */
public long getMaxSeqNo() {
    try (ReleasableLock ignored = readLock.acquire()) {
        ensureOpen();
        final OptionalLong maxSeqNo = Stream.concat(readers.stream(), Stream.of(current)).mapToLong(reader -> reader.getCheckpoint().maxSeqNo).max();
        assert maxSeqNo.isPresent() : "must have at least one translog generation";
        return maxSeqNo.getAsLong();
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) Versions(org.elasticsearch.common.lucene.uid.Versions) Arrays(java.util.Arrays) LongSupplier(java.util.function.LongSupplier) Releasables(org.elasticsearch.common.lease.Releasables) BigArrays(org.elasticsearch.common.util.BigArrays) Term(org.apache.lucene.index.Term) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) VersionType(org.elasticsearch.index.VersionType) Matcher(java.util.regex.Matcher) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) Path(java.nio.file.Path) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Releasable(org.elasticsearch.common.lease.Releasable) UUIDs(org.elasticsearch.common.UUIDs) StandardOpenOption(java.nio.file.StandardOpenOption) BytesReference(org.elasticsearch.common.bytes.BytesReference) EOFException(java.io.EOFException) Collectors(java.util.stream.Collectors) Engine(org.elasticsearch.index.engine.Engine) Objects(java.util.Objects) List(java.util.List) Version(org.elasticsearch.Version) Stream(java.util.stream.Stream) Optional(java.util.Optional) ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) Pattern(java.util.regex.Pattern) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) BytesArray(org.elasticsearch.common.bytes.BytesArray) IndexShardComponent(org.elasticsearch.index.shard.IndexShardComponent) OptionalLong(java.util.OptionalLong) Constants(io.crate.Constants) IndexSettings(org.elasticsearch.index.IndexSettings) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock) SequenceNumbers(org.elasticsearch.index.seqno.SequenceNumbers) Iterator(java.util.Iterator) Files(java.nio.file.Files) IOUtils(io.crate.common.io.IOUtils) IOException(java.io.IOException) LongConsumer(java.util.function.LongConsumer) AbstractIndexShardComponent(org.elasticsearch.index.shard.AbstractIndexShardComponent) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Closeable(java.io.Closeable) FileChannel(java.nio.channels.FileChannel) Collections(java.util.Collections) OptionalLong(java.util.OptionalLong) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock)

Example 47 with ReleasableLock

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

the class Translog method add.

/**
 * Adds an operation to the transaction log.
 *
 * @param operation the operation to add
 * @return the location of the operation in the translog
 * @throws IOException if adding the operation to the translog resulted in an I/O exception
 */
public Location add(final Operation operation) throws IOException {
    final ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(bigArrays);
    boolean successfullySerialized = false;
    try {
        final long start = out.position();
        out.skip(Integer.BYTES);
        writeOperationNoSize(new BufferedChecksumStreamOutput(out), operation);
        final long end = out.position();
        final int operationSize = (int) (end - Integer.BYTES - start);
        out.seek(start);
        out.writeInt(operationSize);
        out.seek(end);
        successfullySerialized = true;
        try (ReleasableBytesReference bytes = new ReleasableBytesReference(out.bytes(), out);
            ReleasableLock ignored = readLock.acquire()) {
            ensureOpen();
            if (operation.primaryTerm() > current.getPrimaryTerm()) {
                assert false : "Operation term is newer than the current term; " + "current term[" + current.getPrimaryTerm() + "], operation term[" + operation + "]";
                throw new IllegalArgumentException("Operation term is newer than the current term; " + "current term[" + current.getPrimaryTerm() + "], operation term[" + operation + "]");
            }
            return current.add(bytes, operation.seqNo());
        }
    } catch (final AlreadyClosedException | IOException ex) {
        closeOnTragicEvent(ex);
        throw ex;
    } catch (final Exception ex) {
        closeOnTragicEvent(ex);
        throw new TranslogException(shardId, "Failed to write operation [" + operation + "]", ex);
    } finally {
        if (successfullySerialized == false) {
            Releasables.close(out);
        }
    }
}
Also used : ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) EOFException(java.io.EOFException) IOException(java.io.IOException) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)

Example 48 with ReleasableLock

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

the class TranslogWriter method syncUpTo.

/**
 * Syncs the translog up to at least the given offset unless already synced
 *
 * @return <code>true</code> if this call caused an actual sync operation
 */
public boolean syncUpTo(long offset) throws IOException {
    if (lastSyncedCheckpoint.offset < offset && syncNeeded()) {
        synchronized (syncLock) {
            // only one sync/checkpoint should happen concurrently but we wait
            if (lastSyncedCheckpoint.offset < offset && syncNeeded()) {
                // double checked locking - we don't want to fsync unless we have to and now that we have
                // the lock we should check again since if this code is busy we might have fsynced enough already
                final Checkpoint checkpointToSync;
                final LongArrayList flushedSequenceNumbers;
                final ArrayDeque<ReleasableBytesReference> toWrite;
                try (ReleasableLock toClose = writeLock.acquire()) {
                    synchronized (this) {
                        ensureOpen();
                        checkpointToSync = getCheckpoint();
                        toWrite = pollOpsToWrite();
                        flushedSequenceNumbers = nonFsyncedSequenceNumbers;
                        nonFsyncedSequenceNumbers = new LongArrayList(64);
                    }
                    try {
                        // Write ops will release operations.
                        writeAndReleaseOps(toWrite);
                    } catch (final Exception ex) {
                        closeWithTragicEvent(ex);
                        throw ex;
                    }
                }
                // we can continue writing to the buffer etc.
                try {
                    channel.force(false);
                    writeCheckpoint(checkpointChannel, checkpointPath, checkpointToSync);
                } catch (final Exception ex) {
                    closeWithTragicEvent(ex);
                    throw ex;
                }
                flushedSequenceNumbers.forEach((LongProcedure) persistedSequenceNumberConsumer::accept);
                assert lastSyncedCheckpoint.offset <= checkpointToSync.offset : "illegal state: " + lastSyncedCheckpoint.offset + " <= " + checkpointToSync.offset;
                // write protected by syncLock
                lastSyncedCheckpoint = checkpointToSync;
                return true;
            }
        }
    }
    return false;
}
Also used : ReleasableBytesReference(org.elasticsearch.common.bytes.ReleasableBytesReference) LongArrayList(com.carrotsearch.hppc.LongArrayList) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) 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