Repository navigation
Instance shutdown safety: rerun an automatic script cut by an instance shutdown - #339
Open
michal-tobiasz-wttech wants to merge 9 commits into
Open
michal-tobiasz-wttech wants to merge 9 commits into
michal-tobiasz-wttech wants to merge 9 commits into
Conversation
…ueue disappears A new OSGi configuration (ShutdownSafety, off by default) makes a run that finds the execution queue gone end as ABORTED with abortCause INSTANCE_SHUTDOWN instead of FAILED, and keeps that result when the lock release fails during the shutdown. With the configuration off, behaviour and stored data are unchanged.
Remove the abortOnShutdown part switch: the enabled option alone turns the behaviour on or off. One switch avoids combinations that make no sense once more parts are added, and keeps the configuration small.
With instance shutdown safety enabled, a boot removes the cron schedules only after the instance is healthy, right before it schedules them again. A boot that gives up on an unhealthy instance, for example on a pod that is shutting down, no longer leaves the cluster without cron schedules until the next boot.
With instance shutdown safety enabled, the leader schedules the normal boot when an instance of its local cluster leaves. A new leader has not booted any script yet, so canRun() decides for every automatic script, and a run cut by the shutdown runs again without waiting for the next deployment. Adds a package import of org.apache.sling.discovery [1.0,2), which AEM 6.5.0+ and AEM as a Cloud Service provide.
Each behaviour can now be turned on alone: abortOnShutdown, keepCronUntilHealthy and bootOnInstanceLeft, all off by default with no main switch. Every instance now logs which instances left the cluster and what it decided.
When an instance is killed, Sling delivers the unfinished job again with the same ID. With rerunKilledExecution on, the run finds the lock its killed attempt left and runs the script once more, instead of waiting for the lock timeout. A refreshed alive time and a stop limit keep two attempts from working at once.
With the experimental ShutdownSafety option `enabled`, automatic scripts get a lock that records the run, its instance and a heartbeat. A run killed with its instance runs again once under its own lock when Sling delivers the job again. A run cut by a graceful stop is taken over by the leader's boot after the instance left the cluster, only when Sling reports the job as finished, so the script never runs twice. A run on an instance still in the cluster never counts as dead. Boots wait while the instance shuts down (shutdown marker file, CRITICAL health checks), and the boot after an instance left waits until Sling moved the queued jobs of that instance. One flag replaces the separate toggles, and the abort-on-shutdown change is removed; with the flag off ACM behaves as before.
…e-left boot delay configurable A failed lock release no longer replaces the script result, so history keeps the real outcome while Sling still gets the job as failed for a later takeover. An attempt refused by its own earlier run no longer writes the shared history entry. The boot delay after an instance left is the setting instanceLeftBootDelay (default 30 s, 0 to 2 minutes). The lock check is a pure read, and logs no longer repeat the marker file or heartbeat warnings.
|
|
||
| private Duration lockTimeout = null; | ||
|
|
||
| static final String LOCK_LOST_MESSAGE = "Execution aborted, its execution lock is held by another run"; |
Collaborator
There was a problem hiding this comment.
why constant ;)
|
|
||
| static final String LOCK_LOST_MESSAGE = "Execution aborted, its execution lock is held by another run"; | ||
|
|
||
| private volatile boolean lockLost; |
Collaborator
There was a problem hiding this comment.
stateful? :/
|
|
||
| private static final String RESOURCE_TYPE = JcrConstants.NT_UNSTRUCTURED; | ||
|
|
||
| private static final String LOCKED_PROP = "locked"; |
Collaborator
There was a problem hiding this comment.
that was intentionally encapsulated to keep lock-related stuff only here
krystian-panek-vmltech
requested changes
Nov 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
During a rolling deployment on AEM as a Cloud Service, the old author pod stays the cluster leader until it stops. ACM runs its queued jobs on the leader, so a boot can start automatic scripts on a pod that is already shutting down, and a script running there is cut in the middle. On
mainnothing runs it again: after a graceful stop the lock is left behind until the lock timeout, and after a kill the job that Sling moves to another instance endsLOCKED.Change
A new experimental OSGi configuration
dev.vml.es.acm.core.instance.ShutdownSafety. The flagenabledis off by default. With the flag off, ACM behaves as onmain. The one exception: a boot or cron job waiting for a healthy instance stops when its thread is interrupted, instead of looping.With
enabled:shutdownMarkerFile, on AEMaaCS/var/run/aem/shutdown) or a Felix health check tagged withhealthCheckTags(defaultsystemhealth) is CRITICAL.lockAttemptLimit, default 3).GIVEN_UP. After the instance leaves the cluster, the leader boots afterinstanceLeftBootDelay(default 30 s) and takes the lock over only when Sling reports the owner's job as finished.Only public Sling APIs are used (
DiscoveryService,JobManager,TopologyEventListener). The ACM queues keep 0 retries. Scripts are not required to callcheckAborted(). Details, settings and limits are in the README section "Instance shutdown safety".Verification
sh mvnw clean install: 291 unit tests incore, all green.kill -9of the leader during a script, three queued scripts, a slow shutdown with a script that does not callcheckAborted(), the attempt limit, the shutdown marker file, a forced lock release failure, and the flag off. Each script ran exactly once, and runs never overlapped.kill -9.