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


##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/TableViewImpl.java:
##########
@@ -180,96 +204,157 @@ public void close() throws PulsarClientException {
     }
 
     private void handleMessage(Message<T> msg) {
-        try {
-            if (msg.hasKey()) {
-                String key = msg.getKey();
-                T cur = msg.size() > 0 ? msg.getValue() : null;
-                if (log.isDebugEnabled()) {
-                    log.debug("Applying message from topic {}. key={} 
value={}",
+        readPositions.put(msg.getTopicName(), msg.getMessageId());
+        if (msg.hasKey()) {
+            String key = msg.getKey();
+            T cur = msg.size() > 0 ? msg.getValue() : null;
+            if (log.isDebugEnabled()) {
+                log.debug("Applying message from topic {}. key={} value={}",
+                        conf.getTopicName(),
+                        key,
+                        cur);
+            }
+
+            boolean update = true;
+            if (compactionStrategy != null) {
+                T prev = data.get(key);
+                update = !compactionStrategy.shouldKeepLeft(prev, cur);
+                if (!update) {
+                    log.info("Skipped the message from topic {}. key={} 
value={} prev={}",
                             conf.getTopicName(),
                             key,
-                            cur);
+                            cur,
+                            prev);
+                    compactionStrategy.handleSkippedMessage(key, cur);
                 }
+            }
 
-                boolean update = true;
-                if (compactionStrategy != null) {
-                    T prev = data.get(key);
-                    update = !compactionStrategy.shouldKeepLeft(prev, cur);
-                    if (!update) {
-                        log.info("Skipped the message from topic {}. key={} 
value={} prev={}",
-                                conf.getTopicName(),
-                                key,
-                                cur,
-                                prev);
-                        compactionStrategy.handleSkippedMessage(key, cur);
+            if (update) {
+                try {
+                    listenersMutex.lock();
+                    if (null == cur) {
+                        data.remove(key);
+                    } else {
+                        data.put(key, cur);
                     }
-                }
-
-                if (update) {
-                    try {
-                        listenersMutex.lock();
-                        if (null == cur) {
-                            data.remove(key);
-                        } else {
-                            data.put(key, cur);
-                        }
 
-                        for (BiConsumer<String, T> listener : listeners) {
-                            try {
-                                listener.accept(key, cur);
-                            } catch (Throwable t) {
-                                log.error("Table view listener raised an 
exception", t);
-                            }
+                    for (BiConsumer<String, T> listener : listeners) {
+                        try {
+                            listener.accept(key, cur);
+                        } catch (Throwable t) {
+                            log.error("Table view listener raised an 
exception", t);
                         }
-                    } finally {
-                        listenersMutex.unlock();
                     }
+                } finally {
+                    listenersMutex.unlock();
                 }
             }
-        } finally {
-            msg.release();
         }
     }
 
-    private CompletableFuture<Reader<T>> readAllExistingMessages(Reader<T> 
reader) {
+    @Override
+    public CompletableFuture<Void> refreshAsync() {
+        CompletableFuture<Void> completableFuture = new CompletableFuture<>();
+        reader.thenCompose(reader -> buildFreshTask(reader, 
completableFuture).thenAccept(lastMessageIds -> {
+            // After get the response of lastMessageIds, put the future and 
result into `refreshMap`
+            // and then filter out partitions that has been read to the 
lastMessageID.
+            synchronized (this) {
+                refreshRequests.put(completableFuture, lastMessageIds);
+                filterReceivedMessages(lastMessageIds);
+                // If there is no new messages, the refresh operation could be 
completed right now.
+                if (lastMessageIds.isEmpty()) {
+                    refreshRequests.remove(completableFuture);
+                    completableFuture.complete(null);
+                }
+            }
+        }));
+        return completableFuture;
+    }
+
+    @Override
+    public void refresh() throws PulsarClientException {
+        refreshAsync().join();
+    }
+
+    private CompletableFuture<Void> readAllExistingMessages(Reader<T> reader) {
         long startTime = System.nanoTime();
         AtomicLong messagesRead = new AtomicLong();
 
-        CompletableFuture<Reader<T>> future = new CompletableFuture<>();
-        reader.getLastMessageIdsAsync().thenAccept(lastMessageIds -> {
+        CompletableFuture<Void> future = new CompletableFuture<>();
+        buildFreshTask(reader, future).thenAccept(maxMessageIds -> {
+            readAllExistingMessages(reader, future, startTime, messagesRead, 
maxMessageIds);
+        });
+        return future;
+    }
+
+    private CompletableFuture<Map<String, TopicMessageId>> 
buildFreshTask(Reader<T> reader,
+                                                                            
CompletableFuture<Void> future) {
+        return reader.getLastMessageIdsAsync().thenApply(lastMessageIds -> {
             Map<String, TopicMessageId> maxMessageIds = new 
ConcurrentHashMap<>();
             lastMessageIds.forEach(topicMessageId -> {
                 maxMessageIds.put(topicMessageId.getOwnerTopic(), 
topicMessageId);
             });
-            readAllExistingMessages(reader, future, startTime, messagesRead, 
maxMessageIds);
+            return maxMessageIds;
         }).exceptionally(ex -> {
             future.completeExceptionally(ex);
             return null;
         });
-        future.thenAccept(__ -> readTailMessages(reader));
-        return future;
     }
 
-    private void readAllExistingMessages(Reader<T> reader, 
CompletableFuture<Reader<T>> future, long startTime,
+    private void filterReceivedMessages(Map<String, TopicMessageId> 
lastMessageIds) {
+        // The `lastMessageIds` and `readPositions` is concurrency-safe data 
types.
+        lastMessageIds.forEach((partition, lastMessageId) -> {
+            if (readPositions.containsKey(partition) && 
lastMessageId.compareTo(readPositions.get(partition)) <= 0) {
+                lastMessageIds.remove(partition);
+            }
+        });

Review Comment:
   Good suggestion!



-- 
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