forked from IBM/sarama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_topics_request.go
199 lines (170 loc) · 4.3 KB
/
create_topics_request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package sarama
import (
"time"
)
type CreateTopicsRequest struct {
// Version defines the protocol version to use for encode and decode
Version int16
// TopicDetails contains the topics to create.
TopicDetails map[string]*TopicDetail
// Timeout contains how long to wait before timing out the request.
Timeout time.Duration
// ValidateOnly if true, check that the topics can be created as specified,
// but don't create anything.
ValidateOnly bool
}
func (c *CreateTopicsRequest) Encode(pe packetEncoder) error {
if err := pe.putArrayLength(len(c.TopicDetails)); err != nil {
return err
}
for topic, detail := range c.TopicDetails {
if err := pe.putString(topic); err != nil {
return err
}
if err := detail.Encode(pe); err != nil {
return err
}
}
pe.putInt32(int32(c.Timeout / time.Millisecond))
if c.Version >= 1 {
pe.putBool(c.ValidateOnly)
}
return nil
}
func (c *CreateTopicsRequest) Decode(pd packetDecoder, version int16) (err error) {
n, err := pd.getArrayLength()
if err != nil {
return err
}
c.TopicDetails = make(map[string]*TopicDetail, n)
for i := 0; i < n; i++ {
topic, err := pd.getString()
if err != nil {
return err
}
c.TopicDetails[topic] = new(TopicDetail)
if err = c.TopicDetails[topic].Decode(pd, version); err != nil {
return err
}
}
timeout, err := pd.getInt32()
if err != nil {
return err
}
c.Timeout = time.Duration(timeout) * time.Millisecond
if version >= 1 {
c.ValidateOnly, err = pd.getBool()
if err != nil {
return err
}
c.Version = version
}
return nil
}
func (c *CreateTopicsRequest) APIKey() int16 {
return 19
}
func (c *CreateTopicsRequest) APIVersion() int16 {
return c.Version
}
func (r *CreateTopicsRequest) HeaderVersion() int16 {
return 1
}
func (c *CreateTopicsRequest) IsValidVersion() bool {
return c.Version >= 0 && c.Version <= 3
}
func (c *CreateTopicsRequest) RequiredVersion() KafkaVersion {
switch c.Version {
case 3:
return V2_0_0_0
case 2:
return V0_11_0_0
case 1:
return V0_10_2_0
case 0:
return V0_10_1_0
default:
return V2_8_0_0
}
}
type TopicDetail struct {
// NumPartitions contains the number of partitions to create in the topic, or
// -1 if we are either specifying a manual partition assignment or using the
// default partitions.
NumPartitions int32
// ReplicationFactor contains the number of replicas to create for each
// partition in the topic, or -1 if we are either specifying a manual
// partition assignment or using the default replication factor.
ReplicationFactor int16
// ReplicaAssignment contains the manual partition assignment, or the empty
// array if we are using automatic assignment.
ReplicaAssignment map[int32][]int32
// ConfigEntries contains the custom topic configurations to set.
ConfigEntries map[string]*string
}
func (t *TopicDetail) Encode(pe packetEncoder) error {
pe.putInt32(t.NumPartitions)
pe.putInt16(t.ReplicationFactor)
if err := pe.putArrayLength(len(t.ReplicaAssignment)); err != nil {
return err
}
for partition, assignment := range t.ReplicaAssignment {
pe.putInt32(partition)
if err := pe.putInt32Array(assignment); err != nil {
return err
}
}
if err := pe.putArrayLength(len(t.ConfigEntries)); err != nil {
return err
}
for configKey, configValue := range t.ConfigEntries {
if err := pe.putString(configKey); err != nil {
return err
}
if err := pe.putNullableString(configValue); err != nil {
return err
}
}
return nil
}
func (t *TopicDetail) Decode(pd packetDecoder, version int16) (err error) {
if t.NumPartitions, err = pd.getInt32(); err != nil {
return err
}
if t.ReplicationFactor, err = pd.getInt16(); err != nil {
return err
}
n, err := pd.getArrayLength()
if err != nil {
return err
}
if n > 0 {
t.ReplicaAssignment = make(map[int32][]int32, n)
for i := 0; i < n; i++ {
replica, err := pd.getInt32()
if err != nil {
return err
}
if t.ReplicaAssignment[replica], err = pd.getInt32Array(); err != nil {
return err
}
}
}
n, err = pd.getArrayLength()
if err != nil {
return err
}
if n > 0 {
t.ConfigEntries = make(map[string]*string, n)
for i := 0; i < n; i++ {
configKey, err := pd.getString()
if err != nil {
return err
}
if t.ConfigEntries[configKey], err = pd.getNullableString(); err != nil {
return err
}
}
}
return nil
}