vongosling closed pull request #127: [ROCKETMQ-233] Fix pull interval issue
URL: https://github.com/apache/rocketmq/pull/127
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/client/src/main/java/org/apache/rocketmq/client/consumer/DefaultMQPushConsumer.java
b/client/src/main/java/org/apache/rocketmq/client/consumer/DefaultMQPushConsumer.java
index 9c9b59ef0..f70cf8730 100644
---
a/client/src/main/java/org/apache/rocketmq/client/consumer/DefaultMQPushConsumer.java
+++
b/client/src/main/java/org/apache/rocketmq/client/consumer/DefaultMQPushConsumer.java
@@ -24,6 +24,7 @@
import org.apache.rocketmq.client.consumer.listener.MessageListener;
import
org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
+import
org.apache.rocketmq.client.consumer.policy.BoundedExponentialPullIntervalPolicy;
import
org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely;
import org.apache.rocketmq.client.consumer.store.OffsetStore;
import org.apache.rocketmq.client.exception.MQBrokerException;
@@ -175,6 +176,8 @@
*/
private long pullInterval = 0;
+ private PullIntervalPolicy pullIntervalPolicy;
+
/**
* Batch consumption size
*/
@@ -231,6 +234,7 @@ public DefaultMQPushConsumer(final String consumerGroup,
RPCHook rpcHook, Alloca
this.consumerGroup = consumerGroup;
this.allocateMessageQueueStrategy = allocateMessageQueueStrategy;
defaultMQPushConsumerImpl = new DefaultMQPushConsumerImpl(this,
rpcHook);
+ this.pullIntervalPolicy = new
BoundedExponentialPullIntervalPolicy(100, 300000);
}
/**
@@ -393,6 +397,14 @@ public void setPullInterval(long pullInterval) {
this.pullInterval = pullInterval;
}
+ public PullIntervalPolicy getPullIntervalPolicy() {
+ return pullIntervalPolicy;
+ }
+
+ public void setPullIntervalPolicy(PullIntervalPolicy pullIntervalPolicy) {
+ this.pullIntervalPolicy = pullIntervalPolicy;
+ }
+
public int getPullThresholdForQueue() {
return pullThresholdForQueue;
}
diff --git
a/client/src/main/java/org/apache/rocketmq/client/consumer/PullIntervalPolicy.java
b/client/src/main/java/org/apache/rocketmq/client/consumer/PullIntervalPolicy.java
new file mode 100644
index 000000000..177960cc8
--- /dev/null
+++
b/client/src/main/java/org/apache/rocketmq/client/consumer/PullIntervalPolicy.java
@@ -0,0 +1,25 @@
+/*
+ * 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.client.consumer;
+
+public interface PullIntervalPolicy {
+
+ void update(PullStatus status);
+
+ int getPullInterval();
+}
diff --git
a/client/src/main/java/org/apache/rocketmq/client/consumer/policy/BoundedExponentialPullIntervalPolicy.java
b/client/src/main/java/org/apache/rocketmq/client/consumer/policy/BoundedExponentialPullIntervalPolicy.java
new file mode 100644
index 000000000..be1030480
--- /dev/null
+++
b/client/src/main/java/org/apache/rocketmq/client/consumer/policy/BoundedExponentialPullIntervalPolicy.java
@@ -0,0 +1,71 @@
+/*
+ * 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.client.consumer.policy;
+
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.rocketmq.client.consumer.PullIntervalPolicy;
+import org.apache.rocketmq.client.consumer.PullStatus;
+
+public class BoundedExponentialPullIntervalPolicy implements
PullIntervalPolicy {
+
+ private final int base;
+ private final int max;
+ private AtomicInteger count;
+
+ private volatile long pullInterval;
+
+ public BoundedExponentialPullIntervalPolicy(int base, int max) {
+ count = new AtomicInteger();
+ if (base > 0) {
+ this.base = base;
+ } else {
+ this.base = 1000;
+ }
+
+ this.max = max;
+ }
+
+ @Override
+ public void update(PullStatus status) {
+ switch (status) {
+ case FOUND:
+ case OFFSET_ILLEGAL:
+ count.set(0);
+ pullInterval = 0;
+ break;
+
+ case NO_MATCHED_MSG:
+ case NO_NEW_MSG:
+ pullInterval = base * (1 << count.get());
+ if (pullInterval > max) {
+ pullInterval = max;
+ }
+ break;
+
+ default:
+ break;
+
+ }
+
+ }
+
+ @Override
+ public int getPullInterval() {
+ return (int)pullInterval;
+ }
+}
diff --git
a/client/src/main/java/org/apache/rocketmq/client/consumer/policy/FixedPullIntervalPolicy.java
b/client/src/main/java/org/apache/rocketmq/client/consumer/policy/FixedPullIntervalPolicy.java
new file mode 100644
index 000000000..a676fa457
--- /dev/null
+++
b/client/src/main/java/org/apache/rocketmq/client/consumer/policy/FixedPullIntervalPolicy.java
@@ -0,0 +1,55 @@
+/*
+ * 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.client.consumer.policy;
+
+import org.apache.rocketmq.client.consumer.PullIntervalPolicy;
+import org.apache.rocketmq.client.consumer.PullStatus;
+
+public class FixedPullIntervalPolicy implements PullIntervalPolicy {
+
+ private final int base;
+
+ private volatile int pullInterval;
+
+ public FixedPullIntervalPolicy(int base) {
+ this.base = base;
+ }
+
+ @Override
+ public void update(PullStatus status) {
+ switch (status) {
+ case OFFSET_ILLEGAL:
+ case FOUND:
+ pullInterval = 0;
+ break;
+
+ case NO_NEW_MSG:
+ case NO_MATCHED_MSG:
+ pullInterval = base;
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ @Override
+ public int getPullInterval() {
+ return pullInterval;
+ }
+}
diff --git
a/client/src/main/java/org/apache/rocketmq/client/consumer/policy/LinearPullIntervalPolicy.java
b/client/src/main/java/org/apache/rocketmq/client/consumer/policy/LinearPullIntervalPolicy.java
new file mode 100644
index 000000000..8c0cd910d
--- /dev/null
+++
b/client/src/main/java/org/apache/rocketmq/client/consumer/policy/LinearPullIntervalPolicy.java
@@ -0,0 +1,65 @@
+/*
+ * 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.client.consumer.policy;
+
+import org.apache.rocketmq.client.consumer.PullIntervalPolicy;
+import org.apache.rocketmq.client.consumer.PullStatus;
+
+public class LinearPullIntervalPolicy implements PullIntervalPolicy {
+
+ private final int low;
+ private final int high;
+ private final int delta;
+
+ private volatile int pullInterval;
+
+ public LinearPullIntervalPolicy(int low, int high, int delta) {
+ this.low = low;
+ this.high = high;
+ this.delta = delta;
+ pullInterval = low;
+ }
+
+ @Override
+ public void update(PullStatus status) {
+ switch (status) {
+ case FOUND:
+ case OFFSET_ILLEGAL:
+ pullInterval = 0;
+ break;
+ case NO_NEW_MSG:
+ case NO_MATCHED_MSG:
+ if (0 == pullInterval) {
+ pullInterval = low;
+ } else if (pullInterval + delta > high) {
+ pullInterval = high;
+ } else {
+ pullInterval += delta;
+ }
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ @Override
+ public int getPullInterval() {
+ return pullInterval;
+ }
+}
diff --git
a/client/src/main/java/org/apache/rocketmq/client/impl/consumer/DefaultMQPushConsumerImpl.java
b/client/src/main/java/org/apache/rocketmq/client/impl/consumer/DefaultMQPushConsumerImpl.java
index 9bf34be8c..326cdbf4e 100644
---
a/client/src/main/java/org/apache/rocketmq/client/impl/consumer/DefaultMQPushConsumerImpl.java
+++
b/client/src/main/java/org/apache/rocketmq/client/impl/consumer/DefaultMQPushConsumerImpl.java
@@ -273,12 +273,25 @@ public void pullMessage(final PullRequest pullRequest) {
final long beginTimestamp = System.currentTimeMillis();
PullCallback pullCallback = new PullCallback() {
+
+ private void scheduleNextPull(PullResult pullResult) {
+ pullRequest.setNextOffset(pullResult.getNextBeginOffset());
+ correctTagsOffset(pullRequest);
+ if
(defaultMQPushConsumer.getPullIntervalPolicy().getPullInterval() > 0) {
+ executePullRequestLater(pullRequest,
defaultMQPushConsumer.getPullIntervalPolicy().getPullInterval());
+ } else {
+
DefaultMQPushConsumerImpl.this.executePullRequestImmediately(pullRequest);
+ }
+ }
+
@Override
public void onSuccess(PullResult pullResult) {
if (pullResult != null) {
pullResult =
DefaultMQPushConsumerImpl.this.pullAPIWrapper.processPullResult(pullRequest.getMessageQueue(),
pullResult,
subscriptionData);
+
defaultMQPushConsumer.getPullIntervalPolicy().update(pullResult.getPullStatus());
+
switch (pullResult.getPullStatus()) {
case FOUND:
long prevRequestOffset =
pullRequest.getNextOffset();
@@ -296,19 +309,13 @@ public void onSuccess(PullResult pullResult) {
DefaultMQPushConsumerImpl.this.getConsumerStatsManager().incPullTPS(pullRequest.getConsumerGroup(),
pullRequest.getMessageQueue().getTopic(),
pullResult.getMsgFoundList().size());
- boolean dispathToConsume =
processQueue.putMessage(pullResult.getMsgFoundList());
+ boolean dispatchToConsume =
processQueue.putMessage(pullResult.getMsgFoundList());
DefaultMQPushConsumerImpl.this.consumeMessageService.submitConsumeRequest(//
pullResult.getMsgFoundList(), //
processQueue, //
pullRequest.getMessageQueue(), //
- dispathToConsume);
-
- if
(DefaultMQPushConsumerImpl.this.defaultMQPushConsumer.getPullInterval() > 0) {
-
DefaultMQPushConsumerImpl.this.executePullRequestLater(pullRequest,
-
DefaultMQPushConsumerImpl.this.defaultMQPushConsumer.getPullInterval());
- } else {
-
DefaultMQPushConsumerImpl.this.executePullRequestImmediately(pullRequest);
- }
+ dispatchToConsume);
+
DefaultMQPushConsumerImpl.this.executePullRequestImmediately(pullRequest);
}
if (pullResult.getNextBeginOffset() <
prevRequestOffset//
@@ -322,18 +329,10 @@ public void onSuccess(PullResult pullResult) {
break;
case NO_NEW_MSG:
-
pullRequest.setNextOffset(pullResult.getNextBeginOffset());
-
-
DefaultMQPushConsumerImpl.this.correctTagsOffset(pullRequest);
-
-
DefaultMQPushConsumerImpl.this.executePullRequestImmediately(pullRequest);
+ scheduleNextPull(pullResult);
break;
case NO_MATCHED_MSG:
-
pullRequest.setNextOffset(pullResult.getNextBeginOffset());
-
-
DefaultMQPushConsumerImpl.this.correctTagsOffset(pullRequest);
-
-
DefaultMQPushConsumerImpl.this.executePullRequestImmediately(pullRequest);
+ scheduleNextPull(pullResult);
break;
case OFFSET_ILLEGAL:
log.warn("the pull request offset illegal, {} {}",
//
diff --git
a/test/src/test/java/org/apache/rocketmq/test/client/producer/tls/TlsIntegrationTestBase.java
b/test/src/test/java/org/apache/rocketmq/test/client/producer/tls/TlsIntegrationTestBase.java
new file mode 100644
index 000000000..bba39fe57
--- /dev/null
+++
b/test/src/test/java/org/apache/rocketmq/test/client/producer/tls/TlsIntegrationTestBase.java
@@ -0,0 +1,29 @@
+/*
+ * 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.test.client.producer.tls;
+
+import org.junit.BeforeClass;
+
+public class TlsIntegrationTestBase {
+
+ @BeforeClass
+ public static void setUp() {
+
+
+ }
+}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services