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;
}
}
}
}
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);
}
}
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);
}
Aggregations