BewareMyPower commented on code in PR #21417:
URL: https://github.com/apache/pulsar/pull/21417#discussion_r1519134947
##########
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:
And I don't understand why did you use `synchronized (this)` for the logic
in `refreshAsync`. `refreshRequests` is a `LinkedHashMap` that is not thread
safe, but you didn't synchronize all places that access `refreshRequests` like
`readTailMessages`. I think we can simply change it to a concurrent hash map.
##########
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:
You don't need to call this method in a synchronized block. `readPositions`
is a concurrent hash map, so you don't need to call `containsKey` and `get`
twice.
```java
lastMessageIds.forEach((partition, lastMessageId) -> {
MessageId readPosition = readPositions.get(partition);
if (readPosition != null &&
lastMessageId.compareTo(readPosition) <= 0) {
lastMessageIds.remove(partition);
}
});
```
--
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]