use of org.apache.ignite.raft.jraft.Status in project ignite-3 by apache.
the class CliServiceImpl method resetLearners.
@Override
public Status resetLearners(final String groupId, final Configuration conf, final List<PeerId> learners) {
checkLearnersOpParams(groupId, conf, learners);
final PeerId leaderId = new PeerId();
final Status st = getLeader(groupId, conf, leaderId);
if (!st.isOk()) {
return st;
}
if (!this.cliClientService.connect(leaderId.getEndpoint())) {
return new Status(-1, "Fail to init channel to leader %s", leaderId);
}
ResetLearnersRequest req = cliOptions.getRaftMessagesFactory().resetLearnersRequest().groupId(groupId).leaderId(leaderId.toString()).learnersList(learners.stream().map(Object::toString).collect(toList())).build();
try {
final Message result = this.cliClientService.resetLearners(leaderId.getEndpoint(), req, null).get();
return processLearnersOpResponse(groupId, result, "resetting learners: %s", learners);
} catch (final Exception e) {
return new Status(-1, e.getMessage());
}
}
use of org.apache.ignite.raft.jraft.Status in project ignite-3 by apache.
the class FSMCallerImpl method doSnapshotLoad.
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(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.lastIncludedIndex(), meta.lastIncludedTerm());
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(ErrorType.ERROR_TYPE_STATE_MACHINE, RaftError.ESTATEMACHINE, "StateMachine onSnapshotLoad failed");
setError(e);
return;
}
if (meta.oldPeersList() == null) {
// Joint stage is not supposed to be noticeable by end users.
final Configuration conf = new Configuration();
if (meta.peersList() != null) {
for (String metaPeer : meta.peersList()) {
final PeerId peer = new PeerId();
Requires.requireTrue(peer.parse(metaPeer), "Parse peer failed");
conf.addPeer(peer);
}
}
this.fsm.onConfigurationCommitted(conf);
}
this.lastAppliedIndex.set(meta.lastIncludedIndex());
this.lastAppliedTerm = meta.lastIncludedTerm();
done.run(Status.OK());
}
use of org.apache.ignite.raft.jraft.Status in project ignite-3 by apache.
the class FSMCallerImpl method doSnapshotSave.
private void doSnapshotSave(final SaveSnapshotClosure done) {
Requires.requireNonNull(done, "SaveSnapshotClosure is null");
final long lastAppliedIndex = this.lastAppliedIndex.get();
final ConfigurationEntry confEntry = this.logManager.getConfiguration(lastAppliedIndex);
if (confEntry == null || confEntry.isEmpty()) {
LOG.error("Empty conf entry for lastAppliedIndex={}", lastAppliedIndex);
Utils.runClosureInThread(this.node.getOptions().getCommonExecutor(), done, new Status(RaftError.EINVAL, "Empty conf entry for lastAppliedIndex=%s", lastAppliedIndex));
return;
}
SnapshotMetaBuilder metaBuilder = msgFactory.snapshotMeta().lastIncludedIndex(lastAppliedIndex).lastIncludedTerm(this.lastAppliedTerm).peersList(confEntry.getConf().getPeers().stream().map(Object::toString).collect(toList())).learnersList(confEntry.getConf().getLearners().stream().map(Object::toString).collect(toList()));
if (confEntry.getOldConf() != null) {
metaBuilder.oldPeersList(confEntry.getOldConf().getPeers().stream().map(Object::toString).collect(toList())).oldLearnersList(confEntry.getOldConf().getLearners().stream().map(Object::toString).collect(toList()));
}
final SnapshotWriter writer = done.start(metaBuilder.build());
if (writer == null) {
done.run(new Status(RaftError.EINVAL, "snapshot_storage create SnapshotWriter failed"));
return;
}
this.fsm.onSnapshotSave(writer, done);
}
use of org.apache.ignite.raft.jraft.Status in project ignite-3 by apache.
the class ItNodeTest method testChangePeersChaosWithSnapshot.
@Test
public void testChangePeersChaosWithSnapshot() throws Exception {
// start cluster
List<PeerId> peers = new ArrayList<>();
peers.add(new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT));
cluster = new TestCluster("unittest", dataPath, peers, ELECTION_TIMEOUT_MILLIS, testInfo);
assertTrue(cluster.start(peers.get(0).getEndpoint(), false, 2));
// start other peers
for (int i = 1; i < 10; i++) {
PeerId peer = new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT + i);
peers.add(peer);
assertTrue(cluster.start(peer.getEndpoint()));
}
ChangeArg arg = new ChangeArg(cluster, peers, false, false);
Future<?> future = startChangePeersThread(arg);
for (int i = 0; i < 5000; ) {
cluster.waitLeader();
Node leader = cluster.getLeader();
if (leader == null)
continue;
SynchronizedClosure done = new SynchronizedClosure();
Task task = new Task(ByteBuffer.wrap(("hello" + i).getBytes(UTF_8)), done);
leader.apply(task);
Status status = done.await();
if (status.isOk()) {
if (++i % 100 == 0)
System.out.println("Progress:" + i);
} else
assertEquals(RaftError.EPERM, status.getRaftError());
}
arg.stop = true;
future.get();
cluster.waitLeader();
SynchronizedClosure done = new SynchronizedClosure();
Node leader = cluster.getLeader();
leader.changePeers(new Configuration(peers), done);
Status st = done.await();
assertTrue(st.isOk(), st.getErrorMsg());
cluster.ensureSame();
assertEquals(10, cluster.getFsms().size());
for (MockStateMachine fsm : cluster.getFsms()) assertTrue(fsm.getLogs().size() >= 5000);
}
use of org.apache.ignite.raft.jraft.Status in project ignite-3 by apache.
the class ItNodeTest method assertReadIndex.
@SuppressWarnings({ "unused", "SameParameterValue" })
private boolean assertReadIndex(Node node, int index) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
byte[] requestContext = TestUtils.getRandomBytes();
AtomicBoolean success = new AtomicBoolean(false);
node.readIndex(requestContext, new ReadIndexClosure() {
@Override
public void run(Status status, long theIndex, byte[] reqCtx) {
if (status.isOk()) {
assertEquals(index, theIndex);
assertArrayEquals(requestContext, reqCtx);
success.set(true);
} else {
assertTrue(status.getErrorMsg().contains("RPC exception:Check connection["), status.getErrorMsg());
assertTrue(status.getErrorMsg().contains("] fail and try to create new one"), status.getErrorMsg());
}
latch.countDown();
}
});
latch.await();
return success.get();
}
Aggregations