| 1 |
package kafka |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"net" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/segmentio/kafka-go/protocol/addpartitionstotxn" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
type AddPartitionToTxn struct { |
| 15 |
|
| 16 |
Partition int |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
type AddPartitionsToTxnRequest struct { |
| 21 |
|
| 22 |
Addr net.Addr |
| 23 |
|
| 24 |
|
| 25 |
TransactionalID string |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
ProducerID int |
| 30 |
|
| 31 |
|
| 32 |
ProducerEpoch int |
| 33 |
|
| 34 |
|
| 35 |
Topics map[string][]AddPartitionToTxn |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
type AddPartitionsToTxnResponse struct { |
| 40 |
|
| 41 |
Throttle time.Duration |
| 42 |
|
| 43 |
|
| 44 |
Topics map[string][]AddPartitionToTxnPartition |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
type AddPartitionToTxnPartition struct { |
| 50 |
|
| 51 |
Partition int |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
Error error |
| 59 |
} |
| 60 |
|
| 61 |
|
| 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 |
} |