Skip to content

Commit ab042ae

Browse files
authored
[fix] peek message will return -1 for partitionIndex (#1267)
### Motivation If peek a partitioned topic, will see a message id: `7316:0:-1:-1`, the parititonIndex should not be -1. ``` pulsarctl subscription peek --count 10 persistent://public/default/my-topic-partition-0 test-sub Message ID : 7316:0:-1:-1 Properties : { "publish-time": "2024-08-08T17:50:39.476+08:00" } Message : 00000000 68 65 6c 6c 6f 2d 31 |hello-1| ``` ### Modifications - Set partition index on peek message.
1 parent 6a2e461 commit ab042ae

4 files changed

Lines changed: 69 additions & 1 deletion

File tree

pulsaradmin/pkg/admin/subscription.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,9 @@ const (
234234
)
235235

236236
func handleResp(topic utils.TopicName, resp *http.Response) ([]*utils.Message, error) {
237+
237238
msgID := resp.Header.Get("X-Pulsar-Message-ID")
238-
ID, err := utils.ParseMessageID(msgID)
239+
ID, err := utils.ParseMessageIDWithPartitionIndex(msgID, topic.GetPartitionIndex())
239240
if err != nil {
240241
return nil, err
241242
}

pulsaradmin/pkg/admin/subscription_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,60 @@ func TestGetMessagesByID(t *testing.T) {
9090

9191
}
9292

93+
func TestPeekMessageForPartitionedTopic(t *testing.T) {
94+
ctx := context.Background()
95+
randomName := newTopicName()
96+
topic := "persistent://public/default/" + randomName
97+
topicName, _ := utils.GetTopicName(topic)
98+
subName := "test-sub"
99+
100+
cfg := &config.Config{}
101+
admin, err := New(cfg)
102+
assert.NoError(t, err)
103+
assert.NotNil(t, admin)
104+
105+
err = admin.Topics().Create(*topicName, 2)
106+
assert.NoError(t, err)
107+
108+
err = admin.Subscriptions().Create(*topicName, subName, utils.Earliest)
109+
assert.NoError(t, err)
110+
111+
client, err := pulsar.NewClient(pulsar.ClientOptions{
112+
URL: lookupURL,
113+
})
114+
assert.NoError(t, err)
115+
defer client.Close()
116+
117+
producer, err := client.CreateProducer(pulsar.ProducerOptions{
118+
Topic: topic,
119+
DisableBatching: true,
120+
})
121+
assert.NoError(t, err)
122+
defer producer.Close()
123+
124+
for i := 0; i < 100; i++ {
125+
producer.SendAsync(ctx, &pulsar.ProducerMessage{
126+
Payload: []byte(fmt.Sprintf("hello-%d", i)),
127+
}, nil)
128+
}
129+
err = producer.Flush()
130+
if err != nil {
131+
return
132+
}
133+
134+
for i := 0; i < 2; i++ {
135+
topicWithPartition := fmt.Sprintf("%s-partition-%d", topic, i)
136+
topicName, err := utils.GetTopicName(topicWithPartition)
137+
assert.NoError(t, err)
138+
messages, err := admin.Subscriptions().PeekMessages(*topicName, subName, 10)
139+
assert.NoError(t, err)
140+
assert.NotNil(t, messages)
141+
for _, msg := range messages {
142+
assert.Equal(t, msg.GetMessageID().PartitionIndex, i)
143+
}
144+
}
145+
}
146+
93147
func TestGetMessageByID(t *testing.T) {
94148
randomName := newTopicName()
95149
topic := "persistent://public/default/" + randomName

pulsaradmin/pkg/utils/message_id.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ type MessageID struct {
3434
var Latest = MessageID{0x7fffffffffffffff, 0x7fffffffffffffff, -1, -1}
3535
var Earliest = MessageID{-1, -1, -1, -1}
3636

37+
func ParseMessageIDWithPartitionIndex(str string, index int) (*MessageID, error) {
38+
id, err := ParseMessageID(str)
39+
if err != nil {
40+
return nil, err
41+
}
42+
id.PartitionIndex = index
43+
return id, nil
44+
}
45+
3746
func ParseMessageID(str string) (*MessageID, error) {
3847
s := strings.Split(str, ":")
3948

pulsaradmin/pkg/utils/topic_name.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ func (t *TopicName) GetPartition(index int) (*TopicName, error) {
143143
return GetTopicName(topicNameWithPartition)
144144
}
145145

146+
func (t *TopicName) GetPartitionIndex() int {
147+
return t.partitionIndex
148+
}
149+
146150
func getPartitionIndex(topic string) int {
147151
if strings.Contains(topic, PARTITIONEDTOPICSUFFIX) {
148152
parts := strings.Split(topic, "-")

0 commit comments

Comments
 (0)