forked from wvanbergen/kazoo-go
/
topic_admin.go
205 lines (166 loc) · 4.29 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
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
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
}
func cycleBrokers(broker []int32, lastIter int32) int32 {
if int(lastIter+1) == len(broker) {
return 0
} else {
return lastIter + 1
}
}
func contains(slice []int32, value int32) bool{
for _,v := range slice {
if v == value {
return true
}
}
return false
}
func getValidBroker(brokerList []int32, removalBrokers []int32) []int32 {
var retVal []int32
for _, broker := range brokerList {
if !contains(removalBrokers, broker) {
retVal = append(retVal, broker)
}
}
return retVal
}
func (kz *Kazoo) RemoveTopicFromBrokers(name string, removalBroker []int32) error {
topic := kz.Topic(name)
var roundRobinBroker int32
brokers, err := kz.brokerIDList()
validBrokers := getValidBroker(brokers, removalBroker)
currentPartitions, err := topic.Partitions()
if err != nil {
return err
}
for _, partition := range currentPartitions {
replicas := partition.Replicas
newReplicas := make([]int32, len(replicas))
for i, replica := range replicas {
if contains(removalBroker, replica) {
//reassign Broker via RoundRobin.
newReplicas[i] = roundRobinBroker
roundRobinBroker = cycleBrokers(validBrokers, roundRobinBroker)
} else {
newReplicas[i] = replica
}
partition.Replicas = newReplicas
}
}
if err = topic.validatePartitionAssignments(validBrokers, currentPartitions); err != nil {
fmt.Println("Error Validation PartitionAssignment:", err)
return err
}
partitionData, err := topic.marshalPartitions(currentPartitions)
if err != nil {
fmt.Println(err)
return err
}
if err = kz.createOrUpdate(topic.metadataPath(), partitionData, false); err != nil {
fmt.Println(err)
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
}
}
}
}
}