Search in sources :

Example 1 with RaftLogIOException

use of org.apache.ratis.server.raftlog.RaftLogIOException 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 2 with RaftLogIOException

use of org.apache.ratis.server.raftlog.RaftLogIOException in project incubator-ratis by apache.

the class LogAppenderDefault method sendAppendEntriesWithRetries.

/**
 * Send an appendEntries RPC; retry indefinitely.
 */
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE")
private AppendEntriesReplyProto sendAppendEntriesWithRetries() throws InterruptedException, InterruptedIOException, RaftLogIOException {
    int retry = 0;
    AppendEntriesRequestProto request = null;
    while (isRunning()) {
        // keep retrying for IOException
        try {
            if (request == null || request.getEntriesCount() == 0) {
                request = newAppendEntriesRequest(CallId.getAndIncrement(), false);
            }
            if (request == null) {
                LOG.trace("{} no entries to send now, wait ...", this);
                return null;
            } else if (!isRunning()) {
                LOG.info("{} is stopped. Skip appendEntries.", this);
                return null;
            }
            getFollower().updateLastRpcSendTime(request.getEntriesCount() == 0);
            final AppendEntriesReplyProto r = getServerRpc().appendEntries(request);
            getFollower().updateLastRpcResponseTime();
            getLeaderState().onFollowerCommitIndex(getFollower(), r.getFollowerCommit());
            return r;
        } catch (InterruptedIOException | RaftLogIOException e) {
            throw e;
        } catch (IOException ioe) {
            // TODO should have more detailed retry policy here.
            if (retry++ % 10 == 0) {
                // to reduce the number of messages
                LOG.warn("{}: Failed to appendEntries (retry={}): {}", this, retry++, ioe);
            }
            handleException(ioe);
        }
        if (isRunning()) {
            getServer().properties().rpcSleepTime().sleep();
        }
    }
    return null;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) RaftLogIOException(org.apache.ratis.server.raftlog.RaftLogIOException) IOException(java.io.IOException) RaftLogIOException(org.apache.ratis.server.raftlog.RaftLogIOException) InterruptedIOException(java.io.InterruptedIOException) AppendEntriesRequestProto(org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto) AppendEntriesReplyProto(org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 3 with RaftLogIOException

use of org.apache.ratis.server.raftlog.RaftLogIOException in project incubator-ratis by apache.

the class SegmentedRaftLogWorker method run.

private void run() {
    // if and when a log task encounters an exception
    RaftLogIOException logIOException = null;
    while (running) {
        try {
            Task task = queue.poll(ONE_SECOND);
            if (task != null) {
                task.stopTimerOnDequeue();
                try {
                    if (logIOException != null) {
                        throw logIOException;
                    } else {
                        final Timer.Context executionTimeContext = raftLogMetrics.getRaftLogTaskExecutionTimer(JavaUtils.getClassSimpleName(task.getClass()).toLowerCase()).time();
                        task.execute();
                        executionTimeContext.stop();
                    }
                } catch (IOException e) {
                    if (task.getEndIndex() < lastWrittenIndex) {
                        LOG.info("Ignore IOException when handling task " + task + " which is smaller than the lastWrittenIndex." + " There should be a snapshot installed.", e);
                    } else {
                        task.failed(e);
                        if (logIOException == null) {
                            logIOException = new RaftLogIOException("Log already failed" + " at index " + task.getEndIndex() + " for task " + task, e);
                        }
                        continue;
                    }
                }
                task.done();
            }
            flushIfNecessary();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            if (running) {
                LOG.warn("{} got interrupted while still running", Thread.currentThread().getName());
            }
            LOG.info(Thread.currentThread().getName() + " was interrupted, exiting. There are " + queue.getNumElements() + " tasks remaining in the queue.");
            return;
        } catch (Exception e) {
            if (!running) {
                LOG.info("{} got closed and hit exception", Thread.currentThread().getName(), e);
            } else {
                LOG.error("{} hit exception", Thread.currentThread().getName(), e);
                Optional.ofNullable(server).ifPresent(RaftServer.Division::close);
            }
        }
    }
}
Also used : Task(org.apache.ratis.server.raftlog.segmented.SegmentedRaftLog.Task) RaftLogIOException(org.apache.ratis.server.raftlog.RaftLogIOException) Timer(com.codahale.metrics.Timer) RaftServer(org.apache.ratis.server.RaftServer) RaftLogIOException(org.apache.ratis.server.raftlog.RaftLogIOException) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) IOException(java.io.IOException) RaftLogIOException(org.apache.ratis.server.raftlog.RaftLogIOException) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) IOException(java.io.IOException)

Example 4 with RaftLogIOException

use of org.apache.ratis.server.raftlog.RaftLogIOException 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)

Aggregations

RaftLogIOException (org.apache.ratis.server.raftlog.RaftLogIOException)4 IOException (java.io.IOException)2 TermIndex (org.apache.ratis.server.protocol.TermIndex)2 Timer (com.codahale.metrics.Timer)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 InterruptedIOException (java.io.InterruptedIOException)1 AppendEntriesReplyProto (org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto)1 AppendEntriesRequestProto (org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto)1 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)1 TimeoutIOException (org.apache.ratis.protocol.exceptions.TimeoutIOException)1 RaftServer (org.apache.ratis.server.RaftServer)1 Task (org.apache.ratis.server.raftlog.segmented.SegmentedRaftLog.Task)1 AutoCloseableLock (org.apache.ratis.util.AutoCloseableLock)1