github.com/deanMdreon/kafka-go@v0.4.32/electleaders.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"time"
     7  
     8  	"github.com/deanMdreon/kafka-go/protocol/electleaders"
     9  )
    10  
    11  // ElectLeadersRequest is a request to the ElectLeaders API.
    12  type ElectLeadersRequest struct {
    13  	// Addr is the address of the kafka broker to send the request to.
    14  	Addr net.Addr
    15  
    16  	// Topic is the name of the topic to do the leader elections in.
    17  	Topic string
    18  
    19  	// Partitions is the list of partitions to run leader elections for.
    20  	Partitions []int
    21  
    22  	// Timeout is the amount of time to wait for the election to run.
    23  	Timeout time.Duration
    24  }
    25  
    26  // ElectLeadersResponse is a response from the ElectLeaders API.
    27  type ElectLeadersResponse struct {
    28  	// ErrorCode is set to a non-nil value if a top-level error occurred.
    29  	Error error
    30  
    31  	// PartitionResults contains the results for each partition leader election.
    32  	PartitionResults []ElectLeadersResponsePartitionResult
    33  }
    34  
    35  // ElectLeadersResponsePartitionResult contains the response details for a single partition.
    36  type ElectLeadersResponsePartitionResult struct {
    37  	// Partition is the ID of the partition.
    38  	Partition int
    39  
    40  	// Error is set to a non-nil value if an error occurred electing leaders
    41  	// for this partition.
    42  	Error error
    43  }
    44  
    45  func (c *Client) ElectLeaders(
    46  	ctx context.Context,
    47  	req *ElectLeadersRequest,
    48  ) (*ElectLeadersResponse, error) {
    49  	partitions32 := []int32{}
    50  	for _, partition := range req.Partitions {
    51  		partitions32 = append(partitions32, int32(partition))
    52  	}
    53  
    54  	protoResp, err := c.roundTrip(
    55  		ctx,
    56  		req.Addr,
    57  		&electleaders.Request{
    58  			TopicPartitions: []electleaders.RequestTopicPartitions{
    59  				{
    60  					Topic:        req.Topic,
    61  					PartitionIDs: partitions32,
    62  				},
    63  			},
    64  			TimeoutMs: int32(req.Timeout.Milliseconds()),
    65  		},
    66  	)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	apiResp := protoResp.(*electleaders.Response)
    71  
    72  	resp := &ElectLeadersResponse{
    73  		Error: makeError(apiResp.ErrorCode, ""),
    74  	}
    75  
    76  	for _, topicResult := range apiResp.ReplicaElectionResults {
    77  		for _, partitionResult := range topicResult.PartitionResults {
    78  			resp.PartitionResults = append(
    79  				resp.PartitionResults,
    80  				ElectLeadersResponsePartitionResult{
    81  					Partition: int(partitionResult.PartitionID),
    82  					Error:     makeError(partitionResult.ErrorCode, partitionResult.ErrorMessage),
    83  				},
    84  			)
    85  		}
    86  	}
    87  
    88  	return resp, nil
    89  }