use of com.alipay.sofa.jraft.entity.ReadIndexState in project sofa-jraft by sofastack.
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());
}
use of com.alipay.sofa.jraft.entity.ReadIndexState 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.entity.ReadIndexState in project sofa-jraft by sofastack.
the class ReadOnlyServiceImpl method executeReadIndexEvents.
private void executeReadIndexEvents(final List<ReadIndexEvent> events) {
if (events.isEmpty()) {
return;
}
final ReadIndexRequest.Builder rb = //
ReadIndexRequest.newBuilder().setGroupId(//
this.node.getGroupId()).setServerId(this.node.getServerId().toString());
final List<ReadIndexState> states = new ArrayList<>(events.size());
for (final ReadIndexEvent event : events) {
rb.addEntries(ZeroByteStringHelper.wrap(event.requestContext.get()));
states.add(new ReadIndexState(event.requestContext, event.done, event.startTime));
}
final ReadIndexRequest request = rb.build();
this.node.handleReadIndexRequest(request, new ReadIndexResponseClosure(states, request));
}
use of com.alipay.sofa.jraft.entity.ReadIndexState in project sofa-jraft by sofastack.
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);
}
}
}
Aggregations