-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add metrics for Kafka offset commit failures #38613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kriti-dev07
wants to merge
3
commits into
apache:master
Choose a base branch
from
Kriti-dev07:add-kafka-offset-metrics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+48
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ | |
| import org.apache.beam.sdk.coders.KvCoder; | ||
| import org.apache.beam.sdk.coders.VarLongCoder; | ||
| import org.apache.beam.sdk.coders.VoidCoder; | ||
| import org.apache.beam.sdk.metrics.Counter; | ||
| import org.apache.beam.sdk.metrics.Metrics; | ||
| import org.apache.beam.sdk.schemas.NoSuchSchemaException; | ||
| import org.apache.beam.sdk.transforms.DoFn; | ||
| import org.apache.beam.sdk.transforms.MapElements; | ||
|
|
@@ -51,22 +53,31 @@ | |
| public class KafkaCommitOffset<K, V> | ||
| extends PTransform< | ||
| PCollection<KV<KafkaSourceDescriptor, KafkaRecord<K, V>>>, PCollection<Void>> { | ||
|
|
||
| private final KafkaIO.ReadSourceDescriptors<K, V> readSourceDescriptors; | ||
| private final boolean use259implementation; | ||
|
|
||
| KafkaCommitOffset( | ||
| KafkaIO.ReadSourceDescriptors<K, V> readSourceDescriptors, boolean use259implementation) { | ||
|
|
||
| this.readSourceDescriptors = readSourceDescriptors; | ||
| this.use259implementation = use259implementation; | ||
| } | ||
|
|
||
| static class CommitOffsetDoFn extends DoFn<KV<KafkaSourceDescriptor, Long>, Void> { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(CommitOffsetDoFn.class); | ||
|
|
||
| private final Counter commitFailures = | ||
| Metrics.counter(CommitOffsetDoFn.class, "commit-failures"); | ||
|
|
||
| private final Map<String, Object> consumerConfig; | ||
|
|
||
| private final SerializableFunction<Map<String, Object>, Consumer<byte[], byte[]>> | ||
| consumerFactoryFn; | ||
|
|
||
| CommitOffsetDoFn(KafkaIO.ReadSourceDescriptors<?, ?> readSourceDescriptors) { | ||
|
|
||
| consumerConfig = readSourceDescriptors.getConsumerConfig(); | ||
| consumerFactoryFn = readSourceDescriptors.getConsumerFactoryFn(); | ||
| } | ||
|
|
@@ -76,15 +87,23 @@ static class CommitOffsetDoFn extends DoFn<KV<KafkaSourceDescriptor, Long>, Void | |
| @RequiresStableInput | ||
| @ProcessElement | ||
| public void processElement(@Element KV<KafkaSourceDescriptor, Long> element) { | ||
|
|
||
| Map<String, Object> updatedConsumerConfig = | ||
| overrideBootstrapServersConfig(consumerConfig, element.getKey()); | ||
|
|
||
| try (Consumer<byte[], byte[]> consumer = consumerFactoryFn.apply(updatedConsumerConfig)) { | ||
|
|
||
| try { | ||
|
|
||
| consumer.commitSync( | ||
| Collections.singletonMap( | ||
| element.getKey().getTopicPartition(), | ||
| new OffsetAndMetadata(element.getValue() + 1))); | ||
|
|
||
| } catch (Exception e) { | ||
|
|
||
| commitFailures.inc(); | ||
|
|
||
| // TODO: consider retrying. | ||
| LOG.warn("Getting exception when committing offset: {}", e.getMessage()); | ||
| } | ||
|
|
@@ -93,29 +112,37 @@ public void processElement(@Element KV<KafkaSourceDescriptor, Long> element) { | |
|
|
||
| private Map<String, Object> overrideBootstrapServersConfig( | ||
| Map<String, Object> currentConfig, KafkaSourceDescriptor description) { | ||
|
|
||
| checkState( | ||
| currentConfig.containsKey(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG) | ||
| || description.getBootStrapServers() != null); | ||
|
|
||
| Map<String, Object> config = new HashMap<>(currentConfig); | ||
|
|
||
| if (description.getBootStrapServers() != null | ||
| && !description.getBootStrapServers().isEmpty()) { | ||
|
|
||
| config.put( | ||
| ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, | ||
| String.join(",", description.getBootStrapServers())); | ||
| } | ||
|
|
||
| return config; | ||
| } | ||
| } | ||
|
|
||
| private static final class MaxOffsetFn<K, V> | ||
| extends DoFn<KV<KafkaSourceDescriptor, KafkaRecord<K, V>>, KV<KafkaSourceDescriptor, Long>> { | ||
|
|
||
| private static class OffsetAndTimestamp { | ||
|
|
||
| OffsetAndTimestamp(long offset, Instant timestamp) { | ||
| this.offset = offset; | ||
| this.timestamp = timestamp; | ||
| } | ||
|
|
||
| void merge(long offset, Instant timestamp) { | ||
|
|
||
| if (this.offset < offset) { | ||
| this.offset = offset; | ||
| this.timestamp = timestamp; | ||
|
|
@@ -130,6 +157,7 @@ void merge(long offset, Instant timestamp) { | |
|
|
||
| @StartBundle | ||
| public void startBundle() { | ||
|
|
||
| if (maxObserved == null) { | ||
| maxObserved = new HashMap<>(); | ||
| } else { | ||
|
|
@@ -143,13 +171,16 @@ public void startBundle() { | |
| public void processElement( | ||
| @Element KV<KafkaSourceDescriptor, KafkaRecord<K, V>> element, | ||
| @Timestamp Instant timestamp) { | ||
|
|
||
| maxObserved.compute( | ||
| element.getKey(), | ||
| (k, v) -> { | ||
| long offset = element.getValue().getOffset(); | ||
|
|
||
| if (v == null) { | ||
| return new OffsetAndTimestamp(offset, timestamp); | ||
| } | ||
|
|
||
| v.merge(offset, timestamp); | ||
| return v; | ||
| }); | ||
|
|
@@ -158,26 +189,33 @@ public void processElement( | |
| @FinishBundle | ||
| @SuppressWarnings("nullness") // startBundle guaranteed to initialize | ||
| public void finishBundle(FinishBundleContext context) { | ||
|
|
||
| maxObserved.forEach( | ||
| (k, v) -> context.output(KV.of(k, v.offset), v.timestamp, GlobalWindow.INSTANCE)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public PCollection<Void> expand(PCollection<KV<KafkaSourceDescriptor, KafkaRecord<K, V>>> input) { | ||
|
|
||
| try { | ||
|
|
||
| PCollection<KV<KafkaSourceDescriptor, Long>> offsets; | ||
|
|
||
| if (use259implementation) { | ||
|
|
||
| offsets = | ||
| input.apply( | ||
| MapElements.into(new TypeDescriptor<KV<KafkaSourceDescriptor, Long>>() {}) | ||
| .via(element -> KV.of(element.getKey(), element.getValue().getOffset()))); | ||
|
|
||
| } else { | ||
|
|
||
| // Reduce the amount of data to combine by calculating a max within the generally dense | ||
| // bundles of reading | ||
| // from a Kafka partition. | ||
| // bundles of reading from a Kafka partition. | ||
| offsets = input.apply(ParDo.of(new MaxOffsetFn<>())); | ||
| } | ||
|
|
||
|
Comment on lines
+200
to
+218
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cleaning up excessive whitespace in the try {
PCollection<KV<KafkaSourceDescriptor, Long>> offsets;
if (use259implementation) {
offsets =
input.apply(
MapElements.into(new TypeDescriptor<KV<KafkaSourceDescriptor, Long>>() {})
.via(element -> KV.of(element.getKey(), element.getValue().getOffset())));
} else {
// Reduce the amount of data to combine by calculating a max within the generally dense
// bundles of reading
// from a Kafka partition.
offsets = input.apply(ParDo.of(new MaxOffsetFn<>()));
} |
||
| return offsets | ||
| .setCoder( | ||
| KvCoder.of( | ||
|
|
@@ -190,7 +228,9 @@ public PCollection<Void> expand(PCollection<KV<KafkaSourceDescriptor, KafkaRecor | |
| .apply(Max.longsPerKey()) | ||
| .apply(ParDo.of(new CommitOffsetDoFn(readSourceDescriptors))) | ||
| .setCoder(VoidCoder.of()); | ||
|
|
||
| } catch (NoSuchSchemaException e) { | ||
|
|
||
| throw new RuntimeException(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing unnecessary blank lines to maintain consistency with the project's coding style.