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/08/31 07:15:07 UTC

[GitHub] [rocketmq] aaron-ai commented on a diff in pull request #4940: [ISSUE #4938, ISSUE #4939] fix the concurrency problems and limit the times of renew when some error occurred in ReceiptHandleProcessor

aaron-ai commented on code in PR #4940:
URL: https://github.com/apache/rocketmq/pull/4940#discussion_r959236200


##########
proxy/src/test/java/org/apache/rocketmq/proxy/common/ReceiptHandleGroupTest.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.proxy.common;
+
+import java.time.Duration;
+import java.util.Random;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executors;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.rocketmq.common.consumer.ReceiptHandle;
+import org.apache.rocketmq.common.message.MessageClientIDSetter;
+import org.apache.rocketmq.proxy.common.utils.FutureUtils;
+import org.apache.rocketmq.proxy.config.InitConfigAndLoggerTest;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class ReceiptHandleGroupTest extends InitConfigAndLoggerTest {
+
+    private static final String TOPIC = "topic";
+    private static final String GROUP = "group";
+    private ReceiptHandleGroup receiptHandleGroup;
+    private String msgID;
+    private final Random random = new Random();
+
+    @Before
+    public void before() throws Throwable {
+        super.before();
+        receiptHandleGroup = new ReceiptHandleGroup();
+        msgID = MessageClientIDSetter.createUniqID();
+    }
+
+    protected String createHandle() {
+        return ReceiptHandle.builder()
+            .startOffset(0L)
+            .retrieveTime(System.currentTimeMillis())
+            .invisibleTime(3000)
+            .reviveQueueId(1)
+            .topicType(ReceiptHandle.NORMAL_TOPIC)
+            .brokerName("brokerName")
+            .queueId(random.nextInt(10))
+            .offset(random.nextInt(10))
+            .commitLogOffset(0L)
+            .build().encode();
+    }
+
+    @Test
+    public void testRemoveWhenComputeIfPresent() throws Exception {
+        String handle1 = createHandle();
+        String handle2 = createHandle();
+        AtomicReference<MessageReceiptHandle> removeHandleRef = new AtomicReference<>();
+
+        receiptHandleGroup.put(msgID, handle1, createMessageReceiptHandle(handle1, msgID));
+        CountDownLatch latch = new CountDownLatch(1);
+        Thread removeThread = new Thread(() -> {
+            try {
+                latch.await();
+                removeHandleRef.set(receiptHandleGroup.remove(msgID, handle1));
+            } catch (Exception ignored) {
+            }
+        }, "removeThread");
+        Thread computeThread = new Thread(() -> {
+            try {
+                receiptHandleGroup.computeIfPresent(msgID, handle1, messageReceiptHandle -> {
+                    try {
+                        latch.await();
+                    } catch (Exception ignored) {
+                    }
+                    messageReceiptHandle.updateReceiptHandle(handle2);
+                    return FutureUtils.addExecutor(CompletableFuture.completedFuture(messageReceiptHandle), Executors.newCachedThreadPool());
+                });
+            } catch (Exception ignored) {
+            }
+        }, "computeThread");
+        removeThread.start();
+        computeThread.start();
+        Thread.sleep(10);

Review Comment:
   Could we refine the sleep here?



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