Search in sources :

Example 1 with ResponseStatus

use of org.apache.gobblin.http.ResponseStatus 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)

Example 2 with ResponseStatus

use of org.apache.gobblin.http.ResponseStatus in project incubator-gobblin by apache.

the class HttpJoinConverter method convertRecord.

@Override
public final Iterable<DO> convertRecord(SO outputSchema, DI inputRecord, WorkUnitState workUnitState) throws DataConversionException {
    // Convert DI to HttpOperation
    HttpOperation operation = generateHttpOperation(inputRecord, workUnitState);
    BufferedRecord<GenericRecord> bufferedRecord = new BufferedRecord<>(operation, WriteCallback.EMPTY);
    // Convert HttpOperation to RQ
    Queue<BufferedRecord<GenericRecord>> buffer = new LinkedBlockingDeque<>();
    buffer.add(bufferedRecord);
    AsyncRequest<GenericRecord, RQ> request = this.requestBuilder.buildRequest(buffer);
    RQ rawRequest = request.getRawRequest();
    try {
        RP response = httpClient.sendRequest(rawRequest);
        ResponseStatus status = responseHandler.handleResponse(request, response);
        switch(status.getType()) {
            case OK:
            case CLIENT_ERROR:
                // Convert (DI, RQ, RP etc..) to output DO
                log.debug("{} send with status type {}", rawRequest, status.getType());
                DO output = convertRecordImpl(outputSchema, inputRecord, rawRequest, status);
                return new SingleRecordIterable<>(output);
            case SERVER_ERROR:
                // Server side error. Retry
                throw new DataConversionException(rawRequest + " send failed due to server error");
            default:
                throw new DataConversionException(rawRequest + " Should not reach here");
        }
    } catch (IOException e) {
        throw new DataConversionException(e);
    }
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) BufferedRecord(org.apache.gobblin.async.BufferedRecord) IOException(java.io.IOException) ResponseStatus(org.apache.gobblin.http.ResponseStatus) HttpOperation(org.apache.gobblin.http.HttpOperation) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 3 with ResponseStatus

use of org.apache.gobblin.http.ResponseStatus in project incubator-gobblin by apache.

the class HttpUtilsTest method testUpdateStatusType.

public void testUpdateStatusType() {
    Set<String> errorCodeWhitelist = new HashSet<>();
    ResponseStatus status = new ResponseStatus(StatusType.OK);
    HttpUtils.updateStatusType(status, 303, errorCodeWhitelist);
    // Client error without whitelist
    Assert.assertTrue(status.getType() == StatusType.CLIENT_ERROR);
    errorCodeWhitelist.add("303");
    HttpUtils.updateStatusType(status, 303, errorCodeWhitelist);
    // Continue with whitelist
    Assert.assertTrue(status.getType() == StatusType.CONTINUE);
    errorCodeWhitelist.clear();
    errorCodeWhitelist.add("3xx");
    HttpUtils.updateStatusType(status, 303, errorCodeWhitelist);
    // Continue with whitelist
    Assert.assertTrue(status.getType() == StatusType.CONTINUE);
    HttpUtils.updateStatusType(status, 404, errorCodeWhitelist);
    // Client error without whitelist
    Assert.assertTrue(status.getType() == StatusType.CLIENT_ERROR);
    errorCodeWhitelist.add("4xx");
    HttpUtils.updateStatusType(status, 404, errorCodeWhitelist);
    // Continue with whitelist
    Assert.assertTrue(status.getType() == StatusType.CONTINUE);
    HttpUtils.updateStatusType(status, 505, errorCodeWhitelist);
    // Server error without whitelist
    Assert.assertTrue(status.getType() == StatusType.SERVER_ERROR);
    errorCodeWhitelist.add("5xx");
    HttpUtils.updateStatusType(status, 505, errorCodeWhitelist);
    // Continue with whitelist
    Assert.assertTrue(status.getType() == StatusType.CONTINUE);
}
Also used : ResponseStatus(org.apache.gobblin.http.ResponseStatus) HashSet(java.util.HashSet)

Aggregations

ResponseStatus (org.apache.gobblin.http.ResponseStatus)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)1 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 BufferedRecord (org.apache.gobblin.async.BufferedRecord)1 DispatchException (org.apache.gobblin.async.DispatchException)1 HttpOperation (org.apache.gobblin.http.HttpOperation)1