|
| 1 | +package org.apache.pulsar.client.api.impl; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.concurrent.ConcurrentHashMap; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | +import org.apache.pulsar.client.admin.PulsarAdmin; |
| 10 | +import org.apache.pulsar.client.admin.PulsarAdminException; |
| 11 | +import org.apache.pulsar.client.api.Consumer; |
| 12 | +import org.apache.pulsar.client.api.Message; |
| 13 | +import org.apache.pulsar.client.api.MessageId; |
| 14 | +import org.apache.pulsar.client.api.MessageIdAdv; |
| 15 | +import org.apache.pulsar.client.api.PulsarPullConsumer; |
| 16 | +import org.apache.pulsar.client.api.PulsarClient; |
| 17 | +import org.apache.pulsar.client.api.PulsarClientException; |
| 18 | +import org.apache.pulsar.client.api.Reader; |
| 19 | +import org.apache.pulsar.client.api.Schema; |
| 20 | +import org.apache.pulsar.client.util.OffsetToMessageIdCache; |
| 21 | +import org.apache.pulsar.client.util.OffsetToMessageIdCacheProvider; |
| 22 | +import org.apache.pulsar.client.util.ReaderCache; |
| 23 | +import org.apache.pulsar.client.util.ReaderCacheProvider; |
| 24 | +import org.apache.pulsar.common.partition.PartitionedTopicMetadata; |
| 25 | + |
| 26 | +public class PulsarPullConsumerImpl<T> implements PulsarPullConsumer<T> { |
| 27 | + private final String topic; |
| 28 | + private final String subscription; |
| 29 | + private final String brokerCluster; |
| 30 | + private final Schema<T> schema; |
| 31 | + private static final String PARTITION_SPLICER = "-partition-"; |
| 32 | + private int partitionNum; |
| 33 | + |
| 34 | + Map<String, Consumer<?>> consumerMap; |
| 35 | + private final OffsetToMessageIdCache offsetToMessageIdCache; |
| 36 | + private final ReaderCache<T> readerCache; |
| 37 | + |
| 38 | + private final PulsarAdmin pulsarAdmin; |
| 39 | + private final PulsarClient pulsarClient; |
| 40 | + |
| 41 | + public PulsarPullConsumerImpl(String topic, String subscription, Schema<T> schema, PulsarClient client, |
| 42 | + PulsarAdmin admin, String brokerCluster) { |
| 43 | + this.topic = topic; |
| 44 | + this.subscription = subscription; |
| 45 | + this.brokerCluster = brokerCluster; |
| 46 | + this.schema = schema; |
| 47 | + this.consumerMap = new ConcurrentHashMap<>(); |
| 48 | + this.pulsarClient = client; |
| 49 | + this.pulsarAdmin = admin; |
| 50 | + this.offsetToMessageIdCache = OffsetToMessageIdCacheProvider.getOffsetToMessageIdCache(admin, brokerCluster); |
| 51 | + this.readerCache = ReaderCacheProvider.getReaderCache(brokerCluster, schema, client, offsetToMessageIdCache); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void start() throws PulsarClientException, PulsarAdminException { |
| 56 | + discoverPartition(); |
| 57 | + } |
| 58 | + |
| 59 | + private void discoverPartition() throws PulsarClientException, PulsarAdminException { |
| 60 | + PartitionedTopicMetadata partitionedTopicMetadata = pulsarAdmin.topics().getPartitionedTopicMetadata(topic); |
| 61 | + this.partitionNum = partitionedTopicMetadata.partitions; |
| 62 | + if (partitionNum == 0) { |
| 63 | + Consumer<?> consumer = pulsarClient.newConsumer(schema) |
| 64 | + .topic(topic) |
| 65 | + .subscriptionName(subscription) |
| 66 | + .subscribe(); |
| 67 | + consumerMap.put(topic, consumer); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + for (int i = 0; i < partitionNum; i++) { |
| 72 | + String partitionTopic = getPartitionTopicName(topic, i); |
| 73 | + Consumer<?> consumer = pulsarClient.newConsumer(schema) |
| 74 | + .topic(partitionTopic) |
| 75 | + .subscriptionName(subscription) |
| 76 | + .subscribe(); |
| 77 | + consumerMap.put(partitionTopic, consumer); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public List<Message<?>> pull(long offset, int partition, int maxNum, int maxSize, |
| 83 | + int timeout, TimeUnit timeUnit) |
| 84 | + throws PulsarClientException { |
| 85 | + List<Message<?>> messages = new ArrayList<>(); |
| 86 | + int totalSize = 0; |
| 87 | + String partitionTopic = getPartitionTopicName(topic, partition); |
| 88 | + Reader<T> reader = readerCache.getReaderByOffset(partitionTopic, offset); |
| 89 | + |
| 90 | + Message<?> lastMessage = null; |
| 91 | + long startTime = System.nanoTime(); |
| 92 | + |
| 93 | + while (messages.size() < maxNum && totalSize < maxSize) { |
| 94 | + long elapsed = System.nanoTime() - startTime; |
| 95 | + long remainingNanos = Math.max(0, TimeUnit.NANOSECONDS.convert(timeout, timeUnit) - elapsed); |
| 96 | + |
| 97 | + Message<?> message = reader.readNext(Math.toIntExact(TimeUnit.MILLISECONDS.convert(remainingNanos, TimeUnit.NANOSECONDS)), |
| 98 | + TimeUnit.MILLISECONDS); |
| 99 | + if (message == null) break; |
| 100 | + messages.add(message); |
| 101 | + lastMessage = message; |
| 102 | + totalSize += message.getData().length; |
| 103 | + } |
| 104 | + if (lastMessage != null) { |
| 105 | + readerCache.putReaderByOffset(partitionTopic, lastMessage.getIndex().get() + 1, reader); |
| 106 | + } |
| 107 | + return messages; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void ack(long offset, int partition) throws PulsarClientException { |
| 112 | + String partitionTopic = getPartitionTopicName(topic, partition); |
| 113 | + Consumer<?> consumer = consumerMap.get(partitionTopic); |
| 114 | + MessageId messageId = offsetToMessageIdCache.getMessageIdByOffset(partitionTopic, offset); |
| 115 | + consumer.acknowledgeCumulative(messageId); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public long searchOffset(String topic, int partition, long timestamp) throws PulsarAdminException { |
| 120 | + String partitionTopic = getPartitionTopicName(topic, partition); |
| 121 | + MessageIdAdv messageId = (MessageIdAdv) pulsarAdmin.topics().getMessageIdByTimestamp(partitionTopic, timestamp); |
| 122 | + List<Message<byte[]>> messages = pulsarAdmin.topics() |
| 123 | + .getMessagesById(partitionTopic, messageId.getLedgerId(), messageId.getEntryId()); |
| 124 | + if (messages == null || messages.isEmpty()) { |
| 125 | + throw new IllegalArgumentException("The message is not found"); |
| 126 | + } |
| 127 | + Message<byte[]> message = messages.get(messages.size() - 1); |
| 128 | + if (message.getIndex().isEmpty()) { |
| 129 | + throw new IllegalArgumentException("The message index is empty"); |
| 130 | + } |
| 131 | + offsetToMessageIdCache.putMessageIdByOffset(partitionTopic, message.getIndex().get(), message.getMessageId()); |
| 132 | + return message.getIndex().get(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public long getConsumeStats(String topic, Integer partition, String group) throws PulsarAdminException { |
| 137 | + String partitionTopic = getPartitionTopicName(topic, partition); |
| 138 | + |
| 139 | + // Get partition stats using proper partition identifier |
| 140 | + String messageIdString = pulsarAdmin.topics().getPartitionedInternalStats(topic) |
| 141 | + .partitions.get(partitionTopic) |
| 142 | + .cursors |
| 143 | + .get(subscription) |
| 144 | + .markDeletePosition; |
| 145 | + |
| 146 | + // Handle potential format errors |
| 147 | + if (!messageIdString.contains(":")) { |
| 148 | + throw new PulsarAdminException("Invalid message ID format: " + messageIdString); |
| 149 | + } |
| 150 | + |
| 151 | + String[] ids = messageIdString.split(":"); |
| 152 | + try { |
| 153 | + long ledgerId = Long.parseLong(ids[0]); |
| 154 | + long entryId = Long.parseLong(ids[1]); |
| 155 | + |
| 156 | + // Use partition-specific topic to retrieve message |
| 157 | + List<Message<byte[]>> messages = pulsarAdmin.topics().getMessagesById(partitionTopic, ledgerId, entryId); |
| 158 | + if (messages == null || messages.isEmpty()) { |
| 159 | + throw new PulsarAdminException("Message not found for offset: " + messageIdString); |
| 160 | + } |
| 161 | + Message<?> message = messages.get(messages.size() - 1); |
| 162 | + if (message.getIndex().isEmpty()) { |
| 163 | + throw new PulsarAdminException("Message index is empty for offset: " + messageIdString); |
| 164 | + } |
| 165 | + offsetToMessageIdCache.putMessageIdByOffset(partitionTopic, message.getIndex().get(), message.getMessageId()); |
| 166 | + return message.getIndex().get(); |
| 167 | + } catch (NumberFormatException e) { |
| 168 | + throw new PulsarAdminException("Invalid message ID components: " + messageIdString, e); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // Add resource cleanup method |
| 173 | + @Override |
| 174 | + public void close() throws IOException { |
| 175 | + // Close all consumers |
| 176 | + for (Consumer<?> consumer : consumerMap.values()) { |
| 177 | + consumer.close(); |
| 178 | + } |
| 179 | + |
| 180 | + offsetToMessageIdCache.cleanup(); |
| 181 | + } |
| 182 | + |
| 183 | + private String getPartitionTopicName(String topic, int partition) { |
| 184 | + if (partitionNum > 0 && partition < 0) { |
| 185 | + throw new IllegalArgumentException("Partition index must be non-negative for partitioned topics"); |
| 186 | + } else if (partition >= partitionNum) { |
| 187 | + throw new IllegalArgumentException("Partition index out of bounds: " + partition + |
| 188 | + " for topic " + topic + " with " + partitionNum + " partitions"); |
| 189 | + } |
| 190 | + return partition >= 0 ? topic + PARTITION_SPLICER + partition : topic; |
| 191 | + } |
| 192 | +} |
0 commit comments