github.com/hack0072008/kafka-go@v1.0.1/alterpartitionreassignments.go (about) 1 package kafka 2 3 import ( 4 "context" 5 "net" 6 "time" 7 8 "github.com/hack0072008/kafka-go/protocol/alterpartitionreassignments" 9 ) 10 11 // AlterPartitionReassignmentsRequest is a request to the AlterPartitionReassignments API. 12 type AlterPartitionReassignmentsRequest struct { 13 // Address of the kafka broker to send the request to. 14 Addr net.Addr 15 16 // Topic is the name of the topic to alter partitions in. 17 Topic string 18 19 // Assignments is the list of partition reassignments to submit to the API. 20 Assignments []AlterPartitionReassignmentsRequestAssignment 21 22 // Timeout is the amount of time to wait for the request to complete. 23 Timeout time.Duration 24 } 25 26 // AlterPartitionReassignmentsRequestAssignment contains the requested reassignments for a single 27 // partition. 28 type AlterPartitionReassignmentsRequestAssignment struct { 29 // PartitionID is the ID of the partition to make the reassignments in. 30 PartitionID int 31 32 // BrokerIDs is a slice of brokers to set the partition replicas to. 33 BrokerIDs []int 34 } 35 36 // AlterPartitionReassignmentsResponse is a response from the AlterPartitionReassignments API. 37 type AlterPartitionReassignmentsResponse struct { 38 // Error is set to a non-nil value including the code and message if a top-level 39 // error was encountered when doing the update. 40 Error error 41 42 // PartitionResults contains the specific results for each partition. 43 PartitionResults []AlterPartitionReassignmentsResponsePartitionResult 44 } 45 46 // AlterPartitionReassignmentsResponsePartitionResult contains the detailed result of 47 // doing reassignments for a single partition. 48 type AlterPartitionReassignmentsResponsePartitionResult struct { 49 // PartitionID is the ID of the partition that was altered. 50 PartitionID int 51 52 // Error is set to a non-nil value including the code and message if an error was encountered 53 // during the update for this partition. 54 Error error 55 } 56 57 func (c *Client) AlterPartitionReassignments( 58 ctx context.Context, 59 req *AlterPartitionReassignmentsRequest, 60 ) (*AlterPartitionReassignmentsResponse, error) { 61 apiPartitions := []alterpartitionreassignments.RequestPartition{} 62 63 for _, assignment := range req.Assignments { 64 replicas := []int32{} 65 for _, brokerID := range assignment.BrokerIDs { 66 replicas = append(replicas, int32(brokerID)) 67 } 68 69 apiPartitions = append( 70 apiPartitions, 71 alterpartitionreassignments.RequestPartition{ 72 PartitionIndex: int32(assignment.PartitionID), 73 Replicas: replicas, 74 }, 75 ) 76 } 77 78 apiReq := &alterpartitionreassignments.Request{ 79 TimeoutMs: int32(req.Timeout.Milliseconds()), 80 Topics: []alterpartitionreassignments.RequestTopic{ 81 { 82 Name: req.Topic, 83 Partitions: apiPartitions, 84 }, 85 }, 86 } 87 88 protoResp, err := c.roundTrip( 89 ctx, 90 req.Addr, 91 apiReq, 92 ) 93 if err != nil { 94 return nil, err 95 } 96 apiResp := protoResp.(*alterpartitionreassignments.Response) 97 98 resp := &AlterPartitionReassignmentsResponse{ 99 Error: makeError(apiResp.ErrorCode, apiResp.ErrorMessage), 100 } 101 102 for _, topicResult := range apiResp.Results { 103 for _, partitionResult := range topicResult.Partitions { 104 resp.PartitionResults = append( 105 resp.PartitionResults, 106 AlterPartitionReassignmentsResponsePartitionResult{ 107 PartitionID: int(partitionResult.PartitionIndex), 108 Error: makeError(partitionResult.ErrorCode, partitionResult.ErrorMessage), 109 }, 110 ) 111 } 112 } 113 114 return resp, nil 115 }