use of org.apache.nifi.remote.exception.ProtocolException in project nifi by apache.
the class AbstractTransaction method complete.
@Override
public final TransactionCompletion complete() throws IOException {
try {
try {
if (state != TransactionState.TRANSACTION_CONFIRMED) {
throw new IllegalStateException("Cannot complete transaction with " + peer + " because state is " + state + "; Transaction can only be completed when state is " + TransactionState.TRANSACTION_CONFIRMED);
}
boolean backoff = false;
if (direction == TransferDirection.RECEIVE) {
if (transfers == 0) {
state = TransactionState.TRANSACTION_COMPLETED;
return new ClientTransactionCompletion(false, 0, 0L, System.nanoTime() - creationNanoTime);
}
// Confirm that we received the data and the peer can now discard it
logger.debug("{} Sending TRANSACTION_FINISHED to {}", this, peer);
writeTransactionResponse(ResponseCode.TRANSACTION_FINISHED);
state = TransactionState.TRANSACTION_COMPLETED;
} else {
final Response transactionResponse;
try {
transactionResponse = readTransactionResponse();
} catch (final IOException e) {
throw new IOException(this + " Failed to receive a response from " + peer + " when expecting a TransactionFinished Indicator. " + "It is unknown whether or not the peer successfully received/processed the data. " + e, e);
}
logger.debug("{} Received {} from {}", this, transactionResponse, peer);
if (transactionResponse.getCode() == ResponseCode.TRANSACTION_FINISHED_BUT_DESTINATION_FULL) {
peer.penalize(destinationId, penaltyMillis);
backoff = true;
} else if (transactionResponse.getCode() != ResponseCode.TRANSACTION_FINISHED) {
throw new ProtocolException("After sending data to " + peer + ", expected TRANSACTION_FINISHED response but got " + transactionResponse);
}
state = TransactionState.TRANSACTION_COMPLETED;
}
return new ClientTransactionCompletion(backoff, transfers, contentBytes, System.nanoTime() - creationNanoTime);
} catch (final IOException ioe) {
throw new IOException("Failed to complete transaction with " + peer + " due to " + ioe, ioe);
}
} catch (final Exception e) {
error();
throw e;
} finally {
close();
}
}
Aggregations