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 08:36:36 UTC

[GitHub] [rocketmq] chenzlalvin opened a new pull request #4019: [RIP-37] Add new APIs for consumer

chenzlalvin opened a new pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019


   As we mentioned in [RIP-37 New and unified APIs](https://shimo.im/docs/m5kv92OeRRU8olqX), establish a new APIs specifications. we divide it into two independent pull requests.
   
   The producer part.
   The consumer part.
   
   Similar to the previous part https://github.com/apache/rocketmq/pull/3987, this pull request is about the consumer part.
   
   related issue: https://github.com/apache/rocketmq/issues/3973


-- 
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



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

Posted by GitBox <gi...@apache.org>.
chenzlalvin commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r831955901



##########
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:
       got. spelling mistake.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
chenzlalvin commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r832745750



##########
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.
+     * @param messageQueue the target messageQueue to pull message.
+     * @param maxMessageNum max message num when server returns.
+     * @return collection of messageViews of this queue.
+     */
+    Collection<MessageView> pull(MessageQueue messageQueue, int maxMessageNum) throws ClientException;
+
+    /**
+     * Commit the offsets for the specified messageQueue.
+     * @param messageQueue the specified messageQueue to commit offset

Review comment:
       Maybe we need to rethink of the design of PullConsumer.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
zhouxinyu commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r834911426



##########
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.
+     * <p> 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 enableManualAssignment();

Review comment:
       Return PullConsumerBuilder

##########
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.
+     * <p> 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 enableManualAssignment();
+
+    /**
+     * Set the max await time when receive message from server.
+     * <p> The simple consumer will hold this long-polling receive requests until  a message is returned or a timeout occurs.
+     * @param awaitDuration The maximum time to block when no message available.
+     * @return the consumer builder instance.
+     */
+    PushConsumerBuilder setAwaitDuration(Duration awaitDuration);

Review comment:
       Return PullConsumerBuilder

##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/SimpleConsumer.java
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.List;
+import java.util.concurrent.CompletableFuture;
+
+import org.apache.rocketmq.apis.exception.*;
+import org.apache.rocketmq.apis.message.MessageView;
+
+/**
+ * SimpleConsumer is a thread-safe rocketmq client which is used to consume message by group.
+ *
+ * <p>Simple consumer is lightweight consumer , if you want fully control the message consumption operation by yourself,
+ * simple 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> Simple consumer divide message consumption to 3 parts.
+ * Firstly, call receive api get messages from server; Then process message by yourself; At last, your must call Ack api to commit this message.
+ * If there is error when process message ,your can reconsume the message later which control by the invisibleDuration parameter.
+ * Also, you can change the invisibleDuration by call changeInvisibleDuration api.
+ */
+public interface SimpleConsumer extends Closeable {
+    /**
+     * Get the load balancing group for simple consumer.
+     *
+     * @return consumer load balancing group.
+     */
+    String getConsumerGroup();
+
+    /**
+     * Add subscription expression dynamically.
+     *
+     * <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 simple consumer instance.
+     */
+    SimpleConsumer subscribe(SubscriptionExpression subscriptionExpression) throws ClientException;

Review comment:
       Each consumer has subscription management and consumer group, consider abstract a super interface type?

##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/PullConsumer.java
##########
@@ -0,0 +1,186 @@
+/*
+ * 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 java.util.concurrent.CompletableFuture;
+
+import org.apache.rocketmq.apis.MessageQueue;
+import org.apache.rocketmq.apis.exception.*;
+import org.apache.rocketmq.apis.message.MessageView;
+
+/**
+ * <p>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

Review comment:
       two load balance modes.

##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/SimpleConsumer.java
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.List;
+import java.util.concurrent.CompletableFuture;
+
+import org.apache.rocketmq.apis.exception.*;
+import org.apache.rocketmq.apis.message.MessageView;
+
+/**
+ * SimpleConsumer is a thread-safe rocketmq client which is used to consume message by group.
+ *
+ * <p>Simple consumer is lightweight consumer , if you want fully control the message consumption operation by yourself,
+ * simple consumer should be your first consideration.

Review comment:
       Consider adding the difference between SimpleConsumer and PullConsumer.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
chenzlalvin commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r831957558



##########
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:
       By default, the subscription mode is enabled. After this assgin mode is enabled, active custom allocation is allowed




-- 
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



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

Posted by GitBox <gi...@apache.org>.
chenzlalvin commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r832745565



##########
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);
+

Review comment:
       Maybe we need to rethink of the design of PullConsumer.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
chenzlalvin commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r832744133



##########
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();
+
+    /**
+     * Add subscription expression dynamically.
+     *
+     * <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 push consumer instance.
+     */
+    PushConsumer subscribe(SubscriptionExpression subscriptionExpression) throws ClientException;
+
+    /**
+     * Remove subscription expression dynamically by topic.
+     *
+     * <p>It stops the backend task to fetch message from remote, and besides that, the local cached message whose topic
+     * was removed before would not be delivered to {@link MessageListener} anymore.
+     *
+     * <p>Nothing occurs if the specified topic does not exist in subscription expressions of push consumer.
+     *
+     * @param topic the topic to remove subscription.
+     * @return push consumer instance.
+     */
+    PushConsumer unsubscribe(String topic) throws ClientException;
+
+    /**
+     * Close the push consumer and release all related resources.
+     *
+     * <p>Once push consumer is closed, <strong>it could not be started once again.</strong> we maintained an FSM
+     * (finite-state machine) to record the different states for each producer, which is similar to
+     * {@link Service.State}.
+     */

Review comment:
       Can you explain the exact scene that need get MessageListener.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
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



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

Posted by GitBox <gi...@apache.org>.
chenzlalvin commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r832745477



##########
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:
       Maybe we need to rethink of the design of PullConsumer.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
zhouxinyu commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r840995054



##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/PullConsumer.java
##########
@@ -0,0 +1,186 @@
+/*
+ * 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 java.util.concurrent.CompletableFuture;
+
+import org.apache.rocketmq.apis.MessageQueue;
+import org.apache.rocketmq.apis.exception.*;
+import org.apache.rocketmq.apis.message.MessageView;
+
+/**
+ * <p>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.
+ * <p> If there is a consumption error, consumer just call seek api to reset the offset for reconsume message again.
+ */
+public interface PullConsumer extends Closeable {
+    /**
+     * 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.
+     * <p> Pull consumer must enable manual assignment mode in {@link PullConsumerBuilder} first, then call assign api.
+     * <p> 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;
+
+    /**
+     * 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> assignments();
+
+    /**
+     * Pull messages for the topics specified by subscribe or assign api synchronously.
+     *
+     * <p> 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 pulling for data.
+     * @param maxMessageNum max message num when server returns.
+     * @return map of messageQueue to messageViews.
+     */
+    Map<MessageQueue, Collection<MessageView>> pull(int maxMessageNum) throws ClientException;
+

Review comment:
       But it's not convenient to catch the real exception(ClientException) when using pullAsync.get.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
ni-ze commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r836386100



##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/PullConsumer.java
##########
@@ -0,0 +1,186 @@
+/*
+ * 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 java.util.concurrent.CompletableFuture;
+
+import org.apache.rocketmq.apis.MessageQueue;
+import org.apache.rocketmq.apis.exception.*;
+import org.apache.rocketmq.apis.message.MessageView;
+
+/**
+ * <p>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.
+ * <p> If there is a consumption error, consumer just call seek api to reset the offset for reconsume message again.
+ */
+public interface PullConsumer extends Closeable {
+    /**
+     * 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.
+     * <p> Pull consumer must enable manual assignment mode in {@link PullConsumerBuilder} first, then call assign api.
+     * <p> 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;
+
+    /**
+     * 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;

Review comment:
       subscribe(HashSet<SubscriptionExpression> subs) is need.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
ni-ze commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r836357713



##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/PullConsumer.java
##########
@@ -0,0 +1,186 @@
+/*
+ * 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 java.util.concurrent.CompletableFuture;
+
+import org.apache.rocketmq.apis.MessageQueue;
+import org.apache.rocketmq.apis.exception.*;
+import org.apache.rocketmq.apis.message.MessageView;
+
+/**
+ * <p>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.
+ * <p> If there is a consumption error, consumer just call seek api to reset the offset for reconsume message again.
+ */
+public interface PullConsumer extends Closeable {
+    /**
+     * 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.
+     * <p> Pull consumer must enable manual assignment mode in {@link PullConsumerBuilder} first, then call assign api.
+     * <p> 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;
+
+    /**
+     * 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> assignments();
+
+    /**
+     * Pull messages for the topics specified by subscribe or assign api synchronously.
+     *
+     * <p> 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 pulling for data.
+     * @param maxMessageNum max message num when server returns.
+     * @return map of messageQueue to messageViews.
+     */
+    Map<MessageQueue, Collection<MessageView>> pull(int maxMessageNum) throws ClientException;
+
+    /**
+     * Pull messages for the topics specified by subscribe or assign api asynchronously.
+     *
+     * <p> 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 pulling for data.
+     * @param maxMessageNum max message num when server returns.
+     * @return future of messageViews.
+     */
+    CompletableFuture<Map<MessageQueue, Collection<MessageView>>> pullAsync(int maxMessageNum) throws ClientException;
+
+    /**
+     * Commit offsets synchronously which returned by the last pull api for all the subscribed topic and messageQueues.
+     *
+     * <p> This offsets meta maintained by kafka, which used for rebalance processing or next startup.
+     * If you store the offsets in your own storage, you may need call seek api before pull and no need to call commit.
+     */
+    void commit() throws ClientException;
+
+    /**
+     * Commit offsets asynchronously which returned by the last pull api for all the subscribed topic and messageQueues.
+     *
+     * <p> This offsets meta maintained by kafka, which used for rebalance processing or next startup.
+     * If you store the offsets in your own storage, you may need call seek api before pull and no need to call commit.
+     */
+    CompletableFuture<Void> commitAsync() throws ClientException;
+
+    /**
+     * Commit specified offset for the specified messageQueue synchronously.
+     * @param messageQueue the specified messageQueue to commit offset
+     * @param committedOffset the specified offset commit to server
+     */
+    void commit(MessageQueue messageQueue, long committedOffset) throws ClientException;

Review comment:
       need commit  a collection of messageQueue  interface;
   and commitedOffset  can not find in the return of pull method.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
chenzlalvin commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r840151907



##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/SimpleConsumer.java
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.List;
+import java.util.concurrent.CompletableFuture;
+
+import org.apache.rocketmq.apis.exception.*;
+import org.apache.rocketmq.apis.message.MessageView;
+
+/**
+ * SimpleConsumer is a thread-safe rocketmq client which is used to consume message by group.
+ *
+ * <p>Simple consumer is lightweight consumer , if you want fully control the message consumption operation by yourself,
+ * simple 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> Simple consumer divide message consumption to 3 parts.
+ * Firstly, call receive api get messages from server; Then process message by yourself; At last, your must call Ack api to commit this message.
+ * If there is error when process message ,your can reconsume the message later which control by the invisibleDuration parameter.
+ * Also, you can change the invisibleDuration by call changeInvisibleDuration api.
+ */
+public interface SimpleConsumer extends Closeable {
+    /**
+     * Get the load balancing group for simple consumer.
+     *
+     * @return consumer load balancing group.
+     */
+    String getConsumerGroup();
+
+    /**
+     * Add subscription expression dynamically.
+     *
+     * <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 simple consumer instance.
+     */
+    SimpleConsumer subscribe(SubscriptionExpression subscriptionExpression) throws ClientException;

Review comment:
       Abstracting subscription behavior alone is not of great value and is easy to complicate the class interface.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
dongeforever commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r831924772



##########
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.
+     * @param messageQueue the target messageQueue to pull message.
+     * @param maxMessageNum max message num when server returns.
+     * @return collection of messageViews of this queue.
+     */
+    Collection<MessageView> pull(MessageQueue messageQueue, int maxMessageNum) throws ClientException;
+
+    /**
+     * Commit the offsets for the specified messageQueue.
+     * @param messageQueue the specified messageQueue to commit offset

Review comment:
       The streaming scenario needs a simple poll(ms) instead of the pull(messagequeue). 
   
   The user does not need to know the messagequeue at each pull. 
   And, the user may need to handle the queue changement, which is not necessary in most cases.
   More importantly, the underlying pull request will be polished as a batch mode to improve performance, which means one request may contain several messagequeues.
   
   This consumer is better to be named as PollConsumer.  Poll means to reduce one by one. 

##########
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();
+
+    /**
+     * Add subscription expression dynamically.
+     *
+     * <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 push consumer instance.
+     */
+    PushConsumer subscribe(SubscriptionExpression subscriptionExpression) throws ClientException;
+
+    /**
+     * Remove subscription expression dynamically by topic.
+     *
+     * <p>It stops the backend task to fetch message from remote, and besides that, the local cached message whose topic
+     * was removed before would not be delivered to {@link MessageListener} anymore.
+     *
+     * <p>Nothing occurs if the specified topic does not exist in subscription expressions of push consumer.
+     *
+     * @param topic the topic to remove subscription.
+     * @return push consumer instance.
+     */
+    PushConsumer unsubscribe(String topic) throws ClientException;
+
+    /**
+     * Close the push consumer and release all related resources.
+     *
+     * <p>Once push consumer is closed, <strong>it could not be started once again.</strong> we maintained an FSM
+     * (finite-state machine) to record the different states for each producer, which is similar to
+     * {@link Service.State}.
+     */

Review comment:
       Maybe we need to get the MessageListenner.

##########
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.
+     * @param messageQueue the target messageQueue to pull message.
+     * @param maxMessageNum max message num when server returns.
+     * @return collection of messageViews of this queue.
+     */
+    Collection<MessageView> pull(MessageQueue messageQueue, int maxMessageNum) throws ClientException;
+
+    /**
+     * Commit the offsets for the specified messageQueue.
+     * @param messageQueue the specified messageQueue to commit offset
+     * @param committedOffset the specified offset commit to server
+     */
+    void commit(MessageQueue messageQueue, long committedOffset) throws ClientException;

Review comment:
       The commit needs an async method.
   
   And more importantly, it needs a simple commit().
   
   In most cases, the user only wants to control the commit time but doesn't want to manage the offset store.
   

##########
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();
+
+    /**
+     * Add subscription expression dynamically.
+     *
+     * <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 push consumer instance.
+     */
+    PushConsumer subscribe(SubscriptionExpression subscriptionExpression) throws ClientException;
+
+    /**
+     * Remove subscription expression dynamically by topic.
+     *
+     * <p>It stops the backend task to fetch message from remote, and besides that, the local cached message whose topic
+     * was removed before would not be delivered to {@link MessageListener} anymore.
+     *
+     * <p>Nothing occurs if the specified topic does not exist in subscription expressions of push consumer.
+     *
+     * @param topic the topic to remove subscription.
+     * @return push consumer instance.
+     */
+    PushConsumer unsubscribe(String topic) throws ClientException;
+

Review comment:
       Why do subscribe and unsubscribe have different arguments?
   
   It is a little strange intuitively.
   

##########
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);
+

Review comment:
       Why need to specify the topic argument?

##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/SimpleConsumer.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.List;
+import java.util.concurrent.CompletableFuture;
+
+import org.apache.rocketmq.apis.exception.*;
+import org.apache.rocketmq.apis.message.MessageView;
+
+/**
+ * SimpleConsumer is a thread-safe rocketmq client which is used to consume message by group.
+ *
+ * <p>Simple consumer is lightweight consumer , if you want fully control the message consumption operation by yourself,
+ * simple 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> Simple consumer divide message consumption to 3 parts.
+ * Firstly, call receive api get messages from server; Then process message by yourself; At last, your must call Ack api to commit this message.
+ * If there is error when process message ,your can reconsume the message later which control by the invisibleDuration parameter.
+ * Also, you can change the invisibleDuration by call changeInvisibleDuration api.
+ */
+public interface SimpleConsumer extends Closeable {
+    /**
+     * Get the load balancing group for simple consumer.
+     *
+     * @return consumer load balancing group.
+     */
+    String getConsumerGroup();
+
+    /**
+     * Add subscription expression dynamically.
+     *
+     * <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 simple consumer instance.
+     */
+    SimpleConsumer subscribe(SubscriptionExpression subscriptionExpression) throws ClientException;
+
+    /**
+     * Remove subscription expression dynamically by topic.
+     *
+     * <p>It stops the backend task to fetch message from remote, and besides that, the local cached message whose topic
+     * was removed before would not be delivered to {@link MessageListener} anymore.
+     *
+     * <p>Nothing occurs if the specified topic does not exist in subscription expressions of push consumer.
+     *
+     * @param topic the topic to remove subscription.
+     * @return simple consumer instance.
+     */
+    SimpleConsumer unsubscribe(String topic) throws ClientException;
+
+    /**
+     * 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.
+     * @param topic special topic want to get messages.
+     * @param maxMessageNum max message num when server returns.
+     * @param invisibleDuration set the invisibleDuration of messages return from server. These messages will be invisible to other consumer unless timout.
+     * @return list of messageView
+     */
+    List<MessageView> receive(String topic, int maxMessageNum, Duration invisibleDuration) throws ClientException;
+
+    /**

Review comment:
       What's the relationship between the topic in receive and topic in subcribe/unsubscribe.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
chenzlalvin commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r831958153



##########
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:
       got. spelling mistake.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
chenzlalvin commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r832743437



##########
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();
+
+    /**
+     * Add subscription expression dynamically.
+     *
+     * <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 push consumer instance.
+     */
+    PushConsumer subscribe(SubscriptionExpression subscriptionExpression) throws ClientException;
+
+    /**
+     * Remove subscription expression dynamically by topic.
+     *
+     * <p>It stops the backend task to fetch message from remote, and besides that, the local cached message whose topic
+     * was removed before would not be delivered to {@link MessageListener} anymore.
+     *
+     * <p>Nothing occurs if the specified topic does not exist in subscription expressions of push consumer.
+     *
+     * @param topic the topic to remove subscription.
+     * @return push consumer instance.
+     */
+    PushConsumer unsubscribe(String topic) throws ClientException;
+

Review comment:
       The subscribe action contains complex parameters such as filter type and expression, but unsubscribe action applies only to topic granularity. We can not unsub with some expression.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
chenzlalvin commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r832745650



##########
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.
+     * @param messageQueue the target messageQueue to pull message.
+     * @param maxMessageNum max message num when server returns.
+     * @return collection of messageViews of this queue.
+     */
+    Collection<MessageView> pull(MessageQueue messageQueue, int maxMessageNum) throws ClientException;
+
+    /**
+     * Commit the offsets for the specified messageQueue.
+     * @param messageQueue the specified messageQueue to commit offset
+     * @param committedOffset the specified offset commit to server
+     */
+    void commit(MessageQueue messageQueue, long committedOffset) throws ClientException;

Review comment:
       Maybe we need to rethink of the design of PullConsumer.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
chenzlalvin commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r832745929



##########
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:
       Maybe we need to rethink of the design of PullConsumer.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
chenzlalvin commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r834864021



##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/SimpleConsumer.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.List;
+import java.util.concurrent.CompletableFuture;
+
+import org.apache.rocketmq.apis.exception.*;
+import org.apache.rocketmq.apis.message.MessageView;
+
+/**
+ * SimpleConsumer is a thread-safe rocketmq client which is used to consume message by group.
+ *
+ * <p>Simple consumer is lightweight consumer , if you want fully control the message consumption operation by yourself,
+ * simple 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> Simple consumer divide message consumption to 3 parts.
+ * Firstly, call receive api get messages from server; Then process message by yourself; At last, your must call Ack api to commit this message.
+ * If there is error when process message ,your can reconsume the message later which control by the invisibleDuration parameter.
+ * Also, you can change the invisibleDuration by call changeInvisibleDuration api.
+ */
+public interface SimpleConsumer extends Closeable {
+    /**
+     * Get the load balancing group for simple consumer.
+     *
+     * @return consumer load balancing group.
+     */
+    String getConsumerGroup();
+
+    /**
+     * Add subscription expression dynamically.
+     *
+     * <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 simple consumer instance.
+     */
+    SimpleConsumer subscribe(SubscriptionExpression subscriptionExpression) throws ClientException;
+
+    /**
+     * Remove subscription expression dynamically by topic.
+     *
+     * <p>It stops the backend task to fetch message from remote, and besides that, the local cached message whose topic
+     * was removed before would not be delivered to {@link MessageListener} anymore.
+     *
+     * <p>Nothing occurs if the specified topic does not exist in subscription expressions of push consumer.
+     *
+     * @param topic the topic to remove subscription.
+     * @return simple consumer instance.
+     */
+    SimpleConsumer unsubscribe(String topic) throws ClientException;
+
+    /**
+     * 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.
+     * @param topic special topic want to get messages.
+     * @param maxMessageNum max message num when server returns.
+     * @param invisibleDuration set the invisibleDuration of messages return from server. These messages will be invisible to other consumer unless timout.
+     * @return list of messageView
+     */
+    List<MessageView> receive(String topic, int maxMessageNum, Duration invisibleDuration) throws ClientException;
+
+    /**

Review comment:
       we remove the topic param, simpleConsumer must subscribe before receive.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
ni-ze commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r836347205



##########
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();
+
+    /**
+     * Add subscription expression dynamically.
+     *
+     * <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 push consumer instance.
+     */
+    PushConsumer subscribe(SubscriptionExpression subscriptionExpression) throws ClientException;
+
+    /**
+     * Remove subscription expression dynamically by topic.
+     *
+     * <p>It stops the backend task to fetch message from remote, and besides that, the local cached message whose topic
+     * was removed before would not be delivered to {@link MessageListener} anymore.
+     *
+     * <p>Nothing occurs if the specified topic does not exist in subscription expressions of push consumer.
+     *
+     * @param topic the topic to remove subscription.
+     * @return push consumer instance.
+     */
+    PushConsumer unsubscribe(String topic) throws ClientException;
+
+    /**
+     * Close the push consumer and release all related resources.
+     *
+     * <p>Once push consumer is closed, <strong>it could not be started once again.</strong> we maintained an FSM
+     * (finite-state machine) to record the different states for each producer, which is similar to
+     * {@link Service.State}.
+     */

Review comment:
       When we use subscribe mode, client will rebalance and poll messages from messageQueue into local buffer. MessageListener will be invoked after this relationship change, and start task to pull message.
   On the other hand,  app wants to be notify once the queue it consumes is determined, init it use messageQueue, messageListener will be used in this scene.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
ni-ze commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r836354555



##########
File path: apis/src/main/java/org/apache/rocketmq/apis/consumer/PullConsumer.java
##########
@@ -0,0 +1,186 @@
+/*
+ * 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 java.util.concurrent.CompletableFuture;
+
+import org.apache.rocketmq.apis.MessageQueue;
+import org.apache.rocketmq.apis.exception.*;
+import org.apache.rocketmq.apis.message.MessageView;
+
+/**
+ * <p>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.
+ * <p> If there is a consumption error, consumer just call seek api to reset the offset for reconsume message again.
+ */
+public interface PullConsumer extends Closeable {
+    /**
+     * 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.
+     * <p> Pull consumer must enable manual assignment mode in {@link PullConsumerBuilder} first, then call assign api.
+     * <p> 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;
+
+    /**
+     * 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> assignments();
+
+    /**
+     * Pull messages for the topics specified by subscribe or assign api synchronously.
+     *
+     * <p> 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 pulling for data.
+     * @param maxMessageNum max message num when server returns.
+     * @return map of messageQueue to messageViews.
+     */
+    Map<MessageQueue, Collection<MessageView>> pull(int maxMessageNum) throws ClientException;
+

Review comment:
       sync pull can use pullAsync().get




-- 
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



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

Posted by GitBox <gi...@apache.org>.
ni-ze commented on a change in pull request #4019:
URL: https://github.com/apache/rocketmq/pull/4019#discussion_r836347205



##########
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();
+
+    /**
+     * Add subscription expression dynamically.
+     *
+     * <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 push consumer instance.
+     */
+    PushConsumer subscribe(SubscriptionExpression subscriptionExpression) throws ClientException;
+
+    /**
+     * Remove subscription expression dynamically by topic.
+     *
+     * <p>It stops the backend task to fetch message from remote, and besides that, the local cached message whose topic
+     * was removed before would not be delivered to {@link MessageListener} anymore.
+     *
+     * <p>Nothing occurs if the specified topic does not exist in subscription expressions of push consumer.
+     *
+     * @param topic the topic to remove subscription.
+     * @return push consumer instance.
+     */
+    PushConsumer unsubscribe(String topic) throws ClientException;
+
+    /**
+     * Close the push consumer and release all related resources.
+     *
+     * <p>Once push consumer is closed, <strong>it could not be started once again.</strong> we maintained an FSM
+     * (finite-state machine) to record the different states for each producer, which is similar to
+     * {@link Service.State}.
+     */

Review comment:
       When we use subscribe mode, client will rebalance and poll messages from messageQueue into local buffer. MessageListener will be invoked after this relationship change, and start task to pull message.
   On the other hand,  app wants to be notify once the queue it consumes is determined, init app with messageQueue, messageListener will be used in this scene.




-- 
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