@@ -943,48 +943,101 @@ func (c *Conn) ReadPartitions(topics ...string) (partitions []Partition, err err
943
943
topics = nil
944
944
}
945
945
}
946
+ metadataVersion , err := c .negotiateVersion (metadata , v1 , v6 )
947
+ if err != nil {
948
+ return nil , err
949
+ }
946
950
947
951
err = c .readOperation (
948
952
func (deadline time.Time , id int32 ) error {
949
- return c .writeRequest (metadata , v1 , id , topicMetadataRequestV1 (topics ))
953
+ switch metadataVersion {
954
+ case v6 :
955
+ return c .writeRequest (metadata , v6 , id , topicMetadataRequestV6 {Topics : topics , AllowAutoTopicCreation : true })
956
+ default :
957
+ return c .writeRequest (metadata , v1 , id , topicMetadataRequestV1 (topics ))
958
+ }
950
959
},
951
960
func (deadline time.Time , size int ) error {
952
- var res metadataResponseV1
961
+ partitions , err = c .readPartitionsResponse (metadataVersion , size )
962
+ return err
963
+ },
964
+ )
965
+ return
966
+ }
953
967
954
- if err := c .readResponse (size , & res ); err != nil {
955
- return err
956
- }
968
+ func (c * Conn ) readPartitionsResponse (metadataVersion apiVersion , size int ) ([]Partition , error ) {
969
+ switch metadataVersion {
970
+ case v6 :
971
+ var res metadataResponseV6
972
+ if err := c .readResponse (size , & res ); err != nil {
973
+ return nil , err
974
+ }
975
+ brokers := readBrokerMetadata (res .Brokers )
976
+ return c .readTopicMetadatav6 (brokers , res .Topics )
977
+ default :
978
+ var res metadataResponseV1
979
+ if err := c .readResponse (size , & res ); err != nil {
980
+ return nil , err
981
+ }
982
+ brokers := readBrokerMetadata (res .Brokers )
983
+ return c .readTopicMetadatav1 (brokers , res .Topics )
984
+ }
985
+ }
957
986
958
- brokers := make (map [int32 ]Broker , len (res .Brokers ))
959
- for _ , b := range res .Brokers {
960
- brokers [b .NodeID ] = Broker {
961
- Host : b .Host ,
962
- Port : int (b .Port ),
963
- ID : int (b .NodeID ),
964
- Rack : b .Rack ,
965
- }
966
- }
987
+ func readBrokerMetadata (brokerMetadata []brokerMetadataV1 ) map [int32 ]Broker {
988
+ brokers := make (map [int32 ]Broker , len (brokerMetadata ))
989
+ for _ , b := range brokerMetadata {
990
+ brokers [b .NodeID ] = Broker {
991
+ Host : b .Host ,
992
+ Port : int (b .Port ),
993
+ ID : int (b .NodeID ),
994
+ Rack : b .Rack ,
995
+ }
996
+ }
997
+ return brokers
998
+ }
967
999
968
- for _ , t := range res .Topics {
969
- if t .TopicErrorCode != 0 && (c .topic == "" || t .TopicName == c .topic ) {
970
- // We only report errors if they happened for the topic of
971
- // the connection, otherwise the topic will simply have no
972
- // partitions in the result set.
973
- return Error (t .TopicErrorCode )
974
- }
975
- for _ , p := range t .Partitions {
976
- partitions = append (partitions , Partition {
977
- Topic : t .TopicName ,
978
- Leader : brokers [p .Leader ],
979
- Replicas : makeBrokers (brokers , p .Replicas ... ),
980
- Isr : makeBrokers (brokers , p .Isr ... ),
981
- ID : int (p .PartitionID ),
982
- })
983
- }
984
- }
985
- return nil
986
- },
987
- )
1000
+ func (c * Conn ) readTopicMetadatav1 (brokers map [int32 ]Broker , topicMetadata []topicMetadataV1 ) (partitions []Partition , err error ) {
1001
+ for _ , t := range topicMetadata {
1002
+ if t .TopicErrorCode != 0 && (c .topic == "" || t .TopicName == c .topic ) {
1003
+ // We only report errors if they happened for the topic of
1004
+ // the connection, otherwise the topic will simply have no
1005
+ // partitions in the result set.
1006
+ return nil , Error (t .TopicErrorCode )
1007
+ }
1008
+ for _ , p := range t .Partitions {
1009
+ partitions = append (partitions , Partition {
1010
+ Topic : t .TopicName ,
1011
+ Leader : brokers [p .Leader ],
1012
+ Replicas : makeBrokers (brokers , p .Replicas ... ),
1013
+ Isr : makeBrokers (brokers , p .Isr ... ),
1014
+ ID : int (p .PartitionID ),
1015
+ OfflineReplicas : []Broker {},
1016
+ })
1017
+ }
1018
+ }
1019
+ return
1020
+ }
1021
+
1022
+ func (c * Conn ) readTopicMetadatav6 (brokers map [int32 ]Broker , topicMetadata []topicMetadataV6 ) (partitions []Partition , err error ) {
1023
+ for _ , t := range topicMetadata {
1024
+ if t .TopicErrorCode != 0 && (c .topic == "" || t .TopicName == c .topic ) {
1025
+ // We only report errors if they happened for the topic of
1026
+ // the connection, otherwise the topic will simply have no
1027
+ // partitions in the result set.
1028
+ return nil , Error (t .TopicErrorCode )
1029
+ }
1030
+ for _ , p := range t .Partitions {
1031
+ partitions = append (partitions , Partition {
1032
+ Topic : t .TopicName ,
1033
+ Leader : brokers [p .Leader ],
1034
+ Replicas : makeBrokers (brokers , p .Replicas ... ),
1035
+ Isr : makeBrokers (brokers , p .Isr ... ),
1036
+ ID : int (p .PartitionID ),
1037
+ OfflineReplicas : makeBrokers (brokers , p .OfflineReplicas ... ),
1038
+ })
1039
+ }
1040
+ }
988
1041
return
989
1042
}
990
1043
0 commit comments