use of org.apache.ignite.raft.jraft.util.Bytes 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."));
}
}
use of org.apache.ignite.raft.jraft.util.Bytes 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());
}
Aggregations