-
Notifications
You must be signed in to change notification settings - Fork 20
Refactor Connector semantics #66
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
Open
fee-mendes
wants to merge
4
commits into
scylladb:master
Choose a base branch
from
fee-mendes:refactor
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
4 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
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
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 |
|---|---|---|
| @@ -1,39 +1,72 @@ | ||
| package com.scylladb.cdc.debezium.connector.transforms; | ||
|
|
||
| import java.util.Map; | ||
| import io.debezium.data.Envelope; | ||
| import io.debezium.transforms.ExtractNewRecordState; | ||
| import io.debezium.transforms.ExtractNewRecordStateConfigDefinition; | ||
| import io.debezium.transforms.ExtractNewRecordStateConfigDefinition.DeleteHandling; | ||
|
|
||
| import org.apache.kafka.common.cache.Cache; | ||
| import org.apache.kafka.common.cache.LRUCache; | ||
| import org.apache.kafka.common.cache.SynchronizedCache; | ||
| import org.apache.kafka.common.config.ConfigDef; | ||
|
|
||
| import org.apache.kafka.connect.connector.ConnectRecord; | ||
| import org.apache.kafka.connect.source.SourceRecord; | ||
| import org.apache.kafka.connect.transforms.util.SchemaUtil; | ||
| import org.apache.kafka.connect.data.Field; | ||
| 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.data.Schema.Type; | ||
| import org.apache.kafka.connect.data.Struct; | ||
|
|
||
| public class ScyllaExtractNewRecordState<R extends ConnectRecord<R>> extends ExtractNewRecordState<R> { | ||
| private Cache<Schema, Schema> schemaUpdateCache = new SynchronizedCache<>(new LRUCache<>(16)); | ||
| private Cache<Integer, Schema> schemaUpdateCache = new SynchronizedCache<>(new LRUCache<>(16)); | ||
| private DeleteHandling handleDeletes; | ||
|
|
||
| @Override | ||
| public void configure(final Map<String, ?> configs) { | ||
| super.configure(configs); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public R apply(final R record) { | ||
| // Debezium by default does not emit a rewrite (delete.handling.mode) | ||
| // when an after field is present. However, in ScyllaDB, we represent | ||
| // the affected keys in the after field. | ||
| boolean isDelete = false; | ||
| boolean hasRewrite = false; | ||
|
Collaborator
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. I don't see |
||
|
|
||
| if (record != null && (record.value() instanceof Struct) && record instanceof SourceRecord) { | ||
| final SourceRecord sr = (SourceRecord) record; | ||
| if (Envelope.operationFor(sr) == Envelope.Operation.DELETE) { | ||
| isDelete = true; | ||
| } | ||
| } | ||
|
|
||
| final R ret = super.apply(record); | ||
| if (ret == null || !(ret.value() instanceof Struct)) { | ||
| return ret; | ||
| } | ||
|
|
||
| final Struct value = (Struct)ret.value(); | ||
|
|
||
| Schema updatedSchema = schemaUpdateCache.get(value.schema()); | ||
| int schemaKey = getSchemaKey(value); | ||
|
|
||
| Schema updatedSchema = schemaUpdateCache.get(schemaKey); | ||
| if (updatedSchema == null) { | ||
| updatedSchema = makeUpdatedSchema(value.schema()); | ||
| schemaUpdateCache.put(value.schema(), updatedSchema); | ||
| updatedSchema = makeUpdatedSchema(value.schema(), value); | ||
| schemaUpdateCache.put(schemaKey, updatedSchema); | ||
| } | ||
|
|
||
| final Struct updatedValue = new Struct(updatedSchema); | ||
|
|
||
| for (Field field : value.schema().fields()) { | ||
| if (isDroppableField(field, value)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (isSimplifiableField(field)) { | ||
| Struct fieldValue = (Struct) value.get(field); | ||
| updatedValue.put(field.name(), fieldValue == null ? null : fieldValue.get("value")); | ||
|
|
@@ -42,6 +75,12 @@ public R apply(final R record) { | |
| } | ||
| } | ||
|
|
||
| if (isDelete) { | ||
| if (handleDeletes == DeleteHandling.REWRITE) { | ||
| updatedValue.put(ExtractNewRecordStateConfigDefinition.DELETED_FIELD, "true"); | ||
| } | ||
| } | ||
|
|
||
| return ret.newRecord(ret.topic(), ret.kafkaPartition(), ret.keySchema(), ret.key(), updatedSchema, updatedValue, ret.timestamp()); | ||
| } | ||
|
|
||
|
|
@@ -51,6 +90,43 @@ public void close() { | |
| schemaUpdateCache = null; | ||
| } | ||
|
|
||
| // getSchemaKey computes a unique schema according to the payload values | ||
| // This ensures different values are stored with different keys in the LRU cache. | ||
| private int getSchemaKey(Struct value) { | ||
| Schema schema = value.schema(); | ||
| final int prime = 31; | ||
| int hash = 1; | ||
|
|
||
| hash += prime * hash + (schema.name() == null ? 0 : schema.name().hashCode()); | ||
|
|
||
| for (Field field : schema.fields()) { | ||
| hash = prime * hash + field.name().hashCode(); | ||
|
|
||
| Object fieldValue = value.get(field); | ||
| boolean isNull = fieldValue == null; | ||
| hash = prime * hash + (isNull ? 0 : 1); | ||
|
|
||
| if (!isNull && field.schema().type() == Type.STRUCT) { | ||
| hash = prime * hash + field.schema().type().hashCode(); | ||
| } | ||
| } | ||
|
|
||
| return hash; | ||
| } | ||
|
|
||
| // isDroppableField drops null Struct values within a Field. It does NOT drops empty Structs representing an actual delta mutation. | ||
| // This allow subscribers to upsert deltas and converge to a stable result. For example, the following Type/Values will return: | ||
| // False (don't drop) -- Type: STRUCT Value: Struct{} | ||
| // True (drop) -- Type: STRUCT Value: null | ||
| private boolean isDroppableField(Field field, Struct value) { | ||
| if (field.schema().type() == Type.STRUCT | ||
| && (value.get(field) == null)) { | ||
| return field.schema().isOptional(); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private boolean isSimplifiableField(Field field) { | ||
| if (field.schema().type() != Type.STRUCT) { | ||
| return false; | ||
|
|
@@ -64,10 +140,14 @@ private boolean isSimplifiableField(Field field) { | |
| return true; | ||
| } | ||
|
|
||
| private Schema makeUpdatedSchema(Schema schema) { | ||
| private Schema makeUpdatedSchema(Schema schema, Struct value) { | ||
| final SchemaBuilder builder = SchemaUtil.copySchemaBasics(schema, SchemaBuilder.struct()); | ||
|
|
||
| for (Field field : schema.fields()) { | ||
| if (isDroppableField(field, value)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (isSimplifiableField(field)) { | ||
| builder.field(field.name(), field.schema().field("value").schema()); | ||
| } else { | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.
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 believe the cache does not help much when so many different possible keys are introduced. Previously it was (generally) 1 schema for 1 table. Now depending on N non-key columns there are 2^N possible custom schemas. Of course not all of them will be generated but even a handful is a significant increase from 1. It could easily exceed 16