Search in sources :

Example 1 with ResponseHeader

use of com.twitter.distributedlog.thrift.service.ResponseHeader in project distributedlog by twitter.

the class ResponseUtils method exceptionToHeader.

public static ResponseHeader exceptionToHeader(Throwable t) {
    ResponseHeader response = new ResponseHeader();
    if (t instanceof DLException) {
        DLException dle = (DLException) t;
        if (dle instanceof OwnershipAcquireFailedException) {
            response.setLocation(((OwnershipAcquireFailedException) dle).getCurrentOwner());
        }
        response.setCode(dle.getCode());
        response.setErrMsg(dle.getMessage());
    } else {
        response.setCode(StatusCode.INTERNAL_SERVER_ERROR);
        response.setErrMsg("Internal server error : " + t.getMessage());
    }
    return response;
}
Also used : OwnershipAcquireFailedException(com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException) ResponseHeader(com.twitter.distributedlog.thrift.service.ResponseHeader) DLException(com.twitter.distributedlog.exceptions.DLException)

Example 2 with ResponseHeader

use of com.twitter.distributedlog.thrift.service.ResponseHeader in project distributedlog by twitter.

the class DistributedLogServiceImpl method executeStreamOp.

private void executeStreamOp(final StreamOp op) {
    // Must attach this as early as possible--returning before this point will cause us to
    // lose the status code.
    op.responseHeader().addEventListener(new FutureEventListener<ResponseHeader>() {

        @Override
        public void onSuccess(ResponseHeader header) {
            if (header.getLocation() != null || header.getCode() == StatusCode.FOUND) {
                redirects.inc();
            }
            countStatusCode(header.getCode());
        }

        @Override
        public void onFailure(Throwable cause) {
        }
    });
    try {
        // Apply the request limiter
        limiter.apply(op);
        // Execute per-op pre-exec code
        op.preExecute();
    } catch (TooManyStreamsException e) {
        // Translate to StreamUnavailableException to ensure that the client will redirect
        // to a different host. Ideally we would be able to return TooManyStreamsException,
        // but the way exception handling works right now we can't control the handling in
        // the client because client changes deploy very slowly.
        op.fail(new StreamUnavailableException(e.getMessage()));
        return;
    } catch (Exception e) {
        op.fail(e);
        return;
    }
    Stream stream;
    try {
        stream = getLogWriter(op.streamName());
    } catch (RegionUnavailableException rue) {
        // redirect the requests to other region
        op.fail(new RegionUnavailableException("Region " + serverRegionId + " is unavailable."));
        return;
    } catch (IOException e) {
        op.fail(e);
        return;
    }
    if (null == stream) {
        // redirect the requests when stream is unavailable.
        op.fail(new ServiceUnavailableException("Server " + clientId + " is closed."));
        return;
    }
    if (op instanceof WriteOpWithPayload) {
        WriteOpWithPayload writeOp = (WriteOpWithPayload) op;
        windowedBps.add(writeOp.getPayloadSize());
        windowedRps.inc();
    }
    stream.submit(op);
}
Also used : StreamUnavailableException(com.twitter.distributedlog.exceptions.StreamUnavailableException) WriteOpWithPayload(com.twitter.distributedlog.service.stream.WriteOpWithPayload) ResponseHeader(com.twitter.distributedlog.thrift.service.ResponseHeader) RegionUnavailableException(com.twitter.distributedlog.exceptions.RegionUnavailableException) TooManyStreamsException(com.twitter.distributedlog.exceptions.TooManyStreamsException) Stream(com.twitter.distributedlog.service.stream.Stream) IOException(java.io.IOException) ServiceUnavailableException(com.twitter.distributedlog.exceptions.ServiceUnavailableException) ServiceUnavailableException(com.twitter.distributedlog.exceptions.ServiceUnavailableException) StreamUnavailableException(com.twitter.distributedlog.exceptions.StreamUnavailableException) RegionUnavailableException(com.twitter.distributedlog.exceptions.RegionUnavailableException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) TooManyStreamsException(com.twitter.distributedlog.exceptions.TooManyStreamsException)

Example 3 with ResponseHeader

use of com.twitter.distributedlog.thrift.service.ResponseHeader 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);
        }
    });
}
Also used : ResponseHeader(com.twitter.distributedlog.thrift.service.ResponseHeader) Optional(com.google.common.base.Optional) ProxyClient(com.twitter.distributedlog.client.proxy.ProxyClient)

Aggregations

ResponseHeader (com.twitter.distributedlog.thrift.service.ResponseHeader)3 Optional (com.google.common.base.Optional)1 ProxyClient (com.twitter.distributedlog.client.proxy.ProxyClient)1 DLException (com.twitter.distributedlog.exceptions.DLException)1 OwnershipAcquireFailedException (com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException)1 RegionUnavailableException (com.twitter.distributedlog.exceptions.RegionUnavailableException)1 ServiceUnavailableException (com.twitter.distributedlog.exceptions.ServiceUnavailableException)1 StreamUnavailableException (com.twitter.distributedlog.exceptions.StreamUnavailableException)1 TooManyStreamsException (com.twitter.distributedlog.exceptions.TooManyStreamsException)1 Stream (com.twitter.distributedlog.service.stream.Stream)1 WriteOpWithPayload (com.twitter.distributedlog.service.stream.WriteOpWithPayload)1 IOException (java.io.IOException)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1