Search in sources :

Example 31 with ReleasableLock

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

the class Translog method prepareCommit.

@Override
public long prepareCommit() throws IOException {
    try (ReleasableLock lock = writeLock.acquire()) {
        ensureOpen();
        if (currentCommittingGeneration != NOT_SET_GENERATION) {
            throw new IllegalStateException("already committing a translog with generation: " + currentCommittingGeneration);
        }
        currentCommittingGeneration = current.getGeneration();
        TranslogReader currentCommittingTranslog = current.closeIntoReader();
        readers.add(currentCommittingTranslog);
        Path checkpoint = location.resolve(CHECKPOINT_FILE_NAME);
        assert Checkpoint.read(checkpoint).generation == currentCommittingTranslog.getGeneration();
        Path commitCheckpoint = location.resolve(getCommitCheckpointFileName(currentCommittingTranslog.getGeneration()));
        Files.copy(checkpoint, commitCheckpoint);
        IOUtils.fsync(commitCheckpoint, false);
        IOUtils.fsync(commitCheckpoint.getParent(), true);
        // create a new translog file - this will sync it and update the checkpoint data;
        current = createWriter(current.getGeneration() + 1);
        logger.trace("current translog set to [{}]", current.getGeneration());
    } catch (Exception e) {
        // tragic event
        IOUtils.closeWhileHandlingException(this);
        throw e;
    }
    return 0L;
}
Also used : Path(java.nio.file.Path) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 32 with ReleasableLock

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

the class Translog method closeFilesIfNoPendingViews.

void closeFilesIfNoPendingViews() throws IOException {
    try (ReleasableLock ignored = writeLock.acquire()) {
        if (closed.get() && outstandingViews.isEmpty()) {
            logger.trace("closing files. translog is closed and there are no pending views");
            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)

Example 33 with ReleasableLock

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

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);
    try {
        final BufferedChecksumStreamOutput checksumStreamOutput = new BufferedChecksumStreamOutput(out);
        final long start = out.position();
        out.skip(Integer.BYTES);
        writeOperationNoSize(checksumStreamOutput, operation);
        final long end = out.position();
        final int operationSize = (int) (end - Integer.BYTES - start);
        out.seek(start);
        out.writeInt(operationSize);
        out.seek(end);
        final ReleasablePagedBytesReference bytes = out.bytes();
        try (ReleasableLock ignored = readLock.acquire()) {
            ensureOpen();
            return current.add(bytes, operation.seqNo());
        }
    } catch (final AlreadyClosedException | IOException ex) {
        try {
            closeOnTragicEvent(ex);
        } catch (final Exception inner) {
            ex.addSuppressed(inner);
        }
        throw ex;
    } catch (final Exception e) {
        try {
            closeOnTragicEvent(e);
        } catch (final Exception inner) {
            e.addSuppressed(inner);
        }
        throw new TranslogException(shardId, "Failed to write operation [" + operation + "]", e);
    } finally {
        Releasables.close(out.bytes());
    }
}
Also used : ReleasablePagedBytesReference(org.elasticsearch.common.bytes.ReleasablePagedBytesReference) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 34 with ReleasableLock

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

the class Cache method put.

private void put(K key, V value, long now) {
    CacheSegment<K, V> segment = getCacheSegment(key);
    Tuple<Entry<K, V>, Entry<K, V>> tuple = segment.put(key, value, now);
    boolean replaced = false;
    try (ReleasableLock ignored = lruLock.acquire()) {
        if (tuple.v2() != null && tuple.v2().state == State.EXISTING) {
            if (unlink(tuple.v2())) {
                replaced = true;
            }
        }
        promote(tuple.v1(), now);
    }
    if (replaced) {
        removalListener.onRemoval(new RemovalNotification<>(tuple.v2().key, tuple.v2().value, RemovalNotification.RemovalReason.REPLACED));
    }
}
Also used : ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock)

Example 35 with ReleasableLock

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

the class InternalEngine method segments.

@Override
public List<Segment> segments(boolean verbose) {
    try (ReleasableLock lock = readLock.acquire()) {
        Segment[] segmentsArr = getSegmentInfo(lastCommittedSegmentInfos, verbose);
        // fill in the merges flag
        Set<OnGoingMerge> onGoingMerges = mergeScheduler.onGoingMerges();
        for (OnGoingMerge onGoingMerge : onGoingMerges) {
            for (SegmentCommitInfo segmentInfoPerCommit : onGoingMerge.getMergedSegments()) {
                for (Segment segment : segmentsArr) {
                    if (segment.getName().equals(segmentInfoPerCommit.info.name)) {
                        segment.mergeId = onGoingMerge.getId();
                        break;
                    }
                }
            }
        }
        return Arrays.asList(segmentsArr);
    }
}
Also used : SegmentCommitInfo(org.apache.lucene.index.SegmentCommitInfo) OnGoingMerge(org.elasticsearch.index.merge.OnGoingMerge) 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