Search in sources :

Example 6 with ReadIndexClosure

use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.

the class ReadOnlyServiceTest method testOverMaxReadIndexLag.

@Test
public void testOverMaxReadIndexLag() throws Exception {
    Mockito.when(this.fsmCaller.getLastAppliedIndex()).thenReturn(1L);
    this.readOnlyServiceImpl.getRaftOptions().setMaxReadIndexLag(50);
    final byte[] requestContext = TestUtils.getRandomBytes();
    final CountDownLatch latch = new CountDownLatch(1);
    final String errMsg = "Fail to run ReadIndex task, the gap of current node's apply index between leader's commit index over maxReadIndexLag";
    this.readOnlyServiceImpl.addRequest(requestContext, new ReadIndexClosure() {

        @Override
        public void run(final Status status, final long index, final byte[] reqCtx) {
            assertFalse(status.isOk());
            assertEquals(status.getErrorMsg(), errMsg);
            assertEquals(index, -1);
            assertArrayEquals(reqCtx, requestContext);
            latch.countDown();
        }
    });
    this.readOnlyServiceImpl.flush();
    final ArgumentCaptor<RpcResponseClosure> closureCaptor = ArgumentCaptor.forClass(RpcResponseClosure.class);
    Mockito.verify(this.node).handleReadIndexRequest(Mockito.argThat(new ArgumentMatcher<ReadIndexRequest>() {

        @Override
        public boolean matches(final Object argument) {
            if (argument instanceof ReadIndexRequest) {
                final ReadIndexRequest req = (ReadIndexRequest) argument;
                return req.getGroupId().equals("test") && req.getServerId().equals("localhost:8081:0") && req.getEntriesCount() == 1 && Arrays.equals(requestContext, req.getEntries(0).toByteArray());
            }
            return false;
        }
    }), closureCaptor.capture());
    final RpcResponseClosure closure = closureCaptor.getValue();
    assertNotNull(closure);
    closure.setResponse(ReadIndexResponse.newBuilder().setIndex(52).setSuccess(true).build());
    closure.run(Status.OK());
    latch.await();
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexStatus(com.alipay.sofa.jraft.entity.ReadIndexStatus) ReadIndexRequest(com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) ArgumentMatcher(org.mockito.ArgumentMatcher) RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 7 with ReadIndexClosure

use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.

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(com.alipay.sofa.jraft.closure.ReadIndexClosure) ReadIndexState(com.alipay.sofa.jraft.entity.ReadIndexState)

Example 8 with ReadIndexClosure

use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.

the class CounterServiceImpl method get.

@Override
public void get(final boolean readOnlySafe, final CounterClosure closure) {
    if (!readOnlySafe) {
        closure.success(getValue());
        closure.run(Status.OK());
        return;
    }
    this.counterServer.getNode().readIndex(BytesUtil.EMPTY_BYTES, new ReadIndexClosure() {

        @Override
        public void run(Status status, long index, byte[] reqCtx) {
            if (status.isOk()) {
                closure.success(getValue());
                closure.run(Status.OK());
                return;
            }
            CounterServiceImpl.this.readIndexExecutor.execute(() -> {
                if (isLeader()) {
                    LOG.debug("Fail to get value with 'ReadIndex': {}, try to applying to the state machine.", status);
                    applyOperation(CounterOperation.createGet(), closure);
                } else {
                    handlerNotLeaderError(closure);
                }
            });
        }
    });
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure)

Example 9 with ReadIndexClosure

use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project incubator-hugegraph by apache.

the class RaftBackendStore method queryByRaft.

private Object queryByRaft(Object query, boolean safeRead, Function<Object, Object> func) {
    if (!safeRead) {
        return func.apply(query);
    }
    RaftClosure<Object> future = new RaftClosure<>();
    ReadIndexClosure readIndexClosure = new ReadIndexClosure() {

        @Override
        public void run(Status status, long index, byte[] reqCtx) {
            if (status.isOk()) {
                future.complete(status, () -> func.apply(query));
            } else {
                future.failure(status, new BackendException("Failed to do raft read-index: %s", status));
            }
        }
    };
    this.node().readIndex(BytesUtil.EMPTY_BYTES, readIndexClosure);
    try {
        return future.waitFinished();
    } catch (Throwable e) {
        LOG.warn("Failed to execute query '{}': {}", query, future.status(), e);
        throw new BackendException("Failed to execute query: %s", e, query);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 10 with ReadIndexClosure

use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project nacos by alibaba.

the class JRaftServer method get.

CompletableFuture<Response> get(final ReadRequest request) {
    final String group = request.getGroup();
    CompletableFuture<Response> future = new CompletableFuture<>();
    final RaftGroupTuple tuple = findTupleByGroup(group);
    if (Objects.isNull(tuple)) {
        future.completeExceptionally(new NoSuchRaftGroupException(group));
        return future;
    }
    final Node node = tuple.node;
    final RequestProcessor processor = tuple.processor;
    try {
        node.readIndex(BytesUtil.EMPTY_BYTES, new ReadIndexClosure() {

            @Override
            public void run(Status status, long index, byte[] reqCtx) {
                if (status.isOk()) {
                    try {
                        Response response = processor.onRequest(request);
                        future.complete(response);
                    } catch (Throwable t) {
                        MetricsMonitor.raftReadIndexFailed();
                        future.completeExceptionally(new ConsistencyException("The conformance protocol is temporarily unavailable for reading", t));
                    }
                    return;
                }
                MetricsMonitor.raftReadIndexFailed();
                Loggers.RAFT.error("ReadIndex has error : {}, go to Leader read.", status.getErrorMsg());
                MetricsMonitor.raftReadFromLeader();
                readFromLeader(request, future);
            }
        });
        return future;
    } catch (Throwable e) {
        MetricsMonitor.raftReadFromLeader();
        Loggers.RAFT.warn("Raft linear read failed, go to Leader read logic : {}", e.toString());
        // run raft read
        readFromLeader(request, future);
        return future;
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Node(com.alipay.sofa.jraft.Node) NoSuchRaftGroupException(com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException) Response(com.alibaba.nacos.consistency.entity.Response) CompletableFuture(java.util.concurrent.CompletableFuture) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) RequestProcessor(com.alibaba.nacos.consistency.RequestProcessor)

Aggregations

ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)20 Status (com.alipay.sofa.jraft.Status)17 CountDownLatch (java.util.concurrent.CountDownLatch)11 Test (org.junit.Test)10 ReadIndexStatus (com.alipay.sofa.jraft.entity.ReadIndexStatus)7 ReadIndexRequest (com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest)6 Node (com.alipay.sofa.jraft.Node)5 ArgumentMatcher (org.mockito.ArgumentMatcher)5 PeerId (com.alipay.sofa.jraft.entity.PeerId)4 ReadIndexState (com.alipay.sofa.jraft.entity.ReadIndexState)4 RpcResponseClosure (com.alipay.sofa.jraft.rpc.RpcResponseClosure)4 Bytes (com.alipay.sofa.jraft.util.Bytes)2 Endpoint (com.alipay.sofa.jraft.util.Endpoint)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 RequestProcessor (com.alibaba.nacos.consistency.RequestProcessor)1 Response (com.alibaba.nacos.consistency.entity.Response)1 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)1 NoSuchRaftGroupException (com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException)1 FSMCaller (com.alipay.sofa.jraft.FSMCaller)1 LastAppliedLogIndexListener (com.alipay.sofa.jraft.FSMCaller.LastAppliedLogIndexListener)1