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

Compatible the HTTP header properties with PIP-279 #1299

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 14 additions & 1 deletion pulsaradmin/pkg/admin/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package admin
import (
"bytes"
"encoding/binary"
"encoding/json"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -230,7 +231,14 @@ func safeRespClose(resp *http.Response) {
const (
PublishTimeHeader = "X-Pulsar-Publish-Time"
BatchHeader = "X-Pulsar-Num-Batch-Message"
PropertyPrefix = "X-Pulsar-Property-"

// PropertyPrefix is part of the old protocol for message properties.
PropertyPrefix = "X-Pulsar-Property-"

// PropertyHeader is part of the new protocol introduced in SNIP-279
// https://github.com/apache/pulsar/pull/20627
// The value is a JSON string representing the properties.
PropertyHeader = "X-Pulsar-Property"
)

func handleResp(topic utils.TopicName, resp *http.Response) ([]*utils.Message, error) {
Expand Down Expand Up @@ -261,6 +269,11 @@ func handleResp(topic utils.TopicName, resp *http.Response) ([]*utils.Message, e
properties[BatchHeader] = h
}
return getIndividualMsgsFromBatch(topic, ID, payload, properties)
case k == PropertyHeader:
propJSON := resp.Header.Get(k)
if err := json.Unmarshal([]byte(propJSON), &properties); err != nil {
return nil, err
}
case strings.Contains(k, PropertyPrefix):
key := strings.TrimPrefix(k, PropertyPrefix)
properties[key] = resp.Header.Get(k)
Expand Down
52 changes: 52 additions & 0 deletions pulsaradmin/pkg/admin/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,58 @@ func TestPeekMessageForPartitionedTopic(t *testing.T) {
}
}

func TestPeekMessageWithProperties(t *testing.T) {
randomName := newTopicName()
topic := "persistent://public/default/" + randomName
topicName, _ := utils.GetTopicName(topic)
subName := "test-sub"

cfg := &config.Config{}
admin, err := New(cfg)
assert.NoError(t, err)
assert.NotNil(t, admin)

client, err := pulsar.NewClient(pulsar.ClientOptions{
URL: lookupURL,
})
assert.NoError(t, err)
defer client.Close()

// Create a producer for non-batch messages
producer, err := client.CreateProducer(pulsar.ProducerOptions{
Topic: topic,
DisableBatching: true,
})
assert.NoError(t, err)
defer producer.Close()

props := map[string]string{
"key1": "value1",
"KEY2": "VALUE2",
"KeY3": "VaLuE3",
"details=man": "good at playing basketball",
}

_, err = producer.Send(context.Background(), &pulsar.ProducerMessage{
Payload: []byte("test-message"),
Properties: props,
})
assert.NoError(t, err)

// Peek messages
messages, err := admin.Subscriptions().PeekMessages(*topicName, subName, 1)
assert.NoError(t, err)
assert.NotNil(t, messages)

// Verify properties of messages
for _, msg := range messages {
assert.Equal(t, "value1", msg.Properties["key1"])
assert.Equal(t, "VALUE2", msg.Properties["KEY2"])
assert.Equal(t, "VaLuE3", msg.Properties["KeY3"])
assert.Equal(t, "good at playing basketball", msg.Properties["details=man"])
}
}

func TestGetMessageByID(t *testing.T) {
randomName := newTopicName()
topic := "persistent://public/default/" + randomName
Expand Down
Loading