diff --git a/common/src/test/java/org/apache/rocketmq/common/statistics/FutureHolderTest.java b/common/src/test/java/org/apache/rocketmq/common/statistics/FutureHolderTest.java new file mode 100644 index 00000000000..f2fe7837e0f --- /dev/null +++ b/common/src/test/java/org/apache/rocketmq/common/statistics/FutureHolderTest.java @@ -0,0 +1,101 @@ +/* + * 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.common.statistics; + +import java.util.concurrent.Future; +import org.junit.Test; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +public class FutureHolderTest { + + @Test + public void testRemoveAllFutureCancelsAllFuturesWithoutInterrupting() { + FutureHolder holder = new FutureHolder<>(); + Future first = mock(Future.class); + Future second = mock(Future.class); + + holder.addFuture("item", first); + holder.addFuture("item", second); + verifyNoInteractions(first, second); + + holder.removeAllFuture("item"); + + verify(first).cancel(false); + verify(second).cancel(false); + verifyNoMoreInteractions(first, second); + } + + @Test + public void testRemoveAllFutureDoesNotAffectOtherKeys() { + FutureHolder holder = new FutureHolder<>(); + Future first = mock(Future.class); + Future second = mock(Future.class); + + holder.addFuture("first", first); + holder.addFuture("second", second); + + holder.removeAllFuture("first"); + + verify(first).cancel(false); + verifyNoInteractions(second); + + holder.removeAllFuture("second"); + + verify(second).cancel(false); + verifyNoMoreInteractions(first, second); + } + + @Test + public void testRemoveAllFutureIgnoresMissingAndRemovedKeys() { + FutureHolder holder = new FutureHolder<>(); + Future future = mock(Future.class); + + holder.removeAllFuture("missing"); + + holder.addFuture("item", future); + holder.removeAllFuture("missing"); + verifyNoInteractions(future); + + holder.removeAllFuture("item"); + holder.removeAllFuture("item"); + + verify(future).cancel(false); + verifyNoMoreInteractions(future); + } + + @Test + public void testAddFutureAfterRemovalUsesNewLifecycle() { + FutureHolder holder = new FutureHolder<>(); + Future previous = mock(Future.class); + Future current = mock(Future.class); + + holder.addFuture("item", previous); + holder.removeAllFuture("item"); + + holder.addFuture("item", current); + verifyNoInteractions(current); + holder.removeAllFuture("item"); + + verify(previous).cancel(false); + verify(current).cancel(false); + verifyNoMoreInteractions(previous, current); + } +} diff --git a/tieredstore/src/test/java/org/apache/rocketmq/tieredstore/index/IndexStoreServiceTest.java b/tieredstore/src/test/java/org/apache/rocketmq/tieredstore/index/IndexStoreServiceTest.java index 0d849d5927f..e84501b098f 100644 --- a/tieredstore/src/test/java/org/apache/rocketmq/tieredstore/index/IndexStoreServiceTest.java +++ b/tieredstore/src/test/java/org/apache/rocketmq/tieredstore/index/IndexStoreServiceTest.java @@ -134,11 +134,15 @@ public void doConvertOldFormatTest() throws IOException { @Test public void concurrentPutTest() throws InterruptedException { + putKeysConcurrently(new IndexStoreService(fileAllocator, filePath)); + } + + private void putKeysConcurrently(IndexStoreService service) throws InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool( 4, new ThreadFactoryImpl("ConcurrentPutTest")); storeConfig.setTieredStoreIndexFileMaxHashSlotNum(500); storeConfig.setTieredStoreIndexFileMaxIndexNum(2000); - indexService = new IndexStoreService(fileAllocator, filePath); + indexService = service; indexService.start(); long timestamp = System.currentTimeMillis(); @@ -170,33 +174,50 @@ public void concurrentPutTest() throws InterruptedException { @Test public void doCompactionTest() throws InterruptedException { - concurrentPutTest(); - IndexFile indexFile = indexService.getNextSealedFile(); - Assert.assertEquals(IndexFile.IndexStatusEnum.SEALED, indexFile.getFileStatus()); - - indexService.doCompactThenUploadFile(indexFile); - indexService.setCompactTimestamp(indexFile.getTimestamp()); - indexFile.destroy(); - - List files = new ArrayList<>(indexService.getTimeStoreTable().values()); - Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, files.get(0).getFileStatus()); - Assert.assertEquals(IndexFile.IndexStatusEnum.SEALED, files.get(1).getFileStatus()); - Assert.assertEquals(IndexFile.IndexStatusEnum.UNSEALED, files.get(2).getFileStatus()); - - indexFile = indexService.getNextSealedFile(); - indexService.doCompactThenUploadFile(indexFile); - indexService.setCompactTimestamp(indexFile.getTimestamp()); - files = new ArrayList<>(indexService.getTimeStoreTable().values()); - Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, files.get(0).getFileStatus()); - Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, files.get(1).getFileStatus()); - Assert.assertEquals(IndexFile.IndexStatusEnum.UNSEALED, files.get(2).getFileStatus()); - - indexFile = indexService.getNextSealedFile(); - Assert.assertNull(indexFile); - files = new ArrayList<>(indexService.getTimeStoreTable().values()); - Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, files.get(0).getFileStatus()); - Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, files.get(1).getFileStatus()); - Assert.assertEquals(IndexFile.IndexStatusEnum.UNSEALED, files.get(2).getFileStatus()); + Object backgroundRunLock = new Object(); + IndexStoreService controlledService = new IndexStoreService(fileAllocator, filePath) { + @Override + public void run() { + synchronized (backgroundRunLock) { + super.run(); + } + } + }; + // Keep background compaction out of the manual compaction assertions. + synchronized (backgroundRunLock) { + try { + putKeysConcurrently(controlledService); + IndexFile indexFile = indexService.getNextSealedFile(); + Assert.assertEquals(IndexFile.IndexStatusEnum.SEALED, indexFile.getFileStatus()); + + Assert.assertTrue(indexService.doCompactThenUploadFile(indexFile)); + indexService.setCompactTimestamp(indexFile.getTimestamp()); + indexFile.destroy(); + + List files = new ArrayList<>(indexService.getTimeStoreTable().values()); + Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, files.get(0).getFileStatus()); + Assert.assertEquals(IndexFile.IndexStatusEnum.SEALED, files.get(1).getFileStatus()); + Assert.assertEquals(IndexFile.IndexStatusEnum.UNSEALED, files.get(2).getFileStatus()); + + indexFile = indexService.getNextSealedFile(); + Assert.assertTrue(indexService.doCompactThenUploadFile(indexFile)); + indexService.setCompactTimestamp(indexFile.getTimestamp()); + files = new ArrayList<>(indexService.getTimeStoreTable().values()); + Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, files.get(0).getFileStatus()); + Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, files.get(1).getFileStatus()); + Assert.assertEquals(IndexFile.IndexStatusEnum.UNSEALED, files.get(2).getFileStatus()); + + indexFile = indexService.getNextSealedFile(); + Assert.assertNull(indexFile); + files = new ArrayList<>(indexService.getTimeStoreTable().values()); + Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, files.get(0).getFileStatus()); + Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, files.get(1).getFileStatus()); + Assert.assertEquals(IndexFile.IndexStatusEnum.UNSEALED, files.get(2).getFileStatus()); + } finally { + // Allow run() to perform its normal shutdown cleanup after releasing the lock. + controlledService.makeStop(); + } + } } @Test