-
Notifications
You must be signed in to change notification settings - Fork 122
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
[FLINK-35283] PoC for supporting unique Kafka producer client ids #101
Open
francis-a
wants to merge
5
commits into
apache:main
Choose a base branch
from
francis-a:faltomare/unique-kafka-producer-ids
base: main
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2182157
PoC for supporting unique kafka producer client ids
francis-a 07eff60
Added prefix to builder
francis-a 62052cd
providing client id
francis-a 845ab66
Refactor to avoid changing state store
francis-a 700660f
Fix style errors
francis-a 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
43 changes: 43 additions & 0 deletions
43
...-connector-kafka/src/main/java/org/apache/flink/connector/kafka/sink/ClientIdFactory.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,43 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.flink.connector.kafka.sink; | ||
|
||
/** | ||
* Construct a Kafka ClientId using a prefix | ||
* and a subtask id. | ||
*/ | ||
public class ClientIdFactory { | ||
private static final String CLIENT_ID_DELIMITER = "-"; | ||
|
||
/** | ||
* Construct a Kafka client id in the following format | ||
* {@code clientIdPrefix-subtaskId}. | ||
* | ||
* @param clientIdPrefix prefix for the id | ||
* @param subtaskId id of the Kafka producer subtask | ||
* @return clientId | ||
*/ | ||
public static String buildClientId( | ||
String clientIdPrefix, | ||
int subtaskId | ||
) { | ||
return clientIdPrefix | ||
+ CLIENT_ID_DELIMITER | ||
+ subtaskId; | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -71,6 +71,7 @@ public class KafkaSinkBuilder<IN> { | |
|
||
private DeliveryGuarantee deliveryGuarantee = DeliveryGuarantee.NONE; | ||
private String transactionalIdPrefix = "kafka-sink"; | ||
private String clientIdPrefix = null; | ||
|
||
private final Properties kafkaProducerConfig; | ||
private KafkaRecordSerializationSchema<IN> recordSerializer; | ||
|
@@ -190,6 +191,20 @@ public KafkaSinkBuilder<IN> setBootstrapServers(String bootstrapServers) { | |
return setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); | ||
} | ||
|
||
/** | ||
* Set the prefix for all KafkaProducer `client.id` values. | ||
* This will overwrite any value set for `client.id` in the provided Kafka producer configuration. | ||
* Instead, a value for `client.id` will be derived from the prefix provided. | ||
* Using a prefix will create a unique Kafka `client.id` for all producers. | ||
* | ||
* @param clientIdPrefix Prefix to use | ||
* @return {@link KafkaSinkBuilder} | ||
*/ | ||
public KafkaSinkBuilder<IN> setClientIdPrefix(String clientIdPrefix) { | ||
this.clientIdPrefix = checkNotNull(clientIdPrefix, "clientIdPrefix"); | ||
return this; | ||
} | ||
|
||
private void sanityCheck() { | ||
checkNotNull( | ||
kafkaProducerConfig.getProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG), | ||
|
@@ -202,6 +217,25 @@ private void sanityCheck() { | |
checkNotNull(recordSerializer, "recordSerializer"); | ||
} | ||
|
||
private static void overwriteConfig(Properties properties, String clientIdPrefix, int subtaskId) { | ||
if (clientIdPrefix == null) { | ||
return; | ||
} | ||
String updatedClientId = ClientIdFactory.buildClientId(clientIdPrefix, subtaskId); | ||
overrideProperty(properties, ProducerConfig.CLIENT_ID_CONFIG, updatedClientId); | ||
} | ||
|
||
private static void overrideProperty(Properties properties, String key, String value) { | ||
String userValue = properties.getProperty(key); | ||
if (userValue != null) { | ||
LOG.warn( | ||
String.format( | ||
"Property %s is provided but will be overridden from %s to %s", | ||
key, userValue, value)); | ||
} | ||
properties.setProperty(key, value); | ||
} | ||
|
||
Comment on lines
+220
to
+238
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. Is this still needed? looks dead to me. |
||
/** | ||
* Constructs the {@link KafkaSink} with the configured properties. | ||
* | ||
|
@@ -210,6 +244,6 @@ private void sanityCheck() { | |
public KafkaSink<IN> build() { | ||
sanityCheck(); | ||
return new KafkaSink<>( | ||
deliveryGuarantee, kafkaProducerConfig, transactionalIdPrefix, recordSerializer); | ||
deliveryGuarantee, kafkaProducerConfig, transactionalIdPrefix, clientIdPrefix, recordSerializer); | ||
} | ||
} |
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
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.
Do we need to include checkpoint id? At any given time we have at least 2 producers open.