use of org.apache.ratis.protocol.ClientInvocationId in project incubator-ratis by apache.
the class NettyClientStreamRpc method getClientHandler.
private ChannelInboundHandler getClientHandler() {
return new ChannelInboundHandlerAdapter() {
private ClientInvocationId clientInvocationId;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (!(msg instanceof DataStreamReply)) {
LOG.error("{}: unexpected message {}", this, msg.getClass());
return;
}
final DataStreamReply reply = (DataStreamReply) msg;
LOG.debug("{}: read {}", this, reply);
clientInvocationId = ClientInvocationId.valueOf(reply.getClientId(), reply.getStreamId());
final ReplyQueue queue = reply.isSuccess() ? replies.get(clientInvocationId) : replies.remove(clientInvocationId);
if (queue != null) {
final CompletableFuture<DataStreamReply> f = queue.poll();
if (f != null) {
f.complete(reply);
if (!reply.isSuccess() && queue.size() > 0) {
final IllegalStateException e = new IllegalStateException(this + ": an earlier request failed with " + reply);
queue.forEach(future -> future.completeExceptionally(e));
}
final Integer emptyId = queue.getEmptyId();
if (emptyId != null) {
timeoutScheduler.onTimeout(replyQueueGracePeriod, // remove the queue if the same queue has been empty for the entire grace period.
() -> replies.computeIfPresent(clientInvocationId, (key, q) -> q == queue && emptyId.equals(q.getEmptyId()) ? null : q), LOG, () -> "Timeout check failed, clientInvocationId=" + clientInvocationId);
}
}
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
LOG.warn(name + ": exceptionCaught", cause);
Optional.ofNullable(clientInvocationId).map(replies::remove).orElse(ReplyQueue.EMPTY).forEach(f -> f.completeExceptionally(cause));
LOG.warn(name + ": exceptionCaught", cause);
ctx.close();
}
};
}
use of org.apache.ratis.protocol.ClientInvocationId in project incubator-ratis by apache.
the class DataStreamManagement method computeDataStreamIfAbsent.
private CompletableFuture<DataStream> computeDataStreamIfAbsent(RaftClientRequest request) throws IOException {
final Division division = server.getDivision(request.getRaftGroupId());
final ClientInvocationId invocationId = ClientInvocationId.valueOf(request);
final MemoizedSupplier<CompletableFuture<DataStream>> supplier = JavaUtils.memoize(() -> {
final RequestMetrics metrics = getMetrics().newRequestMetrics(RequestType.STATE_MACHINE_STREAM);
final RequestContext context = metrics.start();
return division.getStateMachine().data().stream(request).whenComplete((r, e) -> metrics.stop(context, e == null));
});
final CompletableFuture<DataStream> f = division.getDataStreamMap().computeIfAbsent(invocationId, key -> supplier.get());
if (!supplier.isInitialized()) {
throw new AlreadyExistsException("A DataStream already exists for " + invocationId);
}
return f;
}
use of org.apache.ratis.protocol.ClientInvocationId in project incubator-ratis by apache.
the class TestRetryCacheMetrics method testRetryCacheEntryCount.
@Test
public void testRetryCacheEntryCount() {
checkEntryCount(0);
ClientId clientId = ClientId.randomId();
final ClientInvocationId key = ClientInvocationId.valueOf(clientId, 1);
final RetryCacheImpl.CacheEntry entry = new RetryCacheImpl.CacheEntry(key);
retryCache.refreshEntry(entry);
checkEntryCount(1);
}
use of org.apache.ratis.protocol.ClientInvocationId in project incubator-ratis by apache.
the class TestRetryCacheMetrics method testRetryCacheHitMissCount.
@Test
public void testRetryCacheHitMissCount() {
checkHit(0, 1.0);
checkMiss(0, 0.0);
final ClientInvocationId invocationId = ClientInvocationId.valueOf(ClientId.randomId(), 2);
retryCache.getOrCreateEntry(invocationId);
checkHit(0, 0.0);
checkMiss(1, 1.0);
retryCache.getOrCreateEntry(invocationId);
checkHit(1, 0.5);
checkMiss(1, 0.5);
}
use of org.apache.ratis.protocol.ClientInvocationId in project incubator-ratis by apache.
the class RetryCacheTestUtil method createEntry.
public static void createEntry(RetryCache cache, LogEntryProto logEntry) {
if (logEntry.hasStateMachineLogEntry()) {
final ClientInvocationId invocationId = ClientInvocationId.valueOf(logEntry.getStateMachineLogEntry());
getOrCreateEntry(cache, invocationId);
}
}
Aggregations