-
-
Notifications
You must be signed in to change notification settings - Fork 638
/
AbstractKafkaWrapper.java
391 lines (322 loc) · 15.7 KB
/
AbstractKafkaWrapper.java
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package org.akhq.modules;
import com.google.common.collect.ImmutableMap;
import org.akhq.models.Partition;
import org.akhq.utils.Logger;
import org.apache.kafka.clients.admin.*;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.Node;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.acl.AclBinding;
import org.apache.kafka.common.acl.AclBindingFilter;
import org.apache.kafka.common.config.ConfigResource;
import org.apache.kafka.common.errors.ApiException;
import org.apache.kafka.common.errors.ClusterAuthorizationException;
import org.apache.kafka.common.errors.SecurityDisabledException;
import org.apache.kafka.common.errors.TopicAuthorizationException;
import org.apache.kafka.common.errors.UnsupportedVersionException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import jakarta.inject.Inject;
import static java.util.stream.Collectors.*;
abstract public class AbstractKafkaWrapper {
@Inject
private KafkaModule kafkaModule;
private final Map<String, DescribeClusterResult> cluster = new HashMap<>();
public DescribeClusterResult describeCluster(String clusterId) throws ExecutionException {
if (!this.cluster.containsKey(clusterId)) {
DescribeClusterResult cluster = kafkaModule.getAdminClient(clusterId).describeCluster();
Logger.call(cluster.clusterId(), "Get cluster");
Logger.call(cluster.nodes() , "Get nodes");
Logger.call(cluster.controller() , "Get contoller");
return cluster;
}
return this.cluster.get(clusterId);
}
private Map<String, Collection<TopicListing>> listTopics = new HashMap<>();
public Collection<TopicListing> listTopics(String clusterId) throws ExecutionException {
if (!this.listTopics.containsKey(clusterId)) {
this.listTopics.put(clusterId, Logger.call(
kafkaModule.getAdminClient(clusterId).listTopics(
new ListTopicsOptions().listInternal(true)
).listings(),
"List topics"
));
}
return this.listTopics.get(clusterId);
}
private final Map<String, Map<String, TopicDescription>> describeTopics = new HashMap<>();
public Map<String, TopicDescription> describeTopics(String clusterId, List<String> topics) throws ExecutionException {
describeTopics.computeIfAbsent(clusterId, s -> new HashMap<>());
List<String> list = new ArrayList<>(topics);
list.removeIf(value -> this.describeTopics.get(clusterId).containsKey(value));
if (list.size() > 0) {
Map<String, TopicDescription> description = Logger.call(
kafkaModule.getAdminClient(clusterId)
.describeTopics(list)
.all(),
"Describe Topics {}",
topics
);
this.describeTopics.get(clusterId).putAll(description);
}
return this.describeTopics
.get(clusterId)
.entrySet()
.stream()
.filter(e -> topics.contains(e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
public void createTopics(String clusterId, String name, int partitions, short replicationFactor) throws ExecutionException {
Logger.call(kafkaModule
.getAdminClient(clusterId)
.createTopics(Collections.singleton(new NewTopic(name, partitions, replicationFactor)))
.all(),
"Create Topics",
Collections.singletonList(name)
);
listTopics = new HashMap<>();
}
public void deleteTopics(String clusterId, String name) throws ExecutionException {
Logger.call(kafkaModule.getAdminClient(clusterId)
.deleteTopics(Collections.singleton(name))
.all(),
"Delete Topic",
Collections.singletonList(name)
);
listTopics = new HashMap<>();
}
private final Map<String, Map<String, List<Partition.Offsets>>> describeTopicsOffsets = new HashMap<>();
public Map<String, List<Partition.Offsets>> describeTopicsOffsets(String clusterId, List<String> topics) throws ExecutionException, InterruptedException {
describeTopicsOffsets.computeIfAbsent(clusterId, s -> new HashMap<>());
List<String> list = new ArrayList<>(topics);
list.removeIf(value -> this.describeTopicsOffsets.get(clusterId).containsKey(value));
if (list.size() > 0) {
Map<String, List<Partition.Offsets>> finalOffsets = Logger.call(
() -> {
List<TopicPartition> collect = this.describeTopics(clusterId, topics).entrySet()
.stream()
.flatMap(topicDescription -> topicDescription
.getValue()
.partitions()
.stream()
.map(topicPartitionInfo ->
new TopicPartition(topicDescription.getValue().name(), topicPartitionInfo.partition())
)
)
.collect(Collectors.toList());
KafkaConsumer<byte[], byte[]> consumer = kafkaModule.getConsumer(clusterId);
Map<TopicPartition, Long> begins = consumer.beginningOffsets(collect);
Map<TopicPartition, Long> ends = consumer.endOffsets(collect);
consumer.close();
return begins.entrySet().stream()
.collect(groupingBy(
o -> o.getKey().topic(),
mapping(
begin ->
new Partition.Offsets(
begin.getKey().partition(),
begin.getValue(),
ends.get(begin.getKey())
),
toList()
)
));
},
"Describe Topics Offsets {}",
topics
);
this.describeTopicsOffsets.get(clusterId).putAll(finalOffsets);
}
return this.describeTopicsOffsets.get(clusterId);
}
private final Map<String, Collection<ConsumerGroupListing>> listConsumerGroups = new HashMap<>();
public Collection<ConsumerGroupListing> listConsumerGroups(String clusterId) throws ExecutionException {
if (!this.listConsumerGroups.containsKey(clusterId)) {
this.listConsumerGroups.put(clusterId, Logger.call(
kafkaModule.getAdminClient(clusterId).listConsumerGroups().all(),
"List ConsumerGroups",
null
));
}
return this.listConsumerGroups.get(clusterId);
}
private Map<String, Map<String, ConsumerGroupDescription>> describeConsumerGroups = new HashMap<>();
public Map<String, ConsumerGroupDescription> describeConsumerGroups(String clusterId, List<String> groups) throws ExecutionException {
describeConsumerGroups.computeIfAbsent(clusterId, s -> new HashMap<>());
List<String> list = new ArrayList<>(groups);
list.removeIf(value -> this.describeConsumerGroups.get(clusterId).containsKey(value));
if (list.size() > 0) {
Map<String, ConsumerGroupDescription> description = Logger.call(
kafkaModule.getAdminClient(clusterId)
.describeConsumerGroups(groups)
.all(),
"Describe ConsumerGroups {}",
groups
);
this.describeConsumerGroups.get(clusterId).putAll(description);
}
return this.describeConsumerGroups
.get(clusterId)
.entrySet()
.stream()
.filter(e -> groups.contains(e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
public void deleteConsumerGroups(String clusterId, String name) throws ApiException, ExecutionException {
Logger.call(kafkaModule
.getAdminClient(clusterId)
.deleteConsumerGroups(Collections.singleton(name))
.all(),
"deleteConsumerGroups",
Collections.singletonList(name)
);
describeConsumerGroups = new HashMap<>();
consumerGroupOffset = new HashMap<>();
}
private Map<String, Map<String, Map<TopicPartition, OffsetAndMetadata>>> consumerGroupOffset = new HashMap<>();
public Map<TopicPartition, OffsetAndMetadata> consumerGroupsOffsets(String clusterId, String groupId) throws ExecutionException {
consumerGroupOffset.computeIfAbsent(clusterId, s -> new HashMap<>());
if (!this.consumerGroupOffset.get(clusterId).containsKey(groupId)) {
this.consumerGroupOffset.get(clusterId).put(groupId, Logger.call(
kafkaModule.getAdminClient(clusterId)
.listConsumerGroupOffsets(groupId)
.partitionsToOffsetAndMetadata(),
"ConsumerGroup Offsets {}",
Collections.singletonList(groupId)
));
}
return this.consumerGroupOffset.get(clusterId).get(groupId);
}
public void clearConsumerGroupsOffsets() {
this.consumerGroupOffset = new HashMap<>();
}
private final Map<String, Map<Integer, Map<String, LogDirDescription>>> logDirs = new HashMap<>();
public Map<Integer, Map<String, LogDirDescription>> describeLogDir(String clusterId) throws ExecutionException, InterruptedException {
if (!this.logDirs.containsKey(clusterId)) {
this.logDirs.put(clusterId, Logger.call(
() -> {
try {
return kafkaModule.getAdminClient(clusterId)
.describeLogDirs(this.describeCluster(clusterId).nodes().get()
.stream()
.map(Node::id)
.collect(Collectors.toList())
)
.allDescriptions()
.get();
} catch (ExecutionException e) {
if (e.getCause() instanceof ClusterAuthorizationException || e.getCause() instanceof TopicAuthorizationException || e.getCause() instanceof UnsupportedVersionException) {
return new HashMap<>();
}
if (e.getCause() instanceof ApiException) {
throw (ApiException) e.getCause();
}
throw e;
}
},
"List Log dir",
null
));
}
return this.logDirs.get(clusterId);
}
private Map<String, Map<ConfigResource, Config>> describeConfigs = new HashMap<>();
public Map<ConfigResource, Config> describeConfigs(String clusterId, ConfigResource.Type type, List<String> names) throws ExecutionException, InterruptedException {
describeConfigs.computeIfAbsent(clusterId, s -> new HashMap<>());
List<String> list = new ArrayList<>(names);
list.removeIf(value -> this.describeConfigs.get(clusterId).entrySet()
.stream()
.filter(entry -> entry.getKey().type() == type)
.anyMatch(entry -> entry.getKey().name().equals(value))
);
if (list.size() > 0) {
Map<ConfigResource, Config> description = Logger.call(
() -> {
try {
return kafkaModule.getAdminClient(clusterId)
.describeConfigs(names.stream()
.map(s -> new ConfigResource(type, s))
.collect(Collectors.toList())
)
.all()
.get();
} catch (ExecutionException e) {
if (e.getCause() instanceof SecurityDisabledException || e.getCause() instanceof ClusterAuthorizationException || e.getCause() instanceof TopicAuthorizationException) {
return ImmutableMap.of();
}
if (e.getCause() instanceof ApiException) {
throw (ApiException) e.getCause();
}
throw e;
}
},
"Describe Topic Config {}",
names
);
this.describeConfigs.get(clusterId).putAll(description);
}
return this.describeConfigs
.get(clusterId)
.entrySet()
.stream()
.filter(e -> e.getKey().type() == type)
.filter(e -> names.contains(e.getKey().name()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
public void alterConfigs(String clusterId, Map<ConfigResource, Config> configs) throws ExecutionException {
Logger.call(
kafkaModule.getAdminClient(clusterId)
.alterConfigs(configs)
.all(),
"Alter cofigs",
Collections.singletonList(clusterId)
);
this.describeConfigs = new HashMap<>();
}
private final Map<String, Map<AclBindingFilter, Collection<AclBinding>>> describeAcls = new HashMap<>();
public Collection<AclBinding> describeAcls(String clusterId, AclBindingFilter filter) throws ExecutionException, InterruptedException {
describeAcls.computeIfAbsent(clusterId, s -> new HashMap<>());
if (!this.describeAcls.get(clusterId).containsKey(filter)) {
this.describeAcls.get(clusterId).put(filter, Logger.call(
() -> {
try {
return kafkaModule.getAdminClient(clusterId)
.describeAcls(filter)
.values()
.get();
} catch (ApiException e) {
if (e.getCause() instanceof SecurityDisabledException || e.getCause() instanceof ClusterAuthorizationException || e.getCause() instanceof TopicAuthorizationException) {
return Collections.emptyList();
}
if (e.getCause() instanceof ApiException) {
throw (ApiException) e.getCause();
}
throw e;
}
},
"Describe Acls config",
null
));
}
return describeAcls.get(clusterId).get(filter);
}
public void deleteConsumerGroupOffsets(String clusterId, String groupName, String topicName)
throws ExecutionException {
final Map<String, TopicDescription> topics = describeTopics(clusterId, List.of(topicName));
if (topics.containsKey(topicName)) {
final TopicDescription topic = topics.get(topicName);
final Set<TopicPartition> topicPartitions = topic.partitions().stream()
.map(p -> new TopicPartition(topicName, p.partition()))
.collect(toSet());
Logger.call(kafkaModule
.getAdminClient(clusterId)
.deleteConsumerGroupOffsets(groupName, topicPartitions)
.all(),
"Delete consumer group offsets from topic {}",
List.of(groupName, topicName)
);
}
}
}