Skip to content

Commit 1556a13

Browse files
committed
[fix][test] Fix more resource leaks in tests (#24314)
(cherry picked from commit eccc6b6)
1 parent 612a1d0 commit 1556a13

File tree

10 files changed

+326
-265
lines changed

10 files changed

+326
-265
lines changed

buildtools/src/main/java/org/apache/pulsar/tests/ThreadLeakDetectorListener.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ private static boolean shouldSkipThread(Thread thread) {
227227
if (threadName.equals("process reaper")) {
228228
return true;
229229
}
230+
// skip thread created by sun.net.www.http.KeepAliveCache
231+
if (threadName.equals("Keep-Alive-Timer")) {
232+
return true;
233+
}
230234
// skip JVM internal thread related to agent attach
231235
if (threadName.equals("Attach Listener")) {
232236
return true;
@@ -255,6 +259,10 @@ private static boolean shouldSkipThread(Thread thread) {
255259
if (threadName.equals("Grizzly-HttpSession-Expirer")) {
256260
return true;
257261
}
262+
// skip Hadoop LocalFileSystem stats thread
263+
if (threadName.equals("org.apache.hadoop.fs.FileSystem$Statistics$StatisticsDataReferenceCleaner")) {
264+
return true;
265+
}
258266
// Testcontainers AbstractWaitStrategy.EXECUTOR
259267
if (threadName.startsWith("testcontainers-wait-")) {
260268
return true;

managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerFactoryImpl.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ private ManagedLedgerFactoryImpl(MetadataStoreExtended metadataStore,
250250
openTelemetryManagedCursorStats = new OpenTelemetryManagedCursorStats(openTelemetry, this);
251251
}
252252

253-
static class DefaultBkFactory implements BookkeeperFactoryForCustomEnsemblePlacementPolicy {
253+
static class DefaultBkFactory implements BookkeeperFactoryForCustomEnsemblePlacementPolicy, AutoCloseable {
254254

255255
private final BookKeeper bkClient;
256256

@@ -263,6 +263,11 @@ public DefaultBkFactory(ClientConfiguration bkClientConfiguration)
263263
public CompletableFuture<BookKeeper> get(EnsemblePlacementPolicyConfig policy) {
264264
return CompletableFuture.completedFuture(bkClient);
265265
}
266+
267+
@Override
268+
public void close() throws Exception {
269+
bkClient.close();
270+
}
266271
}
267272

268273
private synchronized void handleMetadataStoreNotification(SessionEvent e) {
@@ -646,13 +651,20 @@ public void closeFailed(ManagedLedgerException exception, Object ctx) {
646651
}
647652
}
648653
}));
649-
}).thenAcceptAsync(__ -> {
654+
}).whenCompleteAsync((__, ___) -> {
650655
//wait for tasks in scheduledExecutor executed.
651656
openTelemetryManagedCursorStats.close();
652657
openTelemetryManagedLedgerStats.close();
653658
openTelemetryCacheStats.close();
654659
scheduledExecutor.shutdownNow();
655660
entryCacheManager.clear();
661+
if (bookkeeperFactory instanceof DefaultBkFactory defaultBkFactory) {
662+
try {
663+
defaultBkFactory.close();
664+
} catch (Exception e) {
665+
log.warn("Failed to close bookkeeper client", e);
666+
}
667+
}
656668
});
657669
}
658670

managed-ledger/src/test/java/org/apache/bookkeeper/test/BookKeeperClusterTestCase.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import static org.apache.bookkeeper.util.BookKeeperConstants.AVAILABLE_NODE;
2727
import static org.apache.pulsar.common.util.PortManager.nextLockedFreePort;
2828
import static org.testng.Assert.assertFalse;
29-
3029
import com.google.common.base.Stopwatch;
3130
import java.io.File;
3231
import java.io.IOException;
@@ -73,9 +72,9 @@
7372
import org.awaitility.Awaitility;
7473
import org.slf4j.Logger;
7574
import org.slf4j.LoggerFactory;
76-
import org.testng.annotations.AfterTest;
75+
import org.testng.annotations.AfterClass;
76+
import org.testng.annotations.BeforeClass;
7777
import org.testng.annotations.BeforeMethod;
78-
import org.testng.annotations.BeforeTest;
7978

8079
/**
8180
* A class runs several bookie servers for testing.
@@ -148,7 +147,7 @@ public BookKeeperClusterTestCase(int numBookies, int numOfZKNodes, int testTimeo
148147
}
149148
}
150149

151-
@BeforeTest(alwaysRun = true)
150+
@BeforeClass(alwaysRun = true)
152151
public void setUp() throws Exception {
153152
setUp(getLedgersRootPath());
154153
}
@@ -187,7 +186,7 @@ protected String changeLedgerPath() {
187186
return "";
188187
}
189188

190-
@AfterTest(alwaysRun = true)
189+
@AfterClass(alwaysRun = true)
191190
public void tearDown() throws Exception {
192191
boolean failed = false;
193192
for (Throwable e : asyncExceptions) {

0 commit comments

Comments
 (0)