Search in sources :

Example 16 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)

Example 17 with AutoCloseableLock

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

the class MemoryRaftLog method truncateImpl.

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

Example 18 with AutoCloseableLock

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

the class MemoryRaftLog method getEntries.

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

Example 19 with AutoCloseableLock

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

the class MemoryRaftLog method append.

@Override
public List<CompletableFuture<Long>> append(LogEntryProto... entries) {
    checkLogState();
    try (AutoCloseableLock writeLock = writeLock()) {
        if (entries == null || entries.length == 0) {
            return Collections.emptyList();
        }
        // 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.
        // TODO add a unit test for this
        boolean toTruncate = false;
        int truncateIndex = (int) entries[0].getIndex();
        int index = 0;
        for (; truncateIndex < getNextIndex() && index < entries.length; index++, truncateIndex++) {
            if (this.entries.get(truncateIndex).getTerm() != entries[index].getTerm()) {
                toTruncate = true;
                break;
            }
        }
        final List<CompletableFuture<Long>> futures;
        if (toTruncate) {
            futures = new ArrayList<>(entries.length - index + 1);
            futures.add(truncate(truncateIndex));
        } else {
            futures = new ArrayList<>(entries.length - index);
        }
        for (int i = index; i < entries.length; i++) {
            this.entries.add(entries[i]);
            futures.add(CompletableFuture.completedFuture(entries[i].getIndex()));
        }
        return futures;
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Example 20 with AutoCloseableLock

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

the class MemoryRaftLog method truncate.

@Override
CompletableFuture<Long> truncate(long index) {
    checkLogState();
    try (AutoCloseableLock writeLock = writeLock()) {
        Preconditions.assertTrue(index >= 0);
        final int truncateIndex = (int) index;
        for (int i = entries.size() - 1; i >= truncateIndex; i--) {
            entries.remove(i);
        }
    }
    return CompletableFuture.completedFuture(index);
}
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