Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.apache.yetus.audience.InterfaceAudience;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Store;
import org.junit.jupiter.api.extension.InvocationInterceptor;
Expand All @@ -60,14 +62,18 @@
* the tag.
* <p>
* It also controls the timeout for the whole test class running, while the timeout annotation in
* JUnit5 can only enforce the timeout for each test method.
* JUnit5 can only enforce the timeout for each test method. When a test is timed out, a thread dump
* will be printed to log output.
* <p>
* Finally, it also forbid System.exit call in tests. TODO: need to find a new way as
* SecurityManager has been removed since Java 21.
* It also implements resource check for each test method, using the {@link ResourceChecker} class.
* <p>
* Finally, it also forbid System.exit call in tests. <br>
* TODO: need to find a new way as SecurityManager was deprecated in Java 17 and permanently
* disabled since Java 24.
*/
@InterfaceAudience.Private
public class HBaseJupiterExtension
implements InvocationInterceptor, BeforeAllCallback, AfterAllCallback {
public class HBaseJupiterExtension implements InvocationInterceptor, BeforeAllCallback,
AfterAllCallback, BeforeEachCallback, AfterEachCallback {

private static final Logger LOG = LoggerFactory.getLogger(HBaseJupiterExtension.class);

Expand All @@ -84,6 +90,8 @@ public class HBaseJupiterExtension

private static final String DEADLINE = "deadline";

private static final String RESOURCE_CHECK = "rc";

private Duration pickTimeout(ExtensionContext ctx) {
Set<String> timeoutTags = TAG_TO_TIMEOUT.keySet();
Set<String> timeoutTag = Sets.intersection(timeoutTags, ctx.getTags());
Expand Down Expand Up @@ -130,7 +138,8 @@ public void afterAll(ExtensionContext ctx) throws Exception {
System.setSecurityManager(null);
}

private <T> T runWithTimeout(Invocation<T> invocation, ExtensionContext ctx) throws Throwable {
private <T> T runWithTimeout(Invocation<T> invocation, ExtensionContext ctx, String name)
throws Throwable {
Store store = ctx.getStore(NAMESPACE);
ExecutorService executor = store.get(EXECUTOR, ExecutorService.class);
if (executor == null) {
Expand All @@ -139,12 +148,12 @@ private <T> T runWithTimeout(Invocation<T> invocation, ExtensionContext ctx) thr
Instant deadline = store.get(DEADLINE, Instant.class);
Instant now = Instant.now();
if (!now.isBefore(deadline)) {
fail("Test " + ctx.getDisplayName() + " timed out, deadline is " + deadline);
fail("Test " + name + " timed out, deadline is " + deadline);
return null;
}

Duration remaining = Duration.between(now, deadline);
LOG.info("remaining timeout for {} is {}", ctx.getDisplayName(), remaining);
LOG.info("remaining timeout for {} is {}", name, remaining);
Future<T> future = executor.submit(() -> {
try {
return invocation.proceed();
Expand All @@ -157,14 +166,13 @@ private <T> T runWithTimeout(Invocation<T> invocation, ExtensionContext ctx) thr
return future.get(remaining.toNanos(), TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
fail("Test " + ctx.getDisplayName() + " interrupted");
fail("Test " + name + " interrupted");
return null;
} catch (ExecutionException e) {
throw ExceptionUtils.throwAsUncheckedException(e.getCause());
} catch (TimeoutException e) {
printThreadDump();
throw new JUnitException(
"Test " + ctx.getDisplayName() + " timed out, deadline is " + deadline, e);
throw new JUnitException("Test " + name + " timed out, deadline is " + deadline, e);
}
}

Expand All @@ -177,41 +185,62 @@ private void printThreadDump() {
public void interceptBeforeAllMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext)
throws Throwable {
runWithTimeout(invocation, extensionContext);
runWithTimeout(invocation, extensionContext, extensionContext.getDisplayName() + ".beforeAll");
}

@Override
public void interceptBeforeEachMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext)
throws Throwable {
runWithTimeout(invocation, extensionContext);
runWithTimeout(invocation, extensionContext, extensionContext.getDisplayName() + ".beforeEach");
}

@Override
public void interceptTestMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext)
throws Throwable {
runWithTimeout(invocation, extensionContext);
runWithTimeout(invocation, extensionContext, extensionContext.getDisplayName());
}

@Override
public void interceptAfterEachMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext)
throws Throwable {
runWithTimeout(invocation, extensionContext);
runWithTimeout(invocation, extensionContext, extensionContext.getDisplayName() + ".afterEach");
}

@Override
public void interceptAfterAllMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext)
throws Throwable {
runWithTimeout(invocation, extensionContext);
runWithTimeout(invocation, extensionContext, extensionContext.getDisplayName() + ".afterAll");
}

@Override
public <T> T interceptTestClassConstructor(Invocation<T> invocation,
ReflectiveInvocationContext<Constructor<T>> invocationContext,
ExtensionContext extensionContext) throws Throwable {
return runWithTimeout(invocation, extensionContext);
return runWithTimeout(invocation, extensionContext,
extensionContext.getDisplayName() + ".constructor");
}

// below are for implementing resource checker around test method

@Override
public void beforeEach(ExtensionContext ctx) throws Exception {
ResourceChecker rc = new ResourceChecker(ctx.getDisplayName());
JUnitResourceCheckers.addResourceAnalyzer(rc);
Store store = ctx.getStore(NAMESPACE);
store.put(RESOURCE_CHECK, rc);
rc.start();
}

@Override
public void afterEach(ExtensionContext ctx) throws Exception {
Store store = ctx.getStore(NAMESPACE);
ResourceChecker rc = store.remove(RESOURCE_CHECK, ResourceChecker.class);
if (rc != null) {
rc.end();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* 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.hadoop.hbase;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.hadoop.hbase.ResourceChecker.Phase;
import org.apache.hadoop.hbase.util.JVM;

/**
* ResourceCheckers when running JUnit tests.
*/
public final class JUnitResourceCheckers {

private JUnitResourceCheckers() {
}

private static class ThreadResourceAnalyzer extends ResourceChecker.ResourceAnalyzer {
private Set<String> initialThreadNames = new HashSet<>();
private List<String> stringsToLog = null;

@Override
public int getVal(Phase phase) {
Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
if (phase == Phase.INITIAL) {
stringsToLog = null;
for (Thread t : stackTraces.keySet()) {
initialThreadNames.add(t.getName());
}
} else if (phase == Phase.END) {
if (stackTraces.size() > initialThreadNames.size()) {
stringsToLog = new ArrayList<>();
for (Thread t : stackTraces.keySet()) {
if (!initialThreadNames.contains(t.getName())) {
stringsToLog.add("\nPotentially hanging thread: " + t.getName() + "\n");
StackTraceElement[] stackElements = stackTraces.get(t);
for (StackTraceElement ele : stackElements) {
stringsToLog.add("\t" + ele + "\n");
}
}
}
}
}
return stackTraces.size();
}

@Override
public int getMax() {
return 500;
}

@Override
public List<String> getStringsToLog() {
return stringsToLog;
}
}

private static class OpenFileDescriptorResourceAnalyzer extends ResourceChecker.ResourceAnalyzer {
@Override
public int getVal(Phase phase) {
if (!JVM.isUnix()) {
return 0;
}
JVM jvm = new JVM();
return (int) jvm.getOpenFileDescriptorCount();
}

@Override
public int getMax() {
return 1024;
}
}

private static class MaxFileDescriptorResourceAnalyzer extends ResourceChecker.ResourceAnalyzer {
@Override
public int getVal(Phase phase) {
if (!JVM.isUnix()) {
return 0;
}
JVM jvm = new JVM();
return (int) jvm.getMaxFileDescriptorCount();
}
}

private static class SystemLoadAverageResourceAnalyzer extends ResourceChecker.ResourceAnalyzer {
@Override
public int getVal(Phase phase) {
if (!JVM.isUnix()) {
return 0;
}
return (int) (new JVM().getSystemLoadAverage() * 100);
}
}

private static class ProcessCountResourceAnalyzer extends ResourceChecker.ResourceAnalyzer {
@Override
public int getVal(Phase phase) {
if (!JVM.isUnix()) {
return 0;
}
return new JVM().getNumberOfRunningProcess();
}
}

private static class AvailableMemoryMBResourceAnalyzer extends ResourceChecker.ResourceAnalyzer {
@Override
public int getVal(Phase phase) {
if (!JVM.isUnix()) {
return 0;
}
return (int) (new JVM().getFreeMemory() / (1024L * 1024L));
}
}

public static void addResourceAnalyzer(ResourceChecker rc) {
rc.addResourceAnalyzer(new ThreadResourceAnalyzer());
rc.addResourceAnalyzer(new OpenFileDescriptorResourceAnalyzer());
rc.addResourceAnalyzer(new MaxFileDescriptorResourceAnalyzer());
rc.addResourceAnalyzer(new SystemLoadAverageResourceAnalyzer());
rc.addResourceAnalyzer(new ProcessCountResourceAnalyzer());
rc.addResourceAnalyzer(new AvailableMemoryMBResourceAnalyzer());
}
}
Loading