forked from childe/healer
/
offset_response.go
69 lines (62 loc) · 1.98 KB
/
offset_response.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
package healer
import (
"encoding/binary"
)
/*
OffsetsResponse => [TopicName [PartitionOffsets]]
PartitionOffsets => Partition ErrorCode [Offset]
Partition => int32
ErrorCode => int16
Offset => int64
*/
// TODO rename
type PartitionOffset struct {
Partition int32
ErrorCode int16
Offsets []int64
}
type OffsetsResponse struct {
CorrelationID uint32
TopicPartitionOffsets map[string][]*PartitionOffset
}
func NewOffsetsResponse(payload []byte) (*OffsetsResponse, error) {
offsetsResponse := &OffsetsResponse{}
offset := 0
responseLength := int(binary.BigEndian.Uint32(payload))
if responseLength+4 != len(payload) {
//TODO lenght does not match
}
offset += 4
offsetsResponse.CorrelationID = uint32(binary.BigEndian.Uint32(payload[offset:]))
offset += 4
topicLenght := int(binary.BigEndian.Uint32(payload[offset:]))
offset += 4
offsetsResponse.TopicPartitionOffsets = make(map[string][]*PartitionOffset)
for i := 0; i < topicLenght; i++ {
topicNameLenght := int(binary.BigEndian.Uint16(payload[offset:]))
offset += 2
topicName := string(payload[offset : offset+topicNameLenght])
offset += topicNameLenght
partitionOffsetLength := binary.BigEndian.Uint32(payload[offset:])
offset += 4
offsetsResponse.TopicPartitionOffsets[topicName] = make([]*PartitionOffset, partitionOffsetLength)
for j := uint32(0); j < partitionOffsetLength; j++ {
partition := int32(binary.BigEndian.Uint32(payload[offset:]))
offset += 4
errorCode := int16(binary.BigEndian.Uint16(payload[offset:]))
offset += 2
offsetLength := binary.BigEndian.Uint32(payload[offset:])
offset += 4
offsetsResponse.TopicPartitionOffsets[topicName][j] = &PartitionOffset{
Partition: partition,
ErrorCode: errorCode,
Offsets: make([]int64, offsetLength),
}
for k := uint32(0); k < offsetLength; k++ {
offsetsResponse.TopicPartitionOffsets[topicName][j].Offsets[k] = int64(binary.BigEndian.Uint64(payload[offset:]))
offset += 8
}
}
}
return offsetsResponse, nil
}