use of org.apache.ignite.raft.jraft.Closure in project ignite-3 by apache.
the class AddLearnersRequestProcessorTest method verify.
@Override
public void verify(final String interest, final Node node, final ArgumentCaptor<Closure> doneArg) {
assertEquals(AddLearnersRequest.class.getName(), interest);
Mockito.verify(node).addLearners(eq(Arrays.asList(new PeerId("learner", 8082), new PeerId("test", 8182), new PeerId("test", 8183))), doneArg.capture());
Closure done = doneArg.getValue();
assertNotNull(done);
done.run(Status.OK());
assertNotNull(this.asyncContext.getResponseObject());
assertEquals("[learner:8081, learner:8082, learner:8083]", this.asyncContext.as(LearnersOpResponse.class).oldLearnersList().toString());
assertEquals("[learner:8081, learner:8082, learner:8083, test:8182, test:8183]", this.asyncContext.as(LearnersOpResponse.class).newLearnersList().toString());
}
use of org.apache.ignite.raft.jraft.Closure in project ignite-3 by apache.
the class BallotBoxTest method testCommitAt.
@Test
public void testCommitAt() {
assertFalse(this.box.commitAt(1, 3, new PeerId("localhost", 8081)));
assertTrue(box.resetPendingIndex(1));
assertTrue(this.box.appendPendingTask(JRaftUtils.getConfiguration("localhost:8081,localhost:8082,localhost:8083"), JRaftUtils.getConfiguration("localhost:8081"), new Closure() {
@Override
public void run(Status status) {
}
}));
assertEquals(0, this.box.getLastCommittedIndex());
try {
this.box.commitAt(1, 3, new PeerId("localhost", 8081));
fail();
} catch (ArrayIndexOutOfBoundsException e) {
// No-op.
}
assertTrue(this.box.commitAt(1, 1, new PeerId("localhost", 8081)));
assertEquals(0, this.box.getLastCommittedIndex());
assertEquals(1, this.box.getPendingIndex());
assertTrue(this.box.commitAt(1, 1, new PeerId("localhost", 8082)));
assertEquals(1, this.box.getLastCommittedIndex());
assertEquals(2, this.box.getPendingIndex());
Mockito.verify(this.waiter, Mockito.only()).onCommitted(1);
}
use of org.apache.ignite.raft.jraft.Closure in project ignite-3 by apache.
the class NodeImpl method shutdown.
@Override
public void shutdown(final Closure done) {
this.writeLock.lock();
try {
LOG.info("Node {} shutdown, currTerm={} state={}.", getNodeId(), this.currTerm, this.state);
if (this.state.compareTo(State.STATE_SHUTTING) < 0) {
// If it is follower, call on_stop_following in step_down
if (this.state.compareTo(State.STATE_FOLLOWER) <= 0) {
stepDown(this.currTerm, this.state == State.STATE_LEADER, new Status(RaftError.ESHUTDOWN, "Raft node is going to quit."));
}
this.state = State.STATE_SHUTTING;
// Stop all pending timer callbacks.
stopAllTimers();
if (this.readOnlyService != null) {
this.readOnlyService.shutdown();
}
if (this.logManager != null) {
this.logManager.shutdown();
}
if (this.metaStorage != null) {
this.metaStorage.shutdown();
}
if (this.snapshotExecutor != null) {
this.snapshotExecutor.shutdown();
}
if (this.wakingCandidate != null) {
Replicator.stop(this.wakingCandidate);
}
if (this.fsmCaller != null) {
this.fsmCaller.shutdown();
}
if (this.rpcClientService != null) {
this.rpcClientService.shutdown();
}
if (this.applyQueue != null) {
final CountDownLatch latch = new CountDownLatch(1);
this.shutdownLatch = latch;
Utils.runInThread(this.getOptions().getCommonExecutor(), () -> this.applyQueue.publishEvent((event, sequence) -> {
event.groupId = groupId;
event.shutdownLatch = latch;
}));
}
}
if (this.state != State.STATE_SHUTDOWN) {
if (done != null) {
this.shutdownContinuations.add(done);
}
return;
}
// a writeLock which is already held by the caller
if (done != null) {
Utils.runClosureInThread(this.getOptions().getCommonExecutor(), done);
}
} finally {
this.writeLock.unlock();
}
}
use of org.apache.ignite.raft.jraft.Closure in project ignite-3 by apache.
the class NodeImpl method executeApplyingTasks.
private void executeApplyingTasks(final List<LogEntryAndClosure> tasks) {
this.writeLock.lock();
try {
final int size = tasks.size();
if (this.state != State.STATE_LEADER) {
final Status st = new Status();
if (this.state != State.STATE_TRANSFERRING) {
st.setError(RaftError.EPERM, "Is not leader.");
} else {
st.setError(RaftError.EBUSY, "Is transferring leadership.");
}
LOG.debug("Node {} can't apply, status={}.", getNodeId(), st);
final List<Closure> dones = tasks.stream().map(ele -> ele.done).collect(Collectors.toList());
Utils.runInThread(this.getOptions().getCommonExecutor(), () -> {
for (final Closure done : dones) {
done.run(st);
}
});
return;
}
final List<LogEntry> entries = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
final LogEntryAndClosure task = tasks.get(i);
if (task.expectedTerm != -1 && task.expectedTerm != this.currTerm) {
LOG.debug("Node {} can't apply task whose expectedTerm={} doesn't match currTerm={}.", getNodeId(), task.expectedTerm, this.currTerm);
if (task.done != null) {
final Status st = new Status(RaftError.EPERM, "expected_term=%d doesn't match current_term=%d", task.expectedTerm, this.currTerm);
Utils.runClosureInThread(this.getOptions().getCommonExecutor(), task.done, st);
}
continue;
}
if (!this.ballotBox.appendPendingTask(this.conf.getConf(), this.conf.isStable() ? null : this.conf.getOldConf(), task.done)) {
Utils.runClosureInThread(this.getOptions().getCommonExecutor(), task.done, new Status(RaftError.EINTERNAL, "Fail to append task."));
continue;
}
// set task entry info before adding to list.
task.entry.getId().setTerm(this.currTerm);
task.entry.setType(EnumOutter.EntryType.ENTRY_TYPE_DATA);
entries.add(task.entry);
}
this.logManager.appendEntries(entries, new LeaderStableClosure(entries));
// update conf.first
checkAndSetConfiguration(true);
} finally {
this.writeLock.unlock();
}
}
use of org.apache.ignite.raft.jraft.Closure in project ignite-3 by apache.
the class NodeImpl method afterShutdown.
private void afterShutdown() {
List<Closure> savedDoneList = null;
this.writeLock.lock();
try {
if (!this.shutdownContinuations.isEmpty()) {
savedDoneList = new ArrayList<>(this.shutdownContinuations);
}
if (this.logStorage != null) {
this.logStorage.shutdown();
}
this.state = State.STATE_SHUTDOWN;
} finally {
this.writeLock.unlock();
}
if (savedDoneList != null) {
for (final Closure closure : savedDoneList) {
Utils.runClosureInThread(this.getOptions().getCommonExecutor(), closure);
}
}
}
Aggregations