-
Notifications
You must be signed in to change notification settings - Fork 163
PartitionKey Kafka Interceptor #260
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
slinkydeveloper
merged 2 commits into
cloudevents:master
from
slinkydeveloper:issues/94
Nov 13, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
45 changes: 45 additions & 0 deletions
45
kafka/src/main/java/io/cloudevents/kafka/PartitionKeyExtensionInterceptor.java
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.cloudevents.kafka; | ||
|
||
import io.cloudevents.CloudEvent; | ||
import org.apache.kafka.clients.producer.ProducerInterceptor; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.clients.producer.RecordMetadata; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* This {@link ProducerInterceptor} implements the partitioning extension, | ||
* as described in the <a href="https://github.com/cloudevents/spec/blob/master/kafka-protocol-binding.md#31-key-mapping">CloudEvents Kafka specification</a>. | ||
* <p> | ||
* When using in your producer, it will pick the {@code partitionkey} extension from the event and will set it as record key. | ||
* If the extension is missing, It won't replace the key from the original record. | ||
*/ | ||
public class PartitionKeyExtensionInterceptor implements ProducerInterceptor<Object, CloudEvent> { | ||
|
||
public static final String PARTITION_KEY_EXTENSION = "partitionkey"; | ||
|
||
@Override | ||
public ProducerRecord<Object, CloudEvent> onSend(ProducerRecord<Object, CloudEvent> record) { | ||
if (record.value() == null) { | ||
return record; | ||
} | ||
Object partitionKey = record.value().getExtension(PARTITION_KEY_EXTENSION); | ||
if (partitionKey == null) { | ||
return record; | ||
} | ||
|
||
return new ProducerRecord<>(record.topic(), record.partition(), record.timestamp(), partitionKey, record.value(), record.headers()); | ||
} | ||
|
||
@Override | ||
public void onAcknowledgement(RecordMetadata metadata, Exception exception) { | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
|
||
@Override | ||
public void configure(Map<String, ?> configs) { | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
kafka/src/test/java/io/cloudevents/kafka/PartitionKeyExtensionInterceptorTest.java
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.cloudevents.kafka; | ||
|
||
import io.cloudevents.CloudEvent; | ||
import io.cloudevents.core.builder.CloudEventBuilder; | ||
import io.cloudevents.core.test.Data; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class PartitionKeyExtensionInterceptorTest { | ||
|
||
@Test | ||
public void testNoPartitionKeyAndNoOriginalKey() { | ||
assertKey( | ||
new ProducerRecord<>("aaa", Data.V1_MIN), | ||
null | ||
); | ||
} | ||
|
||
@Test | ||
public void testNoPartitionKey() { | ||
assertKey( | ||
new ProducerRecord<>("aaa", "blabla", Data.V1_MIN), | ||
"blabla" | ||
); | ||
} | ||
|
||
@Test | ||
public void testPartitionKeyAndNoOriginalKey() { | ||
assertKey( | ||
new ProducerRecord<>("aaa", CloudEventBuilder | ||
.v1(Data.V1_MIN) | ||
.withExtension(PartitionKeyExtensionInterceptor.PARTITION_KEY_EXTENSION, "albalb") | ||
.build() | ||
), | ||
"albalb" | ||
); | ||
} | ||
|
||
@Test | ||
public void testPartitionKey() { | ||
assertKey( | ||
new ProducerRecord<>("aaa", "blabla", CloudEventBuilder | ||
.v1(Data.V1_MIN) | ||
.withExtension(PartitionKeyExtensionInterceptor.PARTITION_KEY_EXTENSION, "albalb") | ||
.build() | ||
), | ||
"albalb" | ||
); | ||
} | ||
|
||
private void assertKey(ProducerRecord<Object, CloudEvent> record, Object expectedKey) { | ||
PartitionKeyExtensionInterceptor interceptor = new PartitionKeyExtensionInterceptor(); | ||
assertThat(interceptor.onSend(record).key()) | ||
.isEqualTo(expectedKey); | ||
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. It should validate against a calculated partition-id and not change the key |
||
} | ||
|
||
} |
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.
the second field should be calculated based upon 1) the number of partitions, 2) the extension 3) the input record
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.
I don't think that's what the spec states: https://github.com/cloudevents/spec/blob/master/kafka-protocol-binding.md#31-key-mapping
I don't quite get what the implementation should look like to you, can you provide a sample code/pr of how you would implement 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.
In particular:
It's named partitionkey, but in fact we're talking about the key of the message
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.
That's how this is implemented in other sdks too https://github.com/cloudevents/sdk-go/blob/master/protocol/kafka_sarama/v2/write_producer_message.go#L53