-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathmetadata_response.go
348 lines (290 loc) · 7.21 KB
/
metadata_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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package sarama
import "time"
// PartitionMetadata contains each partition in the topic.
type PartitionMetadata struct {
// Version defines the protocol version to use for encode and decode
Version int16
// Err contains the partition error, or 0 if there was no error.
Err KError
// ID contains the partition index.
ID int32
// Leader contains the ID of the leader broker.
Leader int32
// LeaderEpoch contains the leader epoch of this partition.
LeaderEpoch int32
// Replicas contains the set of all nodes that host this partition.
Replicas []int32
// Isr contains the set of nodes that are in sync with the leader for this partition.
Isr []int32
// OfflineReplicas contains the set of offline replicas of this partition.
OfflineReplicas []int32
}
func (p *PartitionMetadata) decode(pd packetDecoder, version int16) (err error) {
p.Version = version
tmp, err := pd.getInt16()
if err != nil {
return err
}
p.Err = KError(tmp)
if p.ID, err = pd.getInt32(); err != nil {
return err
}
if p.Leader, err = pd.getInt32(); err != nil {
return err
}
if p.Version >= 7 {
if p.LeaderEpoch, err = pd.getInt32(); err != nil {
return err
}
}
if p.Replicas, err = pd.getInt32Array(); err != nil {
return err
}
if p.Isr, err = pd.getInt32Array(); err != nil {
return err
}
if p.Version >= 5 {
if p.OfflineReplicas, err = pd.getInt32Array(); err != nil {
return err
}
}
return nil
}
func (p *PartitionMetadata) encode(pe packetEncoder, version int16) (err error) {
p.Version = version
pe.putInt16(int16(p.Err))
pe.putInt32(p.ID)
pe.putInt32(p.Leader)
if p.Version >= 7 {
pe.putInt32(p.LeaderEpoch)
}
if err := pe.putInt32Array(p.Replicas); err != nil {
return err
}
if err := pe.putInt32Array(p.Isr); err != nil {
return err
}
if p.Version >= 5 {
if err := pe.putInt32Array(p.OfflineReplicas); err != nil {
return err
}
}
return nil
}
// TopicMetadata contains each topic in the response.
type TopicMetadata struct {
// Version defines the protocol version to use for encode and decode
Version int16
// Err contains the topic error, or 0 if there was no error.
Err KError
// Name contains the topic name.
Name string
// IsInternal contains a True if the topic is internal.
IsInternal bool
// Partitions contains each partition in the topic.
Partitions []*PartitionMetadata
}
func (t *TopicMetadata) decode(pd packetDecoder, version int16) (err error) {
t.Version = version
tmp, err := pd.getInt16()
if err != nil {
return err
}
t.Err = KError(tmp)
if t.Name, err = pd.getString(); err != nil {
return err
}
if t.Version >= 1 {
if t.IsInternal, err = pd.getBool(); err != nil {
return err
}
}
if numPartitions, err := pd.getArrayLength(); err != nil {
return err
} else {
t.Partitions = make([]*PartitionMetadata, numPartitions)
for i := 0; i < numPartitions; i++ {
block := &PartitionMetadata{}
if err := block.decode(pd, t.Version); err != nil {
return err
}
t.Partitions[i] = block
}
}
return nil
}
func (t *TopicMetadata) encode(pe packetEncoder, version int16) (err error) {
t.Version = version
pe.putInt16(int16(t.Err))
if err := pe.putString(t.Name); err != nil {
return err
}
if t.Version >= 1 {
pe.putBool(t.IsInternal)
}
if err := pe.putArrayLength(len(t.Partitions)); err != nil {
return err
}
for _, block := range t.Partitions {
if err := block.encode(pe, t.Version); err != nil {
return err
}
}
return nil
}
type MetadataResponse struct {
// Version defines the protocol version to use for encode and decode
Version int16
// ThrottleTimeMs contains the duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota.
ThrottleTimeMs int32
// Brokers contains each broker in the response.
Brokers []*Broker
// ClusterID contains the cluster ID that responding broker belongs to.
ClusterID *string
// ControllerID contains the ID of the controller broker.
ControllerID int32
// Topics contains each topic in the response.
Topics []*TopicMetadata
}
func (r *MetadataResponse) decode(pd packetDecoder, version int16) (err error) {
r.Version = version
if r.Version >= 3 {
if r.ThrottleTimeMs, err = pd.getInt32(); err != nil {
return err
}
}
n, err := pd.getArrayLength()
if err != nil {
return err
}
r.Brokers = make([]*Broker, n)
for i := 0; i < n; i++ {
r.Brokers[i] = new(Broker)
err = r.Brokers[i].decode(pd, version)
if err != nil {
return err
}
}
if r.Version >= 2 {
if r.ClusterID, err = pd.getNullableString(); err != nil {
return err
}
}
if r.Version >= 1 {
if r.ControllerID, err = pd.getInt32(); err != nil {
return err
}
}
if numTopics, err := pd.getArrayLength(); err != nil {
return err
} else {
r.Topics = make([]*TopicMetadata, numTopics)
for i := 0; i < numTopics; i++ {
block := &TopicMetadata{}
if err := block.decode(pd, r.Version); err != nil {
return err
}
r.Topics[i] = block
}
}
return nil
}
func (r *MetadataResponse) encode(pe packetEncoder) (err error) {
if r.Version >= 3 {
pe.putInt32(r.ThrottleTimeMs)
}
if err := pe.putArrayLength(len(r.Brokers)); err != nil {
return err
}
for _, broker := range r.Brokers {
err = broker.encode(pe, r.Version)
if err != nil {
return err
}
}
if r.Version >= 2 {
if err := pe.putNullableString(r.ClusterID); err != nil {
return err
}
}
if r.Version >= 1 {
pe.putInt32(r.ControllerID)
}
if err := pe.putArrayLength(len(r.Topics)); err != nil {
return err
}
for _, block := range r.Topics {
if err := block.encode(pe, r.Version); err != nil {
return err
}
}
return nil
}
func (r *MetadataResponse) key() int16 {
return 3
}
func (r *MetadataResponse) version() int16 {
return r.Version
}
func (r *MetadataResponse) headerVersion() int16 {
return 0
}
func (r *MetadataResponse) requiredVersion() KafkaVersion {
switch r.Version {
case 1:
return V0_10_0_0
case 2:
return V0_10_1_0
case 3, 4:
return V0_11_0_0
case 5:
return V1_0_0_0
case 6:
return V2_0_0_0
case 7:
return V2_1_0_0
default:
return MinVersion
}
}
func (r *MetadataResponse) throttleTime() time.Duration {
return time.Duration(r.ThrottleTimeMs) * time.Millisecond
}
// testing API
func (r *MetadataResponse) AddBroker(addr string, id int32) {
r.Brokers = append(r.Brokers, &Broker{id: id, addr: addr})
}
func (r *MetadataResponse) AddTopic(topic string, err KError) *TopicMetadata {
var tmatch *TopicMetadata
for _, tm := range r.Topics {
if tm.Name == topic {
tmatch = tm
goto foundTopic
}
}
tmatch = new(TopicMetadata)
tmatch.Name = topic
r.Topics = append(r.Topics, tmatch)
foundTopic:
tmatch.Err = err
return tmatch
}
func (r *MetadataResponse) AddTopicPartition(topic string, partition, brokerID int32, replicas, isr []int32, offline []int32, err KError) {
tmatch := r.AddTopic(topic, ErrNoError)
var pmatch *PartitionMetadata
for _, pm := range tmatch.Partitions {
if pm.ID == partition {
pmatch = pm
goto foundPartition
}
}
pmatch = new(PartitionMetadata)
pmatch.ID = partition
tmatch.Partitions = append(tmatch.Partitions, pmatch)
foundPartition:
pmatch.Leader = brokerID
pmatch.Replicas = replicas
pmatch.Isr = isr
pmatch.OfflineReplicas = offline
pmatch.Err = err
}