Skip to content
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

Add connector version in RECORD_METADATA #766

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public class SnowflakeSinkConnectorConfig {
private static final String SNOWFLAKE_METADATA_FLAGS = "Snowflake Metadata Flags";
public static final String SNOWFLAKE_METADATA_CREATETIME = "snowflake.metadata.createtime";
public static final String SNOWFLAKE_METADATA_TOPIC = "snowflake.metadata.topic";
public static final String SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION =
"snowflake.metadata.sf.connector.version";
public static final String SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION_DEFAULT = "false";
public static final String SNOWFLAKE_METADATA_OFFSET_AND_PARTITION =
"snowflake.metadata.offset.and.partition";
public static final String SNOWFLAKE_METADATA_ALL = "snowflake.metadata.all";
Expand Down Expand Up @@ -486,6 +489,17 @@ static ConfigDef newConfigDef() {
2,
ConfigDef.Width.NONE,
SNOWFLAKE_METADATA_TOPIC)
.define(
SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION,
Type.BOOLEAN,
SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION_DEFAULT,
Importance.LOW,
"Flag to control whether Snowflake Connector version is collected in snowflake"
+ " metadata",
SNOWFLAKE_METADATA_FLAGS,
3,
ConfigDef.Width.NONE,
SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION)
.define(
SNOWFLAKE_METADATA_OFFSET_AND_PARTITION,
Type.BOOLEAN,
Expand All @@ -494,7 +508,7 @@ static ConfigDef newConfigDef() {
"Flag to control whether kafka partition and offset are collected in snowflake"
+ " metadata",
SNOWFLAKE_METADATA_FLAGS,
3,
4,
ConfigDef.Width.NONE,
SNOWFLAKE_METADATA_OFFSET_AND_PARTITION)
.define(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class RecordService {
static final String CONTENT = "content";
static final String META = "meta";
static final String SCHEMA_ID = "schema_id";
static final String SF_CONNECTOR_VERSION = "sf_connector_version";
private static final String KEY_SCHEMA_ID = "key_schema_id";
static final String HEADERS = "headers";

Expand Down Expand Up @@ -191,6 +192,9 @@ private SnowflakeTableRow processRecord(SinkRecord record) {
if (metadataConfig.topicFlag) {
meta.put(TOPIC, record.topic());
}
if (metadataConfig.sfConnectorVersionFlag) {
meta.put(SF_CONNECTOR_VERSION, Utils.VERSION);
}
if (metadataConfig.offsetAndPartitionFlag) {
meta.put(OFFSET, record.kafkaOffset());
meta.put(PARTITION, record.kafkaPartition());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class SnowflakeMetadataConfig {
final boolean createtimeFlag;
final boolean topicFlag;
final boolean offsetAndPartitionFlag;
final boolean sfConnectorVersionFlag;
final boolean allFlag;

/** initialize with default config */
Expand All @@ -25,6 +26,7 @@ public SnowflakeMetadataConfig(Map<String, String> config) {
// these values are the default values of the configuration
boolean createtime = true;
boolean topic = true;
boolean version = false;
boolean offsetAndPartition = true;
boolean all = true;
if (config.containsKey(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_CREATETIME)
Expand All @@ -39,6 +41,12 @@ public SnowflakeMetadataConfig(Map<String, String> config) {
.equals(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_DEFAULT)) {
topic = false;
}
if (config.containsKey(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION)
&& !config
.get(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_ALL)
.equals(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_DEFAULT)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set this to true only when SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION is true right? default is false now!

version = false;
}
if (config.containsKey(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_OFFSET_AND_PARTITION)
&& !config
.get(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_OFFSET_AND_PARTITION)
Expand All @@ -54,6 +62,7 @@ public SnowflakeMetadataConfig(Map<String, String> config) {

createtimeFlag = createtime;
topicFlag = topic;
sfConnectorVersionFlag = version;
offsetAndPartitionFlag = offsetAndPartition;
allFlag = all;
}
Expand All @@ -64,6 +73,8 @@ public String toString() {
+ ", "
+ "topicFlag: "
+ topicFlag
+ "versionFlag: "
+ sfConnectorVersionFlag
+ ", "
+ "offsetAndPartitionFlag: "
+ offsetAndPartitionFlag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ConnectorIT {
SnowflakeSinkConnectorConfig.BUFFER_FLUSH_TIME_SEC,
SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_ALL,
SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_TOPIC,
SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION,
SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_OFFSET_AND_PARTITION,
SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_CREATETIME,
SnowflakeSinkConnectorConfig.TOPICS_TABLES_MAP,
Expand Down Expand Up @@ -78,6 +79,7 @@ static Map<String, String> getErrorConfig() {
config.put(SnowflakeSinkConnectorConfig.BUFFER_FLUSH_TIME_SEC, "-1");
config.put(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_ALL, "falseee");
config.put(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_TOPIC, "falseee");
config.put(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION, "falseee");
config.put(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_OFFSET_AND_PARTITION, "falseee");
config.put(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_CREATETIME, "falseee");
config.put(SnowflakeSinkConnectorConfig.TOPICS_TABLES_MAP, "jfsja,,");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public class MetaColumnTest {
put(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_TOPIC, "false");
}
};
private HashMap<String, String> versionConfig =
new HashMap<String, String>() {
{
put(SnowflakeSinkConnectorConfig.SNOWFLAKE_METADATA_SF_CONNECTOR_VERSION, "false");
}
};
private HashMap<String, String> offsetAndPartitionConfig =
new HashMap<String, String>() {
{
Expand Down Expand Up @@ -113,6 +119,18 @@ public void testConfig() throws IOException {
assert result.get(META).has(RecordService.OFFSET);
assert result.get(META).has(RecordService.PARTITION);
assert result.get(META).has(record.timestampType().name);
assert result.get(META).has(RecordService.SF_CONNECTOR_VERSION);

// test metadata configuration -- remove version
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is for default, can you add a test for what happens when you add true to the metadata version?

Also, once you are done, if you dont mind, I would like to add an e2e test here to verify if this works fine.

Then we can push the code. (I will open another PR where you will be seen as co-author)

Copy link
Author

@cchandurkar cchandurkar Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just updated the test - something to note: Checking for default value is a bit different than rest of the metadata fields as they are enabled by default. Here I'm explicitly checking the value of sf connector version config to be "true" for it to enable, it's disabled by default. So this test already tests "what happens when you add true to the metadata version"

metadataConfig = new SnowflakeMetadataConfig(versionConfig);
service.setMetadataConfig(metadataConfig);
result = mapper.readTree(service.getProcessedRecordForSnowpipe(record));
assert result.has(META);
assert !result.get(META).has(RecordService.SF_CONNECTOR_VERSION);
assert result.get(META).has(RecordService.OFFSET);
assert result.get(META).has(RecordService.PARTITION);
assert result.get(META).has(record.timestampType().name);
assert result.get(META).has(RecordService.TOPIC);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something doesn add up in this test.
default is false right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. There was a bug and had to update the test. Fixed it in 1270dc6


// test metadata configuration -- remove offset and partition
metadataConfig = new SnowflakeMetadataConfig(offsetAndPartitionConfig);
Expand All @@ -123,6 +141,7 @@ public void testConfig() throws IOException {
assert !result.get(META).has(RecordService.PARTITION);
assert result.get(META).has(record.timestampType().name);
assert result.get(META).has(RecordService.TOPIC);
assert result.get(META).has(RecordService.SF_CONNECTOR_VERSION);

// test metadata configuration -- remove time stamp
metadataConfig = new SnowflakeMetadataConfig(createTimeConfig);
Expand All @@ -131,6 +150,7 @@ public void testConfig() throws IOException {
assert result.has(META);
assert !result.get(META).has(record.timestampType().name);
assert result.get(META).has(RecordService.TOPIC);
assert result.get(META).has(RecordService.SF_CONNECTOR_VERSION);
assert result.get(META).has(RecordService.OFFSET);
assert result.get(META).has(RecordService.PARTITION);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ public void testSchematizationArrayOfObject() throws JsonProcessingException {
service.setEnableSchematization(true);
String value =
"{\"players\":[{\"name\":\"John Doe\",\"age\":30},{\"name\":\"Jane Doe\",\"age\":30}]}";
byte[] valueContents = (value).getBytes(StandardCharsets.UTF_8);
String value2 =
"{\"cricket\":{\"team\":{\"MI\":{\"players\":[{\"name\":\"John"
+ " Doe\",\"age\":30},{\"name\":\"Jane Doe\",\"age\":30}]}}}}";
byte[] valueContents = (value2).getBytes(StandardCharsets.UTF_8);
Comment on lines +267 to +270
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need this?

SchemaAndValue sv = jsonConverter.toConnectData(topic, valueContents);

SinkRecord record =
Expand Down