Search in sources :

Example 16 with ProducerRecord

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

the class WorkerSourceTaskTest method expectSendRecord.

private Capture<ProducerRecord<byte[], byte[]>> expectSendRecord(boolean anyTimes, boolean isRetry, boolean succeed) throws InterruptedException {
    expectConvertKeyValue(anyTimes);
    expectApplyTransformationChain(anyTimes);
    Capture<ProducerRecord<byte[], byte[]>> sent = EasyMock.newCapture();
    // 1. Offset data is passed to the offset storage.
    if (!isRetry) {
        offsetWriter.offset(PARTITION, OFFSET);
        if (anyTimes)
            PowerMock.expectLastCall().anyTimes();
        else
            PowerMock.expectLastCall();
    }
    // 2. Converted data passed to the producer, which will need callbacks invoked for flush to work
    IExpectationSetters<Future<RecordMetadata>> expect = EasyMock.expect(producer.send(EasyMock.capture(sent), EasyMock.capture(producerCallbacks)));
    IAnswer<Future<RecordMetadata>> expectResponse = new IAnswer<Future<RecordMetadata>>() {

        @Override
        public Future<RecordMetadata> answer() throws Throwable {
            synchronized (producerCallbacks) {
                for (org.apache.kafka.clients.producer.Callback cb : producerCallbacks.getValues()) {
                    cb.onCompletion(new RecordMetadata(new TopicPartition("foo", 0), 0, 0, 0L, 0L, 0, 0), null);
                }
                producerCallbacks.reset();
            }
            return sendFuture;
        }
    };
    if (anyTimes)
        expect.andStubAnswer(expectResponse);
    else
        expect.andAnswer(expectResponse);
    // 3. As a result of a successful producer send callback, we'll notify the source task of the record commit
    expectTaskCommitRecord(anyTimes, succeed);
    return sent;
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) IAnswer(org.easymock.IAnswer) TopicPartition(org.apache.kafka.common.TopicPartition) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Future(java.util.concurrent.Future)

Example 17 with ProducerRecord

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

the class WorkerSourceTaskTest method testSendRecordsPropagatesTimestamp.

@Test
public void testSendRecordsPropagatesTimestamp() throws Exception {
    final Long timestamp = System.currentTimeMillis();
    createWorkerTask();
    List<SourceRecord> records = Collections.singletonList(new SourceRecord(PARTITION, OFFSET, "topic", null, KEY_SCHEMA, KEY, RECORD_SCHEMA, RECORD, timestamp));
    Capture<ProducerRecord<byte[], byte[]>> sent = expectSendRecordAnyTimes();
    PowerMock.replayAll();
    Whitebox.setInternalState(workerTask, "toSend", records);
    Whitebox.invokeMethod(workerTask, "sendRecords");
    assertEquals(timestamp, sent.getValue().timestamp());
    PowerMock.verifyAll();
}
Also used : ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) SourceRecord(org.apache.kafka.connect.source.SourceRecord) ThreadedTest(org.apache.kafka.connect.util.ThreadedTest) Test(org.junit.Test)

Example 18 with ProducerRecord

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

the class WorkerSourceTaskTest method testSendRecordsCorruptTimestamp.

@Test(expected = InvalidRecordException.class)
public void testSendRecordsCorruptTimestamp() throws Exception {
    final Long timestamp = -3L;
    createWorkerTask();
    List<SourceRecord> records = Collections.singletonList(new SourceRecord(PARTITION, OFFSET, "topic", null, KEY_SCHEMA, KEY, RECORD_SCHEMA, RECORD, timestamp));
    Capture<ProducerRecord<byte[], byte[]>> sent = expectSendRecordAnyTimes();
    PowerMock.replayAll();
    Whitebox.setInternalState(workerTask, "toSend", records);
    Whitebox.invokeMethod(workerTask, "sendRecords");
    assertEquals(null, sent.getValue().timestamp());
    PowerMock.verifyAll();
}
Also used : ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) SourceRecord(org.apache.kafka.connect.source.SourceRecord) ThreadedTest(org.apache.kafka.connect.util.ThreadedTest) Test(org.junit.Test)

Example 19 with ProducerRecord

use of org.apache.kafka.clients.producer.ProducerRecord in project incubator-atlas by apache.

the class KafkaNotification method sendInternalToProducer.

@VisibleForTesting
void sendInternalToProducer(Producer p, NotificationType type, String[] messages) throws NotificationException {
    String topic = TOPIC_MAP.get(type);
    List<MessageContext> messageContexts = new ArrayList<>();
    for (String message : messages) {
        ProducerRecord record = new ProducerRecord(topic, message);
        LOG.debug("Sending message for topic {}: {}", topic, message);
        Future future = p.send(record);
        messageContexts.add(new MessageContext(future, message));
    }
    List<String> failedMessages = new ArrayList<>();
    Exception lastFailureException = null;
    for (MessageContext context : messageContexts) {
        try {
            RecordMetadata response = context.getFuture().get();
            LOG.debug("Sent message for topic - {}, partition - {}, offset - {}", response.topic(), response.partition(), response.offset());
        } catch (Exception e) {
            lastFailureException = e;
            failedMessages.add(context.getMessage());
        }
    }
    if (lastFailureException != null) {
        throw new NotificationException(lastFailureException, failedMessages);
    }
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ArrayList(java.util.ArrayList) NotificationException(org.apache.atlas.notification.NotificationException) Future(java.util.concurrent.Future) URISyntaxException(java.net.URISyntaxException) NotificationException(org.apache.atlas.notification.NotificationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) AtlasException(org.apache.atlas.AtlasException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 20 with ProducerRecord

use of org.apache.kafka.clients.producer.ProducerRecord in project incubator-atlas by apache.

the class KafkaNotificationMockTest method shouldSendMessagesSuccessfully.

@Test
@SuppressWarnings("unchecked")
public void shouldSendMessagesSuccessfully() throws NotificationException, ExecutionException, InterruptedException {
    Properties configProperties = mock(Properties.class);
    KafkaNotification kafkaNotification = new KafkaNotification(configProperties);
    Producer producer = mock(Producer.class);
    String topicName = kafkaNotification.getTopicName(NotificationInterface.NotificationType.HOOK);
    String message = "This is a test message";
    Future returnValue = mock(Future.class);
    when(returnValue.get()).thenReturn(new RecordMetadata(new TopicPartition(topicName, 0), 0, 0));
    ProducerRecord expectedRecord = new ProducerRecord(topicName, message);
    when(producer.send(expectedRecord)).thenReturn(returnValue);
    kafkaNotification.sendInternalToProducer(producer, NotificationInterface.NotificationType.HOOK, new String[] { message });
    verify(producer).send(expectedRecord);
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) Producer(org.apache.kafka.clients.producer.Producer) TopicPartition(org.apache.kafka.common.TopicPartition) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Future(java.util.concurrent.Future) Properties(java.util.Properties) Test(org.testng.annotations.Test)

Aggregations

ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)51 Test (org.junit.Test)29 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)14 Future (java.util.concurrent.Future)11 ArrayList (java.util.ArrayList)10 Properties (java.util.Properties)10 Callback (org.apache.kafka.clients.producer.Callback)10 TopicPartition (org.apache.kafka.common.TopicPartition)8 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)7 ExecutionException (java.util.concurrent.ExecutionException)6 TimeoutException (org.apache.kafka.common.errors.TimeoutException)6 Serializer (org.apache.kafka.common.serialization.Serializer)6 MockProcessorContext (org.apache.kafka.test.MockProcessorContext)6 NoOpRecordCollector (org.apache.kafka.test.NoOpRecordCollector)6 MockProducer (org.apache.kafka.clients.producer.MockProducer)5 DefaultPartitioner (org.apache.kafka.clients.producer.internals.DefaultPartitioner)5 SourceRecord (org.apache.kafka.connect.source.SourceRecord)5 HashMap (java.util.HashMap)4 Endpoint (org.apache.camel.Endpoint)4 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)4