Search in sources :

Example 46 with ConsumerRecord

use of org.apache.kafka.clients.consumer.ConsumerRecord in project streamsx.kafka by IBMStreams.

the class TransactionalKafkaProducerClient method getCommittedSequenceIdFromCtrlTopic.

@SuppressWarnings("rawtypes")
private long getCommittedSequenceIdFromCtrlTopic() throws Exception {
    KafkaConsumer<?, ?> consumer = new KafkaConsumer<>(getConsumerProperties());
    HashMap<TopicPartition, Long> endOffsets = getControlTopicEndOffsets(consumer);
    // move the consumer to initial offset to begin consuming from
    consumer.assign(controlTopicInitialOffsets.keySet());
    controlTopicInitialOffsets.forEach((tp, offset) -> {
        consumer.seek(tp, offset);
    });
    long committedSeqId = 0;
    boolean consumerAtEnd = false;
    while (!consumerAtEnd) {
        ConsumerRecords<?, ?> records = consumer.poll(1000);
        if (logger.isDebugEnabled())
            logger.debug("ConsumerRecords: " + records);
        Iterator<?> it = records.iterator();
        // Records from different partitions can be scrambled. So we cannot assume that the last record returned by the iterator contains the last committed sequence-ID.
        while (it.hasNext()) {
            ConsumerRecord record = (ConsumerRecord) it.next();
            Headers headers = record.headers();
            if (logger.isDebugEnabled())
                logger.debug("Headers: " + headers);
            String tid = new String(headers.lastHeader(TRANSACTION_ID).value(), StandardCharsets.UTF_8);
            if (logger.isDebugEnabled())
                logger.debug("Checking tid=" + tid + " (currentTid=" + getTransactionalId() + "); from " + record.topic() + "-" + record.partition());
            if (tid.equals(getTransactionalId())) {
                long decodedSeqId = Long.valueOf(new String(headers.lastHeader(COMMITTED_SEQUENCE_ID).value(), StandardCharsets.UTF_8));
                if (decodedSeqId > committedSeqId)
                    committedSeqId = decodedSeqId;
            }
        }
        consumerAtEnd = isConsumerAtEnd(consumer, endOffsets);
        if (logger.isDebugEnabled())
            logger.debug("consumerAtEnd=" + consumerAtEnd);
    }
    consumer.close(1l, TimeUnit.SECONDS);
    return committedSeqId;
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) Headers(org.apache.kafka.common.header.Headers) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord)

Aggregations

ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)46 Test (org.junit.Test)24 TopicPartition (org.apache.kafka.common.TopicPartition)19 HashMap (java.util.HashMap)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 LinkedHashMap (java.util.LinkedHashMap)10 ConsumerRecords (org.apache.kafka.clients.consumer.ConsumerRecords)10 Struct (org.apache.kafka.connect.data.Struct)9 ArrayList (java.util.ArrayList)8 ClusterConfigState (org.apache.kafka.connect.runtime.distributed.ClusterConfigState)8 List (java.util.List)7 Map (java.util.Map)7 Properties (java.util.Properties)4 KafkaConsumer (org.apache.kafka.clients.consumer.KafkaConsumer)4 SchemaAndValue (org.apache.kafka.connect.data.SchemaAndValue)4 ByteBuffer (java.nio.ByteBuffer)3 Collection (java.util.Collection)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 PartitionInfo (org.apache.kafka.common.PartitionInfo)3 Callback (org.apache.kafka.connect.util.Callback)3