-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Retry count available in header
- Loading branch information
Showing
4 changed files
with
94 additions
and
4 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...lel-consumer-core/src/main/java/io/confluent/parallelconsumer/ParallelConsumerRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.confluent.parallelconsumer; | ||
|
||
/*- | ||
* Copyright (C) 2020-2022 Confluent, Inc. | ||
*/ | ||
|
||
import io.confluent.parallelconsumer.state.WorkContainer; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.common.header.Header; | ||
import org.apache.kafka.common.header.Headers; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
public class ParallelConsumerRecord { | ||
|
||
public static int getFailedCount(final ConsumerRecord<String, String> record) { | ||
Headers headers = record.headers(); | ||
Header header = headers.lastHeader(WorkContainer.FAILED_COUNT_HEADER); | ||
if (header != null) { | ||
byte[] value = header.value(); | ||
ByteBuffer wrap = ByteBuffer.wrap(value); | ||
return wrap.getInt(); | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
parallel-consumer-core/src/test/java/io/confluent/parallelconsumer/FailedCountTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.confluent.parallelconsumer; | ||
|
||
/*- | ||
* Copyright (C) 2020-2022 Confluent, Inc. | ||
*/ | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
class FailedCountTest extends ParallelEoSStreamProcessorTestBase { | ||
|
||
@Test | ||
void failedCountTest() { | ||
primeFirstRecord(); | ||
|
||
AtomicInteger iteration = new AtomicInteger(); | ||
AtomicReference<ConsumerRecord> lastRec = new AtomicReference<>(); | ||
parallelConsumer.poll(consumerRecord -> { | ||
iteration.incrementAndGet(); | ||
|
||
lastRec.set(consumerRecord); | ||
int failedCount = ParallelConsumerRecord.getFailedCount(consumerRecord); | ||
|
||
if (iteration.get() == 1) { | ||
assertThat(failedCount).isEqualTo(0); | ||
throw new RuntimeException("fail 1"); | ||
} else if (iteration.get() == 2) { | ||
assertThat(failedCount).isEqualTo(1); | ||
} | ||
}); | ||
waitForOneLoopCycle(); | ||
parallelConsumer.closeDrainFirst(); | ||
|
||
ConsumerRecord consumerRecord = lastRec.get(); | ||
assertThat(consumerRecord).isNotNull(); | ||
int failedCount = ParallelConsumerRecord.getFailedCount(consumerRecord); | ||
assertThat(failedCount).isEqualTo(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters