Search in sources :

Example 1 with ReadIndexClosure

use of org.apache.ignite.raft.jraft.closure.ReadIndexClosure 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();
}
Also used : Status(org.apache.ignite.raft.jraft.Status) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with ReadIndexClosure

use of org.apache.ignite.raft.jraft.closure.ReadIndexClosure in project ignite-3 by apache.

the class ReadOnlyServiceImpl method notifySuccess.

private void notifySuccess(final ReadIndexStatus status) {
    final long nowMs = Utils.monotonicMs();
    final List<ReadIndexState> states = status.getStates();
    final int taskCount = states.size();
    for (int i = 0; i < taskCount; i++) {
        final ReadIndexState task = states.get(i);
        // stack copy
        final ReadIndexClosure done = task.getDone();
        if (done != null) {
            this.nodeMetrics.recordLatency("read-index", nowMs - task.getStartTimeMs());
            done.setResult(task.getIndex(), task.getRequestContext().get());
            done.run(Status.OK());
        }
    }
}
Also used : ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) ReadIndexState(org.apache.ignite.raft.jraft.entity.ReadIndexState)

Example 3 with ReadIndexClosure

use of org.apache.ignite.raft.jraft.closure.ReadIndexClosure 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)

Example 4 with ReadIndexClosure

use of org.apache.ignite.raft.jraft.closure.ReadIndexClosure in project ignite-3 by apache.

the class ReadOnlyServiceImpl method reportError.

private void reportError(final ReadIndexStatus status, final Status st) {
    final long nowMs = Utils.monotonicMs();
    final List<ReadIndexState> states = status.getStates();
    final int taskCount = states.size();
    for (int i = 0; i < taskCount; i++) {
        final ReadIndexState task = states.get(i);
        final ReadIndexClosure done = task.getDone();
        if (done != null) {
            this.nodeMetrics.recordLatency("read-index", nowMs - task.getStartTimeMs());
            done.run(st);
        }
    }
}
Also used : ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) ReadIndexState(org.apache.ignite.raft.jraft.entity.ReadIndexState)

Example 5 with ReadIndexClosure

use of org.apache.ignite.raft.jraft.closure.ReadIndexClosure in project ignite-3 by apache.

the class ReadOnlyServiceTest method testOnApplied.

@Test
public void testOnApplied() throws Exception {
    final ArrayList<ReadIndexState> states = new ArrayList<>();
    final byte[] reqContext = TestUtils.getRandomBytes();
    final CountDownLatch latch = new CountDownLatch(1);
    final ReadIndexState state = new ReadIndexState(new Bytes(reqContext), new ReadIndexClosure() {

        @Override
        public void run(final Status status, final long index, final byte[] reqCtx) {
            assertTrue(status.isOk());
            assertEquals(index, 1);
            assertArrayEquals(reqCtx, reqContext);
            latch.countDown();
        }
    }, Utils.monotonicMs());
    state.setIndex(1);
    states.add(state);
    final ReadIndexStatus readIndexStatus = new ReadIndexStatus(states, null, 1);
    this.readOnlyServiceImpl.getPendingNotifyStatus().put(1L, Arrays.asList(readIndexStatus));
    this.readOnlyServiceImpl.onApplied(2);
    latch.await();
    assertTrue(this.readOnlyServiceImpl.getPendingNotifyStatus().isEmpty());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) ReadIndexStatus(org.apache.ignite.raft.jraft.entity.ReadIndexStatus) Bytes(org.apache.ignite.raft.jraft.util.Bytes) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) ReadIndexStatus(org.apache.ignite.raft.jraft.entity.ReadIndexStatus) ReadIndexState(org.apache.ignite.raft.jraft.entity.ReadIndexState) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

ReadIndexClosure (org.apache.ignite.raft.jraft.closure.ReadIndexClosure)14 Status (org.apache.ignite.raft.jraft.Status)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 Test (org.junit.jupiter.api.Test)10 ReadIndexStatus (org.apache.ignite.raft.jraft.entity.ReadIndexStatus)6 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)5 ReadIndexRequest (org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest)5 Node (org.apache.ignite.raft.jraft.Node)4 ReadIndexState (org.apache.ignite.raft.jraft.entity.ReadIndexState)4 ArgumentMatcher (org.mockito.ArgumentMatcher)4 RpcResponseClosure (org.apache.ignite.raft.jraft.rpc.RpcResponseClosure)3 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)2 Bytes (org.apache.ignite.raft.jraft.util.Bytes)2 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)2 EventFactory (com.lmax.disruptor.EventFactory)1 EventHandler (com.lmax.disruptor.EventHandler)1 EventTranslator (com.lmax.disruptor.EventTranslator)1 RingBuffer (com.lmax.disruptor.RingBuffer)1