Search in sources :

Example 1 with AutoCloseableLock

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

the class MemoryRaftLog method getEntries.

@Override
public TermIndex[] getEntries(long startIndex, long endIndex) {
    checkLogState();
    try (AutoCloseableLock readLock = readLock()) {
        final int from = (int) startIndex;
        if (startIndex >= entries.size()) {
            return null;
        }
        final int to = (int) Math.min(entries.size(), endIndex);
        TermIndex[] ti = new TermIndex[to - from];
        for (int i = 0; i < ti.length; i++) {
            ti[i] = TermIndex.newTermIndex(entries.get(i).getTerm(), entries.get(i).getIndex());
        }
        return ti;
    }
}
Also used : AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 2 with AutoCloseableLock

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

the class RaftLog method updateLastCommitted.

/**
 * Update the last committed index.
 * @param majorityIndex the index that has achieved majority.
 * @param currentTerm the current term.
 * @return true if update is applied; otherwise, return false, i.e. no update required.
 */
public boolean updateLastCommitted(long majorityIndex, long currentTerm) {
    try (AutoCloseableLock writeLock = writeLock()) {
        if (lastCommitted.get() < majorityIndex) {
            // Only update last committed index for current term. See ยง5.4.2 in
            // paper for details.
            final TermIndex entry = getTermIndex(majorityIndex);
            if (entry != null && entry.getTerm() == currentTerm) {
                LOG.debug("{}: Updating lastCommitted to {}", selfId, majorityIndex);
                lastCommitted.set(majorityIndex);
                return true;
            }
        }
    }
    return false;
}
Also used : AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 3 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();
    LogSegment segment;
    LogRecordWithEntry recordAndEntry;
    try (AutoCloseableLock readLock = readLock()) {
        segment = cache.getSegment(index);
        if (segment == null) {
            return null;
        }
        recordAndEntry = segment.getEntryWithoutLoading(index);
        if (recordAndEntry == null) {
            return null;
        }
        if (recordAndEntry.hasEntry()) {
            return recordAndEntry.getEntry();
        }
    }
    // the entry is not in the segment's cache. Load the cache without holding
    // RaftLog's lock.
    checkAndEvictCache();
    return segment.loadCache(recordAndEntry.getRecord());
}
Also used : LogRecordWithEntry(org.apache.ratis.server.storage.LogSegment.LogRecordWithEntry) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Example 4 with AutoCloseableLock

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

the class MemoryRaftLog method appendImpl.

@Override
public List<CompletableFuture<Long>> appendImpl(LogEntryProto... logEntryProtos) {
    checkLogState();
    if (logEntryProtos == null || logEntryProtos.length == 0) {
        return Collections.emptyList();
    }
    try (AutoCloseableLock writeLock = writeLock()) {
        // Before truncating the entries, we first need to check if some
        // entries are duplicated. If the leader sends entry 6, entry 7, then
        // entry 6 again, without this check the follower may truncate entry 7
        // when receiving entry 6 again. Then before the leader detects this
        // truncation in the next appendEntries RPC, leader may think entry 7 has
        // been committed but in the system the entry has not been committed to
        // the quorum of peers' disks.
        boolean toTruncate = false;
        int truncateIndex = (int) logEntryProtos[0].getIndex();
        int index = 0;
        for (; truncateIndex < getNextIndex() && index < logEntryProtos.length; index++, truncateIndex++) {
            if (this.entries.get(truncateIndex).getTerm() != logEntryProtos[index].getTerm()) {
                toTruncate = true;
                break;
            }
        }
        final List<CompletableFuture<Long>> futures;
        if (toTruncate) {
            futures = new ArrayList<>(logEntryProtos.length - index + 1);
            futures.add(truncate(truncateIndex));
        } else {
            futures = new ArrayList<>(logEntryProtos.length - index);
        }
        for (int i = index; i < logEntryProtos.length; i++) {
            this.entries.add(logEntryProtos[i]);
            futures.add(CompletableFuture.completedFuture(logEntryProtos[i].getIndex()));
        }
        return futures;
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Example 5 with AutoCloseableLock

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

the class MemoryRaftLog method appendEntryImpl.

@Override
protected CompletableFuture<Long> appendEntryImpl(LogEntryProto entry) {
    checkLogState();
    try (AutoCloseableLock writeLock = writeLock()) {
        validateLogEntry(entry);
        entries.add(entry);
    }
    return CompletableFuture.completedFuture(entry.getIndex());
}
Also used : 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