/
source.go
187 lines (150 loc) · 3.97 KB
/
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
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
package kafka
import (
"context"
"time"
"github.com/Shopify/sarama"
"github.com/bsm/sarama-cluster"
"github.com/msales/streams"
"github.com/pkg/errors"
)
// SourceConfig represents the configuration for a Kafka stream source.
type SourceConfig struct {
sarama.Config
Brokers []string
Topic string
GroupId string
Ctx context.Context
KeyDecoder Decoder
ValueDecoder Decoder
BufferSize int
}
type marker struct {
Topic string
Partition int32
Offset int64
}
// NewSourceConfig creates a new Kafka source configuration.
func NewSourceConfig() *SourceConfig {
c := &SourceConfig{
Config: *sarama.NewConfig(),
}
c.Ctx = context.Background()
c.KeyDecoder = ByteDecoder{}
c.ValueDecoder = ByteDecoder{}
c.BufferSize = 1000
return c
}
// Validate checks a Config instance. It will return a
// sarama.ConfigurationError if the specified values don't make sense.
func (c *SourceConfig) Validate() error {
if err := c.Config.Validate(); err != nil {
return err
}
switch {
case c.Brokers == nil || len(c.Brokers) == 0:
return sarama.ConfigurationError("Brokers must have at least one broker")
case c.KeyDecoder == nil:
return sarama.ConfigurationError("KeyDecoder must be an instance of Decoder")
case c.ValueDecoder == nil:
return sarama.ConfigurationError("ValueDecoder must be an instance of Decoder")
case c.BufferSize <= 0:
return sarama.ConfigurationError("BufferSize must be at least 1")
}
return nil
}
// Source represents a Kafka stream source.
type Source struct {
consumer *cluster.Consumer
ctx context.Context
keyDecoder Decoder
valueDecoder Decoder
state map[string]map[int32]int64
buf chan *sarama.ConsumerMessage
lastErr error
}
// NewSource creates a new Kafka stream source.
func NewSource(c *SourceConfig) (*Source, error) {
if err := c.Validate(); err != nil {
return nil, err
}
cc := cluster.NewConfig()
cc.Config = c.Config
cc.Consumer.Return.Errors = true
consumer, err := cluster.NewConsumer(c.Brokers, c.GroupId, []string{c.Topic}, cc)
if err != nil {
return nil, err
}
s := &Source{
consumer: consumer,
ctx: c.Ctx,
keyDecoder: c.KeyDecoder,
valueDecoder: c.ValueDecoder,
buf: make(chan *sarama.ConsumerMessage, c.BufferSize),
state: make(map[string]map[int32]int64),
}
go s.readErrors()
go s.readMessages()
return s, nil
}
// Consume gets the next record from the Source.
func (s *Source) Consume() (*streams.Message, error) {
if s.lastErr != nil {
return nil, s.lastErr
}
select {
case msg := <-s.buf:
k, err := s.keyDecoder.Decode(msg.Key)
if err != nil {
return nil, err
}
v, err := s.valueDecoder.Decode(msg.Value)
if err != nil {
return nil, err
}
m := streams.NewMessageWithContext(s.ctx, k, v).
WithMetadata(s, s.markState(msg))
return m, nil
case <-time.After(100 * time.Millisecond):
return streams.NewMessage(nil, nil), nil
}
}
// Commit marks the consumed records as processed.
func (s *Source) Commit(v interface{}) error {
state := v.([]marker)
for _, m := range state {
s.consumer.MarkPartitionOffset(m.Topic, m.Partition, m.Offset, "")
}
if err := s.consumer.CommitOffsets(); err != nil {
return errors.Wrap(err, "streams: could not commit kafka offset")
}
return nil
}
// Close closes the Source.
func (s *Source) Close() error {
return s.consumer.Close()
}
func (s *Source) markState(msg *sarama.ConsumerMessage) []marker {
partitions, ok := s.state[msg.Topic]
if !ok {
partitions = make(map[int32]int64)
s.state[msg.Topic] = partitions
}
partitions[msg.Partition] = msg.Offset
var markers []marker
for topic, partitions := range s.state {
for partition, offset := range partitions {
markers = append(markers, marker{Topic: topic, Partition: partition, Offset: offset})
}
}
return markers
}
func (s *Source) readErrors() {
for err := range s.consumer.Errors() {
s.lastErr = err
}
}
func (s *Source) readMessages() {
for msg := range s.consumer.Messages() {
s.buf <- msg
}
}