-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DBZ-6721 Add local vgtid transformation
- Loading branch information
Showing
5 changed files
with
325 additions
and
6 deletions.
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
103 changes: 103 additions & 0 deletions
103
src/main/java/io/debezium/connector/vitess/transforms/UseLocalVgtid.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,103 @@ | ||
/* | ||
* Copyright Debezium Authors. | ||
* | ||
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
|
||
package io.debezium.connector.vitess.transforms; | ||
|
||
import static io.debezium.connector.vitess.SourceInfo.SHARD_KEY; | ||
import static io.debezium.connector.vitess.SourceInfo.VGTID_KEY; | ||
|
||
import java.util.Map; | ||
|
||
import org.apache.kafka.common.config.ConfigDef; | ||
import org.apache.kafka.connect.components.Versioned; | ||
import org.apache.kafka.connect.connector.ConnectRecord; | ||
import org.apache.kafka.connect.data.Field; | ||
import org.apache.kafka.connect.data.Schema; | ||
import org.apache.kafka.connect.data.Struct; | ||
import org.apache.kafka.connect.transforms.Transformation; | ||
|
||
import io.debezium.config.Configuration; | ||
import io.debezium.connector.vitess.Module; | ||
import io.debezium.connector.vitess.Vgtid; | ||
import io.debezium.transforms.SmtManager; | ||
|
||
public class UseLocalVgtid<R extends ConnectRecord<R>> implements Transformation<R>, Versioned { | ||
|
||
private SmtManager<R> smtManager; | ||
|
||
@Override | ||
public R apply(R record) { | ||
if (record.value() instanceof Struct) { | ||
Struct value = (Struct) record.value(); | ||
Schema schema = record.valueSchema(); | ||
|
||
String localVgtid = getLocalVgtid(value); | ||
|
||
if (localVgtid != null) { | ||
Struct updatedValue = modifyStruct("", value, schema, localVgtid); | ||
return record.newRecord(record.topic(), record.kafkaPartition(), record.keySchema(), record.key(), | ||
schema, updatedValue, record.timestamp()); | ||
} | ||
|
||
} | ||
return record; | ||
} | ||
|
||
private String getLocalVgtid(Struct value) { | ||
if (value.schema().field("source") != null) { | ||
Struct sourceStruct = (Struct) value.get("source"); | ||
String shard = sourceStruct.getString(SHARD_KEY); | ||
String vgtid = sourceStruct.getString(VGTID_KEY); | ||
return Vgtid.of(vgtid).getLocalVgtid(shard).toString(); | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
|
||
private Struct modifyStruct(String previousPath, Struct struct, Schema schema, String localVgtid) { | ||
// change to work only with exact path match | ||
Struct updatedStruct = new Struct(schema); | ||
for (Field field : schema.fields()) { | ||
String fullName = previousPath.isEmpty() ? field.name() : previousPath + "." + field.name(); | ||
Object fieldValue = struct.get(field); | ||
if (fieldValue instanceof Struct) { | ||
Struct nestedStruct = (Struct) fieldValue; | ||
Struct updatedNestedStruct = modifyStruct(fullName, nestedStruct, field.schema(), localVgtid); | ||
updatedStruct.put(field, updatedNestedStruct); | ||
} | ||
else { | ||
if (fullName.equals("source." + VGTID_KEY)) { | ||
updatedStruct.put(field, localVgtid); | ||
} | ||
else { | ||
updatedStruct.put(field, fieldValue); | ||
} | ||
} | ||
} | ||
return updatedStruct; | ||
} | ||
|
||
@Override | ||
public ConfigDef config() { | ||
return new ConfigDef(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
|
||
@Override | ||
public void configure(Map<String, ?> props) { | ||
final Configuration config = Configuration.from(props); | ||
smtManager = new SmtManager<>(config); | ||
} | ||
|
||
@Override | ||
public String version() { | ||
return Module.version(); | ||
} | ||
} |
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
139 changes: 139 additions & 0 deletions
139
src/test/java/io/debezium/connector/vitess/transforms/UseLocalVgtidTest.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,139 @@ | ||
/* | ||
* Copyright Debezium Authors. | ||
* | ||
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
|
||
package io.debezium.connector.vitess.transforms; | ||
|
||
import static io.debezium.connector.vitess.SourceInfo.SHARD_KEY; | ||
import static io.debezium.connector.vitess.SourceInfo.VGTID_KEY; | ||
import static io.debezium.connector.vitess.VgtidTest.TEST_GTID; | ||
import static io.debezium.connector.vitess.VgtidTest.TEST_KEYSPACE; | ||
import static io.debezium.connector.vitess.VgtidTest.TEST_SHARD; | ||
import static io.debezium.connector.vitess.VgtidTest.VGTID_JSON; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.apache.kafka.connect.data.Schema; | ||
import org.apache.kafka.connect.data.SchemaBuilder; | ||
import org.apache.kafka.connect.data.Struct; | ||
import org.apache.kafka.connect.source.SourceRecord; | ||
import org.junit.Test; | ||
|
||
import io.debezium.data.Envelope; | ||
import io.debezium.schema.SchemaFactory; | ||
|
||
public class UseLocalVgtidTest { | ||
|
||
public static final String LOCAL_VGTID_JSON_WITH_LAST_PK_TEMPLATE = "[" + | ||
"{\"keyspace\":\"%s\",\"shard\":\"%s\",\"gtid\":\"%s\",\"table_p_ks\":[]}" + | ||
"]"; | ||
|
||
public static final String LOCAL_VGTID_JSON_WITH_LAST_PK = String.format( | ||
LOCAL_VGTID_JSON_WITH_LAST_PK_TEMPLATE, | ||
TEST_KEYSPACE, | ||
TEST_SHARD, | ||
TEST_GTID); | ||
|
||
@Test | ||
public void shouldNotReplaceATableWithVgtidColumn() { | ||
Schema recordSchema = SchemaBuilder.struct() | ||
.field(VGTID_KEY, Schema.STRING_SCHEMA) | ||
.build(); | ||
Schema sourceSchema = SchemaBuilder.struct() | ||
.field(VGTID_KEY, Schema.STRING_SCHEMA) | ||
.field(SHARD_KEY, Schema.STRING_SCHEMA).build(); | ||
Envelope envelope = SchemaFactory.get().datatypeEnvelopeSchema() | ||
.withRecord(recordSchema) | ||
.withSource(sourceSchema) | ||
.build(); | ||
Schema valueSchema = envelope.schema(); | ||
Struct valueStruct = new Struct(valueSchema) | ||
.put("before", new Struct(recordSchema).put(VGTID_KEY, "foo")) | ||
.put("after", new Struct(recordSchema).put(VGTID_KEY, "foo")) | ||
.put("op", "c") | ||
.put("source", new Struct(sourceSchema) | ||
.put(VGTID_KEY, VGTID_JSON) | ||
.put(SHARD_KEY, TEST_SHARD)); | ||
|
||
SourceRecord sourceRecord = new SourceRecord( | ||
null, | ||
null, | ||
"topic", | ||
0, | ||
null, | ||
null, | ||
valueSchema, | ||
valueStruct, | ||
null); | ||
|
||
UseLocalVgtid<SourceRecord> useLocalVgtid = new UseLocalVgtid(); | ||
SourceRecord result = useLocalVgtid.apply(sourceRecord); | ||
|
||
Envelope expectedEnvelope = SchemaFactory.get().datatypeEnvelopeSchema() | ||
.withRecord(recordSchema) | ||
.withSource(sourceSchema) | ||
.build(); | ||
Schema expectedValueSchema = expectedEnvelope.schema(); | ||
Struct expectedValueStruct = new Struct(expectedValueSchema) | ||
.put("before", new Struct(recordSchema).put(VGTID_KEY, "foo")) | ||
.put("after", new Struct(recordSchema).put(VGTID_KEY, "foo")) | ||
.put("op", "c") | ||
.put("source", new Struct(sourceSchema) | ||
.put(VGTID_KEY, LOCAL_VGTID_JSON_WITH_LAST_PK) | ||
.put(SHARD_KEY, TEST_SHARD)); | ||
|
||
assertThat(result.value()).isEqualTo(expectedValueStruct); | ||
} | ||
|
||
@Test | ||
public void shouldReplaceWithLocalVgtid() { | ||
Schema recordSchema = SchemaBuilder.struct() | ||
.field("id", Schema.STRING_SCHEMA) | ||
.build(); | ||
Schema sourceSchema = SchemaBuilder.struct() | ||
.field(VGTID_KEY, Schema.STRING_SCHEMA) | ||
.field(SHARD_KEY, Schema.STRING_SCHEMA).build(); | ||
Envelope envelope = SchemaFactory.get().datatypeEnvelopeSchema() | ||
.withRecord(recordSchema) | ||
.withSource(sourceSchema) | ||
.build(); | ||
Schema valueSchema = envelope.schema(); | ||
Struct valueStruct = new Struct(valueSchema) | ||
.put("before", new Struct(recordSchema).put("id", "foo")) | ||
.put("after", new Struct(recordSchema).put("id", "foo")) | ||
.put("op", "c") | ||
.put("source", new Struct(sourceSchema) | ||
.put(VGTID_KEY, VGTID_JSON) | ||
.put(SHARD_KEY, TEST_SHARD)); | ||
|
||
SourceRecord sourceRecord = new SourceRecord( | ||
null, | ||
null, | ||
"topic", | ||
0, | ||
null, | ||
null, | ||
valueSchema, | ||
valueStruct, | ||
null); | ||
|
||
UseLocalVgtid<SourceRecord> useLocalVgtid = new UseLocalVgtid(); | ||
SourceRecord result = useLocalVgtid.apply(sourceRecord); | ||
|
||
Envelope expectedEnvelope = SchemaFactory.get().datatypeEnvelopeSchema() | ||
.withRecord(recordSchema) | ||
.withSource(sourceSchema) | ||
.build(); | ||
Schema expectedValueSchema = expectedEnvelope.schema(); | ||
Struct expectedValueStruct = new Struct(expectedValueSchema) | ||
.put("before", new Struct(recordSchema).put("id", "foo")) | ||
.put("after", new Struct(recordSchema).put("id", "foo")) | ||
.put("op", "c") | ||
.put("source", new Struct(sourceSchema) | ||
.put(VGTID_KEY, LOCAL_VGTID_JSON_WITH_LAST_PK) | ||
.put(SHARD_KEY, TEST_SHARD)); | ||
|
||
assertThat(result.value()).isEqualTo(expectedValueStruct); | ||
} | ||
} |