Search in sources :

Example 21 with Status

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());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) CompletableFuture(java.util.concurrent.CompletableFuture) Message(org.apache.ignite.raft.jraft.rpc.Message) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) RpcResponseClosure(org.apache.ignite.raft.jraft.rpc.RpcResponseClosure) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) SnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest) Test(org.junit.jupiter.api.Test)

Example 22 with Status

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());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.jupiter.api.Test)

Example 23 with Status

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());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) CountDownLatch(java.util.concurrent.CountDownLatch) LogManager(org.apache.ignite.raft.jraft.storage.LogManager) LogId(org.apache.ignite.raft.jraft.entity.LogId) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest) Test(org.junit.jupiter.api.Test)

Example 24 with Status

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();
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status)

Example 25 with Status

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."));
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) ReadIndexStatus(org.apache.ignite.raft.jraft.entity.ReadIndexStatus) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) RaftException(org.apache.ignite.raft.jraft.error.RaftException) IgniteLogger(org.apache.ignite.lang.IgniteLogger) ArrayList(java.util.ArrayList) LastAppliedLogIndexListener(org.apache.ignite.raft.jraft.FSMCaller.LastAppliedLogIndexListener) StripedDisruptor(org.apache.ignite.raft.jraft.disruptor.StripedDisruptor) Map(java.util.Map) RpcResponseClosureAdapter(org.apache.ignite.raft.jraft.rpc.RpcResponseClosureAdapter) EventHandler(com.lmax.disruptor.EventHandler) ReadOnlyServiceOptions(org.apache.ignite.raft.jraft.option.ReadOnlyServiceOptions) ReadIndexResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexResponse) ReadOnlyService(org.apache.ignite.raft.jraft.ReadOnlyService) ReadIndexRequestBuilder(org.apache.ignite.raft.jraft.rpc.ReadIndexRequestBuilder) Iterator(java.util.Iterator) ReentrantLock(java.util.concurrent.locks.ReentrantLock) RingBuffer(com.lmax.disruptor.RingBuffer) Status(org.apache.ignite.raft.jraft.Status) ThreadHelper(org.apache.ignite.raft.jraft.util.ThreadHelper) GroupAware(org.apache.ignite.raft.jraft.disruptor.GroupAware) OnlyForTest(org.apache.ignite.raft.jraft.util.OnlyForTest) TimeUnit(java.util.concurrent.TimeUnit) Utils(org.apache.ignite.raft.jraft.util.Utils) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Lock(java.util.concurrent.locks.Lock) ReadIndexStatus(org.apache.ignite.raft.jraft.entity.ReadIndexStatus) EventTranslator(com.lmax.disruptor.EventTranslator) TreeMap(java.util.TreeMap) FSMCaller(org.apache.ignite.raft.jraft.FSMCaller) ReadIndexRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest) ReadIndexState(org.apache.ignite.raft.jraft.entity.ReadIndexState) DisruptorMetricSet(org.apache.ignite.raft.jraft.util.DisruptorMetricSet) ByteString(org.apache.ignite.raft.jraft.util.ByteString) Bytes(org.apache.ignite.raft.jraft.util.Bytes) EventFactory(com.lmax.disruptor.EventFactory) RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) RaftError(org.apache.ignite.raft.jraft.error.RaftError) Bytes(org.apache.ignite.raft.jraft.util.Bytes) RaftException(org.apache.ignite.raft.jraft.error.RaftException)

Aggregations

Status (org.apache.ignite.raft.jraft.Status)121 Test (org.junit.jupiter.api.Test)49 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)43 CountDownLatch (java.util.concurrent.CountDownLatch)31 Message (org.apache.ignite.raft.jraft.rpc.Message)21 ArrayList (java.util.ArrayList)20 Node (org.apache.ignite.raft.jraft.Node)20 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)20 ReadIndexClosure (org.apache.ignite.raft.jraft.closure.ReadIndexClosure)16 LogId (org.apache.ignite.raft.jraft.entity.LogId)14 RaftException (org.apache.ignite.raft.jraft.error.RaftException)14 LogEntry (org.apache.ignite.raft.jraft.entity.LogEntry)11 JRaftException (org.apache.ignite.raft.jraft.error.JRaftException)11 SynchronizedClosure (org.apache.ignite.raft.jraft.closure.SynchronizedClosure)10 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)10 ByteBuffer (java.nio.ByteBuffer)9 List (java.util.List)9 ConfigurationEntry (org.apache.ignite.raft.jraft.conf.ConfigurationEntry)9 Task (org.apache.ignite.raft.jraft.entity.Task)8 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)8