This repo leverages checkpoint/restore scripts originally provided Seb Deleuze (sdeleuze/spring-boot-crac-demo) It is intended to demonstrate Spring Boot 3.2+ CRaC support by creating a ready to restore container image.
Please note that, while you can build the checkpointed image on both Intel and Apple M* series, in order to deploy to Cloud Run you have to build in a similar architecture as the one you would be running on. See the Cloud Run Container Runtime Contract
Warning: for real projects make sure to not leak sensitive data in CRaC files since they contain a snapshot of the memory of the running JVM instance.
From within the reference-crac
folder, you have the choice to run either on demand checkpoint/restore of a running application with:
./checkpoint.sh
Or to run an automatic checkpoint/restore at startup with:
./checkpointOnRefresh.sh
Restore the application with:
./restore.sh
Let's build the checkpointed CRaC image and deploy it to Cloud Run.
# first, build the image if you have not done so before
./checkpoint.sh
# tag the image
export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
echo $PROJECT_ID
docker tag reference-crac:checkpoint gcr.io/${PROJECT_ID}/reference-crac
# push the image to GCR
docker push gcr.io/${PROJECT_ID}/reference-crac
Deploy the image to Cloud Run and test it
# get the PROJECT_ID
export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
echo $PROJECT_ID
gcloud run deploy reference-crac \
--image=gcr.io/${PROJECT_ID}/reference-crac \
--execution-environment=gen2 \
--allow-unauthenticated \
--region=us-central1 \
--memory 2Gi --cpu 2 --args="--cap-add CHECKPOINT_RESTORE --cap-add SETPCAP -XX:+UnlockExperimentalVMOptions -XX:+IgnoreCPUFeatures"
# There are cases where you would have to ignore CPUFeatures in your architecture of choice.
# https://docs.azul.com/core/crac/cpu-features#xxignorecpufeatures
# Add the following arguments to the deploy command
# --args="-XX:+UnlockExperimentalVMOptions -XX:+IgnoreCPUFeatures"
# observe the deploy output
...
✓ Deploying... Done.
✓ Creating Revision...
✓ Routing traffic...
✓ Setting IAM Policy...
Done.
Service [reference-crac] revision [spring-crac-00003-his] has been deployed and is serving 100 percent of traffic.
Service URL: https://reference-crac-....XYZ
Run a test request:
# use your deployment URL
curl https://reference-crac-....XYZ/start
# Note: if you run your service as authenticated, use
curl -i -H "Authorization: Bearer $(gcloud auth print-identity-token)" https://reference-crac-....XYZ/start