-
Notifications
You must be signed in to change notification settings - Fork 18
/
elect_leaders_request.go
95 lines (79 loc) · 2.44 KB
/
elect_leaders_request.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
package healer
import "encoding/binary"
type ElectLeadersRequest struct {
*RequestHeader
ElectionType int8 `json:"election_type"`
Topics []*TopicPartition `json:"topics"`
TimeoutMS int32 `json:"timeout.ms"`
}
type TopicPartition struct {
Topic string `json:"topic"`
Partitions []int32 `json:"partitions"`
}
// NewElectLeadersRequest returns a new ElectLeadersRequest
func NewElectLeadersRequest(timeoutMS int32) ElectLeadersRequest {
return ElectLeadersRequest{
RequestHeader: &RequestHeader{
APIKey: API_ElectLeaders,
},
Topics: make([]*TopicPartition, 0),
TimeoutMS: timeoutMS,
}
}
// Add adds a topic partition to the request, it does not check if the topic partition already exists
func (r *ElectLeadersRequest) Add(topic string, pid int32) {
for _, t := range r.Topics {
if t.Topic == topic {
t.Partitions = append(t.Partitions, pid)
return
}
}
r.Topics = append(r.Topics, &TopicPartition{
Topic: topic,
Partitions: []int32{pid},
})
return
}
func (r *TopicPartition) encode(payload []byte, version uint16) (offset int) {
binary.BigEndian.PutUint16(payload[offset:], uint16(len(r.Topic)))
offset += 2
offset += copy(payload[offset:], r.Topic)
binary.BigEndian.PutUint32(payload[offset:], uint32(len(r.Partitions)))
offset += 4
for _, partition := range r.Partitions {
binary.BigEndian.PutUint32(payload[offset:], uint32(partition))
offset += 4
}
return
}
func (r *ElectLeadersRequest) length(version uint16) int {
length := r.RequestHeader.length()
length += 4 // timeout_ms
length++ // election_type
length += 4 // topics length
for _, topic := range r.Topics {
length += 2 + len(topic.Topic) // topic
length += 4 // partitions length
length += 4 * len(topic.Partitions) // partitions
}
return length
}
// Encode encodes a create partitions request into []byte
func (r *ElectLeadersRequest) Encode(version uint16) []byte {
requestLength := r.length(version)
payload := make([]byte, requestLength+4)
offset := 0
defer func() {
binary.BigEndian.PutUint32(payload, uint32(offset-4))
}()
offset = 4
offset += r.RequestHeader.EncodeTo(payload[offset:])
binary.BigEndian.PutUint32(payload[offset:], uint32(len(r.Topics)))
offset += 4
for _, topic := range r.Topics {
offset += topic.encode(payload[offset:], version)
}
binary.BigEndian.PutUint32(payload[offset:], uint32(r.TimeoutMS))
offset += 4
return payload[:offset]
}