-
Notifications
You must be signed in to change notification settings - Fork 18
/
elect_leaders_response.go
97 lines (78 loc) · 2.96 KB
/
elect_leaders_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
package healer
import (
"encoding/binary"
"fmt"
)
type ElectLeadersResponse struct {
CorrelationID uint32 `json:"correlation_id"`
ThrottleTimeMS int32 `json:"throttle_time_ms"`
ReplicaElectionResults []*ReplicaElectionResult `json:"replica_election_results"`
}
func (r *ElectLeadersResponse) Error() error {
for _, replicaElectionResult := range r.ReplicaElectionResults {
for _, partitionResult := range replicaElectionResult.PartitionResults {
if partitionResult.ErrorCode != 0 {
err := KafkaError(partitionResult.ErrorCode)
return fmt.Errorf("%s-%d elect leader error: %w. error message: %s", replicaElectionResult.Topic, partitionResult.PartitionID, err, partitionResult.ErrorMessage)
}
}
}
return nil
}
type ReplicaElectionResult struct {
Topic string `json:"topic"`
PartitionResults []*PartitionResult `json:"partition_result"`
}
func newReplicaElectionResult(payload []byte, offset int, version uint16) (r *ReplicaElectionResult, nextOffset int) {
r = &ReplicaElectionResult{}
length := int(binary.BigEndian.Uint16(payload[offset:]))
offset += 2
r.Topic = string(payload[offset : offset+length])
offset += length
numPartitionResults := binary.BigEndian.Uint32(payload[offset:])
offset += 4
r.PartitionResults = make([]*PartitionResult, numPartitionResults)
for i := range r.PartitionResults {
r.PartitionResults[i], offset = newPartitionResult(payload, offset, version)
}
return r, offset
}
type PartitionResult struct {
PartitionID int32 `json:"partition_id"`
ErrorCode int16 `json:"error_code"`
ErrorMessage string `json:"error_message"`
}
func newPartitionResult(payload []byte, offset int, version uint16) (r *PartitionResult, nextOffset int) {
r = &PartitionResult{}
r.PartitionID = int32(binary.BigEndian.Uint32(payload[offset:]))
offset += 4
r.ErrorCode = int16(binary.BigEndian.Uint16(payload[offset:]))
offset += 2
length := int16(binary.BigEndian.Uint16(payload[offset:]))
offset += 2
if length > 0 {
r.ErrorMessage = string(payload[offset : offset+int(length)])
offset += int(length)
}
return r, offset
}
// NewElectLeadersResponse creates a new ElectLeadersResponse.
func NewElectLeadersResponse(payload []byte, version uint16) (r *ElectLeadersResponse, err error) {
r = &ElectLeadersResponse{}
responseLength := int(binary.BigEndian.Uint32(payload))
if responseLength+4 != len(payload) {
return r, fmt.Errorf("response length did not match: %d != %d", responseLength+4, len(payload))
}
offset := 4
r.CorrelationID = binary.BigEndian.Uint32(payload[offset:])
offset += 4
r.ThrottleTimeMS = int32(binary.BigEndian.Uint32(payload[offset:]))
offset += 4
numReplicaElectionResults := binary.BigEndian.Uint32(payload[offset:])
offset += 4
r.ReplicaElectionResults = make([]*ReplicaElectionResult, numReplicaElectionResults)
for i := range r.ReplicaElectionResults {
r.ReplicaElectionResults[i], offset = newReplicaElectionResult(payload, offset, version)
}
return
}