github.com/streamdal/segmentio-kafka-go@v0.4.47-streamdal/createpartitions.go (about) 1 package kafka 2 3 import ( 4 "context" 5 "fmt" 6 "net" 7 "time" 8 9 "github.com/segmentio/kafka-go/protocol/createpartitions" 10 ) 11 12 // CreatePartitionsRequest represents a request sent to a kafka broker to create 13 // and update topic parititions. 14 type CreatePartitionsRequest struct { 15 // Address of the kafka broker to send the request to. 16 Addr net.Addr 17 18 // List of topics to create and their configuration. 19 Topics []TopicPartitionsConfig 20 21 // When set to true, topics are not created but the configuration is 22 // validated as if they were. 23 ValidateOnly bool 24 } 25 26 // CreatePartitionsResponse represents a response from a kafka broker to a partition 27 // creation request. 28 type CreatePartitionsResponse struct { 29 // The amount of time that the broker throttled the request. 30 Throttle time.Duration 31 32 // Mapping of topic names to errors that occurred while attempting to create 33 // the topics. 34 // 35 // The errors contain the kafka error code. Programs may use the standard 36 // errors.Is function to test the error against kafka error codes. 37 Errors map[string]error 38 } 39 40 // CreatePartitions sends a partitions creation request to a kafka broker and returns the 41 // response. 42 func (c *Client) CreatePartitions(ctx context.Context, req *CreatePartitionsRequest) (*CreatePartitionsResponse, error) { 43 topics := make([]createpartitions.RequestTopic, len(req.Topics)) 44 45 for i, t := range req.Topics { 46 topics[i] = createpartitions.RequestTopic{ 47 Name: t.Name, 48 Count: t.Count, 49 Assignments: t.assignments(), 50 } 51 } 52 53 m, err := c.roundTrip(ctx, req.Addr, &createpartitions.Request{ 54 Topics: topics, 55 TimeoutMs: c.timeoutMs(ctx, defaultCreatePartitionsTimeout), 56 ValidateOnly: req.ValidateOnly, 57 }) 58 59 if err != nil { 60 return nil, fmt.Errorf("kafka.(*Client).CreatePartitions: %w", err) 61 } 62 63 res := m.(*createpartitions.Response) 64 ret := &CreatePartitionsResponse{ 65 Throttle: makeDuration(res.ThrottleTimeMs), 66 Errors: make(map[string]error, len(res.Results)), 67 } 68 69 for _, t := range res.Results { 70 ret.Errors[t.Name] = makeError(t.ErrorCode, t.ErrorMessage) 71 } 72 73 return ret, nil 74 } 75 76 type TopicPartitionsConfig struct { 77 // Topic name 78 Name string 79 80 // Topic partition's count. 81 Count int32 82 83 // TopicPartitionAssignments among kafka brokers for this topic partitions. 84 TopicPartitionAssignments []TopicPartitionAssignment 85 } 86 87 func (t *TopicPartitionsConfig) assignments() []createpartitions.RequestAssignment { 88 if len(t.TopicPartitionAssignments) == 0 { 89 return nil 90 } 91 assignments := make([]createpartitions.RequestAssignment, len(t.TopicPartitionAssignments)) 92 for i, a := range t.TopicPartitionAssignments { 93 assignments[i] = createpartitions.RequestAssignment{ 94 BrokerIDs: a.BrokerIDs, 95 } 96 } 97 return assignments 98 } 99 100 type TopicPartitionAssignment struct { 101 // Broker IDs 102 BrokerIDs []int32 103 }