Skip to content

Commit 7f6f47b

Browse files
committed
Property-driven onRefresh checkpoint during application context bootstrap
Closes gh-30606
1 parent b9e972c commit 7f6f47b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java

+44
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434

3535
import org.apache.commons.logging.Log;
3636
import org.apache.commons.logging.LogFactory;
37+
import org.crac.CheckpointException;
38+
import org.crac.Core;
39+
import org.crac.RestoreException;
3740

3841
import org.springframework.beans.factory.BeanFactory;
3942
import org.springframework.beans.factory.BeanFactoryAware;
@@ -45,6 +48,7 @@
4548
import org.springframework.context.Phased;
4649
import org.springframework.context.SmartLifecycle;
4750
import org.springframework.core.NativeDetector;
51+
import org.springframework.core.SpringProperties;
4852
import org.springframework.lang.Nullable;
4953
import org.springframework.util.Assert;
5054
import org.springframework.util.ClassUtils;
@@ -63,6 +67,26 @@
6367
*/
6468
public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactoryAware {
6569

70+
/**
71+
* Property name for checkpoint restore: "spring.checkpoint.restore".
72+
* @since 6.1
73+
* @see #CHECKPOINT_RESTORE_ON_REFRESH
74+
* @see org.crac.Core#checkpointRestore()
75+
*/
76+
public static final String CHECKPOINT_RESTORE_PROPERTY_NAME = "spring.checkpoint.restore";
77+
78+
/**
79+
* Recognized value for checkpoint restore property: "onRefresh".
80+
* @since 6.1
81+
* @see #CHECKPOINT_RESTORE_PROPERTY_NAME
82+
* @see org.crac.Core#checkpointRestore()
83+
*/
84+
public static final String CHECKPOINT_RESTORE_ON_REFRESH = "onRefresh";
85+
86+
87+
private final static boolean checkpointRestoreOnRefresh = CHECKPOINT_RESTORE_ON_REFRESH.equalsIgnoreCase(
88+
SpringProperties.getProperty(CHECKPOINT_RESTORE_PROPERTY_NAME));
89+
6690
private final Log logger = LogFactory.getLog(getClass());
6791

6892
private volatile long timeoutPerShutdownPhase = 30000;
@@ -145,6 +169,10 @@ public void stop() {
145169

146170
@Override
147171
public void onRefresh() {
172+
if (checkpointRestoreOnRefresh) {
173+
new CracDelegate().checkpointRestore();
174+
}
175+
148176
this.stoppedBeans = null;
149177
startBeans(true);
150178
this.running = true;
@@ -462,6 +490,22 @@ public Object registerResource() {
462490
org.crac.Core.getGlobalContext().register(resourceAdapter);
463491
return resourceAdapter;
464492
}
493+
494+
public void checkpointRestore() {
495+
logger.info("Triggering JVM checkpoint/restore");
496+
try {
497+
Core.checkpointRestore();
498+
}
499+
catch (UnsupportedOperationException ex) {
500+
throw new ApplicationContextException("CRaC checkpoint not supported on current JVM", ex);
501+
}
502+
catch (CheckpointException ex) {
503+
throw new ApplicationContextException("Failed to take CRaC checkpoint on refresh", ex);
504+
}
505+
catch (RestoreException ex) {
506+
throw new ApplicationContextException("Failed to restore CRaC checkpoint on refresh", ex);
507+
}
508+
}
465509
}
466510

467511

0 commit comments

Comments
 (0)