Opt-in training run: start, warm up, then exit 0 (for JDK AOT cache, CDS, PGO and agent runs)
Feature description
Add an opt-in training run to Micronaut.run. The application starts, optionally sends a few GET requests to itself, then stops and exits with status 0. The JDK AOT cache (-XX:AOTCacheOutput), dynamic CDS, GraalVM PGO (.iprof) and native-image-agent all need a representative run that ends in an orderly JVM exit.
Why a switch is needed
A Micronaut application never stops on its own:
- HTTP.
Micronaut.start()returns, becauseNettyHttpServer.isKeepAlive()isfalse. The non-daemon event-loop threads then keep the JVM alive. - Messaging-only applications.
MessagingApplication.isServer()istrue, sostart()waits on the keep-alive latch.
So every pipeline scripts a readiness loop, a warm-up and a stop (SIGTERM, a stop endpoint or jcmd), as the Gradle CRaC plugin does for checkpoints (checkpoint.sh). On Windows, Process.destroy() skips shutdown hooks, so the JVM writes nothing. JDK 25.0.4 and later can end an AOT recording with jcmd <pid> AOT.end_recording (JDK-8370203). That needs jcmd, which the default eclipse-temurin:25-jre image does not have (MicronautDockerfile.java), and it does not cover PGO, the agent or a warm-up.
The switch needs a warm-up (measured)
Exiting right after startup, like Spring's spring.context.exit=onRefresh, is not enough. We trained two AOT caches that differ only in when training stopped:
| Stop at "Startup completed" | First 200, then GET /hello ×3 |
|
|---|---|---|
| Median time to first 200 | 306.2 ms | 259.1 ms |
| Classes not served from the cache | 626 of 6,438 (175 io.micronaut, 70 lambdas) |
87 of 6,483 (8 io.micronaut, 5 lambdas) |
| Max RSS | 158.0–160.0 MiB | 130.9–131.3 MiB |
- Paired difference: +49.0 ms [+36.4, +56.7]. Without a cache the median is 520.5 ms, so stopping at startup gives up about 18% of the cache's gain.
- In-process requests match external ones. A wrapper
mainsending 4 GETs throughHttpURLConnection, thenSystem.exit(0), againstcurlplus SIGTERM: −6.5 ms [−10.8, +6.5], with the same misses and RSS. - A training
-javaagentis not an alternative. Its cache missed 802 classes (about 170 MiB max RSS), becausejava.instrumentturns off the cache's optimized module handling.
Proposed change
- Property. An ordinary property, for example
micronaut.application.training.enabled, so-DandMICRONAUT_APPLICATION_TRAINING_ENABLEDboth work. It is off by default and logs a WARN when on. - Exit, in
Micronaut.start()(context). Act right afterembeddedApplication.start()returns. By then every startup listener has run synchronously (NettyHttpServer.java).- Stop the application, close the context and call
System.exit(0). This covers everyEmbeddedApplication, messaging included. - Skip
System.exitin thetestenvironment, ashandleStartupExceptiondoes. - Failures already go through
handleStartupExceptionand exit non-zero, so a failed training run fails the build.
- Stop the application, close the context and call
- Warm-up, in http-server. Optional
…training.warmup.pathsand…warmup.repeat.- A
ServerStartupEventlistener, ordered last, sends the GETs throughjava.net.HttpURLConnection, so no client module is needed. - Request the configured
micronaut.server.host, or loopback when none is set. An unset host binds the wildcard address, whilegetURL()falls back to$HOSTNAME. - A status of 400 or more, or an I/O error, throws. That fails startup, because listener exceptions propagate.
- HTTPS-only servers need a custom warm-up.
- A
- No new SPI. Document custom warm-up (POST, auth, messaging) as a startup listener annotated
@Requires(property = "micronaut.application.training.enabled", value = "true").
Micronaut.start()// after embeddedApplication.start() and the "Startup completed" log, before the shutdown hook and latch
Environment environment = applicationContext.getEnvironment();
if (environment.getProperty(TRAINING_ENABLED, Boolean.class, false)) {
LOG.warn("Training run ({}=true): startup and warm-up done, exiting with 0", TRAINING_ENABLED);
try (applicationContext) {
embeddedApplication.stop();
}
if (!environment.getActiveNames().contains(Environment.TEST)) {
System.exit(0);
}
return applicationContext;
}Compatibility
- Opt-in only. The warm-up adds about 40 JDK networking classes to the cache, with no measured startup or RSS cost.
- Training runs application code. Applications that need external services should train with a training environment or with test resources.
How to verify
- Functional tests: an HTTP application with warm-up paths and a messaging-only application both exit 0; a warm-up path returning 500 exits non-zero; nothing exits under
test. java -XX:AOTCacheOutput=app.aot -Dmicronaut.application.training.enabled=true -jar app.jarwrites the cache. Replacing the output flag with a strict-XX:AOTMode=on -XX:AOTCache=app.aotalso exits 0.- A Windows CI job covers the exit.
How this was found
We profiled micronaut-runner with its benchmark-large sample (Micronaut Platform 5.1.5, Netty).
- Setup. Runner's extracted layout (a JAR-only class path) on OpenJDK 25.0.4.1, macOS on Apple Silicon with 12 CPUs, on a shared machine. Caches were made with
-XX:AOTMode=recordand thencreate. - Timing. From spawn to the first HTTP 200. After 3 warm-up rounds, we ran n=20 paired rounds in random interleaved order and report the median paired difference with a bootstrap 95% CI.
- Diagnostics. Class counts come from one
-Xlog:class+loadlaunch per cache. Max RSS is from/usr/bin/time -lover 3 runs.
Related
- In-image training in micronaut-projects/micronaut-gradle-plugin#1379 and micronaut-projects/micronaut-maven-plugin#1721 would use this switch, falling back to a script on older cores.
- micronaut-runner:
- https://github.com/alvarosanchez/micronaut-runner/issues/150: Runner's training driver works on every Micronaut version with its own readiness check, workload and stop. On a core with this switch,
runToExit = trueplus the property and warm-up paths intraining.jvmArgslets the application warm up and exit by itself, and the driver only waits for exit 0. - https://github.com/alvarosanchez/micronaut-runner/issues/157: training and verifying an AOT-cache layout (it does not use this switch; on JDK 25.0.4 and later it ends the recording with
jcmd <pid> AOT.end_recording). - https://github.com/alvarosanchez/micronaut-runner/issues/125: the AOT-cache recipe, which stops training with SIGTERM, with
jcmd <pid> AOT.end_recordingon JDK 25.0.4 and later, or with the management stop endpoint.
- https://github.com/alvarosanchez/micronaut-runner/issues/150: Runner's training driver works on every Micronaut version with its own readiness check, workload and stop. On a core with this switch,
Source: micronaut-projects/micronaut-core