-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin): implement leader election api
Signed-off-by: joey <[email protected]>
- Loading branch information
Showing
10 changed files
with
483 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package sarama | ||
|
||
type ElectLeadersRequest struct { | ||
Version int16 | ||
Type ElectionType | ||
TopicPartitions map[string][]int32 | ||
TimeoutMs int32 | ||
} | ||
|
||
func (r *ElectLeadersRequest) encode(pe packetEncoder) error { | ||
if r.Version > 0 { | ||
pe.putInt8(int8(r.Type)) | ||
} | ||
|
||
pe.putCompactArrayLength(len(r.TopicPartitions)) | ||
|
||
for topic, partitions := range r.TopicPartitions { | ||
if r.Version < 2 { | ||
if err := pe.putString(topic); err != nil { | ||
return err | ||
} | ||
} else { | ||
if err := pe.putCompactString(topic); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if err := pe.putCompactInt32Array(partitions); err != nil { | ||
return err | ||
} | ||
|
||
if r.Version >= 2 { | ||
pe.putEmptyTaggedFieldArray() | ||
} | ||
} | ||
|
||
pe.putInt32(r.TimeoutMs) | ||
|
||
if r.Version >= 2 { | ||
pe.putEmptyTaggedFieldArray() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *ElectLeadersRequest) decode(pd packetDecoder, version int16) (err error) { | ||
r.Version = version | ||
if r.Version > 0 { | ||
t, err := pd.getInt8() | ||
if err != nil { | ||
return err | ||
} | ||
r.Type = ElectionType(t) | ||
} | ||
|
||
topicCount, err := pd.getCompactArrayLength() | ||
if err != nil { | ||
return err | ||
} | ||
if topicCount > 0 { | ||
r.TopicPartitions = make(map[string][]int32) | ||
for i := 0; i < topicCount; i++ { | ||
var topic string | ||
if r.Version < 2 { | ||
topic, err = pd.getString() | ||
} else { | ||
topic, err = pd.getCompactString() | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
partitionCount, err := pd.getCompactArrayLength() | ||
if err != nil { | ||
return err | ||
} | ||
partitions := make([]int32, partitionCount) | ||
for j := 0; j < partitionCount; j++ { | ||
partition, err := pd.getInt32() | ||
if err != nil { | ||
return err | ||
} | ||
partitions[j] = partition | ||
} | ||
r.TopicPartitions[topic] = partitions | ||
if r.Version >= 2 { | ||
if _, err := pd.getEmptyTaggedFieldArray(); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
|
||
r.TimeoutMs, err = pd.getInt32() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if r.Version >= 2 { | ||
if _, err := pd.getEmptyTaggedFieldArray(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *ElectLeadersRequest) key() int16 { | ||
return 43 | ||
} | ||
|
||
func (r *ElectLeadersRequest) version() int16 { | ||
return r.Version | ||
} | ||
|
||
func (r *ElectLeadersRequest) headerVersion() int16 { | ||
return 2 | ||
} | ||
|
||
func (r *ElectLeadersRequest) isValidVersion() bool { | ||
return r.Version >= 0 && r.Version <= 2 | ||
} | ||
|
||
func (r *ElectLeadersRequest) requiredVersion() KafkaVersion { | ||
switch r.Version { | ||
case 2: | ||
return V2_4_0_0 | ||
case 1: | ||
return V0_11_0_0 | ||
case 0: | ||
return V0_10_0_0 | ||
default: | ||
return V2_4_0_0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package sarama | ||
|
||
import "testing" | ||
|
||
var electLeadersRequestOneTopic = []byte{ | ||
0, // preferred election type | ||
2, // 2-1=1 topic | ||
6, 116, 111, 112, 105, 99, // topic name "topic" as compact string | ||
2, // 2-1=1 partition | ||
0, 0, 0, 0, // partition 0 | ||
0, 0, // empty tagged fields | ||
0, 39, 16, 0, // timeout 10000 | ||
} | ||
|
||
func TestElectLeadersRequest(t *testing.T) { | ||
var request = &ElectLeadersRequest{ | ||
TimeoutMs: int32(10000), | ||
Version: int16(2), | ||
TopicPartitions: map[string][]int32{ | ||
"topic": {0}, | ||
}, | ||
Type: PreferredElection, | ||
} | ||
|
||
testRequest(t, "one topic", request, electLeadersRequestOneTopic) | ||
} |
Oops, something went wrong.