-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
client.go
325 lines (272 loc) · 7.24 KB
/
client.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package kafka
import (
"errors"
"fmt"
"log"
"os"
"time"
"github.com/Shopify/sarama"
)
type TopicMissingError struct {
msg string
}
func (e TopicMissingError) Error() string { return e.msg }
type Client struct {
client sarama.Client
kafkaConfig *sarama.Config
config *Config
supportedAPIs map[int]int
}
func NewClient(config *Config) (*Client, error) {
sarama.Logger = log.New(os.Stdout, "[TRACE] [Sarama]", log.LstdFlags)
log.Printf("[INFO] configuring bootstrap_servers %v", config)
bootstrapServers := *(config.BootstrapServers)
if bootstrapServers == nil {
return nil, fmt.Errorf("No bootstrap_servers provided")
}
kc, err := config.newKafkaConfig()
if err != nil {
log.Printf("[ERROR] Error creating kafka client %v", err)
return nil, err
}
c, err := sarama.NewClient(bootstrapServers, kc)
if err != nil {
log.Printf("[ERROR] Error connecting to kafka %s", err)
return nil, err
}
client := &Client{
client: c,
config: config,
kafkaConfig: kc,
}
err = kc.Validate()
if err != nil {
return client, err
}
client.populateAPIVersions()
return client, nil
}
func (c *Client) SaramaClient() sarama.Client {
return c.client
}
func (c *Client) populateAPIVersions() {
log.Printf("[DEBUG] retrieving supported APIs from broker: %s", c.config.BootstrapServers)
broker, err := c.client.Controller()
if err != nil {
log.Printf("[ERROR] Unable to populate supported API versions. Error retrieving controller: %s", err)
return
}
resp, err := broker.ApiVersions(&sarama.ApiVersionsRequest{})
if err != nil {
log.Printf("[ERROR] Unable to populate supported API versions. %s", err)
return
}
m := map[int]int{}
for _, v := range resp.ApiVersions {
log.Printf("[TRACE] API key %d. Min %d, Max %d", v.ApiKey, v.MinVersion, v.MaxVersion)
m[int(v.ApiKey)] = int(v.MaxVersion)
}
c.supportedAPIs = m
}
func (c *Client) DeleteTopic(t string) error {
broker, err := c.client.Controller()
if err != nil {
return err
}
timeout := time.Duration(c.config.Timeout) * time.Second
req := &sarama.DeleteTopicsRequest{
Topics: []string{t},
Timeout: timeout,
}
res, err := broker.DeleteTopics(req)
if err == nil {
for k, e := range res.TopicErrorCodes {
if e != sarama.ErrNoError {
return fmt.Errorf("%s : %s", k, e)
}
}
} else {
log.Printf("[ERROR] Error deleting topic %s from Kafka: %s", t, err)
return err
}
log.Printf("[INFO] Deleted topic %s from Kafka", t)
return nil
}
func (c *Client) UpdateTopic(topic Topic) error {
broker, err := c.client.Controller()
if err != nil {
return err
}
r := &sarama.AlterConfigsRequest{
Resources: configToResources(topic),
ValidateOnly: false,
}
res, err := broker.AlterConfigs(r)
if err != nil {
return err
}
if err == nil {
for _, e := range res.Resources {
if e.ErrorCode != int16(sarama.ErrNoError) {
return errors.New(e.ErrorMsg)
}
}
}
return nil
}
func (c *Client) CreateTopic(t Topic) error {
broker, err := c.client.Controller()
if err != nil {
return err
}
timeout := time.Duration(c.config.Timeout) * time.Second
log.Printf("[DEBUG] Timeout is %v ", timeout)
req := &sarama.CreateTopicsRequest{
TopicDetails: map[string]*sarama.TopicDetail{
t.Name: {
NumPartitions: t.Partitions,
ReplicationFactor: t.ReplicationFactor,
ConfigEntries: t.Config,
},
},
Timeout: timeout,
}
res, err := broker.CreateTopics(req)
if err == nil {
for _, e := range res.TopicErrors {
if e.Err != sarama.ErrNoError {
return fmt.Errorf("%s", e.Err)
}
}
log.Printf("[INFO] Created topic %s in Kafka", t.Name)
}
return err
}
func (c *Client) AddPartitions(t Topic) error {
broker, err := c.client.Controller()
if err != nil {
return err
}
timeout := time.Duration(c.config.Timeout) * time.Second
tp := map[string]*sarama.TopicPartition{
t.Name: &sarama.TopicPartition{
Count: t.Partitions,
},
}
req := &sarama.CreatePartitionsRequest{
TopicPartitions: tp,
Timeout: timeout,
ValidateOnly: false,
}
log.Printf("[INFO] Adding partitions to %s in Kafka", t.Name)
res, err := broker.CreatePartitions(req)
if err == nil {
for _, e := range res.TopicPartitionErrors {
if e.Err != sarama.ErrNoError {
return fmt.Errorf("%s", e.Err)
}
}
log.Printf("[INFO] Added partitions to %s in Kafka", t.Name)
}
return err
}
func (client *Client) ReadTopic(name string) (Topic, error) {
c := client.client
topic := Topic{
Name: name,
}
err := c.RefreshMetadata()
if err != nil {
log.Printf("[ERROR] Error refreshing metadata %s", err)
return topic, err
}
topics, err := c.Topics()
if err != nil {
log.Printf("[ERROR] Error getting topics %s from Kafka", err)
return topic, err
}
log.Printf("[INFO] There are %d topics", len(topics))
for _, t := range topics {
log.Printf("[DEBUG] Reading Topic %s from Kafka", t)
if name == t {
log.Printf("[DEBUG] Found %s from Kafka", t)
p, err := c.Partitions(t)
if err == nil {
partitionCount := int32(len(p))
log.Printf("[DEBUG] %d Partitions Found: %v from Kafka", partitionCount, p)
topic.Partitions = partitionCount
r, err := ReplicaCount(c, name, p)
if err == nil {
log.Printf("[DEBUG] ReplicationFactor %d from Kafka", r)
topic.ReplicationFactor = int16(r)
}
configToSave, err := client.topicConfig(t)
if err != nil {
log.Printf("[ERROR] Could not get config for topic %s: %s", t, err)
return topic, err
}
log.Printf("[DEBUG] Config %v from Kafka", strPtrMapToStrMap(configToSave))
topic.Config = configToSave
return topic, nil
}
}
}
err = TopicMissingError{msg: fmt.Sprintf("%s could not be found", name)}
return topic, err
}
func (c *Client) versionForKey(apiKey, wantedMaxVersion int) int {
if maxSupportedVersion, ok := c.supportedAPIs[apiKey]; ok {
if maxSupportedVersion < wantedMaxVersion {
return maxSupportedVersion
}
return wantedMaxVersion
}
return 0
}
//topicConfig retrives the non-default config map for a topic
func (c *Client) topicConfig(topic string) (map[string]*string, error) {
conf := map[string]*string{}
request := &sarama.DescribeConfigsRequest{
Version: c.getDescribeConfigAPIVersion(),
Resources: []*sarama.ConfigResource{
{
Type: sarama.TopicResource,
Name: topic,
},
},
}
broker, err := c.client.Controller()
if err != nil {
return conf, err
}
cr, err := broker.DescribeConfigs(request)
if err != nil {
return conf, err
}
if len(cr.Resources) > 0 && len(cr.Resources[0].Configs) > 0 {
for _, tConf := range cr.Resources[0].Configs {
v := tConf.Value
log.Printf("[INFO] Topic: %s. %s: %v. Default %v, Source %v, Version %d", topic, tConf.Name, v, tConf.Default, tConf.Source, cr.Version)
for _, s := range tConf.Synonyms {
log.Printf("[INFO] Syonyms: %v", s)
}
if isDefault(tConf, int(cr.Version)) {
continue
}
conf[tConf.Name] = &v
}
}
return conf, nil
}
func (c *Client) getDescribeAclsRequestAPIVersion() int16 {
return int16(c.versionForKey(29, 1))
}
func (c *Client) getCreateAclsRequestAPIVersion() int16 {
return int16(c.versionForKey(30, 1))
}
func (c *Client) getDeleteAclsRequestAPIVersion() int16 {
return int16(c.versionForKey(31, 1))
}
func (c *Client) getDescribeConfigAPIVersion() int16 {
return int16(c.versionForKey(32, 1))
}