You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2022/03/22 09:22:53 UTC

[GitHub] [rocketmq] aaron-ai commented on a change in pull request #4019: [RIP-37] Add new APIs for consumer

aaron-ai commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r831928824



##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/MessageListener.java
##########
@@ -0,0 +1,38 @@
+/*
+ * 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.rocketmq.apis.consumer;
+
+import java.util.Collection;
+import org.apache.rocketmq.apis.message.MessageView;
+
+/**
+ * MessageListener is used only for push consumer to process message consumption synchronously.
+ *
+ * <p> Refer to {@link PushConsumer}, push consumer will get message from server
+ * and dispatch the message to backend thread pool which control by parameter threadCount to consumer message concurrently.
+ */
+public interface MessageListener {
+    /**
+     * The callback interface for consume message. Your should process the collection of messageViews
+     * and put committed messageViews to committedList. Push consumer will commit the committedList to server.
+     * If consume message throw unexpected exception, Push consumer also commit the committedList.

Review comment:
       Add `<p>` here.

##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/PullConsumer.java
##########
@@ -0,0 +1,172 @@
+/*
+ * 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.rocketmq.apis.consumer;
+
+import java.io.Closeable;
+import java.time.Duration;
+import java.util.Collection;
+import java.util.Map;
+
+import org.apache.rocketmq.apis.MessageQueue;
+import org.apache.rocketmq.apis.exception.*;
+import org.apache.rocketmq.apis.message.MessageView;
+
+/**
+ * PullConsumer is a thread-safe rocketmq client which is used to consume message by queue.
+ * Unlike push consumer and simple consumer, pull consumer implement load balance based on queue granularity.
+ *
+ * <p>Pull consumer is lightweight consumer that better suited to streaming scenarios.
+ * If you want fully control the message consumption operation by yourself like scan by offset or reconsume repeatedly,
+ * pull consumer should be your first consideration.
+ *
+ * <p>Pull consumer support two load balance mode. First is subscription mode, which full manage the rebalance
+ * operation triggered when group membership or cluster and topic metadata change.Another mode is manual assignment mode,which manage the load balance by yourself.
+ *
+ * <p> Pull consumer divide message consumption to 3 parts.
+ * Firstly, determine whether to continue processing from the last consumption or reset the consumption starting point by call seek method;
+ * Then, pull message from servers.
+ * At last, pull consumer no need to commit message by offset meta.
+ * If there is a consumption error, consumer just call seek api to reset the offset for reconsume message again.
+ */
+public interface PullConsumer extends Closeable {
+    /**
+     * Listener that listens for changes of message queues when use manual assignment mode.
+     */
+    interface MessageQueuesChangeListener {
+        /**
+         * This method will be invoked in the condition of message queues changed, These scenarios occur when the
+         * topic is expanded or shrunk.
+         *
+         * @param messageQueues {@link MessageQueue} of topic.
+         */
+        void onChanged(Collection<MessageQueue> messageQueues);
+    }
+
+    /**
+     * Get metadata about the message queues for a given topic. This method will issue a remote call to the server if it
+     * does not already have any metadata about the given topic.
+     *
+     * @param topic message's topic
+     * @return message queues of topic.
+     */
+    Collection<MessageQueue> topicMessageQueues(String topic) throws ClientException;
+
+    /**
+     * Manually assign messageQueue collections to this consumer.
+     * This interface does not allow for incremental assignment and will replace the previous assignment.
+     * If the given collection is empty, it's treated same as unsubscribe().
+     * Manual assignment through this interface will disable the consumerGroup management functionality
+     * and there will be no rebalance operation triggered when group membership or cluster and topic metadata change.
+     * @param messageQueues are the collection for current consumer.
+     * @throws ClientException when assign
+     */
+    void assign(Collection<MessageQueue> messageQueues) throws ClientException;
+

Review comment:
       What would happen if I don't invoke `PullConsumerBuilder#enableManualQueueAssignment` before?

##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/PullConsumer.java
##########
@@ -0,0 +1,172 @@
+/*
+ * 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.rocketmq.apis.consumer;
+
+import java.io.Closeable;
+import java.time.Duration;
+import java.util.Collection;
+import java.util.Map;
+
+import org.apache.rocketmq.apis.MessageQueue;
+import org.apache.rocketmq.apis.exception.*;
+import org.apache.rocketmq.apis.message.MessageView;
+
+/**
+ * PullConsumer is a thread-safe rocketmq client which is used to consume message by queue.
+ * Unlike push consumer and simple consumer, pull consumer implement load balance based on queue granularity.
+ *
+ * <p>Pull consumer is lightweight consumer that better suited to streaming scenarios.
+ * If you want fully control the message consumption operation by yourself like scan by offset or reconsume repeatedly,
+ * pull consumer should be your first consideration.
+ *
+ * <p>Pull consumer support two load balance mode. First is subscription mode, which full manage the rebalance
+ * operation triggered when group membership or cluster and topic metadata change.Another mode is manual assignment mode,which manage the load balance by yourself.
+ *
+ * <p> Pull consumer divide message consumption to 3 parts.
+ * Firstly, determine whether to continue processing from the last consumption or reset the consumption starting point by call seek method;
+ * Then, pull message from servers.
+ * At last, pull consumer no need to commit message by offset meta.
+ * If there is a consumption error, consumer just call seek api to reset the offset for reconsume message again.
+ */
+public interface PullConsumer extends Closeable {
+    /**
+     * Listener that listens for changes of message queues when use manual assignment mode.
+     */
+    interface MessageQueuesChangeListener {
+        /**
+         * This method will be invoked in the condition of message queues changed, These scenarios occur when the
+         * topic is expanded or shrunk.
+         *
+         * @param messageQueues {@link MessageQueue} of topic.
+         */
+        void onChanged(Collection<MessageQueue> messageQueues);
+    }
+
+    /**
+     * Get metadata about the message queues for a given topic. This method will issue a remote call to the server if it
+     * does not already have any metadata about the given topic.
+     *
+     * @param topic message's topic
+     * @return message queues of topic.
+     */
+    Collection<MessageQueue> topicMessageQueues(String topic) throws ClientException;
+
+    /**
+     * Manually assign messageQueue collections to this consumer.
+     * This interface does not allow for incremental assignment and will replace the previous assignment.
+     * If the given collection is empty, it's treated same as unsubscribe().
+     * Manual assignment through this interface will disable the consumerGroup management functionality
+     * and there will be no rebalance operation triggered when group membership or cluster and topic metadata change.
+     * @param messageQueues are the collection for current consumer.
+     * @throws ClientException when assign
+     */
+    void assign(Collection<MessageQueue> messageQueues) throws ClientException;
+
+    /**
+     * Pull consumer query and update metadata about message queues periodically, listener is triggered once metadata
+     * is updated. The listener is required only in manual assignment mode.
+     * When use the subscription mode, no need to care the messageQueue change events.
+     *
+     * @param topic    topic to query and update metadata.
+     * @param listener listener to receive changes of metadata by topic.
+     */
+    void registerMessageQueuesChangeListener(String topic, MessageQueuesChangeListener listener);
+
+    /**
+     * Add subscription expression dynamically when use subscription mode.
+     *
+     * <p>If first {@link SubscriptionExpression} that contains topicA and tag1 is exists already in consumer, then
+     * second {@link SubscriptionExpression} which contains topicA and tag2, <strong>the result is that the second one
+     * replaces the first one instead of integrating them</strong>.
+     *
+     * @param subscriptionExpression new subscription expression to add.
+     * @return pull consumer instance.
+     */
+    PullConsumer subscribe(SubscriptionExpression subscriptionExpression) throws ClientException;
+
+    /**
+     * Remove subscription expression dynamically by topic.
+     *
+     * <p>Nothing occurs if the specified topic does not exist in subscription expressions of pull consumer.
+     *
+     * @param topic the topic to remove subscription.
+     * @return pull consumer instance.
+     */
+    PullConsumer unsubscribe(String topic) throws ClientException;
+
+    /**
+     * Get the collection of messageQueues currently assigned to current consumer.
+     * @return the collection of messageQueues currently assigned to current consumer
+     */
+    Collection<MessageQueue> assignment();

Review comment:
       assignments

##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/PullConsumer.java
##########
@@ -0,0 +1,172 @@
+/*
+ * 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.rocketmq.apis.consumer;
+
+import java.io.Closeable;
+import java.time.Duration;
+import java.util.Collection;
+import java.util.Map;
+
+import org.apache.rocketmq.apis.MessageQueue;
+import org.apache.rocketmq.apis.exception.*;
+import org.apache.rocketmq.apis.message.MessageView;
+
+/**
+ * PullConsumer is a thread-safe rocketmq client which is used to consume message by queue.
+ * Unlike push consumer and simple consumer, pull consumer implement load balance based on queue granularity.
+ *
+ * <p>Pull consumer is lightweight consumer that better suited to streaming scenarios.
+ * If you want fully control the message consumption operation by yourself like scan by offset or reconsume repeatedly,
+ * pull consumer should be your first consideration.
+ *
+ * <p>Pull consumer support two load balance mode. First is subscription mode, which full manage the rebalance
+ * operation triggered when group membership or cluster and topic metadata change.Another mode is manual assignment mode,which manage the load balance by yourself.
+ *
+ * <p> Pull consumer divide message consumption to 3 parts.
+ * Firstly, determine whether to continue processing from the last consumption or reset the consumption starting point by call seek method;
+ * Then, pull message from servers.
+ * At last, pull consumer no need to commit message by offset meta.
+ * If there is a consumption error, consumer just call seek api to reset the offset for reconsume message again.
+ */
+public interface PullConsumer extends Closeable {
+    /**
+     * Listener that listens for changes of message queues when use manual assignment mode.
+     */
+    interface MessageQueuesChangeListener {
+        /**
+         * This method will be invoked in the condition of message queues changed, These scenarios occur when the
+         * topic is expanded or shrunk.
+         *
+         * @param messageQueues {@link MessageQueue} of topic.
+         */
+        void onChanged(Collection<MessageQueue> messageQueues);

Review comment:
       Could user adjust the frequency that the listener is invoked?

##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/PullConsumer.java
##########
@@ -0,0 +1,172 @@
+/*
+ * 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.rocketmq.apis.consumer;
+
+import java.io.Closeable;
+import java.time.Duration;
+import java.util.Collection;
+import java.util.Map;
+
+import org.apache.rocketmq.apis.MessageQueue;
+import org.apache.rocketmq.apis.exception.*;
+import org.apache.rocketmq.apis.message.MessageView;
+
+/**
+ * PullConsumer is a thread-safe rocketmq client which is used to consume message by queue.
+ * Unlike push consumer and simple consumer, pull consumer implement load balance based on queue granularity.
+ *
+ * <p>Pull consumer is lightweight consumer that better suited to streaming scenarios.
+ * If you want fully control the message consumption operation by yourself like scan by offset or reconsume repeatedly,
+ * pull consumer should be your first consideration.
+ *
+ * <p>Pull consumer support two load balance mode. First is subscription mode, which full manage the rebalance
+ * operation triggered when group membership or cluster and topic metadata change.Another mode is manual assignment mode,which manage the load balance by yourself.
+ *
+ * <p> Pull consumer divide message consumption to 3 parts.
+ * Firstly, determine whether to continue processing from the last consumption or reset the consumption starting point by call seek method;
+ * Then, pull message from servers.
+ * At last, pull consumer no need to commit message by offset meta.
+ * If there is a consumption error, consumer just call seek api to reset the offset for reconsume message again.
+ */
+public interface PullConsumer extends Closeable {
+    /**
+     * Listener that listens for changes of message queues when use manual assignment mode.
+     */
+    interface MessageQueuesChangeListener {
+        /**
+         * This method will be invoked in the condition of message queues changed, These scenarios occur when the
+         * topic is expanded or shrunk.
+         *
+         * @param messageQueues {@link MessageQueue} of topic.
+         */
+        void onChanged(Collection<MessageQueue> messageQueues);
+    }
+
+    /**
+     * Get metadata about the message queues for a given topic. This method will issue a remote call to the server if it
+     * does not already have any metadata about the given topic.
+     *
+     * @param topic message's topic
+     * @return message queues of topic.
+     */
+    Collection<MessageQueue> topicMessageQueues(String topic) throws ClientException;
+
+    /**
+     * Manually assign messageQueue collections to this consumer.
+     * This interface does not allow for incremental assignment and will replace the previous assignment.
+     * If the given collection is empty, it's treated same as unsubscribe().
+     * Manual assignment through this interface will disable the consumerGroup management functionality
+     * and there will be no rebalance operation triggered when group membership or cluster and topic metadata change.
+     * @param messageQueues are the collection for current consumer.
+     * @throws ClientException when assign
+     */
+    void assign(Collection<MessageQueue> messageQueues) throws ClientException;
+
+    /**
+     * Pull consumer query and update metadata about message queues periodically, listener is triggered once metadata
+     * is updated. The listener is required only in manual assignment mode.
+     * When use the subscription mode, no need to care the messageQueue change events.
+     *
+     * @param topic    topic to query and update metadata.
+     * @param listener listener to receive changes of metadata by topic.
+     */
+    void registerMessageQueuesChangeListener(String topic, MessageQueuesChangeListener listener);
+
+    /**
+     * Add subscription expression dynamically when use subscription mode.
+     *
+     * <p>If first {@link SubscriptionExpression} that contains topicA and tag1 is exists already in consumer, then
+     * second {@link SubscriptionExpression} which contains topicA and tag2, <strong>the result is that the second one
+     * replaces the first one instead of integrating them</strong>.
+     *
+     * @param subscriptionExpression new subscription expression to add.
+     * @return pull consumer instance.
+     */
+    PullConsumer subscribe(SubscriptionExpression subscriptionExpression) throws ClientException;
+
+    /**
+     * Remove subscription expression dynamically by topic.
+     *
+     * <p>Nothing occurs if the specified topic does not exist in subscription expressions of pull consumer.
+     *
+     * @param topic the topic to remove subscription.
+     * @return pull consumer instance.
+     */
+    PullConsumer unsubscribe(String topic) throws ClientException;
+
+    /**
+     * Get the collection of messageQueues currently assigned to current consumer.
+     * @return the collection of messageQueues currently assigned to current consumer
+     */
+    Collection<MessageQueue> assignment();
+
+    /**
+     * Fetch messages from server synchronously. This method returns immediately if there are messages available.
+     * Otherwise, it will await the passed timeout. If the timeout expires, an empty map will be returned.
+     * An error occurs if you do not subscribe or assign messageQueues before polling for data.

Review comment:
       Add `<p>` here

##########
File path: apis/src/main/java/org/apache/rocketmq/apis/ClientServiceProvider.java
##########
@@ -43,6 +47,27 @@ static ClientServiceProvider loadService() {
      */
     ProducerBuilder newProducerBuilder();
 
+    /**
+     * Get the simple consumer builder by current provider.
+     *
+     * @return the simple consumer builder instance.
+     */
+    SimpleConsumerBuilder newSimpleConsumerBuilder();
+
+    /**
+     * Get the pull consumer builder by current provider.
+     *
+     * @return the pull consumer builder instance.
+     */
+    PullConsumerBuilder newPollConsumerBuilder();

Review comment:
       newPollConsumerBuilder => newPullConsumerBuilder

##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/PushConsumer.java
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.rocketmq.apis.consumer;
+
+import com.google.common.util.concurrent.Service;
+import java.io.Closeable;
+import java.util.Collection;
+
+import org.apache.rocketmq.apis.exception.*;
+
+/**
+ * PushConsumer is a thread-safe rocketmq client which is used to consume message by group.
+ *
+ * <p>Push consumer is fully-managed consumer, if you are confused to choose your consumer, push consumer should be
+ * your first consideration.
+ *
+ * <p>Consumers belong to the same consumer group share messages from server,
+ * so consumer in the same group must have the same {@link SubscriptionExpression}s, otherwise the behavior is
+ * undefined. If a new consumer group's consumer is started first time, it consumes from the latest position. Once
+ * consumer is started, server records its consumption progress and derives it in subsequent startup.
+ *
+ * <p>You may intend to maintain different consumption progress for different consumer, different consumer group
+ * should be set in this case.
+ *
+ * <p>To accelerate the message consumption, push consumer applies
+ * <a href="https://en.wikipedia.org/wiki/Reactive_Streams">reactive streams</a>
+ * . Messages received from server is cached locally before consumption,
+ * {@link PushConsumerBuilder#setMaxCacheMessageCount(int)} and
+ * {@link PushConsumerBuilder#setMaxCacheMessageSizeInBytes(int)} could be used to set the cache threshold in
+ * different dimension.
+ */
+public interface PushConsumer extends Closeable {
+    /**
+     * Get the load balancing group for consumer.
+     *
+     * @return consumer load balancing group.
+     */
+    String getConsumerGroup();
+
+    /**
+     * Get the existed subscription expression in push consumer.
+     *
+     * @return collections of subscription expression.
+     */
+    Collection<SubscriptionExpression> listSubscriptionExpression();

Review comment:
       Considering the `PullConsumer#assignments()` before, maybe we should keep the same style?

##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/PullConsumerBuilder.java
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.rocketmq.apis.consumer;
+
+import org.apache.rocketmq.apis.ClientConfiguration;
+import org.apache.rocketmq.apis.exception.ClientException;
+
+import java.time.Duration;
+
+public interface PullConsumerBuilder {
+    /**
+     * Set the client configuration for pull consumer.
+     *
+     * @param clientConfiguration client's configuration.
+     * @return the pull consumer builder instance.
+     */
+    PullConsumerBuilder setClientConfiguration(ClientConfiguration clientConfiguration);
+
+    /**
+     * Set the load balancing group for consumer.
+     *
+     * @param consumerGroup consumer load balancing group.
+     * @return the consumer builder instance.
+     */
+    PullConsumerBuilder setConsumerGroup(String consumerGroup);
+
+    /**
+     * Enable manual messageQueue assignment consumption mode.
+     * The default mode is subscription mode which manage the rebalance operation triggered when group membership or cluster and topic metadata change.
+     * When pull consumer manual queue assignment mode, must invoke assign method before pull message.
+     * @return the consumer builder instance.
+     */
+    PushConsumerBuilder enableManualQueueAssignment();
+

Review comment:
       or enableManualAssignment?




-- 
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: dev-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org