Search in sources :

Example 1 with RaftException

use of com.alipay.sofa.jraft.error.RaftException in project sofa-jraft by sofastack.

the class FSMCallerTest method testOnError.

@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());
}
Also used : Status(com.alipay.sofa.jraft.Status) RaftException(com.alipay.sofa.jraft.error.RaftException) Test(org.junit.Test)

Example 2 with RaftException

use of com.alipay.sofa.jraft.error.RaftException in project sofa-jraft by sofastack.

the class Replicator method installSnapshot.

void installSnapshot() {
    if (getState() == State.Snapshot) {
        LOG.warn("Replicator {} is installing snapshot, ignore the new request.", this.options.getPeerId());
        unlockId();
        return;
    }
    boolean doUnlock = true;
    if (!this.rpcService.connect(this.options.getPeerId().getEndpoint())) {
        LOG.error("Fail to check install snapshot connection to peer={}, give up to send install snapshot request.", this.options.getPeerId().getEndpoint());
        block(Utils.nowMs(), RaftError.EHOSTDOWN.getNumber());
        return;
    }
    try {
        Requires.requireTrue(this.reader == null, "Replicator %s already has a snapshot reader, current state is %s", this.options.getPeerId(), getState());
        this.reader = this.options.getSnapshotStorage().open();
        if (this.reader == null) {
            final NodeImpl node = this.options.getNode();
            final RaftException error = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_SNAPSHOT);
            error.setStatus(new Status(RaftError.EIO, "Fail to open snapshot"));
            unlockId();
            doUnlock = false;
            node.onError(error);
            return;
        }
        final String uri = this.reader.generateURIForCopy();
        if (uri == null) {
            final NodeImpl node = this.options.getNode();
            final RaftException error = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_SNAPSHOT);
            error.setStatus(new Status(RaftError.EIO, "Fail to generate uri for snapshot reader"));
            releaseReader();
            unlockId();
            doUnlock = false;
            node.onError(error);
            return;
        }
        final RaftOutter.SnapshotMeta meta = this.reader.load();
        if (meta == null) {
            final String snapshotPath = this.reader.getPath();
            final NodeImpl node = this.options.getNode();
            final RaftException error = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_SNAPSHOT);
            error.setStatus(new Status(RaftError.EIO, "Fail to load meta from %s", snapshotPath));
            releaseReader();
            unlockId();
            doUnlock = false;
            node.onError(error);
            return;
        }
        final InstallSnapshotRequest.Builder rb = InstallSnapshotRequest.newBuilder();
        rb.setTerm(this.options.getTerm());
        rb.setGroupId(this.options.getGroupId());
        rb.setServerId(this.options.getServerId().toString());
        rb.setPeerId(this.options.getPeerId().toString());
        rb.setMeta(meta);
        rb.setUri(uri);
        this.statInfo.runningState = RunningState.INSTALLING_SNAPSHOT;
        this.statInfo.lastLogIncluded = meta.getLastIncludedIndex();
        this.statInfo.lastTermIncluded = meta.getLastIncludedTerm();
        final InstallSnapshotRequest request = rb.build();
        setState(State.Snapshot);
        // noinspection NonAtomicOperationOnVolatileField
        this.installSnapshotCounter++;
        final long monotonicSendTimeMs = Utils.monotonicMs();
        final int stateVersion = this.version;
        final int seq = getAndIncrementReqSeq();
        final Future<Message> rpcFuture = this.rpcService.installSnapshot(this.options.getPeerId().getEndpoint(), request, new RpcResponseClosureAdapter<InstallSnapshotResponse>() {

            @Override
            public void run(final Status status) {
                onRpcReturned(Replicator.this.id, RequestType.Snapshot, status, request, getResponse(), seq, stateVersion, monotonicSendTimeMs);
            }
        });
        addInflight(RequestType.Snapshot, this.nextIndex, 0, 0, seq, rpcFuture);
    } finally {
        if (doUnlock) {
            unlockId();
        }
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) RaftException(com.alipay.sofa.jraft.error.RaftException) Message(com.google.protobuf.Message) RaftOutter(com.alipay.sofa.jraft.entity.RaftOutter) InstallSnapshotRequest(com.alipay.sofa.jraft.rpc.RpcRequests.InstallSnapshotRequest) ByteString(com.google.protobuf.ByteString) InstallSnapshotResponse(com.alipay.sofa.jraft.rpc.RpcRequests.InstallSnapshotResponse)

Example 3 with RaftException

use of com.alipay.sofa.jraft.error.RaftException in project sofa-jraft by sofastack.

the class SnapshotExecutorImpl method reportError.

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);
}
Also used : Status(com.alipay.sofa.jraft.Status) RaftException(com.alipay.sofa.jraft.error.RaftException)

Example 4 with RaftException

use of com.alipay.sofa.jraft.error.RaftException in project sofa-jraft by sofastack.

the class NodeTest method testAppendEntriesWhenFollowerIsInErrorState.

@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();
}
Also used : Status(com.alipay.sofa.jraft.Status) RaftException(com.alipay.sofa.jraft.error.RaftException) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 5 with RaftException

use of com.alipay.sofa.jraft.error.RaftException in project sofa-jraft by sofastack.

the class LogManagerImpl method reportError.

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);
}
Also used : Status(com.alipay.sofa.jraft.Status) RaftException(com.alipay.sofa.jraft.error.RaftException)

Aggregations

RaftException (com.alipay.sofa.jraft.error.RaftException)7 Status (com.alipay.sofa.jraft.Status)6 PeerId (com.alipay.sofa.jraft.entity.PeerId)2 RaftOutter (com.alipay.sofa.jraft.entity.RaftOutter)2 Test (org.junit.Test)2 Node (com.alipay.sofa.jraft.Node)1 Configuration (com.alipay.sofa.jraft.conf.Configuration)1 LogId (com.alipay.sofa.jraft.entity.LogId)1 InstallSnapshotRequest (com.alipay.sofa.jraft.rpc.RpcRequests.InstallSnapshotRequest)1 InstallSnapshotResponse (com.alipay.sofa.jraft.rpc.RpcRequests.InstallSnapshotResponse)1 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)1 DisruptorMetricSet (com.alipay.sofa.jraft.util.DisruptorMetricSet)1 Endpoint (com.alipay.sofa.jraft.util.Endpoint)1 NamedThreadFactory (com.alipay.sofa.jraft.util.NamedThreadFactory)1 ByteString (com.google.protobuf.ByteString)1 Message (com.google.protobuf.Message)1 BlockingWaitStrategy (com.lmax.disruptor.BlockingWaitStrategy)1