|
|
@@ -78,7 +78,33 @@ func (bm *brokerManager) getValidLeader(topic string, partition_id int32) (*brok
|
|
|
return leader, nil
|
|
|
}
|
|
|
|
|
|
-func (bm *brokerManager) tryLeader(topic string, partition int32, req encoder, res decoder) error {
|
|
|
+func (bm *brokerManager) choosePartition(topic string, p partitionChooser) (int32, error) {
|
|
|
+ bm.lock.RLock()
|
|
|
+ id_map := bm.partitions[topic]
|
|
|
+ if id_map == nil {
|
|
|
+ bm.lock.RUnlock()
|
|
|
+ err := bm.refreshTopic(topic)
|
|
|
+ if err != nil {
|
|
|
+ return -1, err
|
|
|
+ }
|
|
|
+ bm.lock.RLock()
|
|
|
+ id_map = bm.partitions[topic]
|
|
|
+ if id_map == nil {
|
|
|
+ bm.lock.RUnlock()
|
|
|
+ return -1, UNKNOWN_TOPIC_OR_PARTITION
|
|
|
+ }
|
|
|
+ }
|
|
|
+ partitions := make([]int32, len(id_map))
|
|
|
+ i := 0
|
|
|
+ for id, _ := range id_map {
|
|
|
+ partitions[i] = id
|
|
|
+ i++
|
|
|
+ }
|
|
|
+ bm.lock.RUnlock()
|
|
|
+ return p.choosePartition(partitions), nil
|
|
|
+}
|
|
|
+
|
|
|
+func (bm *brokerManager) sendToPartition(topic string, partition int32, req encoder, res decoder) error {
|
|
|
b, err := bm.getValidLeader(topic, partition)
|
|
|
if err != nil {
|
|
|
return err
|