-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathreader.go
More file actions
209 lines (192 loc) · 5.96 KB
/
Copy pathreader.go
File metadata and controls
209 lines (192 loc) · 5.96 KB
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
/*
* === This file is part of ALICE O² ===
*
* Copyright 2025 CERN and copyright holders of ALICE O².
* Author: Piotr Konopka <pkonopka@cern.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In applying this license CERN does not waive the privileges and
* immunities granted to it by virtue of its status as an
* Intergovernmental Organization or submit itself to any jurisdiction.
*/
package event
import (
"context"
"fmt"
"github.com/AliceO2Group/Control/common/event/topic"
"github.com/AliceO2Group/Control/common/logger/infologger"
pb "github.com/AliceO2Group/Control/common/protos"
"github.com/segmentio/kafka-go"
"github.com/spf13/viper"
"google.golang.org/protobuf/proto"
)
// Reader interface provides methods to read events.
type Reader interface {
// Next should return the next event or cancel if the context is cancelled.
Next(ctx context.Context) (*pb.Event, error)
// Last should return the last available event currently present on the topic (or nil if none)
// or cancel if the context is cancelled.
Last(ctx context.Context) (*pb.Event, error)
Close() error
}
// DummyReader is an implementation of Reader that returns no events.
type DummyReader struct{}
func (*DummyReader) Next(context.Context) (*pb.Event, error) { return nil, nil }
func (*DummyReader) Last(context.Context) (*pb.Event, error) { return nil, nil }
func (*DummyReader) Close() error { return nil }
// KafkaReader reads events from Kafka and provides a blocking, cancellable API to fetch events.
type KafkaReader struct {
*kafka.Reader
topic string
brokers []string
groupID string
}
// NewReaderWithTopic creates a KafkaReader for the provided topic and starts it.
func NewReaderWithTopic(topic topic.Topic, groupID string) *KafkaReader {
cfg := kafka.ReaderConfig{
Brokers: viper.GetStringSlice("kafkaEndpoints"),
Topic: string(topic),
GroupID: groupID,
MinBytes: 1,
MaxBytes: 10e7,
}
rk := &KafkaReader{
Reader: kafka.NewReader(cfg),
topic: string(topic),
brokers: append([]string{}, cfg.Brokers...),
groupID: groupID,
}
return rk
}
// Next blocks until the next event is available or ctx is cancelled.
func (r *KafkaReader) Next(ctx context.Context) (*pb.Event, error) {
if r == nil {
return nil, fmt.Errorf("nil reader")
}
msg, err := r.ReadMessage(ctx)
if err != nil {
return nil, err
}
return kafkaMessageToEvent(msg)
}
// Last fetches the last available message on the topic (considering all partitions).
// If multiple partitions have data, the event with the greatest message timestamp is returned.
func (r *KafkaReader) Last(ctx context.Context) (*pb.Event, error) {
if r == nil {
return nil, fmt.Errorf("nil reader")
}
partitions, err := r.readPartitions()
if err != nil {
return nil, err
}
var latestEvt *pb.Event
var latestEvtTimeNs int64
for _, p := range partitions {
if p.Topic != r.topic {
continue
}
first, last, err := r.readFirstAndLast(p.ID)
if err != nil {
log.WithField(infologger.Level, infologger.IL_Devel).WithError(err).
Warnf("failed to read offsets for %s[%d]", r.topic, p.ID)
continue
}
if last <= first {
continue
}
msg, err := r.readAtOffset(ctx, p.ID, last-1)
if err != nil {
log.WithError(err).
WithField(infologger.Level, infologger.IL_Devel).
Warnf("failed to read last message for %s[%d] at offset %d", r.topic, p.ID, last-1)
continue
}
evt, err := kafkaMessageToEvent(msg)
if err != nil {
log.WithError(err).
WithField(infologger.Level, infologger.IL_Devel).
Warnf("failed to decode last message for %s[%d]", r.topic, p.ID)
continue
}
currentEvtTimeNs := msg.Time.UnixNano()
if latestEvt == nil || currentEvtTimeNs > latestEvtTimeNs {
latestEvt = evt
latestEvtTimeNs = currentEvtTimeNs
}
}
return latestEvt, nil
}
// Close stops the reader.
func (r *KafkaReader) Close() error {
if r == nil {
return nil
}
return r.Reader.Close()
}
func kafkaMessageToEvent(m kafka.Message) (*pb.Event, error) {
var evt pb.Event
if err := proto.Unmarshal(m.Value, &evt); err != nil {
return nil, fmt.Errorf("failed to unmarshal kafka message: %w", err)
}
return &evt, nil
}
func (r *KafkaReader) brokerAddr() (string, error) {
if len(r.brokers) == 0 {
return "", fmt.Errorf("no kafka brokers configured")
}
return r.brokers[0], nil
}
func (r *KafkaReader) readPartitions() ([]kafka.Partition, error) {
addr, err := r.brokerAddr()
if err != nil {
return nil, err
}
conn, err := kafka.Dial("tcp", addr)
if err != nil {
return nil, err
}
defer conn.Close()
return conn.ReadPartitions(r.topic)
}
func (r *KafkaReader) readFirstAndLast(partition int) (int64, int64, error) {
addr, err := r.brokerAddr()
if err != nil {
return 0, 0, err
}
conn, err := kafka.DialLeader(context.Background(), "tcp", addr, r.topic, partition)
if err != nil {
return 0, 0, err
}
defer conn.Close()
first, last, err := conn.ReadOffsets()
return first, last, err
}
func (r *KafkaReader) readAtOffset(ctx context.Context, partition int, offset int64) (kafka.Message, error) {
if offset < 0 {
return kafka.Message{}, fmt.Errorf("invalid offset %d", offset)
}
kr := kafka.NewReader(kafka.ReaderConfig{
Brokers: append([]string{}, r.brokers...),
Topic: r.topic,
Partition: partition,
MinBytes: 1,
MaxBytes: 10e6,
})
defer kr.Close()
if err := kr.SetOffset(offset); err != nil {
return kafka.Message{}, err
}
return kr.ReadMessage(ctx)
}