Search in sources :

Example 1 with DispatchException

use of org.apache.gobblin.async.DispatchException in project incubator-gobblin by apache.

the class AsyncHttpWriter method dispatch.

@Override
protected void dispatch(Queue<BufferedRecord<D>> buffer) throws DispatchException {
    AsyncRequest<D, RQ> asyncRequest = requestBuilder.buildRequest(buffer);
    if (asyncRequest == null) {
        return;
    }
    RQ rawRequest = asyncRequest.getRawRequest();
    RP response;
    int attempt = 0;
    while (attempt < maxAttempts) {
        try {
            response = httpClient.sendRequest(rawRequest);
        } catch (Exception e) {
            // Retry
            attempt++;
            if (attempt == maxAttempts) {
                LOG.error("Fail to send request");
                LOG.info(asyncRequest.toString());
                DispatchException de = new DispatchException("Write failed on IOException", e);
                onFailure(asyncRequest, de);
                throw de;
            } else {
                continue;
            }
        }
        ResponseStatus status = responseHandler.handleResponse(asyncRequest, response);
        switch(status.getType()) {
            case OK:
                // Write succeeds
                onSuccess(asyncRequest, status);
                return;
            case CONTINUE:
                LOG.debug("Http write continues");
                LOG.debug(asyncRequest.toString());
                onSuccess(asyncRequest, status);
                return;
            case CLIENT_ERROR:
                // Client error. Fail!
                LOG.error("Http write failed on client error");
                LOG.info(asyncRequest.toString());
                DispatchException clientExp = new DispatchException("Write failed on client error");
                onFailure(asyncRequest, clientExp);
                throw clientExp;
            case SERVER_ERROR:
                // Server side error. Retry
                attempt++;
                if (attempt == maxAttempts) {
                    LOG.error("Http write request failed on server error");
                    LOG.info(asyncRequest.toString());
                    DispatchException serverExp = new DispatchException("Write failed after " + maxAttempts + " attempts.");
                    onFailure(asyncRequest, serverExp);
                    throw serverExp;
                }
        }
    }
}
Also used : ResponseStatus(org.apache.gobblin.http.ResponseStatus) DispatchException(org.apache.gobblin.async.DispatchException) IOException(java.io.IOException) DispatchException(org.apache.gobblin.async.DispatchException)

Aggregations

IOException (java.io.IOException)1 DispatchException (org.apache.gobblin.async.DispatchException)1 ResponseStatus (org.apache.gobblin.http.ResponseStatus)1