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

PIP-13-1/3: Provide TopicsConsumer to consume from several topics under same namespace #1103

Merged
merged 6 commits into from
Feb 21, 2018
Merged
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 @@ -539,11 +539,11 @@ public void testUnackedCountWithRedeliveries() throws Exception {
producer.send(("hello-" + i).getBytes());
}

Set<MessageIdImpl> c1_receivedMessages = new HashSet<>();
Set<MessageId> c1_receivedMessages = new HashSet<>();

// C-1 gets all messages but doesn't ack
for (int i = 0; i < numMsgs; i++) {
c1_receivedMessages.add((MessageIdImpl) consumer1.receive().getMessageId());
c1_receivedMessages.add(consumer1.receive().getMessageId());
}

// C-2 will not get any message initially, since everything went to C-1 already
Expand Down

Large diffs are not rendered by default.

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.Collection;
import java.util.concurrent.CompletableFuture;

import org.apache.pulsar.client.impl.PulsarClientImpl;
Expand Down Expand Up @@ -245,4 +246,60 @@ public static PulsarClient create(String serviceUrl, ClientConfiguration conf) t
* if the forceful shutdown fails
*/
void shutdown() throws PulsarClientException;


/**
* Subscribe to the given topic and subscription combination with default {@code ConsumerConfiguration}
*
* @param topics
* The collection of topic names, they should be under same namespace
* @param subscription
* The name of the subscription
* @return The {@code Consumer} object
* @throws PulsarClientException
*/
Consumer subscribe(Collection<String> topics, String subscription) throws PulsarClientException;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods should be converted into builder API from #1089


/**
* Asynchronously subscribe to the given topics and subscription combination with
* default {@code ConsumerConfiguration}
*
* @param topics
* The collection of topic names, they should be under same namespace
* @param subscription
* The name of the subscription
* @return Future of the {@code Consumer} object
*/
CompletableFuture<Consumer> subscribeAsync(Collection<String> topics, String subscription);

/**
* Subscribe to the given topics and subscription combination using given {@code ConsumerConfiguration}
*
* @param topics
* The collection of topic names, they should be under same namespace
* @param subscription
* The name of the subscription
* @param conf
* The {@code ConsumerConfiguration} object
* @return Future of the {@code Consumer} object
*/
Consumer subscribe(Collection<String> topics, String subscription, ConsumerConfiguration conf)
throws PulsarClientException;

/**
* Asynchronously subscribe to the given topics and subscription combination using given
* {@code ConsumerConfiguration}
*
* @param topics
* The collection of topic names, they should be under same namespace
* @param subscription
* The name of the subscription
* @param conf
* The {@code ConsumerConfiguration} object
* @return Future of the {@code Consumer} object
*/
CompletableFuture<Consumer> subscribeAsync(Collection<String> topics,
String subscription,
ConsumerConfiguration conf);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.pulsar.client.impl;

import com.google.common.collect.Queues;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
Expand All @@ -27,7 +28,6 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.ConsumerConfiguration;
import org.apache.pulsar.client.api.Message;
Expand All @@ -41,8 +41,6 @@
import org.apache.pulsar.common.util.FutureUtil;
import org.apache.pulsar.common.util.collections.GrowableArrayBlockingQueue;

import com.google.common.collect.Queues;

public abstract class ConsumerBase extends HandlerBase implements Consumer {

enum ConsumerType {
Expand All @@ -57,7 +55,7 @@ enum ConsumerType {
protected final ExecutorService listenerExecutor;
final BlockingQueue<Message> incomingMessages;
protected final ConcurrentLinkedQueue<CompletableFuture<Message>> pendingReceives;
protected final int maxReceiverQueueSize;
protected int maxReceiverQueueSize;

protected ConsumerBase(PulsarClientImpl client, String topic, String subscription, ConsumerConfiguration conf,
int receiverQueueSize, ExecutorService listenerExecutor, CompletableFuture<Consumer> subscribeFuture) {
Expand Down Expand Up @@ -330,7 +328,7 @@ public String getSubscription() {
* the connected consumers. This is a non blocking call and doesn't throw an exception. In case the connection
* breaks, the messages are redelivered after reconnect.
*/
protected abstract void redeliverUnacknowledgedMessages(Set<MessageIdImpl> messageIds);
protected abstract void redeliverUnacknowledgedMessages(Set<MessageId> messageIds);

@Override
public String toString() {
Expand All @@ -340,4 +338,9 @@ public String toString() {
", topic='" + topic + '\'' +
'}';
}

protected void setMaxReceiverQueueSize(int newSize) {
this.maxReceiverQueueSize = newSize;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,9 @@ public void redeliverUnacknowledgedMessages() {
}

@Override
public void redeliverUnacknowledgedMessages(Set<MessageIdImpl> messageIds) {
public void redeliverUnacknowledgedMessages(Set<MessageId> messageIds) {
checkArgument(messageIds.stream().findFirst().get() instanceof MessageIdImpl);

if (conf.getSubscriptionType() != SubscriptionType.Shared) {
// We cannot redeliver single messages if subscription type is not Shared
redeliverUnacknowledgedMessages();
Expand All @@ -1183,7 +1185,10 @@ public void redeliverUnacknowledgedMessages(Set<MessageIdImpl> messageIds) {
ClientCnx cnx = cnx();
if (isConnected() && cnx.getRemoteEndpointProtocolVersion() >= ProtocolVersion.v2.getNumber()) {
int messagesFromQueue = removeExpiredMessagesFromQueue(messageIds);
Iterable<List<MessageIdImpl>> batches = Iterables.partition(messageIds, MAX_REDELIVER_UNACKNOWLEDGED);
Iterable<List<MessageIdImpl>> batches = Iterables.partition(
messageIds.stream()
.map(messageId -> (MessageIdImpl)messageId)
.collect(Collectors.toSet()), MAX_REDELIVER_UNACKNOWLEDGED);
MessageIdData.Builder builder = MessageIdData.newBuilder();
batches.forEach(ids -> {
List<MessageIdData> messageIdDatas = ids.stream().map(messageId -> {
Expand Down Expand Up @@ -1360,7 +1365,7 @@ private MessageIdImpl getMessageIdImpl(Message msg) {
return messageId;
}

private int removeExpiredMessagesFromQueue(Set<MessageIdImpl> messageIds) {
private int removeExpiredMessagesFromQueue(Set<MessageId> messageIds) {
int messagesFromQueue = 0;
Message peek = incomingMessages.peek();
if (peek != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,20 @@ public void redeliverUnacknowledgedMessages() {
}

@Override
public void redeliverUnacknowledgedMessages(Set<MessageIdImpl> messageIds) {
public void redeliverUnacknowledgedMessages(Set<MessageId> messageIds) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you changing this to MessageId? You're casting to MessageIdImpl anyhow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, As above reply, we also need this redeliverUnacknowledgedMessages method in Consumer.java handling TopicMessageIdImpl. would like to change this in Consumer.java and make the case in each child class.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Consumer.java it doesn't know about the MessageIds. The call takes no parameters.

TopicsMessageIdImpl should be a specialization of MessageIdImpl (I assumed it was when I originally commented, until i saw Matteo's comment to the same effect). If it is a specialization of TopicsMessageIdImpl, then this signature doesn't need to change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it is in ConsumerBase.java, sorry for the wrong reference of Consumer.java.
TopicsMessageIdImpl is more need to be a wrapper for MessageIdImpl, and as the reply below, may be better to implements MessageId, instead of extends MessageIdImpl.

checkArgument(messageIds.stream().findFirst().get() instanceof MessageIdImpl);
if (conf.getSubscriptionType() != SubscriptionType.Shared) {
// We cannot redeliver single messages if subscription type is not Shared
redeliverUnacknowledgedMessages();
return;
}
removeExpiredMessagesFromQueue(messageIds);
messageIds.stream()
.collect(Collectors.groupingBy(MessageIdImpl::getPartitionIndex, Collectors.toSet()))
.forEach((partitionIndex, messageIds1) ->
consumers.get(partitionIndex).redeliverUnacknowledgedMessages(messageIds1));
.map(messageId -> (MessageIdImpl)messageId)
.collect(Collectors.groupingBy(MessageIdImpl::getPartitionIndex, Collectors.toSet()))
.forEach((partitionIndex, messageIds1) ->
consumers.get(partitionIndex).redeliverUnacknowledgedMessages(
messageIds1.stream().map(mid -> (MessageId)mid).collect(Collectors.toSet())));
resumeReceivingFromPausedConsumersIfNeeded();
}

Expand Down Expand Up @@ -546,10 +549,10 @@ public UnAckedMessageTracker getUnAckedMessageTracker() {
return unAckedMessageTracker;
}

private void removeExpiredMessagesFromQueue(Set<MessageIdImpl> messageIds) {
private void removeExpiredMessagesFromQueue(Set<MessageId> messageIds) {
Message peek = incomingMessages.peek();
if (peek != null) {
if (!messageIds.contains((MessageIdImpl) peek.getMessageId())) {
if (!messageIds.contains(peek.getMessageId())) {
// first message is not expired, then no message is expired in queue.
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.apache.commons.lang3.StringUtils.isBlank;

import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -281,6 +282,70 @@ public CompletableFuture<Consumer> subscribeAsync(final String topic, final Stri
return consumerSubscribedFuture;
}


@Override
public Consumer subscribe(Collection<String> topics, final String subscription) throws PulsarClientException {
return subscribe(topics, subscription, new ConsumerConfiguration());
}

@Override
public Consumer subscribe(Collection<String> topics,
String subscription,
ConsumerConfiguration conf)
throws PulsarClientException {
try {
return subscribeAsync(topics, subscription, conf).get();
} catch (ExecutionException e) {
Throwable t = e.getCause();
if (t instanceof PulsarClientException) {
throw (PulsarClientException) t;
} else {
throw new PulsarClientException(t);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new PulsarClientException(e);
}
}

@Override
public CompletableFuture<Consumer> subscribeAsync(Collection<String> topics, String subscription) {
return subscribeAsync(topics, subscription, new ConsumerConfiguration());
}

@Override
public CompletableFuture<Consumer> subscribeAsync(Collection<String> topics,
String subscription,
ConsumerConfiguration conf) {
if (topics == null || topics.isEmpty()) {
return FutureUtil.failedFuture(new PulsarClientException.InvalidTopicNameException("Empty topics name"));
}

if (state.get() != State.Open) {
return FutureUtil.failedFuture(new PulsarClientException.AlreadyClosedException("Client already closed"));
}

if (isBlank(subscription)) {
return FutureUtil
.failedFuture(new PulsarClientException.InvalidConfigurationException("Empty subscription name"));
}
if (conf == null) {
return FutureUtil.failedFuture(
new PulsarClientException.InvalidConfigurationException("Consumer configuration undefined"));
}

CompletableFuture<Consumer> consumerSubscribedFuture = new CompletableFuture<>();

ConsumerBase consumer = new TopicsConsumerImpl(PulsarClientImpl.this, topics, subscription,
conf, externalExecutorProvider.getExecutor(),
consumerSubscribedFuture);
synchronized (consumers) {
consumers.put(consumer, Boolean.TRUE);
}

return consumerSubscribedFuture;
}

@Override
public Reader createReader(String topic, MessageId startMessageId, ReaderConfiguration conf)
throws PulsarClientException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pulsar.client.impl;

import org.apache.pulsar.client.api.MessageId;

public class TopicMessageIdImpl implements MessageId {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be extending MessageIdImpl instead of composing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. This would reduce the amount of change needed elsewhere also.

Copy link
Contributor Author

@zhaijack zhaijack Feb 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Here TopicMessageIdImpl is mainly a wrapper for MessageIdImpl, We need keep a reference of MessageIdImpl, because it will be used for one internal-sub-consumer.
If extending, the constructor and getInnerMessageIdInner may be not easy to handle.

private final String topicName;
private final MessageId messageId;

TopicMessageIdImpl(String topicName, MessageId messageId) {
this.topicName = topicName;
this.messageId = messageId;
}

public String getTopicName() {
return topicName;
}

public MessageId getInnerMessageId() {
return messageId;
}

@Override
public byte[] toByteArray() {
return messageId.toByteArray();
}

@Override
public int compareTo(MessageId o) {
return messageId.compareTo(o);
}
}
Loading