@@ -13,7 +13,8 @@ type AlterPartitionReassignmentsRequest struct {
1313 // Address of the kafka broker to send the request to.
1414 Addr net.Addr
1515
16- // Topic is the name of the topic to alter partitions in.
16+ // Topic is the name of the topic to alter partitions in. Keep this field empty and use Topic in AlterPartitionReassignmentsRequestAssignment to
17+ // reassign to multiple topics.
1718 Topic string
1819
1920 // Assignments is the list of partition reassignments to submit to the API.
@@ -26,10 +27,13 @@ type AlterPartitionReassignmentsRequest struct {
2627// AlterPartitionReassignmentsRequestAssignment contains the requested reassignments for a single
2728// partition.
2829type AlterPartitionReassignmentsRequestAssignment struct {
30+ // Topic is the name of the topic to alter partitions in. If empty, the value of Topic in AlterPartitionReassignmentsRequest is used.
31+ Topic string
32+
2933 // PartitionID is the ID of the partition to make the reassignments in.
3034 PartitionID int
3135
32- // BrokerIDs is a slice of brokers to set the partition replicas to.
36+ // BrokerIDs is a slice of brokers to set the partition replicas to, or null to cancel a pending reassignment for this partition .
3337 BrokerIDs []int
3438}
3539
@@ -46,6 +50,9 @@ type AlterPartitionReassignmentsResponse struct {
4650// AlterPartitionReassignmentsResponsePartitionResult contains the detailed result of
4751// doing reassignments for a single partition.
4852type AlterPartitionReassignmentsResponsePartitionResult struct {
53+ // Topic is the topic name.
54+ Topic string
55+
4956 // PartitionID is the ID of the partition that was altered.
5057 PartitionID int
5158
@@ -58,16 +65,29 @@ func (c *Client) AlterPartitionReassignments(
5865 ctx context.Context ,
5966 req * AlterPartitionReassignmentsRequest ,
6067) (* AlterPartitionReassignmentsResponse , error ) {
61- apiPartitions := [] alterpartitionreassignments.RequestPartition {}
68+ apiTopicMap := make ( map [ string ] * alterpartitionreassignments.RequestTopic )
6269
6370 for _ , assignment := range req .Assignments {
71+ topic := assignment .Topic
72+ if topic == "" {
73+ topic = req .Topic
74+ }
75+
76+ apiTopic := apiTopicMap [topic ]
77+ if apiTopic == nil {
78+ apiTopic = & alterpartitionreassignments.RequestTopic {
79+ Name : topic ,
80+ }
81+ apiTopicMap [topic ] = apiTopic
82+ }
83+
6484 replicas := []int32 {}
6585 for _ , brokerID := range assignment .BrokerIDs {
6686 replicas = append (replicas , int32 (brokerID ))
6787 }
6888
69- apiPartitions = append (
70- apiPartitions ,
89+ apiTopic . Partitions = append (
90+ apiTopic . Partitions ,
7191 alterpartitionreassignments.RequestPartition {
7292 PartitionIndex : int32 (assignment .PartitionID ),
7393 Replicas : replicas ,
@@ -77,12 +97,10 @@ func (c *Client) AlterPartitionReassignments(
7797
7898 apiReq := & alterpartitionreassignments.Request {
7999 TimeoutMs : int32 (req .Timeout .Milliseconds ()),
80- Topics : []alterpartitionreassignments.RequestTopic {
81- {
82- Name : req .Topic ,
83- Partitions : apiPartitions ,
84- },
85- },
100+ }
101+
102+ for _ , apiTopic := range apiTopicMap {
103+ apiReq .Topics = append (apiReq .Topics , * apiTopic )
86104 }
87105
88106 protoResp , err := c .roundTrip (
@@ -104,6 +122,7 @@ func (c *Client) AlterPartitionReassignments(
104122 resp .PartitionResults = append (
105123 resp .PartitionResults ,
106124 AlterPartitionReassignmentsResponsePartitionResult {
125+ Topic : topicResult .Name ,
107126 PartitionID : int (partitionResult .PartitionIndex ),
108127 Error : makeError (partitionResult .ErrorCode , partitionResult .ErrorMessage ),
109128 },
0 commit comments