-
Notifications
You must be signed in to change notification settings - Fork 163
/
mi.patch
188 lines (175 loc) · 7.12 KB
/
mi.patch
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
commit 4a07f7c6c0f66695408c3138a1df696ff5dfab48
Author: Hay, David <[email protected]>
Date: Sun Nov 17 16:52:05 2019 -0700
[TA-4646] Rework internals to use Kafka client
diff --git a/src/main/java/com/homeadvisor/kafdrop/service/MessageInspector.java b/src/main/java/com/homeadvisor/kafdrop/service/MessageInspector.java
index 5143bda..4d304ba 100644
--- a/src/main/java/com/homeadvisor/kafdrop/service/MessageInspector.java
+++ b/src/main/java/com/homeadvisor/kafdrop/service/MessageInspector.java
@@ -18,28 +18,26 @@
package com.homeadvisor.kafdrop.service;
+import com.homeadvisor.kafdrop.config.KafkaConfiguration;
import com.homeadvisor.kafdrop.model.MessageVO;
-import com.homeadvisor.kafdrop.model.TopicPartitionVO;
-import com.homeadvisor.kafdrop.model.TopicVO;
-import com.homeadvisor.kafdrop.util.BrokerChannel;
-import kafka.api.FetchRequest;
-import kafka.api.FetchRequestBuilder;
-import kafka.javaapi.FetchResponse;
-import kafka.javaapi.consumer.SimpleConsumer;
-import kafka.javaapi.message.ByteBufferMessageSet;
-import kafka.message.Message;
-import kafka.message.MessageAndOffset;
+import org.apache.kafka.clients.consumer.Consumer;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.clients.consumer.ConsumerRecords;
+import org.apache.kafka.clients.consumer.KafkaConsumer;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.serialization.ByteArrayDeserializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
-import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
-import java.util.stream.Collectors;
+import java.util.Properties;
+import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@Service
@@ -47,81 +45,79 @@ public class MessageInspector
{
private final Logger LOG = LoggerFactory.getLogger(getClass());
- @Autowired
- private KafkaMonitor kafkaMonitor;
+ private KafkaConfiguration config;
+
+ public MessageInspector(KafkaConfiguration config)
+ {
+ this.config = config;
+ }
+
+ private Consumer<byte[], byte[]> createConsumer()
+ {
+ Properties properties = new Properties();
+ config.applyCommon(properties);
+ return new KafkaConsumer(properties, new ByteArrayDeserializer(), new ByteArrayDeserializer());
+
+ }
public List<MessageVO> getMessages(String topicName, int partitionId, long offset, long count)
{
- final TopicVO topic = kafkaMonitor.getTopic(topicName).orElseThrow(TopicNotFoundException::new);
- final TopicPartitionVO partition = topic.getPartition(partitionId).orElseThrow(PartitionNotFoundException::new);
-
- return kafkaMonitor.getBroker(partition.getLeader().getId())
- .map(broker -> {
- SimpleConsumer consumer = new SimpleConsumer(broker.getHost(), broker.getPort(), 10000, 100000, "");
-
- final FetchRequestBuilder fetchRequestBuilder = new FetchRequestBuilder()
- .clientId("KafDrop")
- .maxWait(5000) // todo: make configurable
- .minBytes(1);
-
- List<MessageVO> messages = new ArrayList<>();
- long currentOffset = offset;
- while (messages.size() < count)
- {
- final FetchRequest fetchRequest =
- fetchRequestBuilder
- .addFetch(topicName, partitionId, currentOffset, 1024 * 1024)
- .build();
-
- FetchResponse fetchResponse = consumer.fetch(fetchRequest);
-
- final ByteBufferMessageSet messageSet = fetchResponse.messageSet(topicName, partitionId);
- if (messageSet.validBytes() <= 0) break;
-
-
- int oldSize = messages.size();
- StreamSupport.stream(messageSet.spliterator(), false)
- .limit(count - messages.size())
- .map(MessageAndOffset::message)
- .map(this::createMessage)
- .forEach(messages::add);
- currentOffset += messages.size() - oldSize;
- }
- return messages;
- })
- .orElseGet(Collections::emptyList);
+
+ try (Consumer<byte[], byte[]> consumer = createConsumer())
+ {
+ TopicPartition topicPartition = new TopicPartition(topicName, partitionId);
+ consumer.assign(Collections.singletonList(topicPartition));
+ consumer.seek(topicPartition, offset);
+
+ // Need to adjust how many records we read based on how many are actually available in the partition.
+ long maxOffset = consumer.endOffsets(Collections.singletonList(topicPartition)).get(topicPartition);
+
+ int numRecordsToRead = (int) Math.min(count, maxOffset - offset);
+
+ List<MessageVO> recordList = new ArrayList<>(numRecordsToRead);
+ while (recordList.size() < numRecordsToRead)
+ {
+ ConsumerRecords<byte[], byte[]> records = consumer.poll(Duration.ofMillis(1000L));
+ List<ConsumerRecord<byte[], byte[]>> returnedRecords = records.records(topicPartition);
+
+ returnedRecords.subList(0, Math.min((int) count - recordList.size(), returnedRecords.size()))
+ .stream()
+ .map(this::createMessage)
+ .forEach(recordList::add);
+ }
+
+ return recordList;
+ }
+
}
- private MessageVO createMessage(Message message)
+ private MessageVO createMessage(ConsumerRecord<byte[], byte[]> record)
{
MessageVO vo = new MessageVO();
- if (message.hasKey())
+ vo.setTopic(record.topic());
+ vo.setOffset(record.offset());
+ vo.setPartition(record.partition());
+ if (record.key() != null && record.key().length > 0)
{
- vo.setKey(readString(message.key()));
+ vo.setKey(readString(record.key()));
}
- if (!message.isNull())
+ if (record.value() != null && record.value().length > 0)
{
- vo.setMessage(readString(message.payload()));
+ vo.setMessage(readString(record.value()));
}
- vo.setValid(message.isValid());
- vo.setCompressionCodec(message.compressionCodec().name());
- vo.setChecksum(message.checksum());
- vo.setComputedChecksum(message.computeChecksum());
+ vo.setTimestamp(record.timestamp());
+ vo.setTimestampType(record.timestampType().toString());
+
+ StreamSupport.stream(record.headers().spliterator(), false)
+ .forEachOrdered(header -> vo.addHeader(header.key(), new String(header.value(), StandardCharsets.UTF_8)));
return vo;
}
- private String readString(ByteBuffer buffer)
+ private String readString(byte[] buffer)
{
- try
- {
- return new String(readBytes(buffer), "UTF-8");
- }
- catch (UnsupportedEncodingException e)
- {
- return "<unsupported encoding>";
- }
+ return new String(buffer, StandardCharsets.UTF_8);
}
private byte[] readBytes(ByteBuffer buffer)