Skip to content

Commit

Permalink
Property-driven onRefresh checkpoint during application context boots…
Browse files Browse the repository at this point in the history
…trap

Closes gh-30606
  • Loading branch information
jhoeller committed Jun 6, 2023
1 parent b9e972c commit 7f6f47b
Showing 1 changed file with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.crac.CheckpointException;
import org.crac.Core;
import org.crac.RestoreException;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
Expand All @@ -45,6 +48,7 @@
import org.springframework.context.Phased;
import org.springframework.context.SmartLifecycle;
import org.springframework.core.NativeDetector;
import org.springframework.core.SpringProperties;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
Expand All @@ -63,6 +67,26 @@
*/
public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactoryAware {

/**
* Property name for checkpoint restore: "spring.checkpoint.restore".
* @since 6.1
* @see #CHECKPOINT_RESTORE_ON_REFRESH
* @see org.crac.Core#checkpointRestore()
*/
public static final String CHECKPOINT_RESTORE_PROPERTY_NAME = "spring.checkpoint.restore";

/**
* Recognized value for checkpoint restore property: "onRefresh".
* @since 6.1
* @see #CHECKPOINT_RESTORE_PROPERTY_NAME
* @see org.crac.Core#checkpointRestore()
*/
public static final String CHECKPOINT_RESTORE_ON_REFRESH = "onRefresh";


private final static boolean checkpointRestoreOnRefresh = CHECKPOINT_RESTORE_ON_REFRESH.equalsIgnoreCase(
SpringProperties.getProperty(CHECKPOINT_RESTORE_PROPERTY_NAME));

private final Log logger = LogFactory.getLog(getClass());

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

@Override
public void onRefresh() {
if (checkpointRestoreOnRefresh) {
new CracDelegate().checkpointRestore();
}

this.stoppedBeans = null;
startBeans(true);
this.running = true;
Expand Down Expand Up @@ -462,6 +490,22 @@ public Object registerResource() {
org.crac.Core.getGlobalContext().register(resourceAdapter);
return resourceAdapter;
}

public void checkpointRestore() {
logger.info("Triggering JVM checkpoint/restore");
try {
Core.checkpointRestore();
}
catch (UnsupportedOperationException ex) {
throw new ApplicationContextException("CRaC checkpoint not supported on current JVM", ex);
}
catch (CheckpointException ex) {
throw new ApplicationContextException("Failed to take CRaC checkpoint on refresh", ex);
}
catch (RestoreException ex) {
throw new ApplicationContextException("Failed to restore CRaC checkpoint on refresh", ex);
}
}
}


Expand Down

0 comments on commit 7f6f47b

Please sign in to comment.