Search in sources :

Example 31 with AutoCloseableLock

use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.

the class RaftLogBase method appendMetadataImpl.

private long appendMetadataImpl(long term, long newCommitIndex) {
    checkLogState();
    if (!shouldAppendMetadata(newCommitIndex)) {
        return INVALID_LOG_INDEX;
    }
    final LogEntryProto entry;
    final long nextIndex;
    try (AutoCloseableLock writeLock = writeLock()) {
        nextIndex = getNextIndex();
        entry = LogProtoUtils.toLogEntryProto(newCommitIndex, term, nextIndex);
        appendEntry(entry);
    }
    lastMetadataEntry = entry;
    return nextIndex;
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Example 32 with AutoCloseableLock

use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.

the class RaftStorageTestUtils method printLog.

static void printLog(RaftLogBase log, Consumer<String> println) {
    if (log == null) {
        println.accept("log == null");
        return;
    }
    final TermIndex last;
    final long flushed, committed;
    try (AutoCloseableLock readlock = log.readLock()) {
        last = log.getLastEntryTermIndex();
        flushed = log.getFlushIndex();
        committed = log.getLastCommittedIndex();
    }
    final StringBuilder b = new StringBuilder();
    for (long i = 0; i <= last.getIndex(); i++) {
        b.setLength(0);
        b.append(i == flushed ? 'f' : ' ');
        b.append(i == committed ? 'c' : ' ');
        b.append(String.format("%3d: ", i));
        try {
            b.append(LogProtoUtils.toLogEntryString(log.get(i)));
        } catch (RaftLogIOException e) {
            b.append(e);
        }
        println.accept(b.toString());
    }
}
Also used : RaftLogIOException(org.apache.ratis.server.raftlog.RaftLogIOException) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 33 with AutoCloseableLock

use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.

the class MemoryRaftLog method purgeImpl.

@Override
protected CompletableFuture<Long> purgeImpl(long index) {
    try (AutoCloseableLock writeLock = writeLock()) {
        Preconditions.assertTrue(index >= 0);
        entries.purge(Math.toIntExact(index));
    }
    return CompletableFuture.completedFuture(index);
}
Also used : AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Example 34 with AutoCloseableLock

use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.

the class SegmentedRaftLog method close.

@Override
public void close() throws IOException {
    try (AutoCloseableLock writeLock = writeLock()) {
        super.close();
        cache.close();
    }
    fileLogWorker.close();
    storage.close();
    getRaftLogMetrics().unregister();
}
Also used : AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Example 35 with AutoCloseableLock

use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.

the class SegmentedRaftLog method get.

@Override
public LogEntryProto get(long index) throws RaftLogIOException {
    checkLogState();
    final LogSegment segment;
    final LogRecord record;
    try (AutoCloseableLock readLock = readLock()) {
        segment = cache.getSegment(index);
        if (segment == null) {
            return null;
        }
        record = segment.getLogRecord(index);
        if (record == null) {
            return null;
        }
        final LogEntryProto entry = segment.getEntryFromCache(record.getTermIndex());
        if (entry != null) {
            getRaftLogMetrics().onRaftLogCacheHit();
            return entry;
        }
    }
    // the entry is not in the segment's cache. Load the cache without holding the lock.
    getRaftLogMetrics().onRaftLogCacheMiss();
    checkAndEvictCache();
    return segment.loadCache(record);
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) LogRecord(org.apache.ratis.server.raftlog.segmented.LogSegment.LogRecord) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Aggregations

AutoCloseableLock (org.apache.ratis.util.AutoCloseableLock)35 TermIndex (org.apache.ratis.server.protocol.TermIndex)9 IOException (java.io.IOException)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)4 LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)4 Timer (com.codahale.metrics.Timer)2 File (java.io.File)2 Expression (org.apache.ratis.examples.arithmetic.expression.Expression)2 RaftLogIOException (org.apache.ratis.server.raftlog.RaftLogIOException)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 RaftPeerRole (org.apache.ratis.proto.RaftProtos.RaftPeerRole)1 Message (org.apache.ratis.protocol.Message)1 StateMachineException (org.apache.ratis.protocol.StateMachineException)1 StateMachineException (org.apache.ratis.protocol.exceptions.StateMachineException)1