Search in sources :

Example 1 with Attempt

use of com.amazonaws.services.kinesis.producer.Attempt in project flink by apache.

the class FlinkKinesisProducer method checkAndPropagateAsyncError.

/**
 * Check if there are any asynchronous exceptions. If so, rethrow the exception.
 */
private void checkAndPropagateAsyncError() throws Exception {
    if (thrownException != null) {
        String errorMessages = "";
        if (thrownException instanceof UserRecordFailedException) {
            List<Attempt> attempts = ((UserRecordFailedException) thrownException).getResult().getAttempts();
            for (Attempt attempt : attempts) {
                if (attempt.getErrorMessage() != null) {
                    errorMessages += attempt.getErrorMessage() + "\n";
                }
            }
        }
        if (failOnError) {
            throw new RuntimeException("An exception was thrown while processing a record: " + errorMessages, thrownException);
        } else {
            LOG.warn("An exception was thrown while processing a record: {}.", errorMessages, thrownException);
            // reset, prevent double throwing
            thrownException = null;
        }
    }
}
Also used : Attempt(com.amazonaws.services.kinesis.producer.Attempt) UserRecordFailedException(com.amazonaws.services.kinesis.producer.UserRecordFailedException)

Example 2 with Attempt

use of com.amazonaws.services.kinesis.producer.Attempt in project flink by apache.

the class FlinkKinesisProducer method invoke.

@Override
public void invoke(OUT value) throws Exception {
    if (this.producer == null) {
        throw new RuntimeException("Kinesis producer has been closed");
    }
    if (thrownException != null) {
        String errorMessages = "";
        if (thrownException instanceof UserRecordFailedException) {
            List<Attempt> attempts = ((UserRecordFailedException) thrownException).getResult().getAttempts();
            for (Attempt attempt : attempts) {
                if (attempt.getErrorMessage() != null) {
                    errorMessages += attempt.getErrorMessage() + "\n";
                }
            }
        }
        if (failOnError) {
            throw new RuntimeException("An exception was thrown while processing a record: " + errorMessages, thrownException);
        } else {
            LOG.warn("An exception was thrown while processing a record: {}", thrownException, errorMessages);
            // reset
            thrownException = null;
        }
    }
    String stream = defaultStream;
    String partition = defaultPartition;
    ByteBuffer serialized = schema.serialize(value);
    // maybe set custom stream
    String customStream = schema.getTargetStream(value);
    if (customStream != null) {
        stream = customStream;
    }
    String explicitHashkey = null;
    // maybe set custom partition
    if (customPartitioner != null) {
        partition = customPartitioner.getPartitionId(value);
        explicitHashkey = customPartitioner.getExplicitHashKey(value);
    }
    if (stream == null) {
        if (failOnError) {
            throw new RuntimeException("No target stream set");
        } else {
            LOG.warn("No target stream set. Skipping record");
            return;
        }
    }
    ListenableFuture<UserRecordResult> cb = producer.addUserRecord(stream, partition, explicitHashkey, serialized);
    Futures.addCallback(cb, callback);
}
Also used : Attempt(com.amazonaws.services.kinesis.producer.Attempt) UserRecordFailedException(com.amazonaws.services.kinesis.producer.UserRecordFailedException) UserRecordResult(com.amazonaws.services.kinesis.producer.UserRecordResult) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Attempt (com.amazonaws.services.kinesis.producer.Attempt)2 UserRecordFailedException (com.amazonaws.services.kinesis.producer.UserRecordFailedException)2 UserRecordResult (com.amazonaws.services.kinesis.producer.UserRecordResult)1 ByteBuffer (java.nio.ByteBuffer)1