Skip to content

Support seting topic schema when creating producers/consumers - #81

Closed
lcartercondon wants to merge 3 commits into
apache:masterfrom
lcartercondon:master
Closed

lcartercondon wants to merge 3 commits into
apache:masterfrom
lcartercondon:master

Conversation

@lcartercondon

Copy link
Copy Markdown

Added class to allow setting topic schema when creating
producers/consumers

Added class to allow setting topic schema when creating
producers/consumers
Comment thread src/ProducerConfig.cc Outdated
Comment on lines +36 to +40
static const std::string CFG_SCHEMA = "schema";
static const std::string CFG_SCHEMA_TYPE = "schemaType";
static const std::string CFG_SCHEMA_NAME = "name";
static const std::string CFG_SCHEMA_SCHEMA = "schema";
static const std::string CFG_SCHEMA_PROPS = "properties";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
static const std::string CFG_SCHEMA = "schema";
static const std::string CFG_SCHEMA_TYPE = "schemaType";
static const std::string CFG_SCHEMA_NAME = "name";
static const std::string CFG_SCHEMA_SCHEMA = "schema";
static const std::string CFG_SCHEMA_PROPS = "properties";
static const std::string CFG_SCHEMA = "schema";

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yup, thanks for the heads up on cleanup.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 7a29a67

Comment thread src/SchemaInfo.h Outdated
Comment on lines +31 to +34
// pulsar_schema_type GetSchemaType();
// std::string GetName();
// std::string GetSchema();
// pulsar_string_map_t GetProperties();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
// pulsar_schema_type GetSchemaType();
// std::string GetName();
// std::string GetSchema();
// pulsar_string_map_t GetProperties();

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 7a29a67

@sijie sijie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems that this pull request more focused on passing the schema info to the cpp client. but how does nodejs application use this feature? Can you show an example?

@massakam

Copy link
Copy Markdown
Contributor

Even if we can set the schema information, the current Node.js client can handle message payloads only with Buffer type.

pulsar_message_t *Message::BuildMessage(Napi::Object conf) {
pulsar_message_t *cMessage = pulsar_message_create();
if (conf.Has(CFG_DATA) && conf.Get(CFG_DATA).IsBuffer()) {
Napi::Buffer<char> buf = conf.Get(CFG_DATA).As<Napi::Buffer<char>>();
char *data = buf.Data();
pulsar_message_set_content(cMessage, data, buf.Length());
}

Napi::Value Message::GetData(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
if (!ValidateCMessage(env)) {
return env.Null();
}
void *data = const_cast<void *>(pulsar_message_get_data(this->cMessage));
size_t size = (size_t)pulsar_message_get_length(this->cMessage);
return Napi::Buffer<char>::New(env, (char *)data, size);
}

@lcartercondon

Copy link
Copy Markdown
Author

This simply allows the NodeJS client to specify the schema when creating the producer or consumer - it doesn't go to the same extent that, say, the Python client does where passing an object (to the producer's send) causes that object to get encoded using the schema info or where the subscriber decodes the message payload to a value using the schema info.

I'm planning on adding the automatic encode/decode parts in a similar fashion to how the Python client does it i.e. by wrapping the Producer/Consumer/Message bindings in language-native classes that add information regarding schema and support for automatic encode/decode, but I'm not quite sure yet of the exact implementation.

@lcartercondon

Copy link
Copy Markdown
Author

@massakam I think this is fine - any user of a schema'd topic is (with this implementation) still responsible for serializing to Buffer when sending messages or deserializing from Buffer into whatever schema when consuming messages.

@massakam

massakam commented Mar 25, 2020 •

Copy link
Copy Markdown
Contributor

I think this is fine - any user of a schema'd topic is (with this implementation) still responsible for serializing to Buffer when sending messages or deserializing from Buffer into whatever schema when consuming messages.

Once users implement such an application, isn't it a breaking change for them to support automatic encoding/decoding?

const buffer = /* object serialization> */;
producer.send({
  data: buffer,
});

↓

const obj = {
  key1: "val1",
  key2: "val2",
};
producer.send({
  data: obj,
});

If so, I think parts for automatic encoding/decoding should be added in this PR or as soon as possible.

@lcartercondon

Copy link
Copy Markdown
Author

isn't it a breaking change for them to support automatic encoding/decoding?

This is a great point - I hadn't thought about it yet. I could work around it in producer.send by skipping the automatic encode if data is of a buffer type, or make automatic encode opt-in either during producer creation or on each send call.

It's definitely a bit more tricky for the message handling function in the consumer. Would it be reasonable to do something along the lines of what the Python lib does? It assigns the decoded message.data to a message.value attribute. Alternatively, could make the automatic decode opt-in during consumer creation.

@massakam

Copy link
Copy Markdown
Contributor

In the case of Python and Go libraries, it seems that binary data and deserialized data can be obtained from different fields of the message object. If the Node.js library follows this, it won't be a breaking change.
https://github.com/apache/pulsar/blob/d5036eaefcba42e6c9d159ffb14bee597d13380f/pulsar-client-go/pulsar/message.go#L47-L75

@massakam massakam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@ludusrusso

Copy link
Copy Markdown

Any news about this? It would be great to have schemas in nodejs! Can I help to complete the PR?

@lcartercondon

Copy link
Copy Markdown
Author

Any news about this? It would be great to have schemas in nodejs! Can I help to complete the PR?

I've not taken a look at this in quite some time unfortunately - I was working on this as part of related work at a prior employer. I'll take a look at this again this weekend, but cannot make any guarantees as to completing the requested changes.

Contributions that complete the requests are absolutely welcome, I believe all that's left is:

  1. provide example documentation of how a NodeJS client would use this feature.

There has been some chatter in this PR re adding automatic encode/decode support, similar to how Python and Go client libs do it. Contributions that add that would be above and beyond but are absolutely welcome as well.

I'll update this weekend if I'm able to accomplish the requested changes and/or automatic encode/decode support feature request.

@ludusrusso

Copy link
Copy Markdown

@lcartercondon hi, thanks for the reply.
If it's ok for you, I can continuo working on the two request:
documentation and auto encode/decode, that wold be super helpful for my use case!

Alternatively, you can close this PR with docs and I can submit an new PR with audo encode/decode!

@tschmidt64-SC

Copy link
Copy Markdown

Any updates on this? Seems like a really useful feature. As I understand it, without schema support, Pulsar SQL is basically non-functional without this feature. Am I correct in that understanding?

@thomasechen

thomasechen commented Jun 11, 2022 •

Copy link
Copy Markdown

@sijie Any update on this request ? It is really important to the world because numbers of nodejs developer become more and more now

@k2la

k2la commented Jun 16, 2022

Copy link
Copy Markdown
Contributor

@lcartercondon Could you please rebase to the master branch?

@massakam

Copy link
Copy Markdown
Contributor

I've resolved the conflict, but the current code fails to compile.

Comment thread src/SchemaInfo.cc
Comment on lines +68 to +69
void SchemaInfo::SetProducerSchema(pulsar_producer_configuration_t *cProducerConfiguration) {
pulsar_producer_configuration_set_schema_info(cProducerConfiguration, this->cSchemaType, this->name.c_str(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
void SchemaInfo::SetProducerSchema(pulsar_producer_configuration_t *cProducerConfiguration) {
pulsar_producer_configuration_set_schema_info(cProducerConfiguration, this->cSchemaType, this->name.c_str(),
void SchemaInfo::SetProducerSchema(std::shared_ptr<pulsar_producer_configuration_t> cProducerConfiguration) {
pulsar_producer_configuration_set_schema_info(cProducerConfiguration.get(), this->cSchemaType, this->name.c_str(),

Comment thread src/SchemaInfo.cc
Comment on lines +73 to +74
void SchemaInfo::SetConsumerSchema(pulsar_consumer_configuration_t *cConsumerConfiguration) {
pulsar_consumer_configuration_set_schema_info(cConsumerConfiguration, this->cSchemaType, this->name.c_str(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
void SchemaInfo::SetConsumerSchema(pulsar_consumer_configuration_t *cConsumerConfiguration) {
pulsar_consumer_configuration_set_schema_info(cConsumerConfiguration, this->cSchemaType, this->name.c_str(),
void SchemaInfo::SetConsumerSchema(std::shared_ptr<pulsar_consumer_configuration_t> cConsumerConfiguration) {
pulsar_consumer_configuration_set_schema_info(cConsumerConfiguration.get(), this->cSchemaType, this->name.c_str(),

Comment thread src/SchemaInfo.h
public:
SchemaInfo(const Napi::Object &schemaInfo);
~SchemaInfo();
void SetProducerSchema(pulsar_producer_configuration_t *cProducerConfiguration);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
void SetProducerSchema(pulsar_producer_configuration_t *cProducerConfiguration);
void SetProducerSchema(std::shared_ptr<pulsar_producer_configuration_t> cProducerConfiguration);

Comment thread src/SchemaInfo.h
SchemaInfo(const Napi::Object &schemaInfo);
~SchemaInfo();
void SetProducerSchema(pulsar_producer_configuration_t *cProducerConfiguration);
void SetConsumerSchema(pulsar_consumer_configuration_t *cConsumerConfiguration);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
void SetConsumerSchema(pulsar_consumer_configuration_t *cConsumerConfiguration);
void SetConsumerSchema(std::shared_ptr<pulsar_consumer_configuration_t> cConsumerConfiguration);

@massakam

Copy link
Copy Markdown
Contributor

@lcartercondon Could you fix the compilation error?

@nkurihar

nkurihar commented Jul 5, 2022

Copy link
Copy Markdown
Contributor

This change is merged as a new PR with fixing some compliation errors:
#223

@lcartercondon Thank you for your contribution!

@nkurihar nkurihar closed this Jul 5, 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.

9 participants