-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathoffset_commit_request.go
161 lines (132 loc) · 3.66 KB
/
offset_commit_request.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
package healer
import (
"encoding/binary"
)
type OffsetCommitRequestPartition struct {
PartitionID int32
Offset int64
Metadata string
}
type OffsetCommitRequestTopic struct {
Topic string
Partitions []*OffsetCommitRequestPartition
}
type OffsetCommitRequest struct {
*RequestHeader
GroupID string
GenerationID int32
MemberID string
RetentionTime int64
Topics []*OffsetCommitRequestTopic
}
// request only ONE topic
func NewOffsetCommitRequest(apiVersion uint16, clientID, groupID string) *OffsetCommitRequest {
requestHeader := &RequestHeader{
APIKey: API_OffsetCommitRequest,
APIVersion: apiVersion,
ClientID: &clientID,
}
r := &OffsetCommitRequest{
RequestHeader: requestHeader,
GroupID: groupID,
}
r.Topics = make([]*OffsetCommitRequestTopic, 0)
return r
}
func (r *OffsetCommitRequest) SetGenerationID(generationID int32) {
r.GenerationID = generationID
}
func (r *OffsetCommitRequest) SetMemberID(memberID string) {
r.MemberID = memberID
}
func (r *OffsetCommitRequest) SetRetentionTime(retentionTime int64) {
r.RetentionTime = retentionTime
}
func (r *OffsetCommitRequest) AddPartiton(topic string, partitionID int32, offset int64, metadata string) {
if r.Topics == nil {
r.Topics = make([]*OffsetCommitRequestTopic, 0)
}
var theTopic *OffsetCommitRequestTopic = nil
for _, t := range r.Topics {
if t.Topic == topic {
theTopic = t
break
}
}
if theTopic == nil {
theTopic = &OffsetCommitRequestTopic{
Topic: topic,
Partitions: make([]*OffsetCommitRequestPartition, 0),
}
r.Topics = append(r.Topics, theTopic)
}
for _, p := range theTopic.Partitions {
if p.PartitionID == partitionID {
p.Offset = offset
p.Metadata = metadata
return
}
}
thePartition := &OffsetCommitRequestPartition{
PartitionID: partitionID,
Offset: offset,
Metadata: metadata,
}
theTopic.Partitions = append(theTopic.Partitions, thePartition)
return
}
func (r *OffsetCommitRequest) Length() int {
l := r.RequestHeader.length()
l += 2 + len(r.GroupID)
if r.Version() == 2 {
l += 4 + 2 + len(r.MemberID) + 8
}
l += 4
for _, t := range r.Topics {
l += 2 + len(t.Topic)
l += 4
for _, p := range t.Partitions {
l += 4 + 8 + 2 + len(p.Metadata)
}
}
return l
}
func (r *OffsetCommitRequest) Encode(version uint16) []byte {
requestLength := r.Length()
payload := make([]byte, 4+requestLength)
offset := 0
binary.BigEndian.PutUint32(payload[offset:], uint32(requestLength))
offset += 4
offset += r.RequestHeader.EncodeTo(payload[offset:])
binary.BigEndian.PutUint16(payload[offset:], uint16(len(r.GroupID)))
offset += 2
offset += copy(payload[offset:], r.GroupID)
if r.Version() == 2 {
binary.BigEndian.PutUint32(payload[offset:], uint32(r.GenerationID))
offset += 4
binary.BigEndian.PutUint16(payload[offset:], uint16(len(r.MemberID)))
offset += 2
offset += copy(payload[offset:], r.MemberID)
binary.BigEndian.PutUint64(payload[offset:], uint64(r.RetentionTime))
offset += 8
}
binary.BigEndian.PutUint32(payload[offset:], uint32(len(r.Topics)))
offset += 4
for _, t := range r.Topics {
binary.BigEndian.PutUint16(payload[offset:], uint16(len(t.Topic)))
offset += 2
offset += copy(payload[offset:], t.Topic)
binary.BigEndian.PutUint32(payload[offset:], uint32(len(t.Partitions)))
offset += 4
for _, p := range t.Partitions {
binary.BigEndian.PutUint32(payload[offset:], uint32(p.PartitionID))
offset += 4
binary.BigEndian.PutUint64(payload[offset:], uint64(p.Offset))
offset += 8
binary.BigEndian.PutUint16(payload[offset:], uint16(len(p.Metadata)))
offset += 2
offset += copy(payload[offset:], p.Metadata)
}
}
return payload
}