Skip to content

feat: support multiple schema version for producer and consumer - #611

Merged
freeznet merged 8 commits into
apache:masterfrom
oryx2:multipleschemas
Jul 7, 2022
Merged

freeznet merged 8 commits into
apache:masterfrom
oryx2:multipleschemas

Conversation

@oryx2

@oryx2 oryx2 commented Sep 3, 2021

Copy link
Copy Markdown
Contributor

Motivation

Implement PIP-43.
Support multiple schema version for producer and consumer.

Modifications

  • add schema cache for producer and consumer
  • add DisableMultiSchema option for producer

Verifying this change

  • Make sure that the change passes the CI checks.

(Please pick either of the following options)

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: (yes)
  • The schema: (yes)
  • The default values of configurations: (yes)
  • The wire protocol: (no)

Documentation

  • Does this pull request introduce a new feature? (no)
  • If yes, how is the feature documented? (not applicable / docs / GoDocs / not documented)
  • If a feature is not applicable for documentation, explain why?
  • If a feature is not documented yet in this PR, please create a followup issue for adding the documentation

Comment thread pulsar/consumer_partition.go Outdated
}
}

func (s *schemaInfoCache) get(key string) (schema *Schema) {

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.

Let's remove the variable names here and below and return values from the functions. It makes the code more difficult to follow especially below since some returns have values and others do not.

Comment thread pulsar/impl_message.go Outdated
if err != nil {
return err
}
return (*schema).Decode(msg.payLoad, v)

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.

Why is casting needed here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks review. Because schemaCache store the pointer of schema interface, I have change to schema instance .

Comment thread pulsar/producer_partition.go Outdated
}

if p.options.DisableMultiSchema {
if msg.Schema == nil && p.options.Schema == nil && !msg.Schema.GetSchemaInfo().isSame((p.options.Schema).GetSchemaInfo()) {

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.

Can this be simplified or a comment added. I'm having a difficult time understanding.

@chiragbparikh

Copy link
Copy Markdown

Hello @cckellogg and @oryx2.
Can this PR be merged. It is a great feature and we are looking forward to using it. Thanks!

@chiragbparikh

Copy link
Copy Markdown

Hello @cckellogg and @oryx2. This is a gentle reminder to please review and merge this PR. As I mentioned above we are eagerly waiting for this feature

@lhotari

lhotari commented Jan 19, 2022

Copy link
Copy Markdown
Member

@chiragbparikh there are merge conflicts in this PR. please resolve them so that the PR can be reviewed

@oryx2

oryx2 commented Jan 19, 2022

Copy link
Copy Markdown
Contributor Author

@lhotari I just resolve them, please check.

@lhotari lhotari 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.

Good work @oryx2 !

2 whitespace issues caught my eye, other than that LGTM. I'm not a Golang expert, so could you @zzzming please take a look?

Comment thread pulsar/consumer_partition.go Outdated

log log.Logger

providersMutex sync.RWMutex

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.

whitespace

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.

Run go fmt to fix all formatting issues

Comment thread pulsar/producer.go Outdated
Comment on lines +169 to +170
// Disable multiple Schame Version
// Default false

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.

whitespace

@lhotari

lhotari commented Jan 19, 2022

Copy link
Copy Markdown
Member

@cckellogg PTAL

Comment thread pulsar/consumer_partition.go Outdated

log log.Logger

providersMutex sync.RWMutex

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.

Run go fmt to fix all formatting issues

Comment thread pulsar/consumer_partition.go Outdated
schema = s.cache[key]
s.lock.RUnlock()
if schema != nil {
return

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.

return schema nil

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.

Or
s.lock.RLock()
schema, ok = s.cache[key]
s.lock.RUnlock()
if ok {
return schema, nil
}

s.lock.Lock()
defer s.lock.Unlock()

s.cache[schemaVersionHash] = schema

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.

Do we allow schema version overwrite?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You are right, overwrite is not allowed. It is just a private function for SchemaInfoCache.Get which do the overwrite check.

Comment thread pulsar/consumer_partition.go Outdated
var properties = make(map[string]string)
if pbSchema.Properties != nil {
for _, entry := range pbSchema.Properties {
properties[*entry.Key] = properties[*entry.Value]

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.

since Key and Value are both declared as *string, do you need check the reference is not nil before assigning them to properties?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I found a function do the same job ConvertToStringMap, but no check nil. May be I should add nil check in ConvertToStringMap function, then invoker it.

Comment thread pulsar/internal/lookup_service.go Outdated
}

func (h *httpLookupService) GetSchema(topic string, schemaVersion []byte) (schema *pb.Schema, err error) {
return nil, errors.New("not support")

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.

Can we make this more descriptive? Maybe an error like this helps to debug.
errors.New("GetSchema is supported by httpLookupService")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I will add error message.

Comment thread pulsar/producer_partition.go Outdated
}

if p.options.DisableMultiSchema {
if msg.Schema != p.options.Schema {

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.

I am not sure if we want to compare the pointer or the actual value. Shema is an interface. So the current evaluation just make sure they are pointing to the same reference. Do you want to compare the value of Schema?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. Maybe I can compare the value of Schema with this.
msg.Schema != nil && p.options.Schema != nil && msg.Schema.GetSchemaInfo().hash() != p.options.Schema.GetSchemaInfo().hash()

@chiragbparikh

Copy link
Copy Markdown

@cckellogg @zzzming can we please review the changes on the PR. Thanks in advance.

@wolfstudy wolfstudy added this to the 0.9.0 milestone Feb 16, 2022
@chiragbparikh

Copy link
Copy Markdown

@cckellogg @wolfstudy can we please get this MR merged. We are eagerly waiting to use this feature.

@chiragbparikh

Copy link
Copy Markdown

@cckellogg can we please get a review on this MR? Appreciate it!

@RajatBanerjee

Copy link
Copy Markdown

This feature is something that we are eagerly awaiting.

@freeznet

freeznet commented Jul 4, 2022

Copy link
Copy Markdown
Contributor

@oryx2 could you please check the CI errors? thanks.

@oryx2

oryx2 commented Jul 4, 2022

Copy link
Copy Markdown
Contributor Author

@freeznet OK

@oryx2

oryx2 commented Jul 4, 2022

Copy link
Copy Markdown
Contributor Author

@freeznet OK

Sorry,I mad some syntax error when resolve conflicts.Already fixed.Thanks for review.

@freeznet

freeznet commented Jul 5, 2022

Copy link
Copy Markdown
Contributor

@oryx2 thanks, please check the CI failures again

@pomazanov

Copy link
Copy Markdown

I am curious why this PR is getting so little attention. If there is no need for FORWARD_TRANSITIVE strategy in golang client, what would be a good alternative?

@freeznet
freeznet merged commit ea6eccf into apache:master Jul 7, 2022
RobertIndie pushed a commit that referenced this pull request Jul 2, 2025
Master Issue: https://github.com/apache/pulsar/wiki/PIP-43%3A-producer-send-message-with-different-schema#changespart-1

Related pr #611

### Motivation
Currently pulsar go sdk has supported multi-version schema in above pr, but the pr does not support `getSchema()` method with http lookup service. So that we will encounter error when we call `msg.GetSchemaValue(v interface{}) error` function with http serviceUrl. Demo below:
```
func createClient() Client {
	// create client
	//lookupURL := "pulsar://localhost:6650"
	lookupURL := "http://localhost:8080"   // change to http protocol serviceUrl
	client, err := NewClient(ClientOptions{
		URL: lookupURL,
	})
	if err != nil {
		log.Fatal(err)
	}
	return client
}

func TestBytesSchema(t *testing.T) {
	client := createClient()
	defer client.Close()

	topic := newTopicName()

	properties := make(map[string]string)
	properties["pulsar"] = "hello"
	producerSchemaBytes := NewBytesSchema(properties)
	producer, err := client.CreateProducer(ProducerOptions{
		Topic:  topic,
		Schema: producerSchemaBytes,
	})
	assert.NoError(t, err)

	_, err = producer.Send(context.Background(), &ProducerMessage{
		Value: []byte(`{"key": "value"}`),
	})
	require.NoError(t, err)
	producer.Close()

	// Create consumer
	consumerSchemaBytes := NewBytesSchema(nil)
	assert.NotNil(t, consumerSchemaBytes)
	consumer, err := client.Subscribe(ConsumerOptions{
		Topic:                       topic,
		SubscriptionName:            "sub-1",
		Schema:                      consumerSchemaBytes,
		SubscriptionInitialPosition: SubscriptionPositionEarliest,
	})
	assert.Nil(t, err)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()

	// Receive first message
	var out1 []byte
	msg1, err := consumer.Receive(ctx)
	assert.NoError(t, err)
	err = msg1.GetSchemaValue(&out1)
	assert.NoError(t, err)
	assert.Equal(t, []byte(`{"key": "value"}`), out1)
	consumer.Ack(msg1)
	require.NoError(t, err)
}
```
Test output:
```
/root/go/pkg/mod/golang.org/[email protected]/bin/go tool test2json -t /root/.cache/JetBrains/GoLand2024.1/tmp/GoLand/___TestBytesSchema_in_github_com_apache_pulsar_client_go_pulsar.test -test.v -test.paniconexit0 -test.run ^\QTestBytesSchema\E$
=== RUN   TestBytesSchema
time="2025-05-14T16:32:45+08:00" level=info msg="Connecting to broker" remote_addr="pulsar://localhost:6650"
time="2025-05-14T16:32:45+08:00" level=info msg="TCP connection established" local_addr="127.0.0.1:36638" remote_addr="pulsar://localhost:6650"
time="2025-05-14T16:32:45+08:00" level=info msg="Connection is ready" local_addr="127.0.0.1:36638" remote_addr="pulsar://localhost:6650"
time="2025-05-14T16:32:45+08:00" level=info msg="Connected producer" cnx="127.0.0.1:36638 -> 127.0.0.1:6650" epoch=0 topic="persistent://public/default/my-topic-147368803"
time="2025-05-14T16:32:45+08:00" level=info msg="Created producer" cnx="127.0.0.1:36638 -> 127.0.0.1:6650" producerID=1 producer_name=standalone-42-1 topic="persistent://public/default/my-topic-147368803"
time="2025-05-14T16:32:45+08:00" level=info msg="Closing producer" producerID=1 producer_name=standalone-42-1 topic="persistent://public/default/my-topic-147368803"
time="2025-05-14T16:32:45+08:00" level=info msg="Closed producer" producerID=1 producer_name=standalone-42-1 topic="persistent://public/default/my-topic-147368803"
time="2025-05-14T16:32:45+08:00" level=info msg="Connected consumer" consumerID=1 name=yzway subscription=sub-1 topic="persistent://public/default/my-topic-147368803"
time="2025-05-14T16:32:45+08:00" level=info msg="Created consumer" consumerID=1 name=yzway subscription=sub-1 topic="persistent://public/default/my-topic-147368803"
    schema_test.go:101: 
        	Error Trace:	/data/code/dev/pulsar-client-go/pulsar/schema_test.go:101
        	Error:      	Received unexpected error:
        	            	GetSchema is not supported by httpLookupService
        	Test:       	TestBytesSchema
    schema_test.go:102: 
        	Error Trace:	/data/code/dev/pulsar-client-go/pulsar/schema_test.go:102
        	Error:      	Not equal: 
        	            	expected: []byte{0x7b, 0x22, 0x6b, 0x65, 0x79, 0x22, 0x3a, 0x20, 0x22, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x7d}
        	            	actual  : []byte(nil)
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1,4 +1,2 @@
        	            	-([]uint8) (len=16) {
        	            	- 00000000  7b 22 6b 65 79 22 3a 20  22 76 61 6c 75 65 22 7d  |{"key": "value"}|
        	            	-}
        	            	+([]uint8) <nil>
        	            	 
        	Test:       	TestBytesSchema
    schema_test.go:104: 
        	Error Trace:	/data/code/dev/pulsar-client-go/pulsar/schema_test.go:104
        	Error:      	Received unexpected error:
        	            	GetSchema is not supported by httpLookupService
        	Test:       	TestBytesSchema
time="2025-05-14T16:32:45+08:00" level=info msg="Closing consumer=1" consumerID=1 name=yzway subscription=sub-1 topic="persistent://public/default/my-topic-147368803"
time="2025-05-14T16:32:45+08:00" level=info msg="Closed consumer" consumerID=1 name=yzway subscription=sub-1 topic="persistent://public/default/my-topic-147368803"
--- FAIL: TestBytesSchema (0.12s)

Expected :[]byte{0x7b, 0x22, 0x6b, 0x65, 0x79, 0x22, 0x3a, 0x20, 0x22, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x7d}
Actual   :[]byte(nil)
```

### Modifications
- Update LookupService interface return type from `GetSchema(topic string, schemaVersion []byte) (schema *pb.Schema, err error)` to `GetSchema(topic string, schemaVersion []byte) (*LookupSchema, error)` to support http lookup protocol in `lookup_service.go`
- Support HTTPLookupService `GetSchema(topic string, schemaVersion []byte) (*LookupSchema, error)` function in `lookup_service.go`
- Add http lookup `GetSchema()` related test cases in schema_test.go
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