You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by sc...@apache.org on 2025/05/19 15:08:02 UTC
(beam) branch master updated: [KafkaIO] Remove duplicate offset in range check (#34201)
This is an automated email from the ASF dual-hosted git repository.
scwhittle pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git
The following commit(s) were added to refs/heads/master by this push:
new 0f4f98d600c [KafkaIO] Remove duplicate offset in range check (#34201)
0f4f98d600c is described below
commit 0f4f98d600c2fdfc4fc630b8dc52bc465dfc61d0
Author: Steven van Rossum <sj...@google.com>
AuthorDate: Mon May 19 17:07:54 2025 +0200
[KafkaIO] Remove duplicate offset in range check (#34201)
* Remove obsolete offset within range check which is only present in ancient kafka client versions
---
.../beam/sdk/io/kafka/KafkaUnboundedReader.java | 24 +++----------
.../beam/sdk/io/kafka/ReadFromKafkaDoFn.java | 41 +++-------------------
.../beam/sdk/io/kafka/ReadFromKafkaDoFnTest.java | 10 +++---
3 files changed, 13 insertions(+), 62 deletions(-)
diff --git a/sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/KafkaUnboundedReader.java b/sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/KafkaUnboundedReader.java
index 4680304d5a6..58b2548900d 100644
--- a/sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/KafkaUnboundedReader.java
+++ b/sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/KafkaUnboundedReader.java
@@ -151,9 +151,8 @@ class KafkaUnboundedReader<K, V> extends UnboundedReader<KafkaRecord<K, V>> {
@Override
public boolean advance() throws IOException {
/* Read first record (if any). we need to loop here because :
- * - (a) some records initially need to be skipped if they are before consumedOffset
- * - (b) if curBatch is empty, we want to fetch next batch and then advance.
- * - (c) curBatch is an iterator of iterators. we interleave the records from each.
+ * - (a) if curBatch is empty, we want to fetch next batch and then advance.
+ * - (b) curBatch is an iterator of iterators. we interleave the records from each.
* curBatch.next() might return an empty iterator.
*/
while (true) {
@@ -163,7 +162,7 @@ class KafkaUnboundedReader<K, V> extends UnboundedReader<KafkaRecord<K, V>> {
PartitionState<K, V> pState = curBatch.next();
- if (!pState.recordIter.hasNext()) { // -- (c)
+ if (!pState.recordIter.hasNext()) { // -- (b)
pState.recordIter = Collections.emptyIterator(); // drop ref
curBatch.remove();
continue;
@@ -173,19 +172,6 @@ class KafkaUnboundedReader<K, V> extends UnboundedReader<KafkaRecord<K, V>> {
elementsReadBySplit.inc();
ConsumerRecord<byte[], byte[]> rawRecord = pState.recordIter.next();
- long expected = pState.nextOffset;
- long offset = rawRecord.offset();
-
- if (offset < expected) { // -- (a)
- // this can happen when compression is enabled in Kafka (seems to be fixed in 0.10)
- // should we check if the offset is way off from consumedOffset (say > 1M)?
- LOG.warn(
- "{}: ignoring already consumed offset {} for {}",
- this,
- offset,
- pState.topicPartition);
- continue;
- }
// Apply user deserializers. User deserializers might throw, which will be propagated up
// and 'curRecord' remains unchanged. The runner should close this reader.
@@ -212,7 +198,7 @@ class KafkaUnboundedReader<K, V> extends UnboundedReader<KafkaRecord<K, V>> {
int recordSize =
(rawRecord.key() == null ? 0 : rawRecord.key().length)
+ (rawRecord.value() == null ? 0 : rawRecord.value().length);
- pState.recordConsumed(offset, recordSize);
+ pState.recordConsumed(rawRecord.offset(), recordSize);
bytesRead.inc(recordSize);
bytesReadBySplit.inc(recordSize);
@@ -223,7 +209,7 @@ class KafkaUnboundedReader<K, V> extends UnboundedReader<KafkaRecord<K, V>> {
kafkaResults.flushBufferedMetrics();
return true;
- } else { // -- (b)
+ } else { // -- (a)
kafkaResults = KafkaSinkMetrics.kafkaMetrics();
nextBatch();
kafkaResults.flushBufferedMetrics();
diff --git a/sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/ReadFromKafkaDoFn.java b/sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/ReadFromKafkaDoFn.java
index cd3ad2526d3..70015847e19 100644
--- a/sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/ReadFromKafkaDoFn.java
+++ b/sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/ReadFromKafkaDoFn.java
@@ -588,12 +588,10 @@ abstract class ReadFromKafkaDoFn<K, V>
topicPartition, Optional.ofNullable(watermarkEstimator.currentWatermark()));
}
- long startOffset = tracker.currentRestriction().getFrom();
- long expectedOffset = startOffset;
+ long expectedOffset = tracker.currentRestriction().getFrom();
consumer.resume(Collections.singleton(topicPartition));
- consumer.seek(topicPartition, startOffset);
- long skippedRecords = 0L;
- final Stopwatch sw = Stopwatch.createStarted();
+ consumer.seek(topicPartition, expectedOffset);
+ final Stopwatch pollTimer = Stopwatch.createUnstarted();
final KafkaMetrics kafkaMetrics = KafkaSinkMetrics.kafkaMetrics();
try {
@@ -602,7 +600,7 @@ abstract class ReadFromKafkaDoFn<K, V>
// A consumer will often have prefetches waiting to be returned immediately in which case
// this timer may contribute more latency than it measures.
// See https://shipilev.net/blog/2014/nanotrusting-nanotime/ for more information.
- final Stopwatch pollTimer = Stopwatch.createStarted();
+ pollTimer.reset().start();
// Fetch the next records.
final ConsumerRecords<byte[], byte[]> rawRecords =
consumer.poll(this.consumerPollingTimeout);
@@ -627,37 +625,6 @@ abstract class ReadFromKafkaDoFn<K, V>
// Visible progress within the consumer polling timeout.
// Partially or fully claim and process records in this batch.
for (ConsumerRecord<byte[], byte[]> rawRecord : rawRecords) {
- // If the Kafka consumer returns a record with an offset that is already processed
- // the record can be safely skipped. This is needed because there is a possibility
- // that the seek() above fails to move the offset to the desired position. In which
- // case poll() would return records that are already cnsumed.
- if (rawRecord.offset() < startOffset) {
- // If the start offset is not reached even after skipping the records for 10 seconds
- // then the processing is stopped with a backoff to give the Kakfa server some time
- // catch up.
- if (sw.elapsed().getSeconds() > 10L) {
- LOG.error(
- "The expected offset ({}) was not reached even after"
- + " skipping consumed records for 10 seconds. The offset we could"
- + " reach was {}. The processing of this bundle will be attempted"
- + " at a later time.",
- expectedOffset,
- rawRecord.offset());
- consumer.pause(Collections.singleton(topicPartition));
- return ProcessContinuation.resume()
- .withResumeDelay(org.joda.time.Duration.standardSeconds(10L));
- }
- skippedRecords++;
- continue;
- }
- if (skippedRecords > 0L) {
- LOG.warn(
- "{} records were skipped due to seek returning an"
- + " earlier position than requested position of {}",
- skippedRecords,
- expectedOffset);
- skippedRecords = 0L;
- }
if (!tracker.tryClaim(rawRecord.offset())) {
consumer.seek(topicPartition, rawRecord.offset());
consumer.pause(Collections.singleton(topicPartition));
diff --git a/sdks/java/io/kafka/src/test/java/org/apache/beam/sdk/io/kafka/ReadFromKafkaDoFnTest.java b/sdks/java/io/kafka/src/test/java/org/apache/beam/sdk/io/kafka/ReadFromKafkaDoFnTest.java
index 2504188d97e..4d22b1d6ea9 100644
--- a/sdks/java/io/kafka/src/test/java/org/apache/beam/sdk/io/kafka/ReadFromKafkaDoFnTest.java
+++ b/sdks/java/io/kafka/src/test/java/org/apache/beam/sdk/io/kafka/ReadFromKafkaDoFnTest.java
@@ -19,6 +19,7 @@ package org.apache.beam.sdk.io.kafka;
import static org.apache.beam.sdk.transforms.errorhandling.BadRecordRouter.BAD_RECORD_TAG;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import java.nio.charset.StandardCharsets;
@@ -525,12 +526,9 @@ public class ReadFromKafkaDoFnTest {
new OffsetRangeTracker(new OffsetRange(startOffset, startOffset + 3));
KafkaSourceDescriptor descriptor =
KafkaSourceDescriptor.of(topicPartition, null, null, null, null, null);
- ProcessContinuation result =
- dofnInstanceWithBrokenSeek.processElement(descriptor, tracker, null, receiver);
- assertEquals(ProcessContinuation.stop(), result);
- assertEquals(
- createExpectedRecords(descriptor, startOffset, 3, "key", "value"),
- receiver.getGoodRecords());
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> dofnInstanceWithBrokenSeek.processElement(descriptor, tracker, null, receiver));
}
@Test