forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
250 lines (208 loc) · 4.66 KB
/
protocol.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package kafka
import (
"encoding/binary"
"fmt"
"hash/crc32"
)
type apiKey int16
const (
produceRequest apiKey = 0
fetchRequest apiKey = 1
offsetRequest apiKey = 2
metadataRequest apiKey = 3
offsetCommitRequest apiKey = 8
offsetFetchRequest apiKey = 9
groupCoordinatorRequest apiKey = 10
joinGroupRequest apiKey = 11
heartbeatRequest apiKey = 12
leaveGroupRequest apiKey = 13
syncGroupRequest apiKey = 14
describeGroupsRequest apiKey = 15
listGroupsRequest apiKey = 16
)
type apiVersion int16
const (
v0 apiVersion = 0
v1 apiVersion = 1
v2 apiVersion = 2
)
type requestHeader struct {
Size int32
ApiKey int16
ApiVersion int16
CorrelationID int32
ClientID string
}
// Message types
type messageSet []messageSetItem
type messageSetItem struct {
Offset int64
MessageSize int32
Message message
}
type message struct {
CRC int32
MagicByte int8
Attributes int8
Timestamp int64
Key []byte
Value []byte
}
func (m message) crc32() int32 {
sum := uint32(0)
sum = crc32Int8(sum, m.MagicByte)
sum = crc32Int8(sum, m.Attributes)
sum = crc32Int64(sum, m.Timestamp)
sum = crc32Bytes(sum, m.Key)
sum = crc32Bytes(sum, m.Value)
return int32(sum)
}
func crc32Int8(sum uint32, v int8) uint32 {
b := [1]byte{byte(v)}
return crc32Add(sum, b[:])
}
func crc32Int32(sum uint32, v int32) uint32 {
b := [4]byte{}
binary.BigEndian.PutUint32(b[:], uint32(v))
return crc32Add(sum, b[:])
}
func crc32Int64(sum uint32, v int64) uint32 {
b := [8]byte{}
binary.BigEndian.PutUint64(b[:], uint64(v))
return crc32Add(sum, b[:])
}
func crc32Bytes(sum uint32, b []byte) uint32 {
if b == nil {
sum = crc32Int32(sum, -1)
} else {
sum = crc32Int32(sum, int32(len(b)))
}
return crc32Add(sum, b)
}
func crc32Add(sum uint32, b []byte) uint32 {
return crc32.Update(sum, crc32.IEEETable, b[:])
}
// Metadata API (v0)
type topicMetadataRequestV0 []string
type metadataResponseV0 struct {
Brokers []brokerMetadataV0
Topics []topicMetadataV0
}
type brokerMetadataV0 struct {
NodeID int32
Host string
Port int32
}
type topicMetadataV0 struct {
TopicErrorCode int16
TopicName string
Partitions []partitionMetadataV0
}
type partitionMetadataV0 struct {
PartitionErrorCode int16
PartitionID int32
Leader int32
Replicas []int32
Isr []int32
}
// Produce API (v2)
type produceRequestV2 struct {
RequiredAcks int16
Timeout int32
Topics []produceRequestTopicV2
}
type produceRequestTopicV2 struct {
TopicName string
Partitions []produceRequestPartitionV2
}
type produceRequestPartitionV2 struct {
Partition int32
MessageSetSize int32
MessageSet messageSet
}
type produceResponseV2 struct {
Topics []produceResponseTopicV2
ThrottleTime int32
}
type produceResponseTopicV2 struct {
TopicName string
Partitions []produceResponsePartitionV2
}
type produceResponsePartitionV2 struct {
Partition int32
ErrorCode int16
Offset int64
Timestamp int64
}
// Fetch API (v1)
type fetchRequestV1 struct {
ReplicaID int32
MaxWaitTime int32
MinBytes int32
Topics []fetchRequestTopicV1
}
type fetchRequestTopicV1 struct {
TopicName string
Partitions []fetchRequestPartitionV1
}
type fetchRequestPartitionV1 struct {
Partition int32
FetchOffset int64
MaxBytes int32
}
type fetchResponseV1 struct {
ThrottleTime int32
Topics []fetchResponseTopicV1
}
type fetchResponseTopicV1 struct {
TopicName string
Partitions []fetchResponsePartitionV1
}
type fetchResponsePartitionV1 struct {
Partition int32
ErrorCode int16
HighwaterMarkOffset int64
MessageSetSize int32
MessageSet messageSet
}
// Offset API (v1)
type listOffsetRequestV1 struct {
ReplicaID int32
Topics []listOffsetRequestTopicV1
}
type listOffsetRequestTopicV1 struct {
TopicName string
Partitions []listOffsetRequestPartitionV1
}
type listOffsetRequestPartitionV1 struct {
Partition int32
Time int64
}
type listOffsetResponseV1 struct {
TopicName string
PartitionOffsets []partitionOffsetV1
}
type partitionOffsetV1 struct {
Partition int32
ErrorCode int16
Timestamp int64
Offset int64
}
func makeInt8(b []byte) int8 {
return int8(b[0])
}
func makeInt16(b []byte) int16 {
return int16(binary.BigEndian.Uint16(b))
}
func makeInt32(b []byte) int32 {
return int32(binary.BigEndian.Uint32(b))
}
func makeInt64(b []byte) int64 {
return int64(binary.BigEndian.Uint64(b))
}
func expectZeroSize(sz int, err error) error {
if err == nil && sz != 0 {
err = fmt.Errorf("reading a response left %d unread bytes", sz)
}
return err
}