Search in sources :

Example 31 with Callback

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

the class KafkaStatusBackingStoreTest method putConnectorStateRetriableFailure.

@Test
public void putConnectorStateRetriableFailure() {
    byte[] value = new byte[0];
    when(converter.fromConnectData(eq(STATUS_TOPIC), any(Schema.class), any(Struct.class))).thenReturn(value);
    doAnswer(invocation -> {
        ((Callback) invocation.getArgument(2)).onCompletion(null, new TimeoutException());
        return null;
    }).doAnswer(invocation -> {
        ((Callback) invocation.getArgument(2)).onCompletion(null, null);
        return null;
    }).when(kafkaBasedLog).send(eq("status-connector-conn"), eq(value), any(Callback.class));
    ConnectorStatus status = new ConnectorStatus(CONNECTOR, ConnectorStatus.State.RUNNING, WORKER_ID, 0);
    store.put(status);
    // state is not visible until read back from the log
    assertNull(store.get(CONNECTOR));
}
Also used : WorkerConfig(org.apache.kafka.connect.runtime.WorkerConfig) KafkaBasedLog(org.apache.kafka.connect.util.KafkaBasedLog) MockTime(org.apache.kafka.common.utils.MockTime) ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) Assert.assertThrows(org.junit.Assert.assertThrows) HashMap(java.util.HashMap) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) Schema(org.apache.kafka.connect.data.Schema) HashSet(java.util.HashSet) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ArgumentCaptor(org.mockito.ArgumentCaptor) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) TimestampType(org.apache.kafka.common.record.TimestampType) Before(org.junit.Before) TimeoutException(org.apache.kafka.common.errors.TimeoutException) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ConfigException(org.apache.kafka.common.config.ConfigException) DistributedConfig(org.apache.kafka.connect.runtime.distributed.DistributedConfig) Mockito.verify(org.mockito.Mockito.verify) ConnectorStatus(org.apache.kafka.connect.runtime.ConnectorStatus) Mockito.never(org.mockito.Mockito.never) Assert.assertNull(org.junit.Assert.assertNull) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Struct(org.apache.kafka.connect.data.Struct) Optional(java.util.Optional) Mockito.any(org.mockito.Mockito.any) Callback(org.apache.kafka.clients.producer.Callback) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) TaskStatus(org.apache.kafka.connect.runtime.TaskStatus) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) Callback(org.apache.kafka.clients.producer.Callback) Schema(org.apache.kafka.connect.data.Schema) ConnectorStatus(org.apache.kafka.connect.runtime.ConnectorStatus) Struct(org.apache.kafka.connect.data.Struct) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Test(org.junit.Test)

Example 32 with Callback

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

the class KafkaStatusBackingStoreTest method putConnectorStateNonRetriableFailure.

@Test
public void putConnectorStateNonRetriableFailure() {
    byte[] value = new byte[0];
    when(converter.fromConnectData(eq(STATUS_TOPIC), any(Schema.class), any(Struct.class))).thenReturn(value);
    doAnswer(invocation -> {
        ((Callback) invocation.getArgument(2)).onCompletion(null, new UnknownServerException());
        return null;
    }).when(kafkaBasedLog).send(eq("status-connector-conn"), eq(value), any(Callback.class));
    // the error is logged and ignored
    ConnectorStatus status = new ConnectorStatus(CONNECTOR, ConnectorStatus.State.RUNNING, WORKER_ID, 0);
    store.put(status);
    // state is not visible until read back from the log
    assertNull(store.get(CONNECTOR));
}
Also used : Callback(org.apache.kafka.clients.producer.Callback) Schema(org.apache.kafka.connect.data.Schema) ConnectorStatus(org.apache.kafka.connect.runtime.ConnectorStatus) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 33 with Callback

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

the class KafkaStatusBackingStoreTest method putTaskState.

@Test
public void putTaskState() {
    byte[] value = new byte[0];
    when(converter.fromConnectData(eq(STATUS_TOPIC), any(Schema.class), any(Struct.class))).thenReturn(value);
    doAnswer(invocation -> {
        ((Callback) invocation.getArgument(2)).onCompletion(null, null);
        store.read(consumerRecord(1, "status-connector-conn", value));
        return null;
    }).when(kafkaBasedLog).send(eq("status-task-conn-0"), eq(value), any(Callback.class));
    TaskStatus status = new TaskStatus(TASK, TaskStatus.State.RUNNING, WORKER_ID, 0);
    store.put(status);
    // state is not visible until read back from the log
    assertNull(store.get(TASK));
}
Also used : Callback(org.apache.kafka.clients.producer.Callback) Schema(org.apache.kafka.connect.data.Schema) TaskStatus(org.apache.kafka.connect.runtime.TaskStatus) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 34 with Callback

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

the class KafkaStatusBackingStoreFormatTest method putTopicStateRetriableFailure.

@Test
public void putTopicStateRetriableFailure() {
    TopicStatus topicStatus = new TopicStatus(FOO_TOPIC, new ConnectorTaskId(FOO_CONNECTOR, 0), time.milliseconds());
    String key = TOPIC_STATUS_PREFIX + FOO_TOPIC + TOPIC_STATUS_SEPARATOR + FOO_CONNECTOR;
    ArgumentCaptor<byte[]> valueCaptor = ArgumentCaptor.forClass(byte[].class);
    doAnswer(invocation -> {
        ((Callback) invocation.getArgument(2)).onCompletion(null, new TimeoutException());
        return null;
    }).doAnswer(invocation -> {
        ((Callback) invocation.getArgument(2)).onCompletion(null, null);
        return null;
    }).when(kafkaBasedLog).send(eq(key), valueCaptor.capture(), any(Callback.class));
    store.put(topicStatus);
    verify(kafkaBasedLog, times(2)).send(any(), any(), any());
    // check capture state
    assertEquals(topicStatus, store.parseTopicStatus(valueCaptor.getValue()));
    // state is not visible until read back from the log
    assertNull(store.getTopic(FOO_CONNECTOR, FOO_TOPIC));
}
Also used : KafkaBasedLog(org.apache.kafka.connect.util.KafkaBasedLog) MockTime(org.apache.kafka.common.utils.MockTime) Arrays(java.util.Arrays) TOPIC_STATUS_SEPARATOR(org.apache.kafka.connect.storage.KafkaStatusBackingStore.TOPIC_STATUS_SEPARATOR) ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) TopicStatus(org.apache.kafka.connect.runtime.TopicStatus) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Before(org.junit.Before) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Time(org.apache.kafka.common.utils.Time) SCHEMAS_ENABLE_CONFIG(org.apache.kafka.connect.json.JsonConverterConfig.SCHEMAS_ENABLE_CONFIG) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) CONNECTOR_STATUS_PREFIX(org.apache.kafka.connect.storage.KafkaStatusBackingStore.CONNECTOR_STATUS_PREFIX) TASK_STATUS_PREFIX(org.apache.kafka.connect.storage.KafkaStatusBackingStore.TASK_STATUS_PREFIX) Mockito.verify(org.mockito.Mockito.verify) Assert.assertNull(org.junit.Assert.assertNull) TOPIC_STATUS_PREFIX(org.apache.kafka.connect.storage.KafkaStatusBackingStore.TOPIC_STATUS_PREFIX) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Assert.assertFalse(org.junit.Assert.assertFalse) JsonConverter(org.apache.kafka.connect.json.JsonConverter) Mockito.any(org.mockito.Mockito.any) Callback(org.apache.kafka.clients.producer.Callback) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) Callback(org.apache.kafka.clients.producer.Callback) ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) TopicStatus(org.apache.kafka.connect.runtime.TopicStatus) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Test(org.junit.Test)

Example 35 with Callback

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

the class TracingCallbackTest method on_completion_should_forward_then_finish_span.

@Test
public void on_completion_should_forward_then_finish_span() {
    Span span = tracing.tracer().nextSpan().start();
    Callback delegate = mock(Callback.class);
    Callback tracingCallback = TracingCallback.create(delegate, span, currentTraceContext);
    RecordMetadata md = createRecordMetadata();
    tracingCallback.onCompletion(md, null);
    verify(delegate).onCompletion(md, null);
    assertThat(spans.get(0).finishTimestamp()).isNotZero();
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) 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