use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.
the class AtomicRangeGroup method readFromQuorum.
public void readFromQuorum(final String key, RpcContext asyncContext) {
final byte[] reqContext = new byte[4];
Bits.putInt(reqContext, 0, requestId.incrementAndGet());
this.node.readIndex(reqContext, new ReadIndexClosure() {
@Override
public void run(Status status, long index, byte[] reqCtx) {
if (status.isOk()) {
try {
asyncContext.sendResponse(new ValueCommand(fsm.getValue(key)));
} catch (final KeyNotFoundException e) {
asyncContext.sendResponse(GetCommandProcessor.createKeyNotFoundResponse());
}
} else {
asyncContext.sendResponse(new BooleanCommand(false, status.getErrorMsg()));
}
}
});
}
use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.
the class ReadOnlyServiceImpl method addRequest.
@Override
public void addRequest(final byte[] reqCtx, final ReadIndexClosure closure) {
if (this.shutdownLatch != null) {
Utils.runClosureInThread(closure, new Status(RaftError.EHOSTDOWN, "Was stopped"));
throw new IllegalStateException("Service already shutdown.");
}
try {
EventTranslator<ReadIndexEvent> translator = (event, sequence) -> {
event.done = closure;
event.requestContext = new Bytes(reqCtx);
event.startTime = Utils.monotonicMs();
};
switch(this.node.getOptions().getApplyTaskMode()) {
case Blocking:
this.readIndexQueue.publishEvent(translator);
break;
case NonBlocking:
default:
if (!this.readIndexQueue.tryPublishEvent(translator)) {
final String errorMsg = "Node is busy, has too many read-index requests, queue is full and bufferSize=" + this.readIndexQueue.getBufferSize();
Utils.runClosureInThread(closure, new Status(RaftError.EBUSY, errorMsg));
this.nodeMetrics.recordTimes("read-index-overload-times", 1);
LOG.warn("Node {} ReadOnlyServiceImpl readIndexQueue is overload.", this.node.getNodeId());
if (closure == null) {
throw new OverloadException(errorMsg);
}
}
break;
}
} catch (final Exception e) {
Utils.runClosureInThread(closure, new Status(RaftError.EPERM, "Node is down."));
}
}
use of com.alipay.sofa.jraft.closure.ReadIndexClosure 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);
}
}
}
use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project mmqtt by MrHKing.
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 : {}", status.getErrorMsg());
future.completeExceptionally(new ConsistencyException("The conformance protocol is temporarily unavailable for reading, " + status.getErrorMsg()));
}
});
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;
}
}
use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project incubator-hugegraph by apache.
the class RaftNode method waitStarted.
protected void waitStarted(int timeout) {
String group = this.context.group();
ReadIndexClosure readIndexClosure = new ReadIndexClosure() {
@Override
public void run(Status status, long index, byte[] reqCtx) {
RaftNode.this.started.set(status.isOk());
}
};
long beginTime = System.currentTimeMillis();
while (true) {
this.node.readIndex(BytesUtil.EMPTY_BYTES, readIndexClosure);
if (this.started.get()) {
break;
}
try {
Thread.sleep(RaftSharedContext.POLL_INTERVAL);
} catch (InterruptedException e) {
LOG.info("Waiting for heartbeat is interrupted: {}", e);
}
long consumedTime = System.currentTimeMillis() - beginTime;
if (timeout > 0 && consumedTime >= timeout) {
throw new BackendException("Waiting for raft group '%s' heartbeat timeout(%sms)", group, consumedTime);
}
LOG.warn("Waiting for raft group '{}' heartbeat cost {}s", group, consumedTime / 1000.0);
}
}
Aggregations