/
api_offset_translation.go
166 lines (145 loc) · 4.59 KB
/
api_offset_translation.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
package server
import (
"context"
"encoding/binary"
"errors"
"fmt"
"log"
"time"
pb "github.com/DataDog/kafka-kit/v3/registry/registry"
"github.com/confluentinc/confluent-kafka-go/kafka"
)
var (
// ErrGroupIDEmpty error.
ErrGroupIDEmpty = errors.New("GroupId field must be specified")
)
const (
defaultRemoteAliasCluster = "source"
checkpointsTopicSuffix = ".checkpoints.internal"
unsetOffset = -1001
seekTimeoutMs = 1000
queryWatermarkOffsetsTimeoutMs = 1000
consumerFetchTimeout = 1 * time.Second
)
// Checkpoint holds a record emmitted from the MirrorCheckpointConnector in MirrorMaker2.
type Checkpoint struct {
Topic string
Partition uint32
ConsumerGroupID string
UpstreamOffset uint64
Offset uint64
Metadata string
}
// TranslateOffsets translates the last committed remote consumer group's offset into the corresponding local offsets.
func (s *Server) TranslateOffsets(ctx context.Context, req *pb.TranslateOffsetRequest) (*pb.TranslateOffsetResponse, error) {
ctx, cancel, err := s.ValidateRequest(ctx, req, readRequest)
if err != nil {
return nil, err
}
if cancel != nil {
defer cancel()
}
if req.RemoteClusterAlias == "" {
log.Printf("Using the default remote alias cluster: %s", defaultRemoteAliasCluster)
req.RemoteClusterAlias = defaultRemoteAliasCluster
}
if req.GroupId == "" {
return nil, ErrGroupIDEmpty
}
offsets, err := s.translateOffsets(req)
if err != nil {
return nil, err
}
resp := &pb.TranslateOffsetResponse{Offsets: offsets}
return resp, nil
}
func (s *Server) translateOffsets(req *pb.TranslateOffsetRequest) (map[string]*pb.OffsetMapping, error) {
checkpointTopic := req.RemoteClusterAlias + checkpointsTopicSuffix
checkpointPartition := int32(0)
checkpointTopicPartition := kafka.TopicPartition{
Topic: &checkpointTopic,
Partition: checkpointPartition,
Offset: kafka.OffsetBeginning,
}
checkpointAssignment := []kafka.TopicPartition{checkpointTopicPartition}
s.kafkaconsumer.Assign(checkpointAssignment)
s.kafkaconsumer.Seek(checkpointTopicPartition, seekTimeoutMs)
_, endOffset, err := s.kafkaconsumer.QueryWatermarkOffsets(
checkpointTopic, checkpointPartition, queryWatermarkOffsetsTimeoutMs,
)
if err != nil {
return nil, fmt.Errorf("failed to retrieve the log end offset for %s:%d", checkpointTopic, checkpointPartition)
}
endOfStream := func() bool {
offsets, _ := s.kafkaconsumer.Position(checkpointAssignment)
if offsets[0].Offset == unsetOffset {
return false
}
if int64(offsets[0].Offset) < endOffset {
return false
}
return true
}
offsets := make(map[string]*pb.OffsetMapping)
for !endOfStream() {
msg, err := s.kafkaconsumer.ReadMessage(consumerFetchTimeout)
if err != nil {
return nil, fmt.Errorf("consumer error while fetching a message: %v", err)
}
checkpoint, err := getCheckpoint(msg)
if err != nil {
return nil, err
}
if checkpoint.ConsumerGroupID == req.GroupId {
topicPartition := fmt.Sprintf("%s:%d", checkpoint.Topic, checkpoint.Partition)
offsets[topicPartition] = &pb.OffsetMapping{
UpstreamOffset: checkpoint.UpstreamOffset,
LocalOffset: checkpoint.Offset,
}
}
}
s.kafkaconsumer.Unassign()
return offsets, nil
}
func getCheckpoint(msg *kafka.Message) (*Checkpoint, error) {
checkpoint := Checkpoint{}
err := decodeValue(msg.Value, &checkpoint)
if err != nil {
return nil, err
}
err = decodeKey(msg.Key, &checkpoint)
if err != nil {
return nil, err
}
return &checkpoint, nil
}
func decodeValue(record []byte, checkpoint *Checkpoint) error {
headerVersion := binary.BigEndian.Uint16(record[0:2])
if headerVersion != 0 {
return fmt.Errorf("unknown header version on checkpoint record")
}
checkpoint.UpstreamOffset = binary.BigEndian.Uint64(record[2:10])
checkpoint.Offset = binary.BigEndian.Uint64(record[10:18])
strLength := binary.BigEndian.Uint16(record[18:20])
if strLength == 0 {
checkpoint.Metadata = ""
} else {
checkpoint.Metadata = string(record[20 : 20+strLength])
}
return nil
}
func decodeKey(record []byte, checkpoint *Checkpoint) error {
strLength := binary.BigEndian.Uint16(record[0:2])
if strLength == 0 {
return fmt.Errorf("unexpected consumer group with empty name")
}
checkpoint.ConsumerGroupID = string(record[2 : 2+strLength])
pos := 2 + strLength
strLength = binary.BigEndian.Uint16(record[pos : pos+2])
if strLength == 0 {
return fmt.Errorf("unexpected topic with empty name")
}
checkpoint.Topic = string(record[pos+2 : pos+2+strLength])
checkpoint.Partition = binary.BigEndian.Uint32(record[pos+2+strLength:])
return nil
}