| 1 |
package kafka |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"net" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/segmentio/kafka-go/protocol/alterpartitionreassignments" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
type AlterPartitionReassignmentsRequest struct { |
| 13 |
|
| 14 |
Addr net.Addr |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
Topic string |
| 19 |
|
| 20 |
|
| 21 |
Assignments []AlterPartitionReassignmentsRequestAssignment |
| 22 |
|
| 23 |
|
| 24 |
Timeout time.Duration |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
type AlterPartitionReassignmentsRequestAssignment struct { |
| 30 |
|
| 31 |
Topic string |
| 32 |
|
| 33 |
|
| 34 |
PartitionID int |
| 35 |
|
| 36 |
|
| 37 |
BrokerIDs []int |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
type AlterPartitionReassignmentsResponse struct { |
| 42 |
|
| 43 |
|
| 44 |
Error error |
| 45 |
|
| 46 |
|
| 47 |
PartitionResults []AlterPartitionReassignmentsResponsePartitionResult |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
type AlterPartitionReassignmentsResponsePartitionResult struct { |
| 53 |
|
| 54 |
Topic string |
| 55 |
|
| 56 |
|
| 57 |
PartitionID int |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
Error error |
| 62 |
} |
| 63 |
|
| 64 |
func (c *Client) AlterPartitionReassignments( |
| 65 |
ctx context.Context, |
| 66 |
req *AlterPartitionReassignmentsRequest, |
| 67 |
) (*AlterPartitionReassignmentsResponse, error) { |
| 68 |
apiTopicMap := make(map[string]*alterpartitionreassignments.RequestTopic) |
| 69 |
|
| 70 |
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 |
|
| 84 |
replicas := []int32{} |
| 85 |
for _, brokerID := range assignment.BrokerIDs { |
| 86 |
replicas = append(replicas, int32(brokerID)) |
| 87 |
} |
| 88 |
|
| 89 |
apiTopic.Partitions = append( |
| 90 |
apiTopic.Partitions, |
| 91 |
alterpartitionreassignments.RequestPartition{ |
| 92 |
PartitionIndex: int32(assignment.PartitionID), |
| 93 |
Replicas: replicas, |
| 94 |
}, |
| 95 |
) |
| 96 |
} |
| 97 |
|
| 98 |
apiReq := &alterpartitionreassignments.Request{ |
| 99 |
TimeoutMs: int32(req.Timeout.Milliseconds()), |
| 100 |
} |
| 101 |
|
| 102 |
for _, apiTopic := range apiTopicMap { |
| 103 |
apiReq.Topics = append(apiReq.Topics, *apiTopic) |
| 104 |
} |
| 105 |
|
| 106 |
protoResp, err := c.roundTrip( |
| 107 |
ctx, |
| 108 |
req.Addr, |
| 109 |
apiReq, |
| 110 |
) |
| 111 |
if err != nil { |
| 112 |
return nil, err |
| 113 |
} |
| 114 |
apiResp := protoResp.(*alterpartitionreassignments.Response) |
| 115 |
|
| 116 |
resp := &AlterPartitionReassignmentsResponse{ |
| 117 |
Error: makeError(apiResp.ErrorCode, apiResp.ErrorMessage), |
| 118 |
} |
| 119 |
|
| 120 |
for _, topicResult := range apiResp.Results { |
| 121 |
for _, partitionResult := range topicResult.Partitions { |
| 122 |
resp.PartitionResults = append( |
| 123 |
resp.PartitionResults, |
| 124 |
AlterPartitionReassignmentsResponsePartitionResult{ |
| 125 |
Topic: topicResult.Name, |
| 126 |
PartitionID: int(partitionResult.PartitionIndex), |
| 127 |
Error: makeError(partitionResult.ErrorCode, partitionResult.ErrorMessage), |
| 128 |
}, |
| 129 |
) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return resp, nil |
| 134 |
} |