Search in sources :

Example 1 with TimestampType

use of org.apache.kafka.common.record.TimestampType in project apache-kafka-on-k8s by banzaicloud.

the class WorkerSinkTaskTest method testTimestampPropagation.

@Test
public void testTimestampPropagation() throws Exception {
    final Long timestamp = System.currentTimeMillis();
    final TimestampType timestampType = TimestampType.CREATE_TIME;
    createTask(initialState);
    expectInitializeTask();
    expectPollInitialAssignment();
    expectConsumerPoll(1, timestamp, timestampType);
    expectConversionAndTransformation(1);
    Capture<Collection<SinkRecord>> records = EasyMock.newCapture(CaptureType.ALL);
    sinkTask.put(EasyMock.capture(records));
    PowerMock.replayAll();
    workerTask.initialize(TASK_CONFIG);
    workerTask.initializeAndStart();
    // iter 1 -- initial assignment
    workerTask.iteration();
    // iter 2 -- deliver 1 record
    workerTask.iteration();
    SinkRecord record = records.getValue().iterator().next();
    assertEquals(timestamp, record.timestamp());
    assertEquals(timestampType, record.timestampType());
    PowerMock.verifyAll();
}
Also used : TimestampType(org.apache.kafka.common.record.TimestampType) Collection(java.util.Collection) SinkRecord(org.apache.kafka.connect.sink.SinkRecord) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with TimestampType

use of org.apache.kafka.common.record.TimestampType in project kafka by apache.

the class Fetcher method parseRecord.

/**
 * Parse the record entry, deserializing the key / value fields if necessary
 */
private ConsumerRecord<K, V> parseRecord(TopicPartition partition, RecordBatch batch, Record record) {
    try {
        long offset = record.offset();
        long timestamp = record.timestamp();
        Optional<Integer> leaderEpoch = maybeLeaderEpoch(batch.partitionLeaderEpoch());
        TimestampType timestampType = batch.timestampType();
        Headers headers = new RecordHeaders(record.headers());
        ByteBuffer keyBytes = record.key();
        byte[] keyByteArray = keyBytes == null ? null : Utils.toArray(keyBytes);
        K key = keyBytes == null ? null : this.keyDeserializer.deserialize(partition.topic(), headers, keyByteArray);
        ByteBuffer valueBytes = record.value();
        byte[] valueByteArray = valueBytes == null ? null : Utils.toArray(valueBytes);
        V value = valueBytes == null ? null : this.valueDeserializer.deserialize(partition.topic(), headers, valueByteArray);
        return new ConsumerRecord<>(partition.topic(), partition.partition(), offset, timestamp, timestampType, keyByteArray == null ? ConsumerRecord.NULL_SIZE : keyByteArray.length, valueByteArray == null ? ConsumerRecord.NULL_SIZE : valueByteArray.length, key, value, headers, leaderEpoch);
    } catch (RuntimeException e) {
        throw new RecordDeserializationException(partition, record.offset(), "Error deserializing key/value for partition " + partition + " at offset " + record.offset() + ". If needed, please seek past the record to continue consumption.", e);
    }
}
Also used : Headers(org.apache.kafka.common.header.Headers) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ByteBuffer(java.nio.ByteBuffer) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) RecordDeserializationException(org.apache.kafka.common.errors.RecordDeserializationException) TimestampType(org.apache.kafka.common.record.TimestampType)

Example 3 with TimestampType

use of org.apache.kafka.common.record.TimestampType in project kafka by apache.

the class ConsumerRecordTest method testConstructorsWithChecksum.

@Test
@Deprecated
public void testConstructorsWithChecksum() {
    String topic = "topic";
    int partition = 0;
    long offset = 23;
    long timestamp = 23434217432432L;
    TimestampType timestampType = TimestampType.CREATE_TIME;
    String key = "key";
    String value = "value";
    long checksum = 50L;
    int serializedKeySize = 100;
    int serializedValueSize = 1142;
    ConsumerRecord<String, String> record = new ConsumerRecord<>(topic, partition, offset, timestamp, timestampType, checksum, serializedKeySize, serializedValueSize, key, value);
    assertEquals(topic, record.topic());
    assertEquals(partition, record.partition());
    assertEquals(offset, record.offset());
    assertEquals(key, record.key());
    assertEquals(value, record.value());
    assertEquals(timestampType, record.timestampType());
    assertEquals(timestamp, record.timestamp());
    assertEquals(serializedKeySize, record.serializedKeySize());
    assertEquals(serializedValueSize, record.serializedValueSize());
    assertEquals(Optional.empty(), record.leaderEpoch());
    assertEquals(new RecordHeaders(), record.headers());
    RecordHeaders headers = new RecordHeaders();
    headers.add(new RecordHeader("header key", "header value".getBytes(StandardCharsets.UTF_8)));
    record = new ConsumerRecord<>(topic, partition, offset, timestamp, timestampType, checksum, serializedKeySize, serializedValueSize, key, value, headers);
    assertEquals(topic, record.topic());
    assertEquals(partition, record.partition());
    assertEquals(offset, record.offset());
    assertEquals(key, record.key());
    assertEquals(value, record.value());
    assertEquals(timestampType, record.timestampType());
    assertEquals(timestamp, record.timestamp());
    assertEquals(serializedKeySize, record.serializedKeySize());
    assertEquals(serializedValueSize, record.serializedValueSize());
    assertEquals(Optional.empty(), record.leaderEpoch());
    assertEquals(headers, record.headers());
    Optional<Integer> leaderEpoch = Optional.of(10);
    record = new ConsumerRecord<>(topic, partition, offset, timestamp, timestampType, checksum, serializedKeySize, serializedValueSize, key, value, headers, leaderEpoch);
    assertEquals(topic, record.topic());
    assertEquals(partition, record.partition());
    assertEquals(offset, record.offset());
    assertEquals(key, record.key());
    assertEquals(value, record.value());
    assertEquals(timestampType, record.timestampType());
    assertEquals(timestamp, record.timestamp());
    assertEquals(serializedKeySize, record.serializedKeySize());
    assertEquals(serializedValueSize, record.serializedValueSize());
    assertEquals(leaderEpoch, record.leaderEpoch());
    assertEquals(headers, record.headers());
}
Also used : RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) TimestampType(org.apache.kafka.common.record.TimestampType) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Test(org.junit.jupiter.api.Test)

Example 4 with TimestampType

use of org.apache.kafka.common.record.TimestampType in project beam by apache.

the class KafkaIOTest method mkMockConsumer.

// Update mock consumer with records distributed among the given topics, each with given number
// of partitions. Records are assigned in round-robin order among the partitions.
private static MockConsumer<byte[], byte[]> mkMockConsumer(List<String> topics, int partitionsPerTopic, int numElements, OffsetResetStrategy offsetResetStrategy, Map<String, Object> config, SerializableFunction<Integer, byte[]> keyFunction, SerializableFunction<Integer, byte[]> valueFunction) {
    final List<TopicPartition> partitions = new ArrayList<>();
    final Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> records = new HashMap<>();
    Map<String, List<PartitionInfo>> partitionMap = new HashMap<>();
    for (String topic : topics) {
        List<PartitionInfo> partIds = new ArrayList<>(partitionsPerTopic);
        for (int i = 0; i < partitionsPerTopic; i++) {
            TopicPartition tp = new TopicPartition(topic, i);
            partitions.add(tp);
            partIds.add(new PartitionInfo(topic, i, null, null, null));
            records.put(tp, new ArrayList<>());
        }
        partitionMap.put(topic, partIds);
    }
    int numPartitions = partitions.size();
    final long[] offsets = new long[numPartitions];
    long timestampStartMillis = (Long) config.getOrDefault(TIMESTAMP_START_MILLIS_CONFIG, LOG_APPEND_START_TIME.getMillis());
    TimestampType timestampType = TimestampType.forName((String) config.getOrDefault(TIMESTAMP_TYPE_CONFIG, TimestampType.LOG_APPEND_TIME.toString()));
    for (int i = 0; i < numElements; i++) {
        int pIdx = i % numPartitions;
        TopicPartition tp = partitions.get(pIdx);
        byte[] key = keyFunction.apply(i);
        byte[] value = valueFunction.apply(i);
        records.get(tp).add(new ConsumerRecord<>(tp.topic(), tp.partition(), offsets[pIdx]++, timestampStartMillis + Duration.standardSeconds(i).getMillis(), timestampType, 0, key.length, value.length, key, value));
    }
    // This is updated when reader assigns partitions.
    final AtomicReference<List<TopicPartition>> assignedPartitions = new AtomicReference<>(Collections.<TopicPartition>emptyList());
    final MockConsumer<byte[], byte[]> consumer = new MockConsumer<byte[], byte[]>(offsetResetStrategy) {

        @Override
        public synchronized void assign(final Collection<TopicPartition> assigned) {
            super.assign(assigned);
            assignedPartitions.set(ImmutableList.copyOf(assigned));
            for (TopicPartition tp : assigned) {
                updateBeginningOffsets(ImmutableMap.of(tp, 0L));
                updateEndOffsets(ImmutableMap.of(tp, (long) records.get(tp).size()));
            }
        }

        // Override offsetsForTimes() in order to look up the offsets by timestamp.
        @Override
        public synchronized Map<TopicPartition, OffsetAndTimestamp> offsetsForTimes(Map<TopicPartition, Long> timestampsToSearch) {
            return timestampsToSearch.entrySet().stream().map(e -> {
                // In test scope, timestamp == offset.
                long maxOffset = offsets[partitions.indexOf(e.getKey())];
                long offset = e.getValue();
                OffsetAndTimestamp value = (offset >= maxOffset) ? null : new OffsetAndTimestamp(offset, offset);
                return new SimpleEntry<>(e.getKey(), value);
            }).collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue));
        }
    };
    for (String topic : topics) {
        consumer.updatePartitions(topic, partitionMap.get(topic));
    }
    // MockConsumer does not maintain any relationship between partition seek position and the
    // records added. e.g. if we add 10 records to a partition and then seek to end of the
    // partition, MockConsumer is still going to return the 10 records in next poll. It is
    // our responsibility to make sure currently enqueued records sync with partition offsets.
    // The following task will be called inside each invocation to MockConsumer.poll().
    // We enqueue only the records with the offset >= partition's current position.
    Runnable recordEnqueueTask = new Runnable() {

        @Override
        public void run() {
            // add all the records with offset >= current partition position.
            int recordsAdded = 0;
            for (TopicPartition tp : assignedPartitions.get()) {
                long curPos = consumer.position(tp);
                for (ConsumerRecord<byte[], byte[]> r : records.get(tp)) {
                    if (r.offset() >= curPos) {
                        consumer.addRecord(r);
                        recordsAdded++;
                    }
                }
            }
            if (recordsAdded == 0) {
                if (config.get("inject.error.at.eof") != null) {
                    consumer.setException(new KafkaException("Injected error in consumer.poll()"));
                }
                // MockConsumer.poll(timeout) does not actually wait even when there aren't any
                // records.
                // Add a small wait here in order to avoid busy looping in the reader.
                Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
            // TODO: BEAM-4086: testUnboundedSourceWithoutBoundedWrapper() occasionally hangs
            // without this wait. Need to look into it.
            }
            consumer.schedulePollTask(this);
        }
    };
    consumer.schedulePollTask(recordEnqueueTask);
    return consumer;
}
Also used : Count(org.apache.beam.sdk.transforms.Count) MetricName(org.apache.beam.sdk.metrics.MetricName) Arrays(java.util.Arrays) PipelineExecutionException(org.apache.beam.sdk.Pipeline.PipelineExecutionException) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) CoderUtils(org.apache.beam.sdk.util.CoderUtils) KafkaAvroSerializer(io.confluent.kafka.serializers.KafkaAvroSerializer) UnboundedSource(org.apache.beam.sdk.io.UnboundedSource) KafkaException(org.apache.kafka.common.KafkaException) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) OffsetResetStrategy(org.apache.kafka.clients.consumer.OffsetResetStrategy) Map(java.util.Map) Window(org.apache.beam.sdk.transforms.windowing.Window) Uninterruptibles(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.util.concurrent.Uninterruptibles) TimestampType(org.apache.kafka.common.record.TimestampType) MockSchemaRegistry(io.confluent.kafka.schemaregistry.testutil.MockSchemaRegistry) MetricResult(org.apache.beam.sdk.metrics.MetricResult) UnboundedReader(org.apache.beam.sdk.io.UnboundedSource.UnboundedReader) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) PartitionInfo(org.apache.kafka.common.PartitionInfo) OffsetAndTimestamp(org.apache.kafka.clients.consumer.OffsetAndTimestamp) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) MetricQueryResults(org.apache.beam.sdk.metrics.MetricQueryResults) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) DisplayDataMatchers.hasDisplayItem(org.apache.beam.sdk.transforms.display.DisplayDataMatchers.hasDisplayItem) Matchers.containsString(org.hamcrest.Matchers.containsString) KV(org.apache.beam.sdk.values.KV) Duration(org.joda.time.Duration) RunWith(org.junit.runner.RunWith) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) ArrayList(java.util.ArrayList) SinkMetrics(org.apache.beam.sdk.metrics.SinkMetrics) Read(org.apache.beam.sdk.io.Read) Distinct(org.apache.beam.sdk.transforms.Distinct) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Deserializer(org.apache.kafka.common.serialization.Deserializer) DoFn(org.apache.beam.sdk.transforms.DoFn) ByteArrayDeserializer(org.apache.kafka.common.serialization.ByteArrayDeserializer) ThrowableMessageMatcher.hasMessage(org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage) PAssert(org.apache.beam.sdk.testing.PAssert) Producer(org.apache.kafka.clients.producer.Producer) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) AbstractKafkaAvroSerDeConfig(io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig) MetricResultsMatchers.attemptedMetricsResult(org.apache.beam.sdk.metrics.MetricResultsMatchers.attemptedMetricsResult) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Assert.assertNull(org.junit.Assert.assertNull) IsIterableWithSize(org.hamcrest.collection.IsIterableWithSize) Serializer(org.apache.kafka.common.serialization.Serializer) BoundedWindow(org.apache.beam.sdk.transforms.windowing.BoundedWindow) VarIntCoder(org.apache.beam.sdk.coders.VarIntCoder) Assert.assertEquals(org.junit.Assert.assertEquals) MockProducer(org.apache.kafka.clients.producer.MockProducer) SourceMetrics(org.apache.beam.sdk.metrics.SourceMetrics) Matchers.isA(org.hamcrest.Matchers.isA) MockConsumer(org.apache.kafka.clients.consumer.MockConsumer) PipelineResult(org.apache.beam.sdk.PipelineResult) LoggerFactory(org.slf4j.LoggerFactory) SerializableFunction(org.apache.beam.sdk.transforms.SerializableFunction) ByteBuffer(java.nio.ByteBuffer) PCollectionList(org.apache.beam.sdk.values.PCollectionList) ThrowableCauseMatcher.hasCause(org.junit.internal.matchers.ThrowableCauseMatcher.hasCause) Method(java.lang.reflect.Method) Flatten(org.apache.beam.sdk.transforms.Flatten) MapElements(org.apache.beam.sdk.transforms.MapElements) Min(org.apache.beam.sdk.transforms.Min) Consumer(org.apache.kafka.clients.consumer.Consumer) TopicPartition(org.apache.kafka.common.TopicPartition) Collection(java.util.Collection) BigEndianIntegerCoder(org.apache.beam.sdk.coders.BigEndianIntegerCoder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) VarLongCoder(org.apache.beam.sdk.coders.VarLongCoder) LongSerializer(org.apache.kafka.common.serialization.LongSerializer) Collectors(java.util.stream.Collectors) List(java.util.List) Max(org.apache.beam.sdk.transforms.Max) ParDo(org.apache.beam.sdk.transforms.ParDo) Header(org.apache.kafka.common.header.Header) TypeDescriptors(org.apache.beam.sdk.values.TypeDescriptors) Optional(java.util.Optional) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Assume.assumeTrue(org.junit.Assume.assumeTrue) Values(org.apache.beam.sdk.transforms.Values) MetricNameFilter(org.apache.beam.sdk.metrics.MetricNameFilter) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Headers(org.apache.kafka.common.header.Headers) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AvroGeneratedUser(org.apache.beam.sdk.io.AvroGeneratedUser) PipelineOptionsFactory(org.apache.beam.sdk.options.PipelineOptionsFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConcurrentMap(java.util.concurrent.ConcurrentMap) SimpleEntry(java.util.AbstractMap.SimpleEntry) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) ExpectedException(org.junit.rules.ExpectedException) ExecutorService(java.util.concurrent.ExecutorService) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Utils(org.apache.kafka.common.utils.Utils) DisplayData(org.apache.beam.sdk.transforms.display.DisplayData) GenericRecord(org.apache.avro.generic.GenericRecord) Logger(org.slf4j.Logger) BigEndianLongCoder(org.apache.beam.sdk.coders.BigEndianLongCoder) Assert.assertNotNull(org.junit.Assert.assertNotNull) FixedWindows(org.apache.beam.sdk.transforms.windowing.FixedWindows) Lists(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists) JUnit4(org.junit.runners.JUnit4) PCollection(org.apache.beam.sdk.values.PCollection) MetricsFilter(org.apache.beam.sdk.metrics.MetricsFilter) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) Ignore(org.junit.Ignore) ConfluentSchemaRegistryDeserializerProviderTest.mockDeserializerProvider(org.apache.beam.sdk.io.kafka.ConfluentSchemaRegistryDeserializerProviderTest.mockDeserializerProvider) Instant(org.joda.time.Instant) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) Comparator(java.util.Comparator) Collections(java.util.Collections) IsIterableContainingInAnyOrder(org.hamcrest.collection.IsIterableContainingInAnyOrder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) TimestampType(org.apache.kafka.common.record.TimestampType) ArrayList(java.util.ArrayList) PCollectionList(org.apache.beam.sdk.values.PCollectionList) List(java.util.List) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) PartitionInfo(org.apache.kafka.common.PartitionInfo) MockConsumer(org.apache.kafka.clients.consumer.MockConsumer) OffsetAndTimestamp(org.apache.kafka.clients.consumer.OffsetAndTimestamp) AtomicReference(java.util.concurrent.atomic.AtomicReference) TopicPartition(org.apache.kafka.common.TopicPartition) Collection(java.util.Collection) PCollection(org.apache.beam.sdk.values.PCollection) KafkaException(org.apache.kafka.common.KafkaException) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 5 with TimestampType

use of org.apache.kafka.common.record.TimestampType in project beam by apache.

the class KafkaTestTable method mkMockConsumer.

private MockConsumer<byte[], byte[]> mkMockConsumer(Map<String, Object> config) {
    OffsetResetStrategy offsetResetStrategy = OffsetResetStrategy.EARLIEST;
    final Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> kafkaRecords = new HashMap<>();
    Map<String, List<PartitionInfo>> partitionInfoMap = new HashMap<>();
    Map<String, List<TopicPartition>> partitionMap = new HashMap<>();
    // Create Topic Paritions
    for (String topic : this.getTopics()) {
        List<PartitionInfo> partIds = new ArrayList<>(partitionsPerTopic);
        List<TopicPartition> topicParitions = new ArrayList<>(partitionsPerTopic);
        for (int i = 0; i < partitionsPerTopic; i++) {
            TopicPartition tp = new TopicPartition(topic, i);
            topicParitions.add(tp);
            partIds.add(new PartitionInfo(topic, i, null, null, null));
            kafkaRecords.put(tp, new ArrayList<>());
        }
        partitionInfoMap.put(topic, partIds);
        partitionMap.put(topic, topicParitions);
    }
    TimestampType timestampType = TimestampType.forName((String) config.getOrDefault(TIMESTAMP_TYPE_CONFIG, TimestampType.LOG_APPEND_TIME.toString()));
    for (KafkaTestRecord record : this.records) {
        int partitionIndex = record.getKey().hashCode() % partitionsPerTopic;
        TopicPartition tp = partitionMap.get(record.getTopic()).get(partitionIndex);
        byte[] key = record.getKey().getBytes(UTF_8);
        byte[] value = record.getValue().toByteArray();
        kafkaRecords.get(tp).add(new ConsumerRecord<>(tp.topic(), tp.partition(), kafkaRecords.get(tp).size(), record.getTimeStamp(), timestampType, 0, key.length, value.length, key, value));
    }
    // This is updated when reader assigns partitions.
    final AtomicReference<List<TopicPartition>> assignedPartitions = new AtomicReference<>(Collections.emptyList());
    final MockConsumer<byte[], byte[]> consumer = new MockConsumer<byte[], byte[]>(offsetResetStrategy) {

        @Override
        public synchronized void assign(final Collection<TopicPartition> assigned) {
            Collection<TopicPartition> realPartitions = assigned.stream().map(part -> partitionMap.get(part.topic()).get(part.partition())).collect(Collectors.toList());
            super.assign(realPartitions);
            assignedPartitions.set(ImmutableList.copyOf(realPartitions));
            for (TopicPartition tp : realPartitions) {
                updateBeginningOffsets(ImmutableMap.of(tp, 0L));
                updateEndOffsets(ImmutableMap.of(tp, (long) kafkaRecords.get(tp).size()));
            }
        }

        // Override offsetsForTimes() in order to look up the offsets by timestamp.
        @Override
        public synchronized Map<TopicPartition, OffsetAndTimestamp> offsetsForTimes(Map<TopicPartition, Long> timestampsToSearch) {
            return timestampsToSearch.entrySet().stream().map(e -> {
                // In test scope, timestamp == offset. ????
                long maxOffset = kafkaRecords.get(e.getKey()).size();
                long offset = e.getValue();
                OffsetAndTimestamp value = (offset >= maxOffset) ? null : new OffsetAndTimestamp(offset, offset);
                return new AbstractMap.SimpleEntry<>(e.getKey(), value);
            }).collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
        }
    };
    for (String topic : getTopics()) {
        consumer.updatePartitions(topic, partitionInfoMap.get(topic));
    }
    Runnable recordEnqueueTask = new Runnable() {

        @Override
        public void run() {
            // add all the records with offset >= current partition position.
            int recordsAdded = 0;
            for (TopicPartition tp : assignedPartitions.get()) {
                long curPos = consumer.position(tp);
                for (ConsumerRecord<byte[], byte[]> r : kafkaRecords.get(tp)) {
                    if (r.offset() >= curPos) {
                        consumer.addRecord(r);
                        recordsAdded++;
                    }
                }
            }
            if (recordsAdded == 0) {
                if (config.get("inject.error.at.eof") != null) {
                    consumer.setException(new KafkaException("Injected error in consumer.poll()"));
                }
                // MockConsumer.poll(timeout) does not actually wait even when there aren't any
                // records.
                // Add a small wait here in order to avoid busy looping in the reader.
                Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
            }
            consumer.schedulePollTask(this);
        }
    };
    consumer.schedulePollTask(recordEnqueueTask);
    return consumer;
}
Also used : ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) MockConsumer(org.apache.kafka.clients.consumer.MockConsumer) KafkaException(org.apache.kafka.common.KafkaException) HashMap(java.util.HashMap) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) OffsetResetStrategy(org.apache.kafka.clients.consumer.OffsetResetStrategy) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) KafkaIO(org.apache.beam.sdk.io.kafka.KafkaIO) PTransform(org.apache.beam.sdk.transforms.PTransform) Map(java.util.Map) Uninterruptibles(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.util.concurrent.Uninterruptibles) Row(org.apache.beam.sdk.values.Row) TimestampType(org.apache.kafka.common.record.TimestampType) TopicPartition(org.apache.kafka.common.TopicPartition) KafkaRecord(org.apache.beam.sdk.io.kafka.KafkaRecord) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Collection(java.util.Collection) PartitionInfo(org.apache.kafka.common.PartitionInfo) PCollection(org.apache.beam.sdk.values.PCollection) OffsetAndTimestamp(org.apache.kafka.clients.consumer.OffsetAndTimestamp) Collectors(java.util.stream.Collectors) Schema(org.apache.beam.sdk.schemas.Schema) TimeUnit(java.util.concurrent.TimeUnit) AbstractMap(java.util.AbstractMap) List(java.util.List) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Collections(java.util.Collections) OffsetResetStrategy(org.apache.kafka.clients.consumer.OffsetResetStrategy) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AbstractMap(java.util.AbstractMap) TimestampType(org.apache.kafka.common.record.TimestampType) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) PartitionInfo(org.apache.kafka.common.PartitionInfo) MockConsumer(org.apache.kafka.clients.consumer.MockConsumer) OffsetAndTimestamp(org.apache.kafka.clients.consumer.OffsetAndTimestamp) AtomicReference(java.util.concurrent.atomic.AtomicReference) TopicPartition(org.apache.kafka.common.TopicPartition) Collection(java.util.Collection) PCollection(org.apache.beam.sdk.values.PCollection) KafkaException(org.apache.kafka.common.KafkaException) HashMap(java.util.HashMap) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap)

Aggregations

TimestampType (org.apache.kafka.common.record.TimestampType)11 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)7 ArrayList (java.util.ArrayList)5 Collection (java.util.Collection)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 TopicPartition (org.apache.kafka.common.TopicPartition)5 Test (org.junit.Test)5 ByteBuffer (java.nio.ByteBuffer)4 List (java.util.List)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 RecordHeaders (org.apache.kafka.common.header.internals.RecordHeaders)4 TimeUnit (java.util.concurrent.TimeUnit)3 Collectors (java.util.stream.Collectors)3 KafkaException (org.apache.kafka.common.KafkaException)3 PartitionInfo (org.apache.kafka.common.PartitionInfo)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Collections (java.util.Collections)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 PCollection (org.apache.beam.sdk.values.PCollection)2