-
Notifications
You must be signed in to change notification settings - Fork 27
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
Make Schema Registry Subjects configurable #29
Open
iturcino
wants to merge
12
commits into
OneCricketeer:master
Choose a base branch
from
iturcino:subject-name-configurable
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.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e0d45b3
dependency updates - CP v.5.5.0, Kafka v.2.5.0
iturcino db6bcaa
alignment with kafka v.2.5.0 and latest Schema Registry - ParsedSchem…
iturcino 6d125c5
replaced deprecated AbstractKafkaAvroSerDeConfig with AbstractKafkaSc…
iturcino ebac2a8
imports ordering
iturcino 4549bbf
Removed kafka-clients version, using confluent-version instead. Added…
iturcino 2080a90
bumped version to 0.2.1
iturcino 1ebc644
implementation of configurable key & value subject name strategies fo…
iturcino 8f91e84
tests for key & value subject name strategies - RecordNameSubjectStra…
iturcino 149ee95
updated README.md with description of key.subject.name.strategy and v…
iturcino 4b7bf75
Merge pull request #1 from OneCricketeer/master
iturcino 949f390
Merge branch 'master' into mm2-updates
2887c6a
Merge branch 'mm2-updates' into subject-name-configurable
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 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
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import org.apache.kafka.common.KafkaException; | ||
import org.apache.kafka.common.cache.Cache; | ||
import org.apache.kafka.common.cache.LRUCache; | ||
import org.apache.kafka.common.cache.SynchronizedCache; | ||
|
@@ -23,10 +24,13 @@ | |
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.confluent.kafka.schemaregistry.ParsedSchema; | ||
import io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient; | ||
import io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException; | ||
import io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig; | ||
import io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig; | ||
import io.confluent.kafka.serializers.subject.RecordNameStrategy; | ||
import io.confluent.kafka.serializers.subject.TopicNameStrategy; | ||
import io.confluent.kafka.serializers.subject.TopicRecordNameStrategy; | ||
import io.confluent.kafka.serializers.subject.strategy.SubjectNameStrategy; | ||
|
||
@SuppressWarnings("unused") | ||
|
@@ -44,17 +48,19 @@ public class SchemaRegistryTransfer<R extends ConnectRecord<R>> implements Trans | |
|
||
public static final String SRC_PREAMBLE = "For source consumer's schema registry, "; | ||
public static final String SRC_SCHEMA_REGISTRY_CONFIG_DOC = "A list of addresses for the Schema Registry to copy from. The consumer's Schema Registry."; | ||
public static final String SRC_BASIC_AUTH_CREDENTIALS_SOURCE_CONFIG_DOC = SRC_PREAMBLE + AbstractKafkaAvroSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE_DOC; | ||
public static final String SRC_BASIC_AUTH_CREDENTIALS_SOURCE_CONFIG_DEFAULT = AbstractKafkaAvroSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE_DEFAULT; | ||
public static final String SRC_USER_INFO_CONFIG_DOC = SRC_PREAMBLE + AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_USER_INFO_DOC; | ||
public static final String SRC_USER_INFO_CONFIG_DEFAULT = AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_USER_INFO_DEFAULT; | ||
public static final String SRC_BASIC_AUTH_CREDENTIALS_SOURCE_CONFIG_DOC = SRC_PREAMBLE + AbstractKafkaSchemaSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE_DOC; | ||
public static final String SRC_BASIC_AUTH_CREDENTIALS_SOURCE_CONFIG_DEFAULT = AbstractKafkaSchemaSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE_DEFAULT; | ||
public static final String SRC_USER_INFO_CONFIG_DOC = SRC_PREAMBLE + AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_USER_INFO_DOC; | ||
public static final String SRC_USER_INFO_CONFIG_DEFAULT = AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_USER_INFO_DEFAULT; | ||
|
||
public static final String DEST_PREAMBLE = "For target producer's schema registry, "; | ||
public static final String DEST_SCHEMA_REGISTRY_CONFIG_DOC = "A list of addresses for the Schema Registry to copy to. The producer's Schema Registry."; | ||
public static final String DEST_BASIC_AUTH_CREDENTIALS_SOURCE_CONFIG_DOC = DEST_PREAMBLE + AbstractKafkaAvroSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE_DOC; | ||
public static final String DEST_BASIC_AUTH_CREDENTIALS_SOURCE_CONFIG_DEFAULT = AbstractKafkaAvroSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE_DEFAULT; | ||
public static final String DEST_USER_INFO_CONFIG_DOC = DEST_PREAMBLE + AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_USER_INFO_DOC; | ||
public static final String DEST_USER_INFO_CONFIG_DEFAULT = AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_USER_INFO_DEFAULT; | ||
public static final String DEST_BASIC_AUTH_CREDENTIALS_SOURCE_CONFIG_DOC = DEST_PREAMBLE + AbstractKafkaSchemaSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE_DOC; | ||
public static final String DEST_BASIC_AUTH_CREDENTIALS_SOURCE_CONFIG_DEFAULT = AbstractKafkaSchemaSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE_DEFAULT; | ||
public static final String DEST_USER_INFO_CONFIG_DOC = DEST_PREAMBLE + AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_USER_INFO_DOC; | ||
public static final String DEST_USER_INFO_CONFIG_DEFAULT = AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_USER_INFO_DEFAULT; | ||
public static final String KEY_SUBJECT_NAME_STRATEGY_DEFAULT = TopicNameStrategy.class.getSimpleName(); | ||
public static final String VALUE_SUBJECT_NAME_STRATEGY_DEFAULT = TopicNameStrategy.class.getSimpleName(); | ||
|
||
public static final String TRANSFER_KEYS_CONFIG_DOC = "Whether or not to copy message key schemas between registries."; | ||
public static final Boolean TRANSFER_KEYS_CONFIG_DEFAULT = true; | ||
|
@@ -63,7 +69,8 @@ public class SchemaRegistryTransfer<R extends ConnectRecord<R>> implements Trans | |
|
||
private CachedSchemaRegistryClient sourceSchemaRegistryClient; | ||
private CachedSchemaRegistryClient destSchemaRegistryClient; | ||
private SubjectNameStrategy<org.apache.avro.Schema> subjectNameStrategy; | ||
private SubjectNameStrategy keySubjectNameStrategy; | ||
private SubjectNameStrategy valueSubjectNameStrategy; | ||
private boolean transferKeys, includeHeaders; | ||
|
||
// caches from the source registry to the destination registry | ||
|
@@ -83,8 +90,10 @@ public SchemaRegistryTransfer() { | |
.define(ConfigName.SCHEMA_CAPACITY, ConfigDef.Type.INT, SCHEMA_CAPACITY_CONFIG_DEFAULT, ConfigDef.Importance.LOW, SCHEMA_CAPACITY_CONFIG_DOC) | ||
.define(ConfigName.TRANSFER_KEYS, ConfigDef.Type.BOOLEAN, TRANSFER_KEYS_CONFIG_DEFAULT, ConfigDef.Importance.MEDIUM, TRANSFER_KEYS_CONFIG_DOC) | ||
.define(ConfigName.INCLUDE_HEADERS, ConfigDef.Type.BOOLEAN, INCLUDE_HEADERS_CONFIG_DEFAULT, ConfigDef.Importance.MEDIUM, INCLUDE_HEADERS_CONFIG_DOC) | ||
.define(ConfigName.KEY_SUBJECT_NAME_STRATEGY, ConfigDef.Type.STRING, KEY_SUBJECT_NAME_STRATEGY_DEFAULT, ConfigDef.Importance.MEDIUM, AbstractKafkaSchemaSerDeConfig.KEY_SUBJECT_NAME_STRATEGY_DOC) | ||
.define(ConfigName.VALUE_SUBJECT_NAME_STRATEGY, ConfigDef.Type.STRING, KEY_SUBJECT_NAME_STRATEGY_DEFAULT, ConfigDef.Importance.MEDIUM, AbstractKafkaSchemaSerDeConfig.VALUE_SUBJECT_NAME_STRATEGY_DOC) | ||
; | ||
// TODO: Other properties might be useful, e.g. the Subject Strategies | ||
// TODO: Other properties might be useful | ||
} | ||
|
||
@Override | ||
|
@@ -98,17 +107,17 @@ public void configure(Map<String, ?> props) { | |
|
||
List<String> sourceUrls = config.getList(ConfigName.SRC_SCHEMA_REGISTRY_URL); | ||
final Map<String, String> sourceProps = new HashMap<>(); | ||
sourceProps.put(AbstractKafkaAvroSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE, | ||
sourceProps.put(AbstractKafkaSchemaSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE, | ||
"SRC_" + config.getString(ConfigName.SRC_BASIC_AUTH_CREDENTIALS_SOURCE)); | ||
sourceProps.put(AbstractKafkaAvroSerDeConfig.USER_INFO_CONFIG, | ||
sourceProps.put(AbstractKafkaSchemaSerDeConfig.USER_INFO_CONFIG, | ||
config.getPassword(ConfigName.SRC_USER_INFO) | ||
.value()); | ||
|
||
List<String> destUrls = config.getList(ConfigName.DEST_SCHEMA_REGISTRY_URL); | ||
final Map<String, String> destProps = new HashMap<>(); | ||
destProps.put(AbstractKafkaAvroSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE, | ||
destProps.put(AbstractKafkaSchemaSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE, | ||
"DEST_" + config.getString(ConfigName.DEST_BASIC_AUTH_CREDENTIALS_SOURCE)); | ||
destProps.put(AbstractKafkaAvroSerDeConfig.USER_INFO_CONFIG, | ||
destProps.put(AbstractKafkaSchemaSerDeConfig.USER_INFO_CONFIG, | ||
config.getPassword(ConfigName.DEST_USER_INFO) | ||
.value()); | ||
|
||
|
@@ -121,9 +130,21 @@ public void configure(Map<String, ?> props) { | |
this.transferKeys = config.getBoolean(ConfigName.TRANSFER_KEYS); | ||
this.includeHeaders = config.getBoolean(ConfigName.INCLUDE_HEADERS); | ||
|
||
// TODO: Make the Strategy configurable, may be different for src and dest | ||
// Strategy for the -key and -value subjects | ||
this.subjectNameStrategy = new TopicNameStrategy(); | ||
// Strategy for the subjects | ||
this.keySubjectNameStrategy = createSubjectStrategy(config.getString(ConfigName.KEY_SUBJECT_NAME_STRATEGY)); | ||
this.valueSubjectNameStrategy = createSubjectStrategy(config.getString(ConfigName.VALUE_SUBJECT_NAME_STRATEGY)); | ||
Comment on lines
-124
to
+135
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. Sorry it took me so long to review this. Were you not able to make the source and destination use differing strategies? Or do you think that use case would ultimately cause confusion? |
||
} | ||
|
||
private SubjectNameStrategy createSubjectStrategy(String subjectStrategyName) { | ||
if (TopicNameStrategy.class.getSimpleName().equals(subjectStrategyName)) { | ||
return new TopicNameStrategy(); | ||
} else if (RecordNameStrategy.class.getSimpleName().equals(subjectStrategyName)) { | ||
return new RecordNameStrategy(); | ||
} else if (TopicRecordNameStrategy.class.getSimpleName().equals(subjectStrategyName)) { | ||
return new TopicRecordNameStrategy(); | ||
} else { | ||
throw new KafkaException("Unknown Subject strategy name: " + subjectStrategyName); | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -206,23 +227,22 @@ protected Optional<Integer> copySchema(ByteBuffer buffer, String topic, boolean | |
|
||
schemaAndDestId = schemaCache.get(sourceSchemaId); | ||
if (schemaAndDestId != null) { | ||
log.trace("Schema id {} has been seen before. Not registering with destination registry again."); | ||
log.trace("Schema id {} has been seen before. Not registering with destination registry again.", sourceSchemaId); | ||
} else { // cache miss | ||
log.trace("Schema id {} has not been seen before", sourceSchemaId); | ||
schemaAndDestId = new SchemaAndId(); | ||
try { | ||
log.trace("Looking up schema id {} in source registry", sourceSchemaId); | ||
// Can't do getBySubjectAndId because that requires a Schema object for the strategy | ||
schemaAndDestId.schema = sourceSchemaRegistryClient.getById(sourceSchemaId); | ||
schemaAndDestId.schema = sourceSchemaRegistryClient.getSchemaById(sourceSchemaId); | ||
} catch (IOException | RestClientException e) { | ||
log.error(String.format("Unable to fetch source schema for id %d.", sourceSchemaId), e); | ||
throw new ConnectException(e); | ||
} | ||
|
||
try { | ||
log.trace("Registering schema {} to destination registry", schemaAndDestId.schema); | ||
// It could be possible that the destination naming strategy is different from the source | ||
String subjectName = subjectNameStrategy.subjectName(topic, isKey, schemaAndDestId.schema); | ||
String subjectName = isKey ? keySubjectNameStrategy.subjectName(topic, isKey, schemaAndDestId.schema) : valueSubjectNameStrategy.subjectName(topic, isKey, schemaAndDestId.schema); | ||
schemaAndDestId.id = destSchemaRegistryClient.register(subjectName, schemaAndDestId.schema); | ||
schemaCache.put(sourceSchemaId, schemaAndDestId); | ||
} catch (IOException | RestClientException e) { | ||
|
@@ -244,25 +264,27 @@ public void close() { | |
} | ||
|
||
interface ConfigName { | ||
String SRC_SCHEMA_REGISTRY_URL = "src." + AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG; | ||
String SRC_BASIC_AUTH_CREDENTIALS_SOURCE = "src." + AbstractKafkaAvroSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE; | ||
String SRC_USER_INFO = "src." + AbstractKafkaAvroSerDeConfig.USER_INFO_CONFIG; | ||
String DEST_SCHEMA_REGISTRY_URL = "dest." + AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG; | ||
String DEST_BASIC_AUTH_CREDENTIALS_SOURCE = "dest." + AbstractKafkaAvroSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE; | ||
String DEST_USER_INFO = "dest." + AbstractKafkaAvroSerDeConfig.USER_INFO_CONFIG; | ||
String SRC_SCHEMA_REGISTRY_URL = "src." + AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG; | ||
String SRC_BASIC_AUTH_CREDENTIALS_SOURCE = "src." + AbstractKafkaSchemaSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE; | ||
String SRC_USER_INFO = "src." + AbstractKafkaSchemaSerDeConfig.USER_INFO_CONFIG; | ||
String DEST_SCHEMA_REGISTRY_URL = "dest." + AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG; | ||
String DEST_BASIC_AUTH_CREDENTIALS_SOURCE = "dest." + AbstractKafkaSchemaSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE; | ||
String DEST_USER_INFO = "dest." + AbstractKafkaSchemaSerDeConfig.USER_INFO_CONFIG; | ||
String SCHEMA_CAPACITY = "schema.capacity"; | ||
String TRANSFER_KEYS = "transfer.message.keys"; | ||
String INCLUDE_HEADERS = "include.message.headers"; | ||
String VALUE_SUBJECT_NAME_STRATEGY = AbstractKafkaSchemaSerDeConfig.VALUE_SUBJECT_NAME_STRATEGY; | ||
String KEY_SUBJECT_NAME_STRATEGY = AbstractKafkaSchemaSerDeConfig.KEY_SUBJECT_NAME_STRATEGY; | ||
} | ||
|
||
private static class SchemaAndId { | ||
private Integer id; | ||
private org.apache.avro.Schema schema; | ||
private ParsedSchema schema; | ||
|
||
SchemaAndId() { | ||
} | ||
|
||
SchemaAndId(int id, org.apache.avro.Schema schema) { | ||
SchemaAndId(int id, ParsedSchema schema) { | ||
this.id = id; | ||
this.schema = schema; | ||
} | ||
|
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.
It should be possible to implement the feature without upgrades, IMO.
Can the version bump be a separate PR?