use of org.apache.ignite.raft.jraft.Status in project ignite-3 by apache.
the class LocalSnapshotCopierTest method testCancelByRemote.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testCancelByRemote() throws Exception {
final CompletableFuture<Message> future = new CompletableFuture<>();
final RpcRequests.GetFileRequest rb = raftOptions.getRaftMessagesFactory().getFileRequest().readerId(99).filename(Snapshot.JRAFT_SNAPSHOT_META_FILE).count(Integer.MAX_VALUE).offset(0).readPartly(true).build();
// mock get metadata
ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8081)), eq(rb), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
this.copier.start();
assertTrue(TestUtils.waitForArgumentCapture(argument, 5_000));
final RpcResponseClosure<RpcRequests.GetFileResponse> closure = argument.getValue();
closure.run(new Status(RaftError.ECANCELED, "test cancel"));
this.copier.join();
// start timer
final SnapshotReader reader = this.copier.getReader();
assertNull(reader);
assertEquals(RaftError.ECANCELED.getNumber(), this.copier.getCode());
assertEquals("test cancel", this.copier.getErrorMsg());
}
use of org.apache.ignite.raft.jraft.Status in project ignite-3 by apache.
the class RpcResponseFactoryTest method testNewResponseWithErrorStatus.
@Test
public void testNewResponseWithErrorStatus() {
ErrorResponse response = (ErrorResponse) RaftRpcFactory.DEFAULT.newResponse(msgFactory, new Status(300, "test"));
assertEquals(300, response.errorCode());
assertEquals("test", response.errorMsg());
}
use of org.apache.ignite.raft.jraft.Status in project ignite-3 by apache.
the class LogManagerTest method testAppendEntriesBeforeAppliedIndex.
@Test
public void testAppendEntriesBeforeAppliedIndex() throws Exception {
// Append 0-10
List<LogEntry> mockEntries = TestUtils.mockEntries(10);
for (int i = 0; i < 10; i++) {
mockEntries.get(i).getId().setTerm(1);
}
final CountDownLatch latch1 = new CountDownLatch(1);
this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {
@Override
public void run(final Status status) {
assertTrue(status.isOk());
latch1.countDown();
}
});
latch1.await();
assertEquals(1, this.logManager.getFirstLogIndex());
assertEquals(10, this.logManager.getLastLogIndex());
// waiting for setDiskId()
Thread.sleep(200);
this.logManager.setAppliedId(new LogId(9, 1));
for (int i = 0; i < 10; i++) {
assertNull(this.logManager.getEntryFromMemory(i));
}
// append 1-10 again, already applied, returns OK.
final CountDownLatch latch2 = new CountDownLatch(1);
mockEntries = TestUtils.mockEntries(10);
mockEntries.remove(0);
this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {
@Override
public void run(final Status status) {
assertTrue(status.isOk());
latch2.countDown();
}
});
latch2.await();
assertEquals(1, this.logManager.getFirstLogIndex());
assertEquals(10, this.logManager.getLastLogIndex());
}
use of org.apache.ignite.raft.jraft.Status in project ignite-3 by apache.
the class NodeImpl method handleRequestVoteResponse.
public void handleRequestVoteResponse(final PeerId peerId, final long term, final RequestVoteResponse response) {
this.writeLock.lock();
try {
if (this.state != State.STATE_CANDIDATE) {
LOG.warn("Node {} received invalid RequestVoteResponse from {}, state not in STATE_CANDIDATE but {}.", getNodeId(), peerId, this.state);
return;
}
// check stale term
if (term != this.currTerm) {
LOG.warn("Node {} received stale RequestVoteResponse from {}, term={}, currTerm={}.", getNodeId(), peerId, term, this.currTerm);
return;
}
// check response term
if (response.term() > this.currTerm) {
LOG.warn("Node {} received invalid RequestVoteResponse from {}, term={}, expect={}.", getNodeId(), peerId, response.term(), this.currTerm);
stepDown(response.term(), false, new Status(RaftError.EHIGHERTERMRESPONSE, "Raft node receives higher term request_vote_response."));
return;
}
// check granted quorum?
if (response.granted()) {
this.voteCtx.grant(peerId);
if (this.voteCtx.isGranted()) {
becomeLeader();
}
}
} finally {
this.writeLock.unlock();
}
}
use of org.apache.ignite.raft.jraft.Status in project ignite-3 by apache.
the class ReadOnlyServiceImpl method addRequest.
@Override
public void addRequest(final byte[] reqCtx, final ReadIndexClosure closure) {
if (this.shutdownLatch != null) {
Utils.runClosureInThread(this.node.getOptions().getCommonExecutor(), closure, new Status(RaftError.EHOSTDOWN, "Was stopped"));
throw new IllegalStateException("Service already shutdown.");
}
try {
EventTranslator<ReadIndexEvent> translator = (event, sequence) -> {
event.groupId = this.groupId;
event.done = closure;
event.requestContext = new Bytes(reqCtx);
event.startTime = Utils.monotonicMs();
};
int retryTimes = 0;
while (true) {
if (this.readIndexQueue.tryPublishEvent(translator)) {
break;
} else {
retryTimes++;
if (retryTimes > MAX_ADD_REQUEST_RETRY_TIMES) {
Utils.runClosureInThread(this.node.getOptions().getCommonExecutor(), closure, new Status(RaftError.EBUSY, "Node is busy, has too many read-only requests."));
this.nodeMetrics.recordTimes("read-index-overload-times", 1);
LOG.warn("Node {} ReadOnlyServiceImpl readIndexQueue is overload.", this.node.getNodeId());
return;
}
ThreadHelper.onSpinWait();
}
}
} catch (final Exception e) {
Utils.runClosureInThread(this.node.getOptions().getCommonExecutor(), closure, new Status(RaftError.EPERM, "Node is down."));
}
}
Aggregations