Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve][client] PIP-224: Add getLastMessageIds API to deprecate getLastMessageId #25

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,12 @@ public void testHasMessageAvailable() throws Exception {
assertFalse(lastMsgId instanceof BatchMessageIdImpl);
assertEquals(lastMsgId.getLedgerId(), messageId.getLedgerId());
assertEquals(lastMsgId.getEntryId(), messageId.getEntryId());
List<TopicMessageId> lastMsgIds = reader.getConsumer().getLastMessageIds();
assertEquals(lastMsgIds.size(), 1);
assertEquals(lastMsgIds.get(0).getOwnerTopic(), topicName);
MessageIdAdv lastMsgIdAdv = (MessageIdAdv) lastMsgIds.get(0);
assertEquals(lastMsgIdAdv.getLedgerId(), messageId.getLedgerId());
assertEquals(lastMsgIdAdv.getEntryId(), messageId.getEntryId());
reader.close();

CountDownLatch latch = new CountDownLatch(numOfMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.pulsar.client.api.ConsumerEventListener;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.MessageIdAdv;
import org.apache.pulsar.client.api.MessageRouter;
import org.apache.pulsar.client.api.MessageRoutingMode;
import org.apache.pulsar.client.api.Producer;
Expand All @@ -42,7 +43,9 @@
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.client.api.SubscriptionInitialPosition;
import org.apache.pulsar.client.api.SubscriptionType;
import org.apache.pulsar.client.api.TopicMessageId;
import org.apache.pulsar.client.api.TopicMetadata;
import org.apache.pulsar.common.naming.TopicName;
import org.apache.pulsar.common.policies.data.ClusterData;
import org.apache.pulsar.common.policies.data.PartitionedTopicStats;
import org.apache.pulsar.common.policies.data.SubscriptionStats;
Expand Down Expand Up @@ -1097,6 +1100,11 @@ public void testGetLastMessageId() throws Exception {
admin.topics().createPartitionedTopic(topicName2, 2);
admin.topics().createPartitionedTopic(topicName3, 3);

final Set<String> topics = new HashSet<>();
topics.add(topicName1);
IntStream.range(0, 2).forEach(i -> topics.add(topicName2 + TopicName.PARTITIONED_TOPIC_SUFFIX + i));
IntStream.range(0, 3).forEach(i -> topics.add(topicName3 + TopicName.PARTITIONED_TOPIC_SUFFIX + i));

// 1. producer connect
Producer<byte[]> producer1 = pulsarClient.newProducer().topic(topicName1)
.enableBatching(false)
Expand Down Expand Up @@ -1146,12 +1154,27 @@ public void testGetLastMessageId() throws Exception {
}
});

List<TopicMessageId> msgIds = consumer.getLastMessageIds();
assertEquals(msgIds.size(), 6);
assertEquals(msgIds.stream().map(TopicMessageId::getOwnerTopic).collect(Collectors.toSet()), topics);
for (TopicMessageId msgId : msgIds) {
int numMessages = (int) ((MessageIdAdv) msgId).getEntryId() + 1;
if (msgId.getOwnerTopic().equals(topicName1)) {
assertEquals(numMessages, totalMessages);
} else if (msgId.getOwnerTopic().startsWith(topicName2)) {
assertEquals(numMessages, totalMessages / 2);
} else {
assertEquals(numMessages, totalMessages / 3);
}
}

for (int i = 0; i < totalMessages; i++) {
producer1.send((messagePredicate + "producer1-" + i).getBytes());
producer2.send((messagePredicate + "producer2-" + i).getBytes());
producer3.send((messagePredicate + "producer3-" + i).getBytes());
}


messageId = consumer.getLastMessageId();
assertTrue(messageId instanceof MultiMessageIdImpl);
MultiMessageIdImpl multiMessageId2 = (MultiMessageIdImpl) messageId;
Expand All @@ -1170,6 +1193,20 @@ public void testGetLastMessageId() throws Exception {
}
});

msgIds = consumer.getLastMessageIds();
assertEquals(msgIds.size(), 6);
assertEquals(msgIds.stream().map(TopicMessageId::getOwnerTopic).collect(Collectors.toSet()), topics);
for (TopicMessageId msgId : msgIds) {
int numMessages = (int) ((MessageIdAdv) msgId).getEntryId() + 1;
if (msgId.getOwnerTopic().equals(topicName1)) {
assertEquals(numMessages, totalMessages * 2);
} else if (msgId.getOwnerTopic().startsWith(topicName2)) {
assertEquals(numMessages, totalMessages);
} else {
assertEquals(numMessages, totalMessages / 3 * 2);
}
}

consumer.unsubscribe();
consumer.close();
producer1.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.pulsar.client.api;

import java.io.Closeable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -539,16 +540,36 @@ CompletableFuture<Void> reconsumeLaterCumulativeAsync(Message<?> message,
* Get the last message id available for consume.
*
* @return the last message id.
* @apiNote If the consumer is a multi-topics consumer, the returned value cannot be used anywhere.
* @deprecated Use {@link Consumer#getLastMessageIds()} instead.
*/
@Deprecated
MessageId getLastMessageId() throws PulsarClientException;

/**
* Get the last message id available for consume.
*
* @return a future that can be used to track the completion of the operation.
* @deprecated Use {@link Consumer#getLastMessageIdsAsync()}} instead.
*/
@Deprecated
CompletableFuture<MessageId> getLastMessageIdAsync();

/**
* Get all the last message id of the topics the consumer subscribed.
*
* @return the list of TopicMessageId instances of all the topics that the consumer subscribed
* @throws PulsarClientException if failed to get last message id.
* @apiNote It's guaranteed that the owner topic of each TopicMessageId in the returned list is different from owner
* topics of other TopicMessageId instances
*/
List<TopicMessageId> getLastMessageIds() throws PulsarClientException;

/**
* The asynchronous version of {@link Consumer#getLastMessageIds()}.
*/
CompletableFuture<List<TopicMessageId>> getLastMessageIdsAsync();

/**
* @return Whether the consumer is connected to the broker
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.client.api.SubscriptionType;
import org.apache.pulsar.client.api.TopicMessageId;
import org.apache.pulsar.client.api.transaction.Transaction;
import org.apache.pulsar.client.impl.conf.ConsumerConfigurationData;
import org.apache.pulsar.client.impl.transaction.TransactionImpl;
Expand Down Expand Up @@ -730,6 +731,7 @@ public void close() throws PulsarClientException {
public abstract CompletableFuture<Void> closeAsync();


@Deprecated
@Override
public MessageId getLastMessageId() throws PulsarClientException {
try {
Expand All @@ -742,9 +744,22 @@ public MessageId getLastMessageId() throws PulsarClientException {
}
}

@Deprecated
@Override
public abstract CompletableFuture<MessageId> getLastMessageIdAsync();

@Override
public List<TopicMessageId> getLastMessageIds() throws PulsarClientException {
try {
return getLastMessageIdsAsync().get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw PulsarClientException.unwrap(e);
} catch (ExecutionException e) {
throw PulsarClientException.unwrap(e);
}
}

private boolean isCumulativeAcknowledgementAllowed(SubscriptionType type) {
return SubscriptionType.Shared != type && SubscriptionType.Key_Shared != type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2336,11 +2336,18 @@ private static final class GetLastMessageIdResponse {
}
}

@Deprecated
@Override
public CompletableFuture<MessageId> getLastMessageIdAsync() {
return internalGetLastMessageIdAsync().thenApply(r -> r.lastMessageId);
}

@Override
public CompletableFuture<List<TopicMessageId>> getLastMessageIdsAsync() {
return getLastMessageIdAsync()
.thenApply(msgId -> Collections.singletonList(TopicMessageId.create(topic, msgId)));
}

public CompletableFuture<GetLastMessageIdResponse> internalGetLastMessageIdAsync() {
if (getState() == State.Closing || getState() == State.Closed) {
return FutureUtil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* This is useful when MessageId is need for partition/multi-topics/pattern consumer.
* e.g. seek(), ackCumulative(), getLastMessageId().
*/
@Deprecated
public class MultiMessageIdImpl implements MessageId {
@Getter
private Map<String, MessageId> map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,7 @@ public Timeout getPartitionsAutoUpdateTimeout() {
return partitionsAutoUpdateTimeout;
}

@Deprecated
@Override
public CompletableFuture<MessageId> getLastMessageIdAsync() {
CompletableFuture<MessageId> returnFuture = new CompletableFuture<>();
Expand Down Expand Up @@ -1496,6 +1497,18 @@ public CompletableFuture<MessageId> getLastMessageIdAsync() {
return returnFuture;
}

@Override
public CompletableFuture<List<TopicMessageId>> getLastMessageIdsAsync() {
final List<CompletableFuture<List<TopicMessageId>>> futures = consumers.values().stream()
.map(ConsumerImpl::getLastMessageIdsAsync)
.collect(Collectors.toList());
return FutureUtil.waitForAll(futures).thenApply(__ -> {
final List<TopicMessageId> messageIds = new ArrayList<>();
futures.stream().map(CompletableFuture::join).forEach(messageIds::addAll);
return messageIds;
});
}

private static final Logger log = LoggerFactory.getLogger(MultiTopicsConsumerImpl.class);

public static boolean isIllegalMultiTopicsMessageId(MessageId messageId) {
Expand Down