Search in sources :

Example 56 with Callback

use of org.apache.kafka.clients.producer.Callback in project nakadi by zalando.

the class KafkaTopicRepositoryTest method whenKafkaPublishCallbackWithExceptionThenEventPublishingException.

@Test
public void whenKafkaPublishCallbackWithExceptionThenEventPublishingException() throws Exception {
    final BatchItem firstItem = new BatchItem("{}", BatchItem.EmptyInjectionConfiguration.build(1, true), new BatchItem.InjectionConfiguration[BatchItem.Injection.values().length], Collections.emptyList());
    firstItem.setPartition("1");
    final BatchItem secondItem = new BatchItem("{}", BatchItem.EmptyInjectionConfiguration.build(1, true), new BatchItem.InjectionConfiguration[BatchItem.Injection.values().length], Collections.emptyList());
    secondItem.setPartition("2");
    final List<BatchItem> batch = ImmutableList.of(firstItem, secondItem);
    when(kafkaProducer.partitionsFor(EXPECTED_PRODUCER_RECORD.topic())).thenReturn(ImmutableList.of(new PartitionInfo(EXPECTED_PRODUCER_RECORD.topic(), 1, new Node(1, "host", 9091), null, null), new PartitionInfo(EXPECTED_PRODUCER_RECORD.topic(), 2, new Node(1, "host", 9091), null, null)));
    when(kafkaProducer.send(any(), any())).thenAnswer(invocation -> {
        final ProducerRecord record = (ProducerRecord) invocation.getArguments()[0];
        final Callback callback = (Callback) invocation.getArguments()[1];
        if (record.partition() == 2) {
            // return exception only for second event
            callback.onCompletion(null, new Exception());
        } else {
            callback.onCompletion(null, null);
        }
        return null;
    });
    try {
        kafkaTopicRepository.syncPostBatch(EXPECTED_PRODUCER_RECORD.topic(), batch);
        fail();
    } catch (final EventPublishingException e) {
        assertThat(firstItem.getResponse().getPublishingStatus(), equalTo(EventPublishingStatus.SUBMITTED));
        assertThat(firstItem.getResponse().getDetail(), equalTo(""));
        assertThat(secondItem.getResponse().getPublishingStatus(), equalTo(EventPublishingStatus.FAILED));
        assertThat(secondItem.getResponse().getDetail(), equalTo("internal error"));
    }
}
Also used : Callback(org.apache.kafka.clients.producer.Callback) Node(org.apache.kafka.common.Node) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) BatchItem(org.zalando.nakadi.domain.BatchItem) PartitionInfo(org.apache.kafka.common.PartitionInfo) EventPublishingException(org.zalando.nakadi.exceptions.EventPublishingException) EventPublishingException(org.zalando.nakadi.exceptions.EventPublishingException) KafkaException(org.apache.kafka.common.KafkaException) NakadiException(org.zalando.nakadi.exceptions.NakadiException) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) BufferExhaustedException(org.apache.kafka.clients.producer.BufferExhaustedException) Test(org.junit.Test)

Example 57 with Callback

use of org.apache.kafka.clients.producer.Callback in project open-kilda by telstra.

the class KafkaBolt method execute.

@Override
public void execute(final Tuple input) {
    K key = null;
    V message = null;
    String topic = null;
    try {
        key = mapper.getKeyFromTuple(input);
        message = mapper.getMessageFromTuple(input);
        topic = topicSelector.getTopic(input);
        if (topic != null) {
            Callback callback = null;
            if (!fireAndForget && async) {
                callback = new Callback() {

                    @Override
                    public void onCompletion(RecordMetadata ignored, Exception e) {
                        synchronized (collector) {
                            if (e != null) {
                                collector.reportError(e);
                                collector.fail(input);
                            } else {
                                collector.ack(input);
                            }
                        }
                    }
                };
            }
            Future<RecordMetadata> result = producer.send(new ProducerRecord<K, V>(topic, key, message), callback);
            if (!async) {
                try {
                    result.get();
                    collector.ack(input);
                } catch (ExecutionException err) {
                    collector.reportError(err);
                    collector.fail(input);
                }
            } else if (fireAndForget) {
                collector.ack(input);
            }
        } else {
            LOG.warn("skipping key = " + key + ", topic selector returned null.");
            collector.ack(input);
        }
    } catch (Exception ex) {
        collector.reportError(ex);
        collector.fail(input);
    }
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) Callback(org.apache.kafka.clients.producer.Callback) ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 58 with Callback

use of org.apache.kafka.clients.producer.Callback in project brave by openzipkin.

the class TracingCallbackTest method on_completion_should_tag_if_exception.

@Test
public void on_completion_should_tag_if_exception() {
    Span span = tracing.tracer().nextSpan().start();
    Callback tracingCallback = TracingCallback.create(null, span, currentTraceContext);
    tracingCallback.onCompletion(null, error);
    assertThat(spans.get(0).finishTimestamp()).isNotZero();
    assertThat(spans.get(0).error()).isEqualTo(error);
}
Also used : Callback(org.apache.kafka.clients.producer.Callback) Span(brave.Span) Test(org.junit.Test)

Example 59 with Callback

use of org.apache.kafka.clients.producer.Callback in project brave by openzipkin.

the class TracingCallbackTest method on_completion_should_forward_then_tag_if_exception.

@Test
public void on_completion_should_forward_then_tag_if_exception() {
    Span span = tracing.tracer().nextSpan().start();
    Callback delegate = mock(Callback.class);
    Callback tracingCallback = TracingCallback.create(delegate, span, currentTraceContext);
    RecordMetadata md = createRecordMetadata();
    tracingCallback.onCompletion(md, error);
    verify(delegate).onCompletion(md, error);
    assertThat(spans.get(0).finishTimestamp()).isNotZero();
    assertThat(spans.get(0).error()).isEqualTo(error);
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) Callback(org.apache.kafka.clients.producer.Callback) Span(brave.Span) Test(org.junit.Test)

Example 60 with Callback

use of org.apache.kafka.clients.producer.Callback in project brave by openzipkin.

the class TracingCallbackTest method on_completion_should_have_span_in_scope.

@Test
public void on_completion_should_have_span_in_scope() {
    Span span = tracing.tracer().nextSpan().start();
    Callback delegate = (metadata, exception) -> assertThat(currentTraceContext.get()).isSameAs(span.context());
    TracingCallback.create(delegate, span, currentTraceContext).onCompletion(createRecordMetadata(), null);
    assertThat(spans.get(0).finishTimestamp()).isNotZero();
}
Also used : TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Span(brave.Span) Test(org.junit.Test) RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) Callback(org.apache.kafka.clients.producer.Callback) Mockito.mock(org.mockito.Mockito.mock) Mockito.verify(org.mockito.Mockito.verify) Callback(org.apache.kafka.clients.producer.Callback) Span(brave.Span) Test(org.junit.Test)

Aggregations

Callback (org.apache.kafka.clients.producer.Callback)81 Test (org.junit.Test)47 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)39 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)37 KafkaException (org.apache.kafka.common.KafkaException)21 Future (java.util.concurrent.Future)18 TimeoutException (org.apache.kafka.common.errors.TimeoutException)18 ExecutionException (java.util.concurrent.ExecutionException)15 ArrayList (java.util.ArrayList)14 List (java.util.List)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 MockProducer (org.apache.kafka.clients.producer.MockProducer)13 HashMap (java.util.HashMap)12 Properties (java.util.Properties)12 DefaultPartitioner (org.apache.kafka.clients.producer.internals.DefaultPartitioner)12 TopicPartition (org.apache.kafka.common.TopicPartition)12 Schema (org.apache.kafka.connect.data.Schema)12 Struct (org.apache.kafka.connect.data.Struct)12 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)11 StreamsException (org.apache.kafka.streams.errors.StreamsException)11