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);
}
}
};
}
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;
}
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);
}
}
}
}
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());
}
}
Aggregations