On this page
CRaC
This Micronaut module adds support for CRaC (Coordinated Restore at Checkpoint) within the framework.
For this project, you can find a list of releases (with release notes) here:
Add the following the dependency to your build:
implementation("io.micronaut.crac:micronaut-crac")We currently support Hikari DataSources that are configured to allow suspension with:
datasources.default.allow-pool-suspension=trueWhen a checkpoint is taken, the pool is suspended and the connections are closed. When the checkpoint is restored, the pool is resumed and the connections are re-established.
Since v1.2.1 of this library, we also support Redis connections.
When the checkpoint is taken, we destroy RedisClient, RedisCache and StatefulRedisConnection beans that exist in the application.
These will be re-created once the checkpoint is restored, however you will need to add your beans to the refresh scope so that these new beans are used.
The resources for each of the above can be disabled via configuration:
crac.redis.enabled=false
crac.redis.client-enabled=false
crac.redis.cache-enabled=false
crac.redis.connection-enabled=falseTo provide custom CRaC resources, create beans of type OrderedResource.
Micronaut CRaC registers resources for you into the CRaC Context. You just focus on providing implementations for OrderedResource::beforeCheckpoint and OrderedResource::afterRestore in your resources.
Micronaut CRaC registers resources in order. You can control the order by overriding OrderedResource::getOrder.
Prior to a checkpoint being taken, a RefreshEvent will be published to invalidate all beans in the @Refreshable scope.
This behaviour can be disabled by setting the crac.refresh-beans property to false in the application config.
To notify external components when the default Resource handlers execute, there are two events; BeforeCheckpointEvent and AfterRestoreEvent.
These events contain the java.time.Instant that the action completed, the length of time it took to execute the action in nanoseconds, and the Resource that was acted upon.
Please see the Micronaut Framework guide for information on how to listen for these events.
GlobalCracContextProvider, Micronaut CRaC’s default implementation of CracContextProvider, returns the global context. You can provide a replacement for CracContextProvider and provide your custom Context.
Support for building CRaC-enabled Docker images is provided by the Micronaut Gradle or Maven plugin. Either one is capable of generating a docker image containing a CRaC enabled JDK and a pre-warmed, checkpointed application.
When you create a Micronaut Framework application (either via https://launch.micronaut.io or the command line application), it creates a ContextConfigurer with enables eager singleton initialization.
As tests annotated with @MicronautTest are implicitly in the Singleton scope, this can cause problems injecting some beans (for example an HttpClient) into your test class.
To avoid this, you can either disable eager singleton initialization for your tests, or you will need to manually get an instance of the bean you would normally inject. As an example, to get an HttpClient you could do:
When you create a Micronaut Framework application (either via https://launch.micronaut.io or the command line application), it creates a ContextConfigurer with enables eager singleton initialization.
As tests annotated with @MicronautTest are implicitly in the Singleton scope, this can cause problems injecting some beans (for example an HttpClient) into your test class.
To avoid this, you can either disable eager singleton initialization for your tests, or you will need to manually get an instance of the bean you would normally inject. As an example, to get an HttpClient you could do:
With CRaC, you run your application to a point and then "checkpoint" it. This calls the app to close all its sockets and file handles, and then dumps the memory to disk. When it restarts from this snapshot, it calls the app again to say it’s been restored, and one can re-open files and network connections.
The simulator allows you to synthesise these two calls (before checkpoint and after restore), so that under a test you can check your service works again after it was closed and recreated.
The following example demonstrates how to write and test custom OrderedResource implementations. Here it is doing nothing for sake of the example, but in reality it would have a socket or file that needs to be closed, or something along those lines.
An OrderedResource which will stop the ResourceBean before a checkpoint and start it again after a restore.
OrderedResouce for the beanCheckpointSimulator in a testIf the info endpoint is enabled, then a crac section will be automatically added which shows the restore time and uptime since restore, both in milliseconds and taken from the CRaCMXBean provided by the CRaC API.
This can be disabled by setting endpoints.info.crac.enabled configuration to false.
See the following list of guides to learn more about working with CRaC in the Micronaut Framework:
You can find the source code of this project in this repository: