-
Notifications
You must be signed in to change notification settings - Fork 5
/
pulsar_util.go
152 lines (141 loc) · 5.2 KB
/
pulsar_util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package utils
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/gogo/protobuf/proto"
"github.com/paashzj/kafka_go_pulsar/pkg/constant"
"github.com/paashzj/kafka_go_pulsar/pkg/model"
"github.com/protocol-laboratory/pulsar-codec-go/pb"
"github.com/sirupsen/logrus"
"strings"
"time"
)
func PartitionedTopic(topic string, partition int) string {
return topic + fmt.Sprintf(constant.PartitionSuffixFormat, partition)
}
func ReadEarliestMsg(partitionedTopic string, maxWaitMs int, pulsarClient pulsar.Client) (pulsar.Message, error) {
readerOptions := pulsar.ReaderOptions{
Topic: partitionedTopic,
Name: constant.OffsetReaderEarliestName,
StartMessageID: pulsar.EarliestMessageID(),
}
return readNextMsg(readerOptions, maxWaitMs, pulsarClient)
}
func GetLatestMsgId(partitionedTopic, addr string) (msg []byte, err error) {
tenant, namespace, shortPartitionedTopic, err := getTenantNamespaceTopicFromPartitionedTopic(partitionedTopic)
if err != nil {
logrus.Errorf("get tenant and namespace failed. topic: %s, err: %s", partitionedTopic, err)
return nil, err
}
urlFormat := addr + constant.LastMsgIdUrl
url := fmt.Sprintf(urlFormat, tenant, namespace, shortPartitionedTopic)
msg, err = HttpGet(url, nil, nil)
if err != nil {
logrus.Errorf("unmarshal message id failed., topic: %s, err: %s", partitionedTopic, err)
return nil, err
}
return msg, nil
}
func ReadLastedMsg(partitionedTopic string, maxWaitMs int, msgIdBytes []byte, pulsarClient pulsar.Client) (pulsar.Message, error) {
var msgId pulsar.MessageID
bytes, err := generateMsgBytes(msgIdBytes)
if err != nil {
logrus.Errorf("genrate msg bytes failed. topic: %s, err: %s", partitionedTopic, err)
return nil, err
}
msgId, err = pulsar.DeserializeMessageID(bytes)
if err != nil {
logrus.Errorf("deserialize messageId failed. msgBytes: %s, topic: %s, err: %s", string(msgIdBytes), partitionedTopic, err)
return nil, err
}
readerOptions := pulsar.ReaderOptions{
Topic: partitionedTopic,
Name: constant.OffsetReaderEarliestName,
StartMessageID: msgId,
StartMessageIDInclusive: true,
}
return readNextMsg(readerOptions, maxWaitMs, pulsarClient)
}
func getTenantNamespaceTopicFromPartitionedTopic(partitionedTopic string) (tenant, namespace, shortPartitionedTopic string, err error) {
if strings.Contains(partitionedTopic, "//") {
topicArr := strings.Split(partitionedTopic, "//")
if len(topicArr) < 2 {
return "", "", "", errors.New("get tenant and namespace failed")
}
list := strings.Split(topicArr[1], "/")
if len(list) < 3 {
return "", "", "", errors.New("get tenant and namespace failed")
}
return list[0], list[1], list[2], nil
}
return "", "", "", errors.New("get tenant and namespace failed")
}
func readNextMsg(operation pulsar.ReaderOptions, maxWaitMs int, pulsarClient pulsar.Client) (pulsar.Message, error) {
reader, err := pulsarClient.CreateReader(operation)
if err != nil {
logrus.Warnf("create pulsar lasted read failed. topic: %s, err: %s", operation.Topic, err)
return nil, err
}
defer reader.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(maxWaitMs)*time.Millisecond)
defer cancel()
message, err := reader.Next(ctx)
if err != nil {
logrus.Errorf("get message failed. topic: %s, err: %s", operation.Topic, err)
if strings.Contains(err.Error(), constant.ReadMsgTimeoutErr) {
return message, nil
}
return nil, err
}
return message, nil
}
func CalculateMsgLength(message pulsar.Message) int {
length := 0
length += len([]byte(message.Key()))
length += len(message.Payload())
properties := message.Properties()
for key, value := range properties {
length += len([]byte(key))
length += len([]byte(value))
}
return length
}
func generateMsgBytes(msgBytes []byte) ([]byte, error) {
var msgId model.MessageID
err := json.Unmarshal(msgBytes, &msgId)
if err != nil {
logrus.Errorf("unmarsha failed. msg: %s, err: %s", string(msgBytes), err)
return nil, err
}
pulsarMessageData := pb.MessageIdData{
LedgerId: proto.Uint64(uint64(msgId.LedgerID)),
EntryId: proto.Uint64(uint64(msgId.EntryID)),
BatchIndex: proto.Int32(msgId.BatchIdx),
Partition: proto.Int32(msgId.PartitionIdx),
}
data, err := proto.Marshal(&pulsarMessageData)
if err != nil {
logrus.Errorf("unmarsha failed. msg: %s, err: %s", string(msgBytes), err)
return nil, err
}
return data, nil
}