From 576be17738b6ba67e4a89474f39267f5434a1084 Mon Sep 17 00:00:00 2001 From: yash Date: Wed, 29 Nov 2023 04:11:03 +0530 Subject: [PATCH 1/2] [ISSUE #7592] testCleanBuffer unit test modifies, changed non-direct to direct buffer allocation --- .../src/test/java/org/apache/rocketmq/common/UtilAllTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java b/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java index cb288578cc9..8c6a1defe1a 100644 --- a/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java @@ -215,8 +215,8 @@ public void setSubField1(boolean subField1) { @Test public void testCleanBuffer() { UtilAll.cleanBuffer(null); - UtilAll.cleanBuffer(ByteBuffer.allocate(10)); - UtilAll.cleanBuffer(ByteBuffer.allocate(0)); + UtilAll.cleanBuffer(ByteBuffer.allocateDirect(10)); + UtilAll.cleanBuffer(ByteBuffer.allocateDirect(0)); } @Test From a2e6706b0951372ff1ade11c021c152a42a10dd7 Mon Sep 17 00:00:00 2001 From: Li Zhanhui Date: Wed, 29 Nov 2023 11:26:32 +0800 Subject: [PATCH 2/2] fix: consolidate UtilAll#cleanBuffer by checking if the given buffer is direct or not Signed-off-by: Li Zhanhui --- .../main/java/org/apache/rocketmq/common/UtilAll.java | 9 +++++++++ .../java/org/apache/rocketmq/common/UtilAllTest.java | 1 + .../rocketmq/store/timer/TimerMessageStoreTest.java | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/org/apache/rocketmq/common/UtilAll.java b/common/src/main/java/org/apache/rocketmq/common/UtilAll.java index 2808f106ae2..19efa9aa902 100644 --- a/common/src/main/java/org/apache/rocketmq/common/UtilAll.java +++ b/common/src/main/java/org/apache/rocketmq/common/UtilAll.java @@ -699,10 +699,19 @@ public static void deleteEmptyDirectory(File file) { } } + /** + * Free direct-buffer's memory actively. + * @param buffer Direct buffer to free. + */ public static void cleanBuffer(final ByteBuffer buffer) { if (null == buffer) { return; } + + if (!buffer.isDirect()) { + return; + } + PlatformDependent.freeDirectBuffer(buffer); } diff --git a/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java b/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java index 8c6a1defe1a..2d22d5254ab 100644 --- a/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java @@ -217,6 +217,7 @@ public void testCleanBuffer() { UtilAll.cleanBuffer(null); UtilAll.cleanBuffer(ByteBuffer.allocateDirect(10)); UtilAll.cleanBuffer(ByteBuffer.allocateDirect(0)); + UtilAll.cleanBuffer(ByteBuffer.allocate(10)); } @Test diff --git a/store/src/test/java/org/apache/rocketmq/store/timer/TimerMessageStoreTest.java b/store/src/test/java/org/apache/rocketmq/store/timer/TimerMessageStoreTest.java index 63ec97cdb0b..02ff35681d0 100644 --- a/store/src/test/java/org/apache/rocketmq/store/timer/TimerMessageStoreTest.java +++ b/store/src/test/java/org/apache/rocketmq/store/timer/TimerMessageStoreTest.java @@ -387,7 +387,7 @@ public void testStateAndRecover() throws Exception { assertEquals(PutMessageStatus.PUT_OK, putMessageResult.getPutMessageStatus()); } - // Wait until messages have wrote to TimerLog and currReadTimeMs catches up current time. + // Wait until messages have written to TimerLog and currReadTimeMs catches up current time. await().atMost(5000, TimeUnit.MILLISECONDS).until(new Callable() { @Override public Boolean call() {