liangyepianzhou commented on code in PR #21270:
URL: https://github.com/apache/pulsar/pull/21270#discussion_r1379501450


##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/TableViewImpl.java:
##########
@@ -235,20 +237,39 @@ private CompletableFuture<Reader<T>> 
readAllExistingMessages(Reader<T> reader) {
         AtomicLong messagesRead = new AtomicLong();
 
         CompletableFuture<Reader<T>> future = new CompletableFuture<>();
-        readAllExistingMessages(reader, future, startTime, messagesRead);
+        reader.getLastMessageIdsAsync().thenAccept(lastMessageIds -> {
+            Map<String, TopicMessageId> maxMessageIds = new 
ConcurrentHashMap<>();
+            lastMessageIds.forEach(topicMessageId -> {
+                maxMessageIds.put(topicMessageId.getOwnerTopic(), 
topicMessageId);
+            });
+            readAllExistingMessages(reader, future, startTime, messagesRead, 
maxMessageIds);
+        }).exceptionally(ex -> {
+            future.completeExceptionally(ex);
+            return null;
+        });
         return future;
     }
 
     private void readAllExistingMessages(Reader<T> reader, 
CompletableFuture<Reader<T>> future, long startTime,
-                                         AtomicLong messagesRead) {
+                                         AtomicLong messagesRead, Map<String, 
TopicMessageId> maxMessageIds) {
         reader.hasMessageAvailableAsync()
                 .thenAccept(hasMessage -> {
                    if (hasMessage) {
                        reader.readNextAsync()
                                .thenAccept(msg -> {
                                   messagesRead.incrementAndGet();
                                   handleMessage(msg);
-                                  readAllExistingMessages(reader, future, 
startTime, messagesRead);
+                                  // The message is read one by one in a 
single thread,
+                                  // so it's fine that uses a hashmap to store 
last message ID.
+                                  TopicMessageId maxMessageId = 
maxMessageIds.get(msg.getTopicName());

Review Comment:
   > We need to remove it from the `maxMessageIds` once the partition has been 
read completely.
   > ```java
   >         TopicMessageId maxMessageId = 
maxMessageIds.get(msg.getTopicName());
   >         if (maxMessageId != null && 
msg.getMessageId().compareTo(maxMessageId) >= 0) {
   >             maxMessageIds.remove(msg.getTopicName());
   >         }
   > ```
   
   
   @BewareMyPower I mean this is not an unexpected case.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to