Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change commit comment to
pull all partition offsets in a single call to Kafka. -> Retrieve in bulk partition offsets

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Iterables.getOnlyElement;
import static io.trino.plugin.kafka.KafkaErrorCode.KAFKA_SPLIT_ERROR;
import static io.trino.plugin.kafka.KafkaInternalFieldManager.InternalFieldId.OFFSET_TIMESTAMP_FIELD;
import static io.trino.plugin.kafka.KafkaInternalFieldManager.InternalFieldId.PARTITION_ID_FIELD;
Expand All @@ -57,6 +56,7 @@
import static java.lang.Math.floorDiv;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toMap;

public class KafkaFilterManager
{
Expand Down Expand Up @@ -123,13 +123,19 @@ public KafkaFilteringResult getKafkaFilterResult(
try (KafkaConsumer<byte[], byte[]> kafkaConsumer = consumerFactory.create(session)) {
// filter negative value to avoid java.lang.IllegalArgumentException when using KafkaConsumer offsetsForTimes
if (offsetTimestampRanged.get().begin() > INVALID_KAFKA_RANGE_INDEX) {
partitionBeginOffsets = overridePartitionBeginOffsets(partitionBeginOffsets,
partition -> findOffsetsForTimestampGreaterOrEqual(kafkaConsumer, partition, offsetTimestampRanged.get().begin()));
long partitionBeginTimestamp = floorDiv(offsetTimestampRanged.get().begin(), MICROSECONDS_PER_MILLISECOND);
Map<TopicPartition, Long> partitionBeginTimestamps = partitionBeginOffsets.entrySet().stream()
.collect(toMap(Map.Entry::getKey, _ -> partitionBeginTimestamp));
Map<TopicPartition, Optional<Long>> beginOffsets = findOffsetsForTimestampGreaterOrEqual(kafkaConsumer, partitionBeginTimestamps);
partitionBeginOffsets = overridePartitionBeginOffsets(partitionBeginOffsets, beginOffsets::get);
}
if (isTimestampUpperBoundPushdownEnabled(session, kafkaTableHandle.topicName())) {
if (offsetTimestampRanged.get().end() > INVALID_KAFKA_RANGE_INDEX) {
partitionEndOffsets = overridePartitionEndOffsets(partitionEndOffsets,
partition -> findOffsetsForTimestampGreaterOrEqual(kafkaConsumer, partition, offsetTimestampRanged.get().end()));
long partitionEndTimestamp = floorDiv(offsetTimestampRanged.get().end(), MICROSECONDS_PER_MILLISECOND);
Map<TopicPartition, Long> partitionEndTimestamps = partitionEndOffsets.entrySet().stream()
.collect(toMap(Map.Entry::getKey, _ -> partitionEndTimestamp));
Map<TopicPartition, Optional<Long>> endOffsets = findOffsetsForTimestampGreaterOrEqual(kafkaConsumer, partitionEndTimestamps);
partitionEndOffsets = overridePartitionEndOffsets(partitionEndOffsets, endOffsets::get);
}
}
}
Expand Down Expand Up @@ -172,11 +178,12 @@ private boolean isTimestampUpperBoundPushdownEnabled(ConnectorSession session, S
return KafkaSessionProperties.isTimestampUpperBoundPushdownEnabled(session);
}

private static Optional<Long> findOffsetsForTimestampGreaterOrEqual(KafkaConsumer<byte[], byte[]> kafkaConsumer, TopicPartition topicPartition, long timestamp)
private static Map<TopicPartition, Optional<Long>> findOffsetsForTimestampGreaterOrEqual(KafkaConsumer<byte[], byte[]> kafkaConsumer, Map<TopicPartition, Long> timestamps)
{
final long transferTimestamp = floorDiv(timestamp, MICROSECONDS_PER_MILLISECOND);
Map<TopicPartition, OffsetAndTimestamp> topicPartitionOffsets = kafkaConsumer.offsetsForTimes(ImmutableMap.of(topicPartition, transferTimestamp));
return Optional.ofNullable(getOnlyElement(topicPartitionOffsets.values(), null)).map(OffsetAndTimestamp::offset);
Map<TopicPartition, OffsetAndTimestamp> topicPartitionOffsetAndTimestamps = kafkaConsumer.offsetsForTimes(timestamps);
return topicPartitionOffsetAndTimestamps.entrySet().stream()
.collect(toMap(Map.Entry::getKey, entry -> Optional.of(entry.getValue())
.map(OffsetAndTimestamp::offset)));
}

private static Map<TopicPartition, Long> overridePartitionBeginOffsets(Map<TopicPartition, Long> partitionBeginOffsets,
Expand Down