Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metaspace improvements in QuarkusUnitTest - round 2 #35407

Merged
merged 8 commits into from
Aug 18, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ public void close() {
RuntimeUpdatesProcessor.INSTANCE.close();
} catch (IOException e) {
log.error("Failed to close compiler", e);
} finally {
RuntimeUpdatesProcessor.INSTANCE = null;
}
}
for (HotReplacementSetup i : hotReplacementSetups) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ public void close() {
RuntimeUpdatesProcessor.INSTANCE.close();
} catch (IOException e) {
log.error("Failed to close compiler", e);
} finally {
RuntimeUpdatesProcessor.INSTANCE = null;
}
for (HotReplacementSetup i : hotReplacementSetups) {
i.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public void close() {
RuntimeUpdatesProcessor.INSTANCE.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
RuntimeUpdatesProcessor.INSTANCE = null;
}
for (HotReplacementSetup i : hotReplacementSetups) {
i.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -322,6 +324,8 @@ public synchronized void stop() {
for (var runner : moduleRunners) {
runner.abort();
}
TestWatchedFiles.setWatchedFilesListener(
(BiConsumer<Map<String, Boolean>, List<Map.Entry<Predicate<String>, Boolean>>>) null);
}

public void runTests() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public ProxyFactory(ProxyConfiguration<T> configuration) {
}
}

public ClassLoader getClassLoader() {
return classLoader;
}

private boolean findConstructor(Class<?> clazz, boolean allowPackagePrivate, boolean allowInject) {
Constructor<?>[] ctors = clazz.getDeclaredConstructors();
if (allowInject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ class RecordingProxyFactories {
static <T> void put(Class<T> clazz, ProxyFactory<T> proxyFactory) {
RECORDING_PROXY_FACTORIES.put(clazz, proxyFactory);

if (clazz.getClassLoader() instanceof QuarkusClassLoader) {
((QuarkusClassLoader) clazz.getClassLoader()).addCloseTask(new Runnable() {
ClassLoader proxyClassLoader = proxyFactory.getClassLoader();
if (proxyClassLoader instanceof QuarkusClassLoader) {
((QuarkusClassLoader) proxyClassLoader).addCloseTask(new Runnable() {
@Override
public void run() {
RecordingProxyFactories.RECORDING_PROXY_FACTORIES.remove(clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem;
import io.quarkus.deployment.builditem.ArchiveRootBuildItem;
import io.quarkus.deployment.builditem.BytecodeTransformerBuildItem;
import io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.LiveReloadBuildItem;
import io.quarkus.deployment.builditem.RemovedResourceBuildItem;
Expand Down Expand Up @@ -71,13 +72,19 @@ public static byte[] transform(String className, byte[] classData) {
return lastTransformers.apply(className, classData);
}

private static void reset() {
lastTransformers = null;
transformedClassesCache.clear();
}

@BuildStep
TransformedClassesBuildItem handleClassTransformation(List<BytecodeTransformerBuildItem> bytecodeTransformerBuildItems,
ApplicationArchivesBuildItem appArchives, LiveReloadBuildItem liveReloadBuildItem,
LaunchModeBuildItem launchModeBuildItem, ClassLoadingConfig classLoadingConfig,
CurateOutcomeBuildItem curateOutcomeBuildItem, List<RemovedResourceBuildItem> removedResourceBuildItems,
ArchiveRootBuildItem archiveRoot, LaunchModeBuildItem launchMode, PackageConfig packageConfig,
ExecutorService buildExecutor)
ExecutorService buildExecutor,
CuratedApplicationShutdownBuildItem shutdown)
throws ExecutionException, InterruptedException {
if (bytecodeTransformerBuildItems.isEmpty() && classLoadingConfig.removedResources.isEmpty()
&& removedResourceBuildItems.isEmpty()) {
Expand Down Expand Up @@ -117,6 +124,7 @@ TransformedClassesBuildItem handleClassTransformation(List<BytecodeTransformerBu
final ConcurrentLinkedDeque<Future<TransformedClassesBuildItem.TransformedClass>> transformed = new ConcurrentLinkedDeque<>();
final Map<Path, Set<TransformedClassesBuildItem.TransformedClass>> transformedClassesByJar = new HashMap<>();
ClassLoader transformCl = Thread.currentThread().getContextClassLoader();
shutdown.addCloseTask(ClassTransformingBuildStep::reset, true);
lastTransformers = new BiFunction<String, byte[], byte[]>() {
@Override
public byte[] apply(String className, byte[] originalBytes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public synchronized static void setWatchedFilePaths(Map<String, Boolean> watched
public synchronized static void setWatchedFilesListener(
BiConsumer<Map<String, Boolean>, List<Entry<Predicate<String>, Boolean>>> watchedFilesListener) {
TestWatchedFiles.watchedFilesListener = watchedFilesListener;
if (watchedFilePaths != null) {
if (watchedFilesListener != null && watchedFilePaths != null) {
watchedFilesListener.accept(watchedFilePaths, watchedFilePredicates);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
public class NioThreadPoolRecorder {

public void updateTccl(ShutdownContext context) {
ClassLoader old = NioThreadPoolThreadFactory.updateTccl(Thread.currentThread().getContextClassLoader());
context.addLastShutdownTask(new Runnable() {
@Override
public void run() {
NioThreadPoolThreadFactory.updateTccl(old);
}
});
ClassLoader newTccl = Thread.currentThread().getContextClassLoader();
ClassLoader oldTccl = NioThreadPoolThreadFactory.updateTccl(newTccl);
if (newTccl != oldTccl) {
context.addLastShutdownTask(new Runnable() {
@Override
public void run() {
NioThreadPoolThreadFactory.updateTccl(oldTccl);
}
});
}
// Else: don't add an unnecessary shutdown task that may hold a reference to a QuarkusClassLoader,
// which could be a problem with QuarkusUnitTest since it creates one classloader per test.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.ConfigDescriptionBuildItem;
import io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem;
import io.quarkus.deployment.builditem.DevServicesLauncherConfigResultBuildItem;
import io.quarkus.dev.config.CurrentConfig;
import io.quarkus.dev.console.DevConsoleManager;
Expand Down Expand Up @@ -95,7 +96,8 @@ void registerConfigs(List<ConfigDescriptionBuildItem> configDescriptionBuildItem
void registerJsonRpcService(
BuildProducer<JsonRPCProvidersBuildItem> jsonRPCProvidersProducer,
BuildProducer<SyntheticBeanBuildItem> syntheticBeanProducer,
ConfigDevUIRecorder recorder) {
ConfigDevUIRecorder recorder,
CuratedApplicationShutdownBuildItem shutdown) {

DevConsoleManager.register("config-update-property", map -> {
Map<String, String> values = Collections.singletonMap(map.get("name"), map.get("value"));
Expand All @@ -116,6 +118,13 @@ void registerJsonRpcService(
.done());

CurrentConfig.EDITOR = ConfigurationProcessor::updateConfig;
shutdown.addCloseTask(new Runnable() {
@Override
public void run() {
CurrentConfig.EDITOR = null;
CurrentConfig.CURRENT = Collections.emptyList();
}
}, true);

jsonRPCProvidersProducer.produce(new JsonRPCProvidersBuildItem("devui-configuration", ConfigJsonRPCService.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public static void shutDownDevMode() {
}
rootHandler = null;
hotReplacementHandler = null;

hotReplacementContext = null;
}

public static void startServerAfterFailedStart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ public void handle(AsyncResult<Void> ar) {
Thread.currentThread().interrupt();
throw new IllegalStateException("Exception when closing Vert.x instance", e);
}
LateBoundMDCProvider.setMDCProviderDelegate(null);
vertx = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public static Vertx getVertx() {

void destroy() {
messageConsumers = null;
vertx = null;
}

void registerMessageConsumers(Map<String, ConsumeEvent> messageConsumerConfigurations) {
Expand Down