use of com.twitter.distributedlog.client.proxy.ProxyClient in project distributedlog by twitter.
the class DistributedLogClientImpl method sendWriteRequest.
private void sendWriteRequest(final SocketAddress addr, final StreamOp op) {
// Get corresponding finagle client
final ProxyClient sc = clientManager.getClient(addr);
final long startTimeNanos = System.nanoTime();
// write the request to that host.
op.sendRequest(sc).addEventListener(new FutureEventListener<ResponseHeader>() {
@Override
public void onSuccess(ResponseHeader header) {
if (logger.isDebugEnabled()) {
logger.debug("Received response; header: {}", header);
}
clientStats.completeProxyRequest(addr, header.getCode(), startTimeNanos);
// update routing context
op.routingContext.addTriedHost(addr, header.getCode());
switch(header.getCode()) {
case SUCCESS:
// success handling is done per stream op
break;
case FOUND:
handleRedirectResponse(header, op, addr);
break;
// for overcapacity, dont report failure since this normally happens quite a bit
case OVER_CAPACITY:
logger.debug("Failed to write request to {} : {}", op.stream, header);
op.fail(addr, DLException.of(header));
break;
// we should fail them immediately (e.g. TOO_LARGE_RECORD, METADATA_EXCEPTION)
case NOT_IMPLEMENTED:
case METADATA_EXCEPTION:
case LOG_EMPTY:
case LOG_NOT_FOUND:
case TRUNCATED_TRANSACTION:
case END_OF_STREAM:
case TRANSACTION_OUT_OF_ORDER:
case INVALID_STREAM_NAME:
case REQUEST_DENIED:
case TOO_LARGE_RECORD:
case CHECKSUM_FAILED:
// since the proxy may still own the stream.
case STREAM_NOT_READY:
op.fail(addr, DLException.of(header));
break;
case SERVICE_UNAVAILABLE:
handleServiceUnavailable(addr, sc, Optional.of(op));
break;
case REGION_UNAVAILABLE:
// region is unavailable, redirect the request to hosts in other region
redirect(op, null);
break;
// we didn't have it in the first place.
case TOO_MANY_STREAMS:
handleRedirectableError(addr, op, header);
break;
case STREAM_UNAVAILABLE:
case ZOOKEEPER_ERROR:
case LOCKING_EXCEPTION:
case UNEXPECTED:
case INTERRUPTED:
case BK_TRANSMIT_ERROR:
case FLUSH_TIMEOUT:
default:
// when we are receiving these exceptions from proxy, it means proxy or the stream is closed
// redirect the request.
ownershipCache.removeOwnerFromStream(op.stream, addr, header.getCode().name());
handleRedirectableError(addr, op, header);
break;
}
}
@Override
public void onFailure(Throwable cause) {
Optional<StreamOp> opOptional = Optional.of(op);
cause = showRootCause(opOptional, cause);
clientStats.failProxyRequest(addr, cause, startTimeNanos);
handleRequestException(addr, sc, opOptional, cause);
}
});
}
Aggregations