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

[fix][io] KCA sink: handle null values with KeyValue<Avro,Avro> schema #19861

Merged
merged 4 commits into from
Mar 21, 2023

Conversation

nicoloboschi
Copy link
Contributor

Motivation

When using a KCA based sink connector with a topic with schema KeyValue<Avro, Avro>, if a message has a null value, the connector throws:

023-03-15T08:00:22,139+0000 [public/default/sink] ERROR org.apache.pulsar.io.kafka.connect.KafkaConnectSink - Error sending the record SinkRecord(sourceRecord=PulsarRecord(topicName=Optional[persistent://public/default/data-baselines.keyvalue_short_tests], partition=0, message=Optional[org.apache.pulsar.client.impl.MessageImpl@2ddf2db4], schema=KeyValueSchema(SEPARATED,org.apache.pulsar.client.impl.schema.generic.GenericAvroSchema@33df2fa0,org.apache.pulsar.client.impl.schema.generic.GenericAvroSchema@359ba6c9), failFunction=org.apache.pulsar.functions.source.PulsarSource$$Lambda$264/0x00000008404bb440@227fd2ce, ackFunction=org.apache.pulsar.functions.source.PulsarSource$$Lambda$263/0x00000008404bb040@7619e9b8), value=(key = "org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord@3c717af4", value = "null"))
org.apache.kafka.connect.errors.DataException: Invalid null value for required STRUCT field
	at org.apache.pulsar.io.kafka.connect.schema.KafkaConnectData.defaultOrThrow(KafkaConnectData.java:385) ~[pulsar-io-kafka-connect-adaptor-2.10.3.5.jar:2.10.3.5]
	at org.apache.pulsar.io.kafka.connect.schema.KafkaConnectData.getKafkaConnectData(KafkaConnectData.java:66) ~[pulsar-io-kafka-connect-adaptor-2.10.3.5.jar:2.10.3.5]
	at org.apache.pulsar.io.kafka.connect.KafkaConnectSink.toSinkRecord(KafkaConnectSink.java:442) ~[pulsar-io-kafka-connect-adaptor-2.10.3.5.jar:2.10.3.5]
	at org.apache.pulsar.io.kafka.connect.KafkaConnectSink.write(KafkaConnectSink.java:125) ~[pulsar-io-kafka-connect-adaptor-2.10.3.5.jar:2.10.3.5]
	at org.apache.pulsar.functions.instance.JavaInstanceRunnable.sendOutputMessage(JavaInstanceRunnable.java:424) ~[?:?]
	at org.apache.pulsar.functions.instance.JavaInstanceRunnable.handleResult(JavaInstanceRunnable.java:394) ~[?:?]
	at org.apache.pulsar.functions.instance.JavaInstanceRunnable.run(JavaInstanceRunnable.java:335) ~[?:?]
	at java.lang.Thread.run(Thread.java:829) ~[?:?]

Modifications

Handle null value conversion to the kafka value

Verifying this change

  • Make sure that the change passes the CI checks.

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

(cherry picked from commit f3747a1978c539c3f5a5f408988b9cc9125471a9)
@github-actions github-actions bot added the doc-not-needed Your PR changes do not impact docs label Mar 20, 2023
Copy link
Contributor

@eolivelli eolivelli left a comment

Choose a reason for hiding this comment

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

Lgtm
Nice catch

@nicoloboschi nicoloboschi self-assigned this Mar 20, 2023
@nicoloboschi nicoloboschi added this to the 3.0.0 milestone Mar 20, 2023
@dlg99
Copy link
Contributor

dlg99 commented Mar 20, 2023

Looking at this a bit deeper:
This should cover the specific issue we have right now.
However, I think the rootcause is that pulsar does not have "optional" marker in the schema.
In kafka's schema there is .isOptional(), in avro it is done via union with null type "type": [ "null", "string" ],
In pulsar it is lost.

So the fix will hack this for the top level key value schema, but the issue may reappear if the schema is generated for something like

Schema.KeyValue(Schema.INT32, Schema.KeyValue(Schema.INT32, Schema.KeyValue(Schema.INT32, schema)))

and the nested value is null.

We may need to remove "if (kafkaSchema.isOptional())" check in KafkaConnectData.defaultOrThrow()

@eolivelli
Copy link
Contributor

actually in Pulsar a KeyValue schema allows null for both the key and the value.

@dlg99 in Pulsar it is not allowed to nest KeyValue schemas. KeyValue schema is a (bad) trick to provide a Schema to the key of the message.

any usage of nested KeyValue schemas is not supported (we may issue a warning or throw an error in Pulsar 3.0) and it cannot work properly

so this schema doesn't work
Schema.KeyValue(Schema.INT32, Schema.KeyValue(Schema.INT32, Schema.KeyValue(Schema.INT32, schema)))

@dlg99
Copy link
Contributor

dlg99 commented Mar 20, 2023

actually in Pulsar a KeyValue schema allows null for both the key and the value.

@dlg99 in Pulsar it is not allowed to nest KeyValue schemas. KeyValue schema is a (bad) trick to provide a Schema to the key of the message.

any usage of nested KeyValue schemas is not supported (we may issue a warning or throw an error in Pulsar 3.0) and it cannot work properly

so this schema doesn't work Schema.KeyValue(Schema.INT32, Schema.KeyValue(Schema.INT32, Schema.KeyValue(Schema.INT32, schema)))

that's fine, nest any other pulsar schema.

The problem is in the loss of optional flag in nested schema in translation form kafka schema to pulsar (source -> topic -> transform -> sink chain)

@@ -80,6 +153,11 @@ private static org.apache.avro.Schema parseAvroSchema(String schemaJson) {
return parser.parse(schemaJson);
}

public static Schema getOptionalKafkaConnectSchema(org.apache.pulsar.client.api.Schema pulsarSchema) {
Schema s = getKafkaConnectSchema(pulsarSchema);
return new OptionalForcingSchema(s);
Copy link
Contributor

@dlg99 dlg99 Mar 20, 2023

Choose a reason for hiding this comment

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

the only place it is used is within cache's get() call:

            return schemaCache.get(pulsarSchema.getSchemaInfo().getSchema(), () -> {
                if (pulsarSchema.getSchemaInfo().getType() == SchemaType.KEY_VALUE) {
                    KeyValueSchema kvSchema = (KeyValueSchema) pulsarSchema;
                    return SchemaBuilder.map(getKafkaConnectSchema(kvSchema.getKeySchema()),
                                    getOptionalKafkaConnectSchema(kvSchema.getValueSchema()))
                                .build();
                }

Copy link
Contributor

@eolivelli eolivelli left a comment

Choose a reason for hiding this comment

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

LGTM

@dlg99 dlg99 merged commit 03f8b80 into apache:master Mar 21, 2023
dlg99 pushed a commit to dlg99/pulsar that referenced this pull request Mar 21, 2023
apache#19861)

Co-authored-by: Andrey Yegorov <andrey.yegorov@datastax.com>
(cherry picked from commit 03f8b80)
dlg99 added a commit to datastax/pulsar that referenced this pull request Mar 21, 2023
apache#19861) (#169)

Co-authored-by: Andrey Yegorov <andrey.yegorov@datastax.com>
(cherry picked from commit 03f8b80)

Co-authored-by: Nicolò Boschi <boschi1997@gmail.com>
dlg99 pushed a commit that referenced this pull request Mar 22, 2023
#19861)

Co-authored-by: Andrey Yegorov <andrey.yegorov@datastax.com>
(cherry picked from commit 03f8b80)
@nicoloboschi nicoloboschi deleted the kca-null-value branch March 23, 2023 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants