-
Notifications
You must be signed in to change notification settings - Fork 2
/
pulsar_source.go
123 lines (111 loc) · 3.67 KB
/
pulsar_source.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
/*
Copyright 2022 The Numaproj Authors.
Licensed 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 apachepulsar
import (
"context"
"fmt"
"log"
"os"
"sync"
"github.com/apache/pulsar-client-go/pulsar"
sourcesdk "github.com/numaproj/numaflow-go/pkg/sourcer"
pulsaradmin "github.com/streamnative/pulsar-admin-go"
"github.com/streamnative/pulsar-admin-go/pkg/utils"
)
type PulsarSource struct {
client pulsar.Client
adminClient pulsaradmin.Client
consumer pulsar.Consumer
toAckSet map[string]pulsar.Message
lock *sync.Mutex
}
func NewPulsarSource(client pulsar.Client, adminClient pulsaradmin.Client, consumer pulsar.Consumer) *PulsarSource {
return &PulsarSource{client: client, adminClient: adminClient, consumer: consumer, toAckSet: map[string]pulsar.Message{}, lock: new(sync.Mutex)}
}
func (ps *PulsarSource) Read(_ context.Context, readRequest sourcesdk.ReadRequest, messageCh chan<- sourcesdk.Message) {
ctx, cancel := context.WithTimeout(context.Background(), readRequest.TimeOut())
defer cancel()
// If we have un-acked data, we return without reading any new data.
if len(ps.toAckSet) > 0 {
return
}
for i := 0; i < int(readRequest.Count()); i++ {
select {
case <-ctx.Done():
return
default:
ps.lock.Lock()
msg, err := ps.consumer.Receive(ctx)
if msg == nil {
log.Println("received empty message")
} else if err != nil {
log.Printf("error receiving message %s", err)
} else {
messageCh <- sourcesdk.NewMessage(
msg.Payload(),
sourcesdk.NewOffset([]byte(msg.ID().String()), 0),
msg.PublishTime(),
)
ps.toAckSet[msg.ID().String()] = msg
}
ps.lock.Unlock()
}
}
}
func (ps *PulsarSource) Pending(_ context.Context) int64 {
topic, _ := utils.GetTopicName(fmt.Sprintf("%s/%s/%s", os.Getenv("PULSAR_TENANT"), os.Getenv("PULSAR_NAMESPACE"), os.Getenv("PULSAR_TOPIC")))
stats, err := ps.adminClient.Topics().GetStats(*topic)
if err != nil {
log.Printf("error getting pending count %s", err)
return 0
}
pendingMessages := stats.Subscriptions[os.Getenv("PULSAR_SUBSCRIPTION")].UnAckedMessages
return pendingMessages
}
func (ps *PulsarSource) Ack(ctx context.Context, request sourcesdk.AckRequest) {
for _, offset := range request.Offsets() {
select {
case <-ctx.Done():
return
default:
func() {
ps.lock.Lock()
defer ps.lock.Unlock()
message := ps.toAckSet[string(offset.Value())]
err := ps.consumer.Ack(message)
if err != nil {
log.Printf("error in acknowledging message %s", err)
return
}
delete(ps.toAckSet, string(offset.Value()))
}()
}
}
}
func (ps *PulsarSource) Partitions(ctx context.Context) []int32 {
topic, err := utils.GetTopicName(fmt.Sprintf("%s/%s/%s", os.Getenv("PULSAR_TENANT"), os.Getenv("PULSAR_NAMESPACE"), os.Getenv("PULSAR_TOPIC")))
if err != nil {
log.Printf("error getting partitions from admin endpoint %s", err)
return sourcesdk.DefaultPartitions()
}
stats, err := ps.adminClient.Topics().GetMetadata(*topic)
if err != nil {
log.Printf("error getting partitions from admin endpoint %s", err)
return sourcesdk.DefaultPartitions()
}
var partitions []int32
for i := 0; i < stats.Partitions; i++ {
partitions = append(partitions, int32(i))
}
return partitions
}