Skip to content

Instance shutdown safety: rerun an automatic script cut by an instance shutdown - #339

Open
michal-tobiasz-wttech wants to merge 9 commits into
wttech:mainfrom
michal-tobiasz-wttech:instance-shutdown-safety
Open

michal-tobiasz-wttech wants to merge 9 commits into
wttech:mainfrom
michal-tobiasz-wttech:instance-shutdown-safety

Conversation

@michal-tobiasz-wttech

Copy link
Copy Markdown

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 main nothing 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 ends LOCKED.

Change

A new experimental OSGi configuration dev.vml.es.acm.core.instance.ShutdownSafety. The flag enabled is off by default. With the flag off, ACM behaves as on main. The one exception: a boot or cron job waiting for a healthy instance stops when its thread is interrupted, instead of looping.

With enabled:

  • Boot waits on a stopping instance: while the shutdown marker file exists (shutdownMarkerFile, on AEMaaCS /var/run/aem/shutdown) or a Felix health check tagged with healthCheckTags (default systemhealth) is CRITICAL.
  • Lock with an owner for automatic scripts: execution ID, attempt, owner Sling ID and a heartbeat. A run on another instance that is still in the cluster is never treated as dead.
  • Rerun after a kill: the job that Sling delivers again runs the script once more under its own lock (lockAttemptLimit, default 3).
  • Takeover after a graceful stop: the left lock makes the Sling job end GIVEN_UP. After the instance leaves the cluster, the leader boots after instanceLeftBootDelay (default 30 s) and takes the lock over only when Sling reports the owner's job as finished.
  • Cron schedules are replaced only once the instance is healthy.

Only public Sling APIs are used (DiscoveryService, JobManager, TopologyEventListener). The ACM queues keep 0 retries. Scripts are not required to call checkAborted(). Details, settings and limits are in the README section "Instance shutdown safety".

Verification

  • sh mvnw clean install: 291 unit tests in core, all green.
  • Local 2-node author cluster on MongoDB: graceful stop and kill -9 of the leader during a script, three queued scripts, a slow shutdown with a script that does not call checkAborted(), 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.
  • AEM 6.5.26 (Java 11) and AEM 6.5 LTS (Java 21): the bundle is active, and the Felix Health Check API import is optional. Flag off and on, marker file, rerun after kill -9.
  • Not yet run on AEM as a Cloud Service.

…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";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why constant ;)


static final String LOCK_LOST_MESSAGE = "Execution aborted, its execution lock is held by another run";

private volatile boolean lockLost;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stateful? :/


private static final String RESOURCE_TYPE = JcrConstants.NT_UNSTRUCTURED;

private static final String LOCKED_PROP = "locked";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that was intentionally encapsulated to keep lock-related stuff only here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants