Search in sources :

Example 96 with RecordHeaders

use of org.apache.kafka.common.header.internals.RecordHeaders in project kafka by apache.

the class ProcessorContextImplTest method shouldSendRecordHeadersToChangelogTopicWhenConsistencyEnabled.

@Test
public void shouldSendRecordHeadersToChangelogTopicWhenConsistencyEnabled() {
    final Position position = Position.emptyPosition();
    final Headers headers = new RecordHeaders();
    headers.add(ChangelogRecordDeserializationHelper.CHANGELOG_VERSION_HEADER_RECORD_CONSISTENCY);
    headers.add(new RecordHeader(ChangelogRecordDeserializationHelper.CHANGELOG_POSITION_HEADER_KEY, PositionSerde.serialize(position).array()));
    recordCollector.send(CHANGELOG_PARTITION.topic(), KEY_BYTES, VALUE_BYTES, headers, CHANGELOG_PARTITION.partition(), TIMESTAMP, BYTES_KEY_SERIALIZER, BYTEARRAY_VALUE_SERIALIZER);
    final StreamTask task = EasyMock.createNiceMock(StreamTask.class);
    replay(recordCollector, task);
    context = new ProcessorContextImpl(mock(TaskId.class), streamsConfigWithConsistencyMock(), stateManager, mock(StreamsMetricsImpl.class), mock(ThreadCache.class));
    context.transitionToActive(task, recordCollector, null);
    context.logChange(REGISTERED_STORE_NAME, KEY_BYTES, VALUE_BYTES, TIMESTAMP, position);
    verify(recordCollector);
}
Also used : RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) Position(org.apache.kafka.streams.query.Position) Headers(org.apache.kafka.common.header.Headers) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Test(org.junit.Test)

Example 97 with RecordHeaders

use of org.apache.kafka.common.header.internals.RecordHeaders in project kafka by apache.

the class RecordCollectorTest method shouldThrowInformativeStreamsExceptionOnKeyClassCastException.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldThrowInformativeStreamsExceptionOnKeyClassCastException() {
    final StreamsException expected = assertThrows(StreamsException.class, () -> this.collector.send("topic", "key", "value", new RecordHeaders(), 0, 0L, // need to add cast to trigger `ClassCastException`
    (Serializer) new LongSerializer(), new StringSerializer()));
    assertThat(expected.getCause(), instanceOf(ClassCastException.class));
    assertThat(expected.getMessage(), equalTo("ClassCastException while producing data to topic topic. " + "A serializer (key: org.apache.kafka.common.serialization.LongSerializer / value: org.apache.kafka.common.serialization.StringSerializer) " + "is not compatible to the actual key or value type (key type: java.lang.String / value type: java.lang.String). " + "Change the default Serdes in StreamConfig or provide correct Serdes via method parameters " + "(for example if using the DSL, `#to(String topic, Produced<K, V> produced)` with `Produced.keySerde(WindowedSerdes.timeWindowedSerdeFrom(String.class))`)."));
}
Also used : RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) LongSerializer(org.apache.kafka.common.serialization.LongSerializer) StreamsException(org.apache.kafka.streams.errors.StreamsException) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) LongSerializer(org.apache.kafka.common.serialization.LongSerializer) ByteArraySerializer(org.apache.kafka.common.serialization.ByteArraySerializer) Serializer(org.apache.kafka.common.serialization.Serializer) Test(org.junit.Test)

Example 98 with RecordHeaders

use of org.apache.kafka.common.header.internals.RecordHeaders in project kafka by apache.

the class RecordCollectorTest method shouldThrowInformativeStreamsExceptionOnKeyAndNullValueClassCastException.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldThrowInformativeStreamsExceptionOnKeyAndNullValueClassCastException() {
    final StreamsException expected = assertThrows(StreamsException.class, () -> this.collector.send("topic", "key", null, new RecordHeaders(), 0, 0L, // need to add cast to trigger `ClassCastException`
    (Serializer) new LongSerializer(), new StringSerializer()));
    assertThat(expected.getCause(), instanceOf(ClassCastException.class));
    assertThat(expected.getMessage(), equalTo("ClassCastException while producing data to topic topic. " + "A serializer (key: org.apache.kafka.common.serialization.LongSerializer / value: org.apache.kafka.common.serialization.StringSerializer) " + "is not compatible to the actual key or value type (key type: java.lang.String / value type: unknown because value is null). " + "Change the default Serdes in StreamConfig or provide correct Serdes via method parameters " + "(for example if using the DSL, `#to(String topic, Produced<K, V> produced)` with `Produced.keySerde(WindowedSerdes.timeWindowedSerdeFrom(String.class))`)."));
}
Also used : RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) LongSerializer(org.apache.kafka.common.serialization.LongSerializer) StreamsException(org.apache.kafka.streams.errors.StreamsException) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) LongSerializer(org.apache.kafka.common.serialization.LongSerializer) ByteArraySerializer(org.apache.kafka.common.serialization.ByteArraySerializer) Serializer(org.apache.kafka.common.serialization.Serializer) Test(org.junit.Test)

Example 99 with RecordHeaders

use of org.apache.kafka.common.header.internals.RecordHeaders in project kafka by apache.

the class RecordCollectorTest method shouldThrowInformativeStreamsExceptionOnValueAndNullKeyClassCastException.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldThrowInformativeStreamsExceptionOnValueAndNullKeyClassCastException() {
    final StreamsException expected = assertThrows(StreamsException.class, () -> this.collector.send("topic", null, "value", new RecordHeaders(), 0, 0L, new StringSerializer(), // need to add cast to trigger `ClassCastException`
    (Serializer) new LongSerializer()));
    assertThat(expected.getCause(), instanceOf(ClassCastException.class));
    assertThat(expected.getMessage(), equalTo("ClassCastException while producing data to topic topic. " + "A serializer (key: org.apache.kafka.common.serialization.StringSerializer / value: org.apache.kafka.common.serialization.LongSerializer) " + "is not compatible to the actual key or value type (key type: unknown because key is null / value type: java.lang.String). " + "Change the default Serdes in StreamConfig or provide correct Serdes via method parameters " + "(for example if using the DSL, `#to(String topic, Produced<K, V> produced)` with `Produced.keySerde(WindowedSerdes.timeWindowedSerdeFrom(String.class))`)."));
}
Also used : RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) LongSerializer(org.apache.kafka.common.serialization.LongSerializer) StreamsException(org.apache.kafka.streams.errors.StreamsException) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) LongSerializer(org.apache.kafka.common.serialization.LongSerializer) ByteArraySerializer(org.apache.kafka.common.serialization.ByteArraySerializer) Serializer(org.apache.kafka.common.serialization.Serializer) Test(org.junit.Test)

Example 100 with RecordHeaders

use of org.apache.kafka.common.header.internals.RecordHeaders in project kafka by apache.

the class RecordCollectorTest method shouldSendWithPartitioner.

@Test
public void shouldSendWithPartitioner() {
    final Headers headers = new RecordHeaders(new Header[] { new RecordHeader("key", "value".getBytes()) });
    collector.send(topic, "3", "0", null, null, stringSerializer, stringSerializer, streamPartitioner);
    collector.send(topic, "9", "0", null, null, stringSerializer, stringSerializer, streamPartitioner);
    collector.send(topic, "27", "0", null, null, stringSerializer, stringSerializer, streamPartitioner);
    collector.send(topic, "81", "0", null, null, stringSerializer, stringSerializer, streamPartitioner);
    collector.send(topic, "243", "0", null, null, stringSerializer, stringSerializer, streamPartitioner);
    collector.send(topic, "28", "0", headers, null, stringSerializer, stringSerializer, streamPartitioner);
    collector.send(topic, "82", "0", headers, null, stringSerializer, stringSerializer, streamPartitioner);
    collector.send(topic, "244", "0", headers, null, stringSerializer, stringSerializer, streamPartitioner);
    collector.send(topic, "245", "0", null, null, stringSerializer, stringSerializer, streamPartitioner);
    final Map<TopicPartition, Long> offsets = collector.offsets();
    assertEquals(4L, (long) offsets.get(new TopicPartition(topic, 0)));
    assertEquals(2L, (long) offsets.get(new TopicPartition(topic, 1)));
    assertEquals(0L, (long) offsets.get(new TopicPartition(topic, 2)));
    assertEquals(9, mockProducer.history().size());
    // returned offsets should not be modified
    final TopicPartition topicPartition = new TopicPartition(topic, 0);
    assertThrows(UnsupportedOperationException.class, () -> offsets.put(topicPartition, 50L));
}
Also used : RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) Headers(org.apache.kafka.common.header.Headers) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) TopicPartition(org.apache.kafka.common.TopicPartition) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Test(org.junit.Test)

Aggregations

RecordHeaders (org.apache.kafka.common.header.internals.RecordHeaders)149 Test (org.junit.Test)107 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)49 ProcessorRecordContext (org.apache.kafka.streams.processor.internals.ProcessorRecordContext)41 Headers (org.apache.kafka.common.header.Headers)33 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)24 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)24 TopicPartition (org.apache.kafka.common.TopicPartition)22 Position (org.apache.kafka.streams.query.Position)17 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 ByteBuffer (java.nio.ByteBuffer)11 Struct (org.apache.kafka.connect.data.Struct)11 Test (org.junit.jupiter.api.Test)11 Header (org.apache.kafka.common.header.Header)10 LinkedHashMap (java.util.LinkedHashMap)9 Bytes (org.apache.kafka.common.utils.Bytes)9 StreamsException (org.apache.kafka.streams.errors.StreamsException)9 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)8 Metrics (org.apache.kafka.common.metrics.Metrics)8