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

[Schema] Fix AutoConsumeSchema decode null schema version data #10811

Merged

Conversation

gaoran10
Copy link
Contributor

@gaoran10 gaoran10 commented Jun 3, 2021

Motivation

Currently, the AutoConsumeSchema decode messages which has a null schema version will cause the NPE problem.

java.lang.NullPointerException: null
	at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936) ~[?:1.8.0_181]
	at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:964) ~[?:1.8.0_181]
	at org.apache.pulsar.client.impl.schema.AutoConsumeSchema.ensureSchemaInitialized(AutoConsumeSchema.java:73) ~[io.streamnative-pulsar-client-original-2.8.0-rc-202105291235.jar:2.8.0-rc-202105291235]
	at org.apache.pulsar.client.impl.schema.AutoConsumeSchema.decode(AutoConsumeSchema.java:110) ~[io.streamnative-pulsar-client-original-2.8.0-rc-202105291235.jar:2.8.0-rc-202105291235]
	at org.apache.pulsar.client.impl.schema.AutoConsumeSchema.decode(AutoConsumeSchema.java:45) ~[io.streamnative-pulsar-client-original-2.8.0-rc-202105291235.jar:2.8.0-rc-202105291235]
	at org.apache.pulsar.client.api.Schema.decode(Schema.java:107) ~[io.streamnative-pulsar-client-api-2.8.0-rc-202105291235.jar:2.8.0-rc-202105291235]
	at org.apache.pulsar.client.impl.MessageImpl.decode(MessageImpl.java:469) ~[io.streamnative-pulsar-client-original-2.8.0-rc-202105291235.jar:2.8.0-rc-202105291235]
	at org.apache.pulsar.client.impl.MessageImpl.getValue(MessageImpl.java:449) ~[io.streamnative-pulsar-client-original-2.8.0-rc-202105291235.jar:2.8.0-rc-202105291235]
	at org.apache.pulsar.functions.source.PulsarRecord.getValue(PulsarRecord.java:81) ~[io.streamnative-pulsar-functions-instance-2.8.0-rc-202105291235.jar:2.8.0-rc-202105291235]
	at org.apache.pulsar.functions.instance.JavaInstanceRunnable.readInput(JavaInstanceRunnable.java:386) ~[io.streamnative-pulsar-functions-instance-2.8.0-rc-202105291235.jar:2.8.0-rc-202105291235]
	at org.apache.pulsar.functions.instance.JavaInstanceRunnable.run(JavaInstanceRunnable.java:248) ~[io.streamnative-pulsar-functions-instance-2.8.0-rc-202105291235.jar:2.8.0-rc-202105291235]
	at java.lang.Thread.run(Thread.java:748) ~[?:1.8.0_181]

Modifications

Check the schema version, if the schema version is null, fallback uses the BYTES schema to decode.

Verifying this change

Test decode message data which has a null schema version.

Does this pull request potentially affect one of the following parts:

If yes was chosen, please highlight the changes

  • Dependencies (does it add or upgrade a dependency): (no)
  • The public API: (no)
  • The schema: (no)
  • The default values of configurations: (no)
  • The wire protocol: (no)
  • The rest endpoints: (no)
  • The admin cli options: (no)
  • Anything that affects deployment: (no)

@@ -308,13 +311,26 @@ public void fetchSchemaIfNeeded(SchemaVersion schemaVersion) throws SchemaSerial
}
}

private SchemaVersion getSchemaVersion(byte[] schemaVersion) {
Copy link
Contributor

Choose a reason for hiding this comment

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

static ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does the static method have some advantage?

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably not a big deal from a performance perspective.
But if a method does not use state from the instance it is better to mark it 'static'.

Copy link
Contributor Author

@gaoran10 gaoran10 Jun 4, 2021

Choose a reason for hiding this comment

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

Ok, I'll fix this, thanks.

byte[] bytes = "bytes data".getBytes();
MessageImpl<GenericRecord> message = MessageImpl.create(
new MessageMetadata(), ByteBuffer.wrap(bytes), autoConsumeSchema);
Assert.assertNull(message.getSchemaVersion());
Copy link
Contributor

Choose a reason for hiding this comment

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

how can it happen that we receive a message with null schemaVersion ?
Autoconsume is meant to be used only on the Consumer side, on a real message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If using Python client produces messages without specifying a schema, the schema version will be null.

Copy link
Contributor

Choose a reason for hiding this comment

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

Very interesting.
I didn't know

@sijie sijie added this to the 2.8.0 milestone Jun 3, 2021
@eolivelli
Copy link
Contributor

@sijie AutoConsumeSchema code is very different in branch-2.7.
This patch cannot be cherry picked.
We should open a new PR if you want this fix for 2.7.3

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

@eolivelli eolivelli merged commit d8567a8 into apache:master Jun 4, 2021
@codelipenghui
Copy link
Contributor

@sijie I have moved the label release/2.7.3 since the issue is only happens in the master branch

@gaoran10 gaoran10 deleted the auto-consume-schema-deocde-null-schema branch June 7, 2021 01:07
eolivelli pushed a commit to datastax/pulsar that referenced this pull request Jun 7, 2021
bharanic-dev pushed a commit to bharanic-dev/pulsar that referenced this pull request Mar 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants