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();
}
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());
}
}
}
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);
}
});
}
});
}
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);
}
}
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;
}
}
Aggregations