forked from DataDog/kafka-kit
/
api_topics.go
247 lines (196 loc) · 5.87 KB
/
api_topics.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
package server
import (
"context"
"errors"
"fmt"
"regexp"
"sort"
"github.com/DataDog/kafka-kit/kafkazk"
pb "github.com/DataDog/kafka-kit/registry/protos"
)
var (
// ErrFetchingTopics error.
ErrFetchingTopics = errors.New("error fetching topics")
// ErrTopicNotExist error.
ErrTopicNotExist = errors.New("topic does not exist")
// ErrTopicNameEmpty error.
ErrTopicNameEmpty = errors.New("topic Name field must be specified")
// Misc.
tregex = regexp.MustCompile(".*")
)
// TopicSet is a mapping of topic name to *pb.Topic.
type TopicSet map[string]*pb.Topic
// GetTopics gets topics. If the input *pb.TopicRequest Name field is
// non-nil, the specified topic is matched if it exists. Otherwise, all
// topics found in ZooKeeper are matched. Matched topics are then filtered
// by all tags specified, if specified, in the *pb.TopicRequest tag field.
func (s *Server) GetTopics(ctx context.Context, req *pb.TopicRequest) (*pb.TopicResponse, error) {
if err := s.ValidateRequest(ctx, req, readRequest); err != nil {
return nil, err
}
// Get topics.
topics, err := s.fetchTopicSet(req)
if err != nil {
return nil, err
}
// Populate the response Topics field.
resp := &pb.TopicResponse{Topics: topics}
return resp, nil
}
// ListTopics gets topic names. If the input *pb.TopicRequest Name field is
// non-nil, the specified topic is matched if it exists. Otherwise, all
// topics found in ZooKeeper are matched. Matched topics are then filtered
// by all tags specified, if specified, in the *pb.TopicRequest tag field.
func (s *Server) ListTopics(ctx context.Context, req *pb.TopicRequest) (*pb.TopicResponse, error) {
if err := s.ValidateRequest(ctx, req, readRequest); err != nil {
return nil, err
}
// Get topics.
topics, err := s.fetchTopicSet(req)
if err != nil {
return nil, err
}
// Populate the response Names field.
resp := &pb.TopicResponse{Names: topics.Names()}
return resp, nil
}
// TopicMappings returns all broker IDs that hold at least one partition for
// the requested topic. The topic is specified in the TopicRequest.Name
// field.
func (s *Server) TopicMappings(ctx context.Context, req *pb.TopicRequest) (*pb.BrokerResponse, error) {
if err := s.ValidateRequest(ctx, req, readRequest); err != nil {
return nil, err
}
if req.Name == "" {
return nil, ErrTopicNameEmpty
}
// Get a kafkazk.PartitionMap for the topic.
pm, err := s.ZK.GetPartitionMap(req.Name)
if err != nil {
switch err.(type) {
case kafkazk.ErrNoNode:
return nil, ErrTopicNotExist
default:
return nil, err
}
}
// Get a kafkazk.BrokerMap from the PartitionMap.
bm := kafkazk.BrokerMapFromPartitionMap(pm, nil, false)
// Get all brokers as a []int of IDs.
allf := func(*kafkazk.Broker) bool { return true }
bl := bm.Filter(allf).List()
bl.SortByID()
var ids []uint32
// Populate broker IDs.
for _, b := range bl {
ids = append(ids, uint32(b.ID))
}
resp := &pb.BrokerResponse{Ids: ids}
return resp, nil
}
// TagTopic sets custom tags for the specified topic. Any previously existing
// tags that were not specified in the request remain unmodified.
func (s *Server) TagTopic(ctx context.Context, req *pb.TopicRequest) (*pb.TagResponse, error) {
if err := s.ValidateRequest(ctx, req, writeRequest); err != nil {
return nil, err
}
if req.Name == "" {
return nil, ErrTopicNameEmpty
}
if len(req.Tag) == 0 {
return nil, ErrNilTags
}
// Get a TagSet from the supplied tags.
ts, err := Tags(req.Tag).TagSet()
if err != nil {
return nil, err
}
// Ensure the topic exists.
// Get topics from ZK.
r := regexp.MustCompile(fmt.Sprintf("^%s$", req.Name))
tr := []*regexp.Regexp{r}
topics, errs := s.ZK.GetTopics(tr)
if errs != nil {
return nil, ErrFetchingTopics
}
if len(topics) == 0 {
return nil, ErrTopicNotExist
}
// Set the tags.
err = s.Tags.Store.SetTags(KafkaObject{Type: "topic", ID: req.Name}, ts)
if err != nil {
return nil, err
}
return &pb.TagResponse{Message: "success"}, nil
}
// DeleteTopicTag deletes custom tags for the specified topic.
func (s *Server) DeleteTopicTags(ctx context.Context, req *pb.TopicRequest) (*pb.TagResponse, error) {
if err := s.ValidateRequest(ctx, req, writeRequest); err != nil {
return nil, err
}
if req.Name == "" {
return nil, ErrTopicNameEmpty
}
if len(req.Tag) == 0 {
return nil, ErrNilTags
}
// Ensure the topic exists.
// Get topics from ZK.
r := regexp.MustCompile(fmt.Sprintf("^%s$", req.Name))
tr := []*regexp.Regexp{r}
topics, errs := s.ZK.GetTopics(tr)
if errs != nil {
return nil, ErrFetchingTopics
}
if len(topics) == 0 {
return nil, ErrTopicNotExist
}
// Delete the tags.
err := s.Tags.Store.DeleteTags(KafkaObject{Type: "topic", ID: req.Name}, req.Tag)
if err != nil {
return nil, err
}
return &pb.TagResponse{Message: "success"}, nil
}
// fetchBrokerSet fetches metadata for all topics.
func (s *Server) fetchTopicSet(req *pb.TopicRequest) (TopicSet, error) {
topicRegex := []*regexp.Regexp{}
// Check if a specific topic is being fetched.
if req.Name != "" {
r := regexp.MustCompile(fmt.Sprintf("^%s$", req.Name))
topicRegex = append(topicRegex, r)
} else {
topicRegex = append(topicRegex, tregex)
}
// Fetch topics from ZK.
topics, errs := s.ZK.GetTopics(topicRegex)
if errs != nil {
return nil, ErrFetchingTopics
}
matched := TopicSet{}
// Populate all topics.
for _, t := range topics {
s, _ := s.ZK.GetTopicState(t)
matched[t] = &pb.Topic{
Name: t,
Partitions: uint32(len(s.Partitions)),
// TODO more sophisticated check than the
// first partition len.
Replication: uint32(len(s.Partitions["0"])),
}
}
filtered, err := s.Tags.FilterTopics(matched, req.Tag)
if err != nil {
return nil, err
}
return filtered, nil
}
// Names returns a []string of topic names from a TopicSet.
func (t TopicSet) Names() []string {
var names = []string{}
for n := range t {
names = append(names, n)
}
sort.Strings(names)
return names
}