-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathalter_partition_reassignments_response.go
174 lines (141 loc) · 5.25 KB
/
alter_partition_reassignments_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
package healer
import (
"bytes"
"encoding/binary"
"fmt"
)
// AlterPartitionReassignmentsResponse is the response of AlterPartitionReassignmentsRequest
type AlterPartitionReassignmentsResponse struct {
ResponseHeader
ThrottleTimeMs int32 `json:"throttle_time_ms"`
ErrorCode int16 `json:"error_code"`
ErrorMsg *string `json:"error_msg"`
Responses []*alterPartitionReassignmentsResponseTopic `json:"responses"`
TaggedFields TaggedFields `json:"tagged_fields"`
}
func (r *AlterPartitionReassignmentsResponse) Error() error {
if r.ErrorCode != 0 {
return fmt.Errorf("%w: %s", KafkaError(r.ErrorCode), *r.ErrorMsg)
}
for _, t := range r.Responses {
for _, p := range t.Partitions {
if p.ErrorCode != 0 {
return fmt.Errorf("%w: %s", KafkaError(p.ErrorCode), *p.ErrorMsg)
}
}
}
return nil
}
// just for test
func (r *AlterPartitionReassignmentsResponse) Encode(version uint16) (payload []byte) {
defer func() {
length := len(payload)
binary.BigEndian.PutUint32(payload, uint32(length-4))
payload = payload[:length]
}()
buf := &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, uint32(0)) // length
buf.Write(r.ResponseHeader.Encode())
binary.Write(buf, binary.BigEndian, r.ThrottleTimeMs)
binary.Write(buf, binary.BigEndian, r.ErrorCode)
writeCompactNullableString(buf, r.ErrorMsg)
buf.Write(encodeCompactArrayLength(len(r.Responses)))
for _, t := range r.Responses {
buf.Write(t.encode())
}
buf.Write(r.TaggedFields.Encode())
return buf.Bytes()
}
// alterPartitionReassignmentsResponseTopic is the response of AlterPartitionReassignmentsRequest
type alterPartitionReassignmentsResponseTopic struct {
Name string `json:"name"`
Partitions []*alterPartitionReassignmentsResponseTopicPartition `json:"partitions"`
TaggedFields TaggedFields `json:"tagged_fields"`
}
func (r *alterPartitionReassignmentsResponseTopic) encode() []byte {
buf := &bytes.Buffer{}
writeCompactString(buf, r.Name)
buf.Write(encodeCompactArrayLength(len(r.Partitions)))
for _, p := range r.Partitions {
buf.Write(p.encode())
}
buf.Write(r.TaggedFields.Encode())
return buf.Bytes()
}
type alterPartitionReassignmentsResponseTopicPartition struct {
PartitionID int32 `json:"partition_id"`
ErrorCode int16 `json:"error_code"`
ErrorMsg *string `json:"error_msg"`
TaggedFields TaggedFields `json:"tagged_fields"`
}
func (r *alterPartitionReassignmentsResponseTopicPartition) encode() []byte {
buf := &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, r.PartitionID)
binary.Write(buf, binary.BigEndian, r.ErrorCode)
writeCompactNullableString(buf, r.ErrorMsg)
buf.Write(r.TaggedFields.Encode())
return buf.Bytes()
}
func decodeToAlterPartitionReassignmentsResponseTopicPartition(payload []byte, version uint16) (p *alterPartitionReassignmentsResponseTopicPartition, length int) {
p = &alterPartitionReassignmentsResponseTopicPartition{}
offset := 0
p.PartitionID = int32(binary.BigEndian.Uint32(payload[offset:]))
offset += 4
p.ErrorCode = int16(binary.BigEndian.Uint16(payload[offset:]))
offset += 2
errorMsg, o := compactNullableString(payload[offset:])
offset += o
p.ErrorMsg = errorMsg
taggedFields, n := DecodeTaggedFields(payload[offset:])
offset += n
p.TaggedFields = taggedFields
return p, offset
}
// NewAlterPartitionReassignmentsResponse create a new AlterPartitionReassignmentsResponse
func NewAlterPartitionReassignmentsResponse(payload []byte, version uint16) (*AlterPartitionReassignmentsResponse, error) {
responseLength := int32(binary.BigEndian.Uint32(payload))
if int(responseLength)+4 != len(payload) {
return nil, fmt.Errorf("AlterPartitionReassignments response length did not match: %d!=%d", responseLength+4, len(payload))
}
offset := 4
header, o := DecodeResponseHeader(payload[offset:], API_AlterPartitionReassignments, version)
offset += o
r := &AlterPartitionReassignmentsResponse{
ResponseHeader: header,
}
r.ThrottleTimeMs = int32(binary.BigEndian.Uint32(payload[offset:]))
offset += 4
r.ErrorCode = int16(binary.BigEndian.Uint16(payload[offset:]))
offset += 2
errorMsg, o := compactNullableString(payload[offset:])
offset += o
r.ErrorMsg = errorMsg
responseCount, o := compactArrayLength(payload[offset:])
offset += o
if responseCount < 0 { // actually it should be -1
r.Responses = nil
} else {
r.Responses = make([]*alterPartitionReassignmentsResponseTopic, responseCount)
for i := 0; i < int(responseCount); i++ {
topic := &alterPartitionReassignmentsResponseTopic{}
r.Responses[i] = topic
topic.Name, o = compactString(payload[offset:])
offset += o
partitionCount, o := compactArrayLength(payload[offset:])
offset += o
if partitionCount <= 0 {
topic.Partitions = nil
}
topic.Partitions = make([]*alterPartitionReassignmentsResponseTopicPartition, partitionCount)
for j := 0; j < int(partitionCount); j++ {
topic.Partitions[j], o = decodeToAlterPartitionReassignmentsResponseTopicPartition(payload[offset:], version)
offset += o
}
topic.TaggedFields, o = DecodeTaggedFields(payload[offset:])
offset += o
}
}
r.TaggedFields, o = DecodeTaggedFields(payload[offset:])
offset += o
return r, nil
}