forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
305 lines (263 loc) · 8.5 KB
/
input.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package pulsar
import (
"context"
"errors"
"fmt"
"strconv"
"sync"
"time"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/dafanshu/benthos/v3/internal/bundle"
"github.com/dafanshu/benthos/v3/internal/docs"
"github.com/dafanshu/benthos/v3/internal/impl/pulsar/auth"
"github.com/dafanshu/benthos/v3/internal/shutdown"
"github.com/dafanshu/benthos/v3/lib/input"
"github.com/dafanshu/benthos/v3/lib/input/reader"
"github.com/dafanshu/benthos/v3/lib/log"
"github.com/dafanshu/benthos/v3/lib/message"
"github.com/dafanshu/benthos/v3/lib/metrics"
"github.com/dafanshu/benthos/v3/lib/types"
)
const (
defaultSubscriptionType = "shared"
)
func init() {
bundle.AllInputs.Add(bundle.InputConstructorFromSimple(func(c input.Config, nm bundle.NewManagement) (input.Type, error) {
var a reader.Async
var err error
if a, err = newPulsarReader(c.Pulsar, nm.Logger(), nm.Metrics()); err != nil {
return nil, err
}
return input.NewAsyncReader(input.TypePulsar, false, a, nm.Logger(), nm.Metrics())
}), docs.ComponentSpec{
Name: input.TypePulsar,
Type: docs.TypeInput,
Status: docs.StatusExperimental,
Version: "3.43.0",
Summary: `Reads messages from an Apache Pulsar server.`,
Description: `
### Metadata
This input adds the following metadata fields to each message:
` + "```text" + `
- pulsar_message_id
- pulsar_key
- pulsar_ordering_key
- pulsar_event_time_unix
- pulsar_publish_time_unix
- pulsar_topic
- pulsar_producer_name
- pulsar_redelivery_count
- All properties of the message
` + "```" + `
You can access these metadata fields using
[function interpolation](/docs/configuration/interpolation#metadata).`,
Categories: []string{
string(input.CategoryServices),
},
Config: docs.FieldComponent().WithChildren(
docs.FieldCommon("url",
"A URL to connect to.",
"pulsar://localhost:6650",
"pulsar://pulsar.us-west.example.com:6650",
"pulsar+ssl://pulsar.us-west.example.com:6651",
),
docs.FieldString("topics", "A list of topics to subscribe to.").Array(),
docs.FieldCommon("subscription_name", "Specify the subscription name for this consumer."),
docs.FieldCommon("subscription_type", "Specify the subscription type for this consumer.\n\n> NOTE: Using a `key_shared` subscription type will __allow out-of-order delivery__ since nack-ing messages sets non-zero nack delivery delay - this can potentially cause consumers to stall. See [Pulsar documentation](https://pulsar.apache.org/docs/en/2.8.1/concepts-messaging/#negative-acknowledgement) and [this Github issue](https://github.com/apache/pulsar/issues/12208) for more details.").
HasOptions("shared", "key_shared", "failover", "exclusive").
HasDefault(defaultSubscriptionType),
auth.FieldSpec(),
).ChildDefaultAndTypesFromStruct(input.NewPulsarConfig()),
})
}
//------------------------------------------------------------------------------
type pulsarReader struct {
client pulsar.Client
consumer pulsar.Consumer
conf input.PulsarConfig
stats metrics.Type
log log.Modular
m sync.RWMutex
shutSig *shutdown.Signaller
}
func newPulsarReader(conf input.PulsarConfig, log log.Modular, stats metrics.Type) (*pulsarReader, error) {
if conf.URL == "" {
return nil, errors.New("field url must not be empty")
}
if len(conf.Topics) == 0 {
return nil, errors.New("field topics must not be empty")
}
if conf.SubscriptionName == "" {
return nil, errors.New("field subscription_name must not be empty")
}
if conf.SubscriptionType == "" {
conf.SubscriptionType = defaultSubscriptionType // set default subscription type if empty
}
if _, err := parseSubscriptionType(conf.SubscriptionType); err != nil {
return nil, fmt.Errorf("field subscription_type is invalid: %v", err)
}
if err := conf.Auth.Validate(); err != nil {
return nil, fmt.Errorf("field auth is invalid: %v", err)
}
p := pulsarReader{
conf: conf,
stats: stats,
log: log,
shutSig: shutdown.NewSignaller(),
}
return &p, nil
}
func parseSubscriptionType(subType string) (pulsar.SubscriptionType, error) {
// Pulsar docs: https://pulsar.apache.org/docs/en/2.8.0/concepts-messaging/#subscriptions
switch subType {
case "shared":
return pulsar.Shared, nil
case "key_shared":
return pulsar.KeyShared, nil
case "failover":
return pulsar.Failover, nil
case "exclusive":
return pulsar.Exclusive, nil
}
return pulsar.Shared, fmt.Errorf("could not parse subscription type: %s", subType)
}
//------------------------------------------------------------------------------
// ConnectWithContext establishes a connection to an Pulsar server.
func (p *pulsarReader) ConnectWithContext(ctx context.Context) error {
p.m.Lock()
defer p.m.Unlock()
if p.client != nil {
return nil
}
var (
client pulsar.Client
consumer pulsar.Consumer
subType pulsar.SubscriptionType
err error
)
opts := pulsar.ClientOptions{
Logger: DefaultLogger(p.log),
ConnectionTimeout: time.Second * 3,
URL: p.conf.URL,
}
if p.conf.Auth.OAuth2.Enabled {
opts.Authentication = pulsar.NewAuthenticationOAuth2(p.conf.Auth.OAuth2.ToMap())
} else if p.conf.Auth.Token.Enabled {
opts.Authentication = pulsar.NewAuthenticationToken(p.conf.Auth.Token.Token)
}
if client, err = pulsar.NewClient(opts); err != nil {
return err
}
if subType, err = parseSubscriptionType(p.conf.SubscriptionType); err != nil {
return err
}
if consumer, err = client.Subscribe(pulsar.ConsumerOptions{
Topics: p.conf.Topics,
SubscriptionName: p.conf.SubscriptionName,
Type: subType,
KeySharedPolicy: &pulsar.KeySharedPolicy{
AllowOutOfOrderDelivery: true,
},
}); err != nil {
client.Close()
return err
}
p.client = client
p.consumer = consumer
p.log.Infof("Receiving Pulsar messages to URL: %v\n", p.conf.URL)
return nil
}
// disconnect safely closes a connection to an Pulsar server.
func (p *pulsarReader) disconnect(ctx context.Context) error {
p.m.Lock()
defer p.m.Unlock()
if p.client == nil {
return nil
}
p.consumer.Close()
p.client.Close()
p.consumer = nil
p.client = nil
if p.shutSig.ShouldCloseAtLeisure() {
p.shutSig.ShutdownComplete()
}
return nil
}
//------------------------------------------------------------------------------
// ReadWithContext a new Pulsar message.
func (p *pulsarReader) ReadWithContext(ctx context.Context) (types.Message, reader.AsyncAckFn, error) {
var r pulsar.Consumer
p.m.RLock()
if p.consumer != nil {
r = p.consumer
}
p.m.RUnlock()
if r == nil {
return nil, nil, types.ErrNotConnected
}
// Receive next message
pulMsg, err := r.Receive(ctx)
if err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
err = types.ErrTimeout
} else {
p.log.Errorf("Lost connection due to: %v\n", err)
p.disconnect(ctx)
err = types.ErrNotConnected
}
return nil, nil, err
}
msg := message.New(nil)
part := message.NewPart(pulMsg.Payload())
part.Metadata().Set("pulsar_message_id", string(pulMsg.ID().Serialize()))
part.Metadata().Set("pulsar_topic", pulMsg.Topic())
part.Metadata().Set("pulsar_publish_time_unix", strconv.FormatInt(pulMsg.PublishTime().Unix(), 10))
part.Metadata().Set("pulsar_redelivery_count", strconv.FormatInt(int64(pulMsg.RedeliveryCount()), 10))
if key := pulMsg.Key(); len(key) > 0 {
part.Metadata().Set("pulsar_key", key)
}
if orderingKey := pulMsg.OrderingKey(); len(orderingKey) > 0 {
part.Metadata().Set("pulsar_ordering_key", orderingKey)
}
if !pulMsg.EventTime().IsZero() {
part.Metadata().Set("pulsar_event_time_unix", strconv.FormatInt(pulMsg.EventTime().Unix(), 10))
}
if producerName := pulMsg.ProducerName(); producerName != "" {
part.Metadata().Set("pulsar_producer_name", producerName)
}
for k, v := range pulMsg.Properties() {
part.Metadata().Set(k, v)
}
msg.Append(part)
return msg, func(ctx context.Context, res types.Response) error {
var r pulsar.Consumer
p.m.RLock()
if p.consumer != nil {
r = p.consumer
}
p.m.RUnlock()
if r != nil {
if res.Error() != nil {
r.Nack(pulMsg)
} else {
r.Ack(pulMsg)
}
}
return nil
}, nil
}
// CloseAsync shuts down the Pulsar input and stops processing requests.
func (p *pulsarReader) CloseAsync() {
p.shutSig.CloseAtLeisure()
go p.disconnect(context.Background())
}
// WaitForClose blocks until the Pulsar input has closed down.
func (p *pulsarReader) WaitForClose(timeout time.Duration) error {
select {
case <-p.shutSig.HasClosedChan():
case <-time.After(timeout):
return types.ErrTimeout
}
return nil
}
//------------------------------------------------------------------------------