Search in sources :

Example 26 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class SegmentedRaftLog method newServerLogMethods.

/**
 * When the server is null, return the dummy instance of {@link ServerLogMethods}.
 * Otherwise, the server is non-null, return the implementation using the given server.
 */
private ServerLogMethods newServerLogMethods(RaftServer.Division impl, Consumer<LogEntryProto> notifyTruncatedLogEntry) {
    if (impl == null) {
        return ServerLogMethods.DUMMY;
    }
    return new ServerLogMethods() {

        @Override
        public long[] getFollowerNextIndices() {
            return impl.getInfo().getFollowerNextIndices();
        }

        @Override
        public long getLastAppliedIndex() {
            return impl.getInfo().getLastAppliedIndex();
        }

        @Override
        public void notifyTruncatedLogEntry(TermIndex ti) {
            try {
                final LogEntryProto entry = get(ti.getIndex());
                notifyTruncatedLogEntry.accept(entry);
            } catch (RaftLogIOException e) {
                LOG.error("{}: Failed to read log {}", getName(), ti, e);
            }
        }
    };
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftLogIOException(org.apache.ratis.server.raftlog.RaftLogIOException) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 27 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class LogSegment method getEntrySize.

static long getEntrySize(LogEntryProto entry, Op op) {
    LogEntryProto e = entry;
    if (op == Op.CHECK_SEGMENT_FILE_FULL) {
        e = LogProtoUtils.removeStateMachineData(entry);
    } else if (op == Op.LOAD_SEGMENT_FILE || op == Op.WRITE_CACHE_WITH_STATE_MACHINE_CACHE) {
        Preconditions.assertTrue(entry == LogProtoUtils.removeStateMachineData(entry), () -> "Unexpected LogEntryProto with StateMachine data: op=" + op + ", entry=" + entry);
    } else {
        Preconditions.assertTrue(op == Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE || op == Op.REMOVE_CACHE, () -> "Unexpected op " + op + ", entry=" + entry);
    }
    final int serialized = e.getSerializedSize();
    return serialized + CodedOutputStream.computeUInt32SizeNoTag(serialized) + 4L;
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto)

Example 28 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class SegmentedRaftLogInputStream method scanEditLog.

/**
 * Find the last valid entry index in the stream.
 * If there are invalid or corrupt entries in the middle of the stream,
 * scanEditLog will skip over them.
 *
 * This reads through the stream but does not close it.
 *
 * @param maxIndexToScan Maximum entry index to try to scan. The scan returns
 *                       after reading this or a higher index. The file
 *                       portion beyond this index is potentially being
 *                       updated.
 */
static LogValidation scanEditLog(SegmentedRaftLogInputStream in, long maxIndexToScan) {
    long lastPos = 0;
    long end = INVALID_LOG_INDEX;
    long numValid = 0;
    boolean hitError = false;
    while (end < maxIndexToScan) {
        long index;
        lastPos = in.getPosition();
        try {
            if (hitError) {
                LogEntryProto entry = in.nextEntry();
                index = entry != null ? entry.getIndex() : INVALID_LOG_INDEX;
                LOG.warn("After resync, position is " + in.getPosition());
            } else {
                index = in.scanNextEntry();
            }
            if (index == INVALID_LOG_INDEX) {
                break;
            } else {
                hitError = false;
            }
        } catch (Exception e) {
            LOG.warn("Caught exception after scanning through {} ops from {}" + " while determining its valid length. Position was " + lastPos, numValid, in, e);
            hitError = true;
            continue;
        }
        if (end == INVALID_LOG_INDEX || index > end) {
            end = index;
        }
        numValid++;
    }
    return new LogValidation(lastPos, end, false);
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 29 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class SegmentedRaftLogInputStream method nextEntry.

public LogEntryProto nextEntry() throws IOException {
    if (state.isUnopened()) {
        try {
            init();
        } catch (Exception e) {
            LOG.error("caught exception initializing " + this, e);
            throw IOUtils.asIOException(e);
        }
    }
    Preconditions.assertTrue(!state.isUnopened());
    if (state.isOpened()) {
        final LogEntryProto entry = reader.readEntry();
        if (entry != null) {
            long index = entry.getIndex();
            if (!isOpen() && index >= endIndex) {
                /*
             * The end index may be derived from the segment recovery
             * process. It is possible that we still have some uncleaned garbage
             * in the end. We should skip them.
             */
                long skipAmt = logFile.length() - reader.getPos();
                if (skipAmt > 0) {
                    LOG.debug("skipping {} bytes at the end of log '{}': reached" + " entry {} out of {}", skipAmt, getName(), index, endIndex);
                    reader.skipFully(skipAmt);
                }
            }
        }
        return entry;
    } else if (state.isClosed()) {
        return null;
    }
    throw new IOException("Failed to get next entry from " + this, state.getThrowable());
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 30 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class RaftLogBase method appendImpl.

private long appendImpl(long term, TransactionContext operation) 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 (StateMachineException e) {
            throw e;
        } catch (IOException e) {
            throw new StateMachineException(memberId, e);
        }
        // build the log entry after calling the StateMachine
        final LogEntryProto e = operation.initLogEntry(term, nextIndex);
        int entrySize = e.getSerializedSize();
        if (entrySize > maxBufferSize) {
            throw new StateMachineException(memberId, new RaftLogIOException("Log entry size " + entrySize + " exceeds the max buffer limit of " + maxBufferSize));
        }
        appendEntry(e);
        return nextIndex;
    }
}
Also used : StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) IOException(java.io.IOException)

Aggregations

LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)61 Test (org.junit.Test)22 BaseTest (org.apache.ratis.BaseTest)20 SimpleOperation (org.apache.ratis.RaftTestUtil.SimpleOperation)15 CompletableFuture (java.util.concurrent.CompletableFuture)14 File (java.io.File)13 IOException (java.io.IOException)13 StateMachineLogEntryProto (org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)12 RaftLog (org.apache.ratis.server.raftlog.RaftLog)12 RaftStorage (org.apache.ratis.server.storage.RaftStorage)11 ArrayList (java.util.ArrayList)10 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)10 TermIndex (org.apache.ratis.server.protocol.TermIndex)9 LogEntryHeader (org.apache.ratis.server.raftlog.LogEntryHeader)7 RaftClient (org.apache.ratis.client.RaftClient)6 RaftProperties (org.apache.ratis.conf.RaftProperties)6 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)6 RaftGroupMemberId (org.apache.ratis.protocol.RaftGroupMemberId)6 RaftServer (org.apache.ratis.server.RaftServer)6 RandomAccessFile (java.io.RandomAccessFile)5