com.alipay.sofa.jraft.error.RaftException Java Examples

The following examples show how to use com.alipay.sofa.jraft.error.RaftException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NodeImpl.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
public void onError(final RaftException error) {
    LOG.warn("Node {} got error: {}.", getNodeId(), error);
    if (this.fsmCaller != null) {
        // onError of fsmCaller is guaranteed to be executed once.
        this.fsmCaller.onError(error);
    }
    if (this.readOnlyService != null) {
        this.readOnlyService.setError(error);
    }
    this.writeLock.lock();
    try {
        // If it is leader, need to wake up a new one;
        // If it is follower, also step down to call on_stop_following.
        if (this.state.compareTo(State.STATE_FOLLOWER) <= 0) {
            stepDown(this.currTerm, this.state == State.STATE_LEADER, new Status(RaftError.EBADNODE,
                "Raft node(leader or candidate) is in error."));
        }
        if (this.state.compareTo(State.STATE_ERROR) < 0) {
            this.state = State.STATE_ERROR;
        }
    } finally {
        this.writeLock.unlock();
    }
}
 
Example #2
Source File: ReplicatorTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testInstallSnapshotNoReader() {
    final Replicator r = getReplicator();
    this.id.unlock();

    final Future<Message> rpcInFly = r.getRpcInFly();
    assertNotNull(rpcInFly);
    r.installSnapshot();
    final ArgumentCaptor<RaftException> errArg = ArgumentCaptor.forClass(RaftException.class);
    Mockito.verify(this.node).onError(errArg.capture());
    Assert.assertEquals(RaftError.EIO, errArg.getValue().getStatus().getRaftError());
    Assert.assertEquals("Fail to open snapshot", errArg.getValue().getStatus().getErrorMsg());
}
 
Example #3
Source File: FSMCallerTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnError() throws Exception {
    this.fsmCaller.onError(new RaftException(ErrorType.ERROR_TYPE_LOG, new Status(-1, "test")));
    this.fsmCaller.flush();
    assertFalse(this.fsmCaller.getError().getStatus().isOk());
    assertEquals(ErrorType.ERROR_TYPE_LOG, this.fsmCaller.getError().getType());
    Mockito.verify(this.node).onError(Mockito.any());
    Mockito.verify(this.fsm).onError(Mockito.any());
}
 
Example #4
Source File: FSMCallerImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public boolean init(final FSMCallerOptions opts) {
    this.logManager = opts.getLogManager();
    this.fsm = opts.getFsm();
    this.closureQueue = opts.getClosureQueue();
    this.afterShutdown = opts.getAfterShutdown();
    this.node = opts.getNode();
    this.nodeMetrics = this.node.getNodeMetrics();
    this.lastAppliedIndex.set(opts.getBootstrapId().getIndex());
    notifyLastAppliedIndexUpdated(this.lastAppliedIndex.get());
    this.lastAppliedTerm = opts.getBootstrapId().getTerm();
    this.disruptor = DisruptorBuilder.<ApplyTask> newInstance() //
        .setEventFactory(new ApplyTaskFactory()) //
        .setRingBufferSize(opts.getDisruptorBufferSize()) //
        .setThreadFactory(new NamedThreadFactory("JRaft-FSMCaller-Disruptor-", true)) //
        .setProducerType(ProducerType.MULTI) //
        .setWaitStrategy(new BlockingWaitStrategy()) //
        .build();
    this.disruptor.handleEventsWith(new ApplyTaskHandler());
    this.disruptor.setDefaultExceptionHandler(new LogExceptionHandler<Object>(getClass().getSimpleName()));
    this.taskQueue = this.disruptor.start();
    if (this.nodeMetrics.getMetricRegistry() != null) {
        this.nodeMetrics.getMetricRegistry().register("jraft-fsm-caller-disruptor",
            new DisruptorMetricSet(this.taskQueue));
    }
    this.error = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_NONE);
    LOG.info("Starts FSMCaller successfully.");
    return true;
}
 
Example #5
Source File: FSMCallerImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
private boolean enqueueTask(final EventTranslator<ApplyTask> tpl) {
    if (this.shutdownLatch != null) {
        // Shutting down
        LOG.warn("FSMCaller is stopped, can not apply new task.");
        return false;
    }
    if (!this.taskQueue.tryPublishEvent(tpl)) {
        setError(new RaftException(ErrorType.ERROR_TYPE_STATE_MACHINE, new Status(RaftError.EBUSY,
            "FSMCaller is overload.")));
        return false;
    }
    return true;
}
 
Example #6
Source File: FSMCallerImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onError(final RaftException error) {
    if (!this.error.getStatus().isOk()) {
        LOG.warn("FSMCaller already in error status, ignore new error: {}", error);
        return false;
    }
    final OnErrorClosure c = new OnErrorClosure(error);
    return enqueueTask((task, sequence) -> {
        task.type = TaskType.ERROR;
        task.done = c;
    });
}
 
Example #7
Source File: FSMCallerImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
private void setError(final RaftException e) {
    if (this.error.getType() != EnumOutter.ErrorType.ERROR_TYPE_NONE) {
        // already report
        return;
    }
    this.error = e;
    if (this.fsm != null) {
        this.fsm.onError(e);
    }
    if (this.node != null) {
        this.node.onError(e);
    }
}
 
Example #8
Source File: DBStateMachine.java    From KitDB with Apache License 2.0 4 votes vote down vote up
@Override
public void onError(RaftException e) {
    LOG.error("onSnapshotLoad error", e);
}
 
Example #9
Source File: AtomicStateMachine.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public void onError(final RaftException e) {
    LOG.error("Raft error: {}", e, e);
}
 
Example #10
Source File: MetaStateMachine.java    From distkv with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void onError(final RaftException e) {
  LOG.error("Raft error: %s", e);
}
 
Example #11
Source File: CounterStateMachine.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public void onError(final RaftException e) {
    LOG.error("Raft error: {}", e, e);
}
 
Example #12
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Test
public void testAppendEntriesWhenFollowerIsInErrorState() throws Exception {
    // start five nodes
    final List<PeerId> peers = TestUtils.generatePeers(5);

    final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers, 1000);

    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint()));
    }

    cluster.waitLeader();
    final Node oldLeader = cluster.getLeader();
    assertNotNull(oldLeader);
    // apply something
    this.sendTestTaskAndWait(oldLeader);

    // set one follower into error state
    final List<Node> followers = cluster.getFollowers();
    assertEquals(4, followers.size());
    final Node errorNode = followers.get(0);
    final PeerId errorPeer = errorNode.getNodeId().getPeerId().copy();
    final Endpoint errorFollowerAddr = errorPeer.getEndpoint();
    LOG.info("Set follower {} into error state", errorNode);
    ((NodeImpl) errorNode).onError(new RaftException(EnumOutter.ErrorType.ERROR_TYPE_STATE_MACHINE, new Status(-1,
        "Follower has something wrong.")));

    // increase term  by stopping leader and electing a new leader again
    final Endpoint oldLeaderAddr = oldLeader.getNodeId().getPeerId().getEndpoint().copy();
    assertTrue(cluster.stop(oldLeaderAddr));
    cluster.waitLeader();
    final Node leader = cluster.getLeader();
    assertNotNull(leader);
    LOG.info("Elect a new leader {}", leader);
    // apply something again
    this.sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);

    // stop error follower
    Thread.sleep(20);
    LOG.info("Stop error follower {}", errorNode);
    assertTrue(cluster.stop(errorFollowerAddr));
    // restart error and old leader
    LOG.info("Restart error follower {} and old leader {}", errorFollowerAddr, oldLeaderAddr);

    assertTrue(cluster.start(errorFollowerAddr));
    assertTrue(cluster.start(oldLeaderAddr));
    cluster.ensureSame();
    assertEquals(5, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(20, fsm.getLogs().size());
    }

    cluster.stopAll();
}
 
Example #13
Source File: LocalRaftMetaStorageTest.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Test
public void testSaveFail() throws IOException {
    FileUtils.deleteDirectory(new File(this.path));
    assertFalse(this.raftMetaStorage.setVotedFor(new PeerId("localhost", 8081)));
    Mockito.verify(this.node, Mockito.times(1)).onError((RaftException) Mockito.any());
}
 
Example #14
Source File: ReadOnlyServiceImpl.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void setError(final RaftException error) {
    if (this.error == null) {
        this.error = error;
    }
}
 
Example #15
Source File: StateMachineAdapter.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public void onError(final RaftException e) {
    LOG.error(
        "Encountered an error={} on StateMachine {}, it's highly recommended to implement this method as raft stops working since some error occurs, you should figure out the cause and repair or remove this node.",
        e.getStatus(), getClassName(), e);
}
 
Example #16
Source File: IteratorImpl.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
private RaftException getOrCreateError() {
    if (this.error == null) {
        this.error = new RaftException();
    }
    return this.error;
}
 
Example #17
Source File: IteratorImpl.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
public RaftException getError() {
    return this.error;
}
 
Example #18
Source File: FSMCallerImpl.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@OnlyForTest
RaftException getError() {
    return this.error;
}
 
Example #19
Source File: FSMCallerImpl.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
private void doSnapshotLoad(final LoadSnapshotClosure done) {
    Requires.requireNonNull(done, "LoadSnapshotClosure is null");
    final SnapshotReader reader = done.start();
    if (reader == null) {
        done.run(new Status(RaftError.EINVAL, "open SnapshotReader failed"));
        return;
    }
    final RaftOutter.SnapshotMeta meta = reader.load();
    if (meta == null) {
        done.run(new Status(RaftError.EINVAL, "SnapshotReader load meta failed"));
        if (reader.getRaftError() == RaftError.EIO) {
            final RaftException err = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_SNAPSHOT, RaftError.EIO,
                "Fail to load snapshot meta");
            setError(err);
        }
        return;
    }
    final LogId lastAppliedId = new LogId(this.lastAppliedIndex.get(), this.lastAppliedTerm);
    final LogId snapshotId = new LogId(meta.getLastIncludedIndex(), meta.getLastIncludedTerm());
    if (lastAppliedId.compareTo(snapshotId) > 0) {
        done.run(new Status(
            RaftError.ESTALE,
            "Loading a stale snapshot last_applied_index=%d last_applied_term=%d snapshot_index=%d snapshot_term=%d",
            lastAppliedId.getIndex(), lastAppliedId.getTerm(), snapshotId.getIndex(), snapshotId.getTerm()));
        return;
    }
    if (!this.fsm.onSnapshotLoad(reader)) {
        done.run(new Status(-1, "StateMachine onSnapshotLoad failed"));
        final RaftException e = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_STATE_MACHINE,
            RaftError.ESTATEMACHINE, "StateMachine onSnapshotLoad failed");
        setError(e);
        return;
    }
    if (meta.getOldPeersCount() == 0) {
        // Joint stage is not supposed to be noticeable by end users.
        final Configuration conf = new Configuration();
        for (int i = 0, size = meta.getPeersCount(); i < size; i++) {
            final PeerId peer = new PeerId();
            Requires.requireTrue(peer.parse(meta.getPeers(i)), "Parse peer failed");
            conf.addPeer(peer);
        }
        this.fsm.onConfigurationCommitted(conf);
    }
    this.lastAppliedIndex.set(meta.getLastIncludedIndex());
    this.lastAppliedTerm = meta.getLastIncludedTerm();
    done.run(Status.OK());
}
 
Example #20
Source File: FSMCallerImpl.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
public void setError(final RaftException error) {
    this.error = error;
}
 
Example #21
Source File: FSMCallerImpl.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
public RaftException getError() {
    return this.error;
}
 
Example #22
Source File: FSMCallerImpl.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
public OnErrorClosure(final RaftException error) {
    super();
    this.error = error;
}
 
Example #23
Source File: LogManagerImpl.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
private void reportError(final int code, final String fmt, final Object... args) {
    this.hasError = true;
    final RaftException error = new RaftException(ErrorType.ERROR_TYPE_LOG);
    error.setStatus(new Status(code, fmt, args));
    this.fsmCaller.onError(error);
}
 
Example #24
Source File: LocalRaftMetaStorage.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
private void reportIOError() {
    this.node.onError(new RaftException(ErrorType.ERROR_TYPE_META, RaftError.EIO,
        "Fail to save raft meta, path=%s", this.path));
}
 
Example #25
Source File: SnapshotExecutorImpl.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
private void reportError(final int errCode, final String fmt, final Object... args) {
    final RaftException error = new RaftException(ErrorType.ERROR_TYPE_SNAPSHOT);
    error.setStatus(new Status(errCode, fmt, args));
    this.fsmCaller.onError(error);
}
 
Example #26
Source File: FSMCaller.java    From sofa-jraft with Apache License 2.0 2 votes vote down vote up
/**
 * Called when error happens.
 *
 * @param error error info
 */
boolean onError(final RaftException error);
 
Example #27
Source File: ReadOnlyService.java    From sofa-jraft with Apache License 2.0 2 votes vote down vote up
/**
 * Called when the node is turned into error state.
 * @param error error with raft info
 */
void setError(final RaftException error);
 
Example #28
Source File: StateMachine.java    From sofa-jraft with Apache License 2.0 2 votes vote down vote up
/**
 * This method is called when a critical error was encountered, after this
 * point, no any further modification is allowed to applied to this node
 * until the error is fixed and this node restarts.
 *
 * @param e raft error message
 */
void onError(final RaftException e);