-
Notifications
You must be signed in to change notification settings - Fork 892
Expand file tree
/
Copy pathKafkaMonitorImpl.java
More file actions
423 lines (371 loc) · 16 KB
/
KafkaMonitorImpl.java
File metadata and controls
423 lines (371 loc) · 16 KB
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* Copyright 2017 Kafdrop contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package kafdrop.service;
import kafdrop.model.AclVO;
import kafdrop.model.BrokerVO;
import kafdrop.model.ClusterSummaryVO;
import kafdrop.model.ConsumerPartitionVO;
import kafdrop.model.ConsumerTopicVO;
import kafdrop.model.ConsumerVO;
import kafdrop.model.CreateTopicVO;
import kafdrop.model.MessageVO;
import kafdrop.model.SearchResultsVO;
import kafdrop.model.TopicPartitionVO;
import kafdrop.model.TopicVO;
import kafdrop.util.Deserializers;
import org.apache.kafka.clients.admin.ConfigEntry.ConfigSource;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.PartitionInfo;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.header.Headers;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.util.function.Predicate.not;
@Service
public final class KafkaMonitorImpl implements KafkaMonitor {
private static final Logger LOG = LoggerFactory.getLogger(KafkaMonitorImpl.class);
private final KafkaHighLevelConsumer highLevelConsumer;
private final KafkaHighLevelAdminClient highLevelAdminClient;
public KafkaMonitorImpl(KafkaHighLevelConsumer highLevelConsumer, KafkaHighLevelAdminClient highLevelAdminClient) {
this.highLevelConsumer = highLevelConsumer;
this.highLevelAdminClient = highLevelAdminClient;
}
@Override
public List<BrokerVO> getBrokers() {
final var clusterDescription = highLevelAdminClient.describeCluster();
return clusterDescription.nodes().stream()
.map(node -> {
boolean isController = node.id() == clusterDescription.controller().id();
return new BrokerVO(node.id(), node.host(), node.port(), node.rack(), isController);
})
.toList();
}
@Override
public Optional<BrokerVO> getBroker(int id) {
return getBrokers().stream().filter(brokerVo -> brokerVo.id() == id).findAny();
}
@Override
public ClusterSummaryVO getClusterSummary(Collection<TopicVO> topics) {
final var topicSummary = topics.stream()
.map(topic -> {
final var summary = new ClusterSummaryVO();
summary.setPartitionCount(topic.getPartitions().size());
summary.setUnderReplicatedCount(topic.getUnderReplicatedPartitions().size());
summary.setPreferredReplicaPercent(topic.getPreferredReplicaPercent());
topic.getPartitions()
.forEach(partition -> {
if (partition.getLeader() != null) {
summary.addBrokerLeaderPartition(partition.getLeader().getId());
}
if (partition.getPreferredLeader() != null) {
summary.addBrokerPreferredLeaderPartition(partition.getPreferredLeader().getId());
}
});
return summary;
})
.reduce((s1, s2) -> {
s1.setPartitionCount(s1.getPartitionCount() + s2.getPartitionCount());
s1.setUnderReplicatedCount(s1.getUnderReplicatedCount() + s2.getUnderReplicatedCount());
s1.setPreferredReplicaPercent(s1.getPreferredReplicaPercent() + s2.getPreferredReplicaPercent());
s2.getBrokerLeaderPartitionCount().forEach(s1::addBrokerLeaderPartition);
s2.getBrokerPreferredLeaderPartitionCount().forEach(s1::addBrokerPreferredLeaderPartition);
return s1;
})
.orElseGet(ClusterSummaryVO::new);
topicSummary.setTopicCount(topics.size());
topicSummary.setPreferredReplicaPercent(topics.isEmpty() ? 0 :
topicSummary.getPreferredReplicaPercent() / topics.size());
return topicSummary;
}
@Override
public List<TopicVO> getTopics() {
return getTopicMetadata(highLevelConsumer.getAllTopics()).values().stream()
.sorted(Comparator.comparing(TopicVO::getName))
.collect(Collectors.toList());
}
public List<TopicVO> getTopics(String[] topics) {
Map<String, List<PartitionInfo>> topicsMap = highLevelConsumer.getAllTopics();
ArrayList<TopicVO> topicVos = new ArrayList<>(getTopicMetadata(topicsMap, topics).values());
setTopicPartitionSizes(topicVos);
return topicVos;
}
@Override
public Optional<TopicVO> getTopic(String topic) {
String[] topics = {topic};
return getTopics(topics).stream().findAny();
}
private Map<String, TopicVO> getTopicMetadata(Map<String, List<PartitionInfo>> allTopicsMap, String... topics) {
final var topicInfos = highLevelConsumer.getTopicInfos(allTopicsMap, topics);
final var retrievedTopicNames = topicInfos.keySet();
final var topicConfigs = highLevelAdminClient.describeTopicConfigs(retrievedTopicNames);
for (var topicVo : topicInfos.values()) {
final var config = topicConfigs.get(topicVo.getName());
if (config != null) {
final var configMap = new TreeMap<String, String>();
for (var configEntry : config.entries()) {
if (configEntry.source() != ConfigSource.DEFAULT_CONFIG &&
configEntry.source() != ConfigSource.STATIC_BROKER_CONFIG) {
configMap.put(configEntry.name(), configEntry.value());
}
}
topicVo.setConfig(configMap);
} else {
LOG.warn("Missing config for topic {}", topicVo.getName());
}
}
return topicInfos;
}
@Override
public List<MessageVO> getMessages(String topic, int count,
Deserializers deserializers) {
final var records = highLevelConsumer.getLatestRecords(topic, count, deserializers);
if (records != null) {
final var messageVos = new ArrayList<MessageVO>();
for (var rec : records) {
final var messageVo = new MessageVO();
messageVo.setPartition(rec.partition());
messageVo.setOffset(rec.offset());
messageVo.setKey(rec.key());
messageVo.setMessage(rec.value());
messageVo.setHeaders(headersToMap(rec.headers()));
messageVo.setTimestamp(new Date(rec.timestamp()));
messageVos.add(messageVo);
}
return messageVos;
} else {
return Collections.emptyList();
}
}
@Override
public List<MessageVO> getMessages(TopicPartition topicPartition, long offset, int count,
Deserializers deserializers) {
final var records = highLevelConsumer.getLatestRecords(topicPartition, offset, count, deserializers);
if (records != null) {
final var messageVos = new ArrayList<MessageVO>();
for (var rec : records) {
final var messageVo = new MessageVO();
messageVo.setPartition(topicPartition.partition());
messageVo.setOffset(rec.offset());
messageVo.setKey(rec.key());
messageVo.setMessage(rec.value());
messageVo.setHeaders(headersToMap(rec.headers()));
messageVo.setTimestamp(new Date(rec.timestamp()));
messageVos.add(messageVo);
}
return messageVos;
} else {
return Collections.emptyList();
}
}
private static Map<String, String> headersToMap(Headers headers) {
final var map = new TreeMap<String, String>();
for (var header : headers) {
final var value = header.value();
map.put(header.key(), (value == null) ? null : new String(value));
}
return map;
}
private void setTopicPartitionSizes(List<TopicVO> topics) {
highLevelConsumer.setTopicPartitionSizes(topics);
}
public List<ConsumerVO> getConsumersByGroup(String groupId) {
List<ConsumerGroupOffsets> consumerGroupOffsets = getConsumerOffsets(groupId);
String[] uniqueTopicNames = consumerGroupOffsets.stream()
.flatMap(consumerGroupOffset -> consumerGroupOffset.offsets.keySet()
.stream().map(TopicPartition::topic))
.distinct()
.toArray(String[]::new);
List<TopicVO> topicVOs = getTopics(uniqueTopicNames);
LOG.debug("consumerGroupOffsets: {}", consumerGroupOffsets);
LOG.debug("topicVos: {}", topicVOs);
return convert(consumerGroupOffsets, topicVOs);
}
@Override
public List<ConsumerVO> getConsumersByTopics(Collection<TopicVO> topicVos) {
final var topics = topicVos.stream().map(TopicVO::getName).collect(Collectors.toSet());
final var consumerGroupOffsets = getConsumerOffsets(topics);
LOG.debug("consumerGroupOffsets: {}", consumerGroupOffsets);
LOG.debug("topicVos: {}", topicVos);
return convert(consumerGroupOffsets, topicVos);
}
@Override
public SearchResultsVO searchMessages(String topic,
String searchString,
Integer maxmuimCount,
Date startTimestamp,
Deserializers deserializers) {
final var records = highLevelConsumer.searchRecords(topic, searchString, maxmuimCount, startTimestamp,
deserializers);
final var results = new SearchResultsVO();
if (records != null) {
final var messageVos = new ArrayList<MessageVO>();
results.setMessages(messageVos);
for (var record : records.getResults()) {
final var messageVo = new MessageVO();
messageVo.setPartition(record.partition());
messageVo.setOffset(record.offset());
messageVo.setKey(record.key());
messageVo.setMessage(record.value());
messageVo.setHeaders(headersToMap(record.headers()));
messageVo.setTimestamp(new Date(record.timestamp()));
messageVos.add(messageVo);
}
switch (records.getCompletionReason()) {
case FOUND_REQUESTED_NUMBER_OF_RESULTS:
results.setCompletionDetails(String.format("Search completed after finding requested number of results. " +
"Scanned %d messages.", records.getMessagesScannedCount()));
break;
case EXCEEDED_MAX_SCAN_COUNT:
results.setCompletionDetails(
String.format(
"Search timed out after scanning %d messages. Last scanned message timestamp was %d. Adjust your time" +
" span for more results.",
records.getMessagesScannedCount(),
records.getFinalMessageTimestamp()));
break;
case NO_MORE_MESSAGES_IN_TOPIC:
results.setCompletionDetails(
String.format(
"Search reached the end of the topic before finding requested number of results. Scanned %d messages.",
records.getMessagesScannedCount()));
break;
}
} else {
results.setCompletionDetails("Unknown error");
results.setMessages(Collections.emptyList());
}
return results;
}
@Override
public void createTopic(CreateTopicVO createTopicDto) {
var newTopic = new NewTopic(
createTopicDto.getName(), createTopicDto.getPartitionsNumber(), (short) createTopicDto.getReplicationFactor()
);
highLevelAdminClient.createTopic(newTopic);
}
@Override
public void deleteTopic(String topic) {
highLevelAdminClient.deleteTopic(topic);
}
@Override
public List<AclVO> getAcls() {
final var acls = highLevelAdminClient.listAcls();
final var aclVos = new ArrayList<AclVO>(acls.size());
for (var acl : acls) {
aclVos.add(new AclVO(acl.pattern().resourceType().toString(), acl.pattern().name(),
acl.pattern().patternType().toString(), acl.entry().principal(),
acl.entry().host(), acl.entry().operation().toString(),
acl.entry().permissionType().toString()));
}
Collections.sort(aclVos);
return aclVos;
}
private static List<ConsumerVO> convert(List<ConsumerGroupOffsets> consumerGroupOffsets,
Collection<TopicVO> topicVos) {
final var topicVoMap = topicVos.stream().collect(Collectors.toMap(TopicVO::getName, Function.identity()));
final var groupTopicPartitionOffsetMap = new TreeMap<String, Map<String, Map<Integer, Long>>>();
for (var consumerGroupOffset : consumerGroupOffsets) {
final var groupId = consumerGroupOffset.groupId;
for (var topicPartitionOffset : consumerGroupOffset.offsets.entrySet()) {
final var topic = topicPartitionOffset.getKey().topic();
final var partition = topicPartitionOffset.getKey().partition();
final var offset = topicPartitionOffset.getValue().offset();
groupTopicPartitionOffsetMap
.computeIfAbsent(groupId, unused -> new TreeMap<>())
.computeIfAbsent(topic, unused -> new TreeMap<>())
.put(partition, offset);
}
}
final var consumerVos = new ArrayList<ConsumerVO>(consumerGroupOffsets.size());
for (var groupTopicPartitionOffset : groupTopicPartitionOffsetMap.entrySet()) {
final var groupId = groupTopicPartitionOffset.getKey();
final var consumerVo = new ConsumerVO(groupId);
consumerVos.add(consumerVo);
for (var topicPartitionOffset : groupTopicPartitionOffset.getValue().entrySet()) {
final var topic = topicPartitionOffset.getKey();
final var consumerTopicVo = new ConsumerTopicVO(topic);
consumerVo.addTopic(consumerTopicVo);
for (var partitionOffset : topicPartitionOffset.getValue().entrySet()) {
final var partition = partitionOffset.getKey();
final var offset = partitionOffset.getValue();
final var offsetVo = new ConsumerPartitionVO(groupId, topic, partition);
consumerTopicVo.addOffset(offsetVo);
offsetVo.setOffset(offset);
final var topicVo = topicVoMap.get(topic);
final var topicPartitionVo = topicVo.getPartition(partition);
offsetVo.setSize(topicPartitionVo.map(TopicPartitionVO::getSize).orElse(-1L));
offsetVo.setFirstOffset(topicPartitionVo.map(TopicPartitionVO::getFirstOffset).orElse(-1L));
}
}
}
return consumerVos;
}
private static final class ConsumerGroupOffsets {
final String groupId;
final Map<TopicPartition, OffsetAndMetadata> offsets;
ConsumerGroupOffsets(String groupId, Map<TopicPartition, OffsetAndMetadata> offsets) {
this.groupId = groupId;
this.offsets = offsets;
}
boolean isEmpty() {
return offsets.isEmpty();
}
ConsumerGroupOffsets forTopics(Set<String> topics) {
final var filteredOffsets = offsets.entrySet().stream()
.filter(e -> e.getValue() != null)
.filter(e -> topics.contains(e.getKey().topic()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
return new ConsumerGroupOffsets(groupId, filteredOffsets);
}
@Override
public String toString() {
return ConsumerGroupOffsets.class.getSimpleName() + " [groupId=" + groupId + ", offsets=" + offsets + "]";
}
}
private ConsumerGroupOffsets resolveOffsets(String groupId) {
return new ConsumerGroupOffsets(groupId, highLevelAdminClient.listConsumerGroupOffsetsIfAuthorized(groupId));
}
private List<ConsumerGroupOffsets> getConsumerOffsets(String groupId) {
return Collections.singletonList(resolveOffsets(groupId));
}
private List<ConsumerGroupOffsets> getConsumerOffsets(Set<String> topics) {
final var consumerGroups = highLevelAdminClient.listConsumerGroups();
return consumerGroups.stream()
.map(this::resolveOffsets)
.map(offsets -> offsets.forTopics(topics))
.filter(not(ConsumerGroupOffsets::isEmpty))
.collect(Collectors.toList());
}
}