-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-8065: restore original input record timestamp in forward() #6393
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
Merged
Merged
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
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 |
|---|---|---|
|
|
@@ -348,6 +348,32 @@ public void shouldConsiderModifiedTimeStamps() { | |
| assertNextOutputRecord(OUTPUT_TOPIC_1, "key3", "value3", partition, 40L); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldConsiderModifiedTimeStampsForMultipleProcessors() { | ||
| final int partition = 10; | ||
| driver = new TopologyTestDriver(createMultiProcessorTimestampTopology(partition), props); | ||
|
|
||
| driver.pipeInput(recordFactory.create(INPUT_TOPIC_1, "key1", "value1", 10L)); | ||
| assertNextOutputRecord(OUTPUT_TOPIC_1, "key1", "value1", partition, 10L); | ||
| assertNextOutputRecord(OUTPUT_TOPIC_2, "key1", "value1", partition, 20L); | ||
| assertNextOutputRecord(OUTPUT_TOPIC_1, "key1", "value1", partition, 15L); | ||
|
Member
Author
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. Without the fix, this fails, because output record ts is 25. And the following timestamps below would be: 35, 37, 47 (it's accumulating also for "deep" modifications) |
||
| assertNextOutputRecord(OUTPUT_TOPIC_2, "key1", "value1", partition, 20L); | ||
| assertNextOutputRecord(OUTPUT_TOPIC_1, "key1", "value1", partition, 12L); | ||
| assertNextOutputRecord(OUTPUT_TOPIC_2, "key1", "value1", partition, 22L); | ||
| assertNoOutputRecord(OUTPUT_TOPIC_1); | ||
| assertNoOutputRecord(OUTPUT_TOPIC_2); | ||
|
|
||
| driver.pipeInput(recordFactory.create(INPUT_TOPIC_1, "key2", "value2", 20L)); | ||
| assertNextOutputRecord(OUTPUT_TOPIC_1, "key2", "value2", partition, 20L); | ||
| assertNextOutputRecord(OUTPUT_TOPIC_2, "key2", "value2", partition, 30L); | ||
| assertNextOutputRecord(OUTPUT_TOPIC_1, "key2", "value2", partition, 25L); | ||
| assertNextOutputRecord(OUTPUT_TOPIC_2, "key2", "value2", partition, 30L); | ||
| assertNextOutputRecord(OUTPUT_TOPIC_1, "key2", "value2", partition, 22L); | ||
| assertNextOutputRecord(OUTPUT_TOPIC_2, "key2", "value2", partition, 32L); | ||
| assertNoOutputRecord(OUTPUT_TOPIC_1); | ||
| assertNoOutputRecord(OUTPUT_TOPIC_2); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldConsiderHeaders() { | ||
| final int partition = 10; | ||
|
|
@@ -489,6 +515,16 @@ private Topology createTimestampTopology(final int partition) { | |
| .addSink("sink", OUTPUT_TOPIC_1, constantPartitioner(partition), "processor"); | ||
| } | ||
|
|
||
| private Topology createMultiProcessorTimestampTopology(final int partition) { | ||
| return topology | ||
| .addSource("source", STRING_DESERIALIZER, STRING_DESERIALIZER, INPUT_TOPIC_1) | ||
| .addProcessor("processor", define(new FanOutTimestampProcessor("child1", "child2")), "source") | ||
| .addProcessor("child1", define(new ForwardingProcessor()), "processor") | ||
| .addProcessor("child2", define(new TimestampProcessor()), "processor") | ||
| .addSink("sink1", OUTPUT_TOPIC_1, constantPartitioner(partition), "child1") | ||
| .addSink("sink2", OUTPUT_TOPIC_2, constantPartitioner(partition), "child2"); | ||
| } | ||
|
|
||
| private Topology createMultiplexingTopology() { | ||
| return topology | ||
| .addSource("source", STRING_DESERIALIZER, STRING_DESERIALIZER, INPUT_TOPIC_1) | ||
|
|
@@ -582,6 +618,25 @@ public void process(final String key, final String value) { | |
| } | ||
| } | ||
|
|
||
| protected static class FanOutTimestampProcessor extends AbstractProcessor<String, String> { | ||
| private final String firstChild; | ||
| private final String secondChild; | ||
|
|
||
| FanOutTimestampProcessor(final String firstChild, | ||
| final String secondChild) { | ||
| this.firstChild = firstChild; | ||
| this.secondChild = secondChild; | ||
| } | ||
|
|
||
| @Override | ||
| public void process(final String key, final String value) { | ||
| context().forward(key, value); | ||
| context().forward(key, value, To.child(firstChild).withTimestamp(context().timestamp() + 5)); | ||
| context().forward(key, value, To.child(secondChild)); | ||
| context().forward(key, value, To.all().withTimestamp(context().timestamp() + 2)); | ||
| } | ||
| } | ||
|
|
||
| protected static class AddHeaderProcessor extends AbstractProcessor<String, String> { | ||
| @Override | ||
| public void process(final String key, final String value) { | ||
|
|
||
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.
Should we consider saving/restoring the whole record context, just in case downstream processors have changed other parts of the context, not just the timestamp?
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.
Headers work differently. Users would mutate the input record header so they understand that the header changes.
We can still address it (I agree that the current header API is not great) but I would exclude it from this PR. Feel free to create a ticket for it.
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.
Ah, this is a good point. I was thinking that downstream processors were directly manipulating the record context, but if they do that, it's clearly their responsibility to restore it when they're done.
I think I won't bother with a ticket right now, it seems like the mutable-headers thing is actually just part of the key and value also being mutable. Plus, there's a bunch of other stuff that's not well defined about header handling in Streams. It seems like altogether too much to scope in a ticket without investing some serious design work up front.
I think the current change is fine as-is. Thanks!