BewareMyPower commented on code in PR #21417:
URL: https://github.com/apache/pulsar/pull/21417#discussion_r1519244269
##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/TableViewImpl.java:
##########
@@ -180,96 +204,154 @@ 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.
+ 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,
Review Comment:
This method name is hard to understand. Maybe `getLastMessageIds` would be a
better name?
##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/TableViewImpl.java:
##########
@@ -180,96 +204,154 @@ 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.
+ 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);
Review Comment:
If `buildFreshTask` failed, it will return a future that completes with null
rather than a future that completes exceptionally. Then NPE might happen in the
caller code like:
```java
// lastMessageIds is null when `reader.getLastMessageIdsAsync()`
completes exceptionally
reader.thenCompose(reader -> buildFreshTask(reader,
completableFuture).thenAccept(lastMessageIds -> {
```
Here is an example that simulates the behavior I described.
```java
private static CompletableFuture<Integer> f(int x) {
final CompletableFuture<Integer> future = new CompletableFuture<>();
if (x == 0) {
future.completeExceptionally(new RuntimeException("failed"));
} else {
future.complete(x);
}
return future;
}
public static void main(String[] args) {
final CompletableFuture<Integer> anotherFuture = new
CompletableFuture<>();
final CompletableFuture<Integer> future = f(0).thenApply(x -> x +
1).exceptionally(e -> {
anotherFuture.completeExceptionally(e);
return null;
});
future.thenAccept(x -> System.out.println(x + 1)).join();
}
```
Why not write a method like
```java
private CompletableFuture<Map<String, TopicMessageId>>
getLastMessageIds(Reader<T> reader) {
return reader.getLastMessageIdsAsync().thenApply(lastMessageIds -> {
Map<String, TopicMessageId> maxMessageIds = new HashMap<>();
/* ... */
});
}
```
--
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]