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