/
metadata_response.go
177 lines (165 loc) · 3.43 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
package protocol
type Broker struct {
NodeID int32
Host string
Port int32
Rack *string
}
type PartitionMetadata struct {
PartitionErrorCode int16
PartitionID int32
Leader int32
Replicas []int32
ISR []int32
}
type TopicMetadata struct {
TopicErrorCode int16
Topic string
IsInternal bool
PartitionMetadata []*PartitionMetadata
}
type MetadataResponse struct {
APIVersion int16
ThrottleTime int32
ClusterID *string
Brokers []*Broker
ControllerID int32
TopicMetadata []*TopicMetadata
}
func (r *MetadataResponse) Encode(e PacketEncoder) (err error) {
if err = e.PutArrayLength(len(r.Brokers)); err != nil {
return err
}
for _, b := range r.Brokers {
e.PutInt32(b.NodeID)
if err = e.PutString(b.Host); err != nil {
return err
}
e.PutInt32(b.Port)
if err = e.PutNullableString(b.Rack); err != nil {
return err
}
}
if r.APIVersion >= 2 {
if err = e.PutNullableString(r.ClusterID); err != nil {
return err
}
}
if r.APIVersion >= 1 {
e.PutInt32(r.ControllerID)
}
if err = e.PutArrayLength(len(r.TopicMetadata)); err != nil {
return err
}
for _, t := range r.TopicMetadata {
e.PutInt16(t.TopicErrorCode)
if err = e.PutString(t.Topic); err != nil {
return err
}
if r.APIVersion >= 1 {
e.PutBool(t.IsInternal)
}
if err = e.PutArrayLength(len(t.PartitionMetadata)); err != nil {
return err
}
for _, p := range t.PartitionMetadata {
e.PutInt16(p.PartitionErrorCode)
e.PutInt32(p.PartitionID)
e.PutInt32(p.Leader)
if err = e.PutInt32Array(p.Replicas); err != nil {
return err
}
if err = e.PutInt32Array(p.ISR); err != nil {
return err
}
}
}
return nil
}
func (r *MetadataResponse) Decode(d PacketDecoder, version int16) (err error) {
r.APIVersion = version
brokerCount, err := d.ArrayLength()
if err != nil {
return err
}
r.Brokers = make([]*Broker, brokerCount)
for i := range r.Brokers {
nodeID, err := d.Int32()
if err != nil {
return err
}
host, err := d.String()
if err != nil {
return err
}
port, err := d.Int32()
if err != nil {
return err
}
r.Brokers[i] = &Broker{
NodeID: nodeID,
Host: host,
Port: port,
}
}
if version >= 1 {
r.ControllerID, err = d.Int32()
if err != nil {
return err
}
}
topicCount, err := d.ArrayLength()
if err != nil {
return err
}
r.TopicMetadata = make([]*TopicMetadata, topicCount)
for i := range r.TopicMetadata {
m := &TopicMetadata{}
m.TopicErrorCode, err = d.Int16()
if err != nil {
return err
}
m.Topic, err = d.String()
if err != nil {
return err
}
partitionCount, err := d.ArrayLength()
if err != nil {
return err
}
partitions := make([]*PartitionMetadata, partitionCount)
for i := range partitions {
p := &PartitionMetadata{}
p.PartitionErrorCode, err = d.Int16()
if err != nil {
return err
}
p.PartitionID, err = d.Int32()
if err != nil {
return err
}
p.Leader, err = d.Int32()
if err != nil {
return err
}
p.Replicas, err = d.Int32Array()
if err != nil {
return err
}
p.ISR, err = d.Int32Array()
if err != nil {
return err
}
partitions[i] = p
}
m.PartitionMetadata = partitions
r.TopicMetadata[i] = m
}
return nil
}
func (r *MetadataResponse) Version() int16 {
return r.APIVersion
}
type Brokers []*Broker
type TopicMetadatas []*TopicMetadata
type PartitionMetadatas []*PartitionMetadata