Search in sources :

Example 1 with Task

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

Aggregations

Timer (com.codahale.metrics.Timer)1 IOException (java.io.IOException)1 TimeoutIOException (org.apache.ratis.protocol.exceptions.TimeoutIOException)1 RaftServer (org.apache.ratis.server.RaftServer)1 RaftLogIOException (org.apache.ratis.server.raftlog.RaftLogIOException)1 Task (org.apache.ratis.server.raftlog.segmented.SegmentedRaftLog.Task)1