use of com.palantir.tracing.CloseableSpan in project dialogue by palantir.
the class QueuedChannel method scheduleNextTask.
/**
* Get the next call and attempt to execute it. If it is runnable, wire up the underlying future to the one
* previously returned to the caller. If it is not runnable, add it back into the queue. Returns true if more
* tasks may be able to be scheduled, and false otherwise.
*/
private boolean scheduleNextTask() {
DeferredCall queueHead = queuedCalls.poll();
if (queueHead == null) {
return false;
}
SettableFuture<Response> queuedResponse = queueHead.response();
// request will be quickly cancelled in that case.
if (queuedResponse.isDone()) {
decrementQueueSize();
queueHead.span().complete();
queueHead.timer().stop();
return true;
}
try (CloseableSpan ignored = queueHead.span().attach()) {
Endpoint endpoint = queueHead.endpoint();
Optional<ListenableFuture<Response>> maybeResponse = delegate.maybeExecute(endpoint, queueHead.request(), DO_NOT_SKIP_LIMITS);
if (maybeResponse.isPresent()) {
decrementQueueSize();
ListenableFuture<Response> response = maybeResponse.get();
queueHead.span().complete();
queueHead.timer().stop();
DialogueFutures.addDirectCallback(response, new ForwardAndSchedule(queuedResponse));
DialogueFutures.addDirectListener(queuedResponse, () -> {
if (queuedResponse.isCancelled()) {
// Currently cancel(false) will be converted to cancel(true)
if (!response.cancel(true) && log.isDebugEnabled()) {
log.debug("Failed to cancel delegate response, it should be reported by ForwardAndSchedule " + "logging", SafeArg.of("channel", channelName), SafeArg.of("service", endpoint.serviceName()), SafeArg.of("endpoint", endpoint.endpointName()));
}
}
});
return true;
} else {
if (!queuedCalls.offerFirst(queueHead)) {
// Should never happen, ConcurrentLinkedDeque has no maximum size
log.error("Failed to add an attempted call back to the deque", SafeArg.of("channel", channelName), SafeArg.of("service", endpoint.serviceName()), SafeArg.of("endpoint", endpoint.endpointName()));
decrementQueueSize();
queueHead.timer().stop();
if (!queuedResponse.setException(new SafeRuntimeException("Failed to req-queue request", SafeArg.of("channel", channelName), SafeArg.of("service", endpoint.serviceName()), SafeArg.of("endpoint", endpoint.endpointName())))) {
if (log.isDebugEnabled()) {
log.debug("Queued response has already been completed", SafeArg.of("channel", channelName), SafeArg.of("service", endpoint.serviceName()), SafeArg.of("endpoint", endpoint.endpointName()));
}
}
}
return false;
}
}
}
Aggregations