github.com/hoveychen/kafka-go@v0.4.42/addpartitionstotxn.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/hoveychen/kafka-go/protocol/addpartitionstotxn"
    10  )
    11  
    12  // AddPartitionToTxn represents a partition to be added
    13  // to a transaction.
    14  type AddPartitionToTxn struct {
    15  	// Partition is the ID of a partition to add to the transaction.
    16  	Partition int
    17  }
    18  
    19  // AddPartitionsToTxnRequest is the request structure fo the AddPartitionsToTxn function.
    20  type AddPartitionsToTxnRequest struct {
    21  	// Address of the kafka broker to send the request to.
    22  	Addr net.Addr
    23  
    24  	// The transactional id key
    25  	TransactionalID string
    26  
    27  	// The Producer ID (PID) for the current producer session;
    28  	// received from an InitProducerID request.
    29  	ProducerID int
    30  
    31  	// The epoch associated with the current producer session for the given PID
    32  	ProducerEpoch int
    33  
    34  	// Mappings of topic names to lists of partitions.
    35  	Topics map[string][]AddPartitionToTxn
    36  }
    37  
    38  // AddPartitionsToTxnResponse is the response structure for the AddPartitionsToTxn function.
    39  type AddPartitionsToTxnResponse struct {
    40  	// The amount of time that the broker throttled the request.
    41  	Throttle time.Duration
    42  
    43  	// Mappings of topic names to partitions being added to a transactions.
    44  	Topics map[string][]AddPartitionToTxnPartition
    45  }
    46  
    47  // AddPartitionToTxnPartition represents the state of a single partition
    48  // in response to adding to a transaction.
    49  type AddPartitionToTxnPartition struct {
    50  	// The ID of the partition.
    51  	Partition int
    52  
    53  	// An error that may have occurred when attempting to add the partition
    54  	// to a transaction.
    55  	//
    56  	// The errors contain the kafka error code. Programs may use the standard
    57  	// errors.Is function to test the error against kafka error codes.
    58  	Error error
    59  }
    60  
    61  // AddPartitionsToTnx sends an add partitions to txn request to a kafka broker and returns the response.
    62  func (c *Client) AddPartitionsToTxn(
    63  	ctx context.Context,
    64  	req *AddPartitionsToTxnRequest,
    65  ) (*AddPartitionsToTxnResponse, error) {
    66  	protoReq := &addpartitionstotxn.Request{
    67  		TransactionalID: req.TransactionalID,
    68  		ProducerID:      int64(req.ProducerID),
    69  		ProducerEpoch:   int16(req.ProducerEpoch),
    70  	}
    71  	protoReq.Topics = make([]addpartitionstotxn.RequestTopic, 0, len(req.Topics))
    72  
    73  	for topic, partitions := range req.Topics {
    74  		reqTopic := addpartitionstotxn.RequestTopic{
    75  			Name:       topic,
    76  			Partitions: make([]int32, len(partitions)),
    77  		}
    78  		for i, partition := range partitions {
    79  			reqTopic.Partitions[i] = int32(partition.Partition)
    80  		}
    81  		protoReq.Topics = append(protoReq.Topics, reqTopic)
    82  	}
    83  
    84  	m, err := c.roundTrip(ctx, req.Addr, protoReq)
    85  	if err != nil {
    86  		return nil, fmt.Errorf("kafka.(*Client).AddPartitionsToTxn: %w", err)
    87  	}
    88  
    89  	r := m.(*addpartitionstotxn.Response)
    90  
    91  	res := &AddPartitionsToTxnResponse{
    92  		Throttle: makeDuration(r.ThrottleTimeMs),
    93  		Topics:   make(map[string][]AddPartitionToTxnPartition, len(r.Results)),
    94  	}
    95  
    96  	for _, result := range r.Results {
    97  		partitions := make([]AddPartitionToTxnPartition, 0, len(result.Results))
    98  		for _, rp := range result.Results {
    99  			partitions = append(partitions, AddPartitionToTxnPartition{
   100  				Partition: int(rp.PartitionIndex),
   101  				Error:     makeError(rp.ErrorCode, ""),
   102  			})
   103  		}
   104  		res.Topics[result.Name] = partitions
   105  	}
   106  
   107  	return res, nil
   108  }