-
Notifications
You must be signed in to change notification settings - Fork 48
/
topic_admin.go
130 lines (104 loc) · 2.65 KB
/
topic_admin.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
package kazoo
import (
"errors"
"fmt"
"time"
"github.com/samuel/go-zookeeper/zk"
)
var (
ErrTopicExists = errors.New("Topic already exists")
ErrTopicMarkedForDelete = errors.New("Topic is already marked for deletion")
ErrDeletionTimedOut = errors.New("Timed out while waiting for a topic to be deleted")
)
// CreateTopic creates a new kafka topic with the specified parameters and properties
func (kz *Kazoo) CreateTopic(name string, partitionCount int, replicationFactor int, topicConfig map[string]string) error {
topic := kz.Topic(name)
// Official kafka sdk checks if topic exists, then always writes the config unconditionally
// but only writes the partition map if ones does not exist.
exists, err := topic.Exists()
if err != nil {
return err
} else if exists {
return ErrTopicExists
}
brokerList, err := kz.brokerIDList()
if err != nil {
return err
}
partitionList, err := topic.generatePartitionAssignments(brokerList, partitionCount, replicationFactor)
if err != nil {
return err
}
configData, err := topic.marshalConfig(topicConfig)
if err != nil {
return err
}
partitionData, err := topic.marshalPartitions(partitionList)
if err != nil {
return err
}
if err = kz.createOrUpdate(topic.configPath(), configData, false); err != nil {
return err
}
if err = kz.create(topic.metadataPath(), partitionData, false); err != nil {
return err
}
return nil
}
// DeleteTopic marks a kafka topic for deletion. Deleting a topic is asynchronous and
// DeleteTopic will return before Kafka actually does the deletion.
func (kz *Kazoo) DeleteTopic(name string) error {
node := fmt.Sprintf("%s/admin/delete_topics/%s", kz.conf.Chroot, name)
exists, err := kz.exists(node)
if err != nil {
return err
}
if exists {
return ErrTopicMarkedForDelete
}
if err := kz.create(node, nil, false); err != nil {
return err
}
return nil
}
// DeleteTopicSync marks a kafka topic for deletion and waits until it is deleted
// before returning.
func (kz *Kazoo) DeleteTopicSync(name string, timeout time.Duration) error {
err := kz.DeleteTopic(name)
if err != nil {
return err
}
topic := kz.Topic(name)
if exists, err := topic.Exists(); err != nil {
return err
} else if !exists {
return nil
}
changes, err := topic.Watch()
if err != nil {
return nil
}
if timeout > 0 {
timer := time.NewTimer(timeout)
defer timer.Stop()
for {
select {
case <-timer.C:
return ErrDeletionTimedOut
case c := <-changes:
if c.Type == zk.EventNodeDeleted {
return nil
}
}
}
} else {
for {
select {
case c := <-changes:
if c.Type == zk.EventNodeDeleted {
return nil
}
}
}
}
}