Search in sources :

Example 21 with AutoCloseableLock

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

the class MemoryRaftLog method append.

@Override
public long append(long term, RaftConfiguration newConf) {
    checkLogState();
    try (AutoCloseableLock writeLock = writeLock()) {
        final long nextIndex = getNextIndex();
        final LogEntryProto e = ServerProtoUtils.toLogEntryProto(newConf, term, nextIndex);
        entries.add(e);
        return nextIndex;
    }
}
Also used : LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Example 22 with AutoCloseableLock

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

the class RaftLog method append.

/**
 * Generate a log entry for the given term and message, and append the entry.
 * Used by the leader.
 * @return the index of the new log entry.
 */
public long append(long term, TransactionContext operation, ClientId clientId, long callId) throws StateMachineException {
    checkLogState();
    try (AutoCloseableLock writeLock = writeLock()) {
        final long nextIndex = getNextIndex();
        // the SM wants to attach a logic depending on ordered execution in the log commit order.
        try {
            operation = operation.preAppendTransaction();
        } catch (IOException e) {
            throw new StateMachineException(selfId, e);
        }
        // build the log entry after calling the StateMachine
        final LogEntryProto e = ProtoUtils.toLogEntryProto(operation.getSMLogEntry(), term, nextIndex, clientId, callId);
        int entrySize = e.getSerializedSize();
        if (entrySize > maxBufferSize) {
            throw new StateMachineException(selfId, new RaftLogIOException("Log entry size " + entrySize + " exceeds the max buffer limit of " + maxBufferSize));
        }
        appendEntry(e);
        operation.setLogEntry(e);
        return nextIndex;
    }
}
Also used : StateMachineException(org.apache.ratis.protocol.StateMachineException) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) IOException(java.io.IOException)

Example 23 with AutoCloseableLock

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

the class RaftLog method append.

/**
 * Generate a log entry for the given term and configurations,
 * and append the entry. Used by the leader.
 * @return the index of the new log entry.
 */
public long append(long term, RaftConfiguration newConf) {
    checkLogState();
    try (AutoCloseableLock writeLock = writeLock()) {
        final long nextIndex = getNextIndex();
        final LogEntryProto e = ServerProtoUtils.toLogEntryProto(newConf, term, nextIndex);
        appendEntry(e);
        return nextIndex;
    }
}
Also used : LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Example 24 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.clear();
    }
    fileLogWorker.close();
    storage.close();
}
Also used : AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Example 25 with AutoCloseableLock

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

the class SegmentedRaftLog method loadLogSegments.

private void loadLogSegments(long lastIndexInSnapshot, Consumer<LogEntryProto> logConsumer) throws IOException {
    try (AutoCloseableLock writeLock = writeLock()) {
        List<LogPathAndIndex> paths = storage.getStorageDir().getLogSegmentFiles();
        int i = 0;
        for (LogPathAndIndex pi : paths) {
            boolean isOpen = pi.endIndex == RaftServerConstants.INVALID_LOG_INDEX;
            // During the initial loading, we can only confirm the committed
            // index based on the snapshot. This means if a log segment is not kept
            // in cache after the initial loading, later we have to load its content
            // again for updating the state machine.
            // TODO we should let raft peer persist its committed index periodically
            // so that during the initial loading we can apply part of the log
            // entries to the state machine
            boolean keepEntryInCache = (paths.size() - i++) <= cache.getMaxCachedSegments();
            cache.loadSegment(pi, isOpen, keepEntryInCache, logConsumer);
        }
        // committing the log and taking snapshot)
        if (!cache.isEmpty() && cache.getEndIndex() < lastIndexInSnapshot) {
            LOG.warn("End log index {} is smaller than last index in snapshot {}", cache.getEndIndex(), lastIndexInSnapshot);
            cache.clear();
        // TODO purge all segment files
        }
    }
}
Also used : AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) LogPathAndIndex(org.apache.ratis.server.storage.RaftStorageDirectory.LogPathAndIndex)

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