Search in sources :

Example 16 with ProtocolException

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();
    }
}
Also used : Response(org.apache.nifi.remote.protocol.Response) ProtocolException(org.apache.nifi.remote.exception.ProtocolException) IOException(java.io.IOException) IOException(java.io.IOException) ProtocolException(org.apache.nifi.remote.exception.ProtocolException)

Aggregations

ProtocolException (org.apache.nifi.remote.exception.ProtocolException)16 IOException (java.io.IOException)10 DataInputStream (java.io.DataInputStream)5 CommunicationsSession (org.apache.nifi.remote.protocol.CommunicationsSession)5 InputStream (java.io.InputStream)4 FlowFile (org.apache.nifi.flowfile.FlowFile)4 HandshakeException (org.apache.nifi.remote.exception.HandshakeException)4 Response (org.apache.nifi.remote.protocol.Response)4 StandardDataPacket (org.apache.nifi.remote.util.StandardDataPacket)4 StopWatch (org.apache.nifi.util.StopWatch)4 DataOutputStream (java.io.DataOutputStream)3 HashMap (java.util.HashMap)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 SocketTimeoutException (java.net.SocketTimeoutException)2 HashSet (java.util.HashSet)2 CheckedInputStream (java.util.zip.CheckedInputStream)2 ProcessSession (org.apache.nifi.processor.ProcessSession)2 ProcessException (org.apache.nifi.processor.exception.ProcessException)2 BadRequestException (org.apache.nifi.remote.exception.BadRequestException)2 NotAuthorizedException (org.apache.nifi.remote.exception.NotAuthorizedException)2