Repository navigation
feat(aws): shut down build instances whose launcher has gone - #29
Closed
iAmMichaelConnor wants to merge 1 commit into
Closed
iAmMichaelConnor wants to merge 1 commit into
iAmMichaelConnor wants to merge 1 commit into
Conversation
When the process running bootstrap_ec2 dies (the job is cancelled, the runner crashes, the job times out), nothing tells the build instance. It builds on with nobody waiting for the result until its shutdown timer. The launcher now keeps a Redis key, ci3-launcher-<CI_LOG_ID>, alive while it runs (120 s expiry, refreshed every 30 s). The build container checks the key every 60 s. Once the key has been seen, 3 misses in a row make it log that the launcher has gone and leave a flag file in /tmp, and the host shuts the instance down. The container never acts before it has seen the key, and never on a Redis error or unexpected reply. On by default under GitHub Actions only; CI3_LAUNCHER_WATCHDOG=0/1 turns it off or on. Always off with NO_TERMINATE=1, for releases, and when the launcher cannot write the key.
Contributor
Author
|
Closing. As written this never turns on: GitHub-hosted runners have no Redis access, so the launcher can't write its liveness key. Cancels and job timeouts are handled by the normal cleanup path (see AztecProtocol/aztec-packages#25894), and unclaimed instances are bounded by the boot timer in #28. The remaining case, a runner lost after the instance is claimed, is uncommon and still bounded by the instance's shutdown timer. |
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
bootstrap_ec2runs on a CI runner. It starts a build on an EC2 instance, sends it the build script over SSM, and waits for the result. If the runner side dies (the job is cancelled, the runner crashes, the job times out), nothing tells the instance. It keeps building, with nobody waiting for the result, until itsshutdown -h +$AWS_SHUTDOWN_TIMEtimer fires 60 to 180 minutes later.Design
A heartbeat already runs from the build container to Redis (
hb-<CI_LOG_ID>). This PR adds one in the other direction, from the launcher to the build, using the same Redis. The logic lives ininternal/source_launcher_watchdog.Launcher (
bootstrap_ec2). After the instance is provisioned, it setsci3-launcher-<CI_LOG_ID>withSETEX ... 120 1, then refreshes it every 30 s from a background loop. Itscleanuptrap stops the loop, and so does a spot-eviction retry before itexecs. If the launcher is SIGKILLed, the trap never runs. The loop is normally killed along with it. If the loop survives, it notices on its next turn that the launcher has gone and stops. The loop's output goes to/dev/null, so it never holds open a pipe thatparallelor the CI runner waits on.CI3_LAUNCHER_WATCHDOG=1is passed into the container only if the first write returnedOK.Build container. Next to the existing heartbeat,
watch_launcherrunsredis_cli EXISTS ci3-launcher-<CI_LOG_ID>every 60 s:1: the watcher is armed, and the miss count goes back to zero;0once armed: one miss;redis_clihides behind empty output, a timeout, an error reply, any other text): nothing changes.After 3 misses in a row it prints a
LAUNCHER WATCHDOG: ...line saying that the launcher has gone and the instance is shutting down. It then creates/tmp/ci3-launcher-gone(the host's/tmpis mounted at/tmpin the container) and stops. The line goes to the container's output, and is also appended to the run's log file, whichcache_logpublishes to the dashboard every few seconds. That copy is best effort: the instance may go down before the next publish.Host. The host script's monitoring loop, which already checks for spot-termination notices every 5 s, also checks for the flag file. When it finds one, it prints a
HOST:line and runssudo shutdown -h now. If the launch template setsInstanceInitiatedShutdownBehavior: terminate(fix(aws): bound the lifetime of build instances whose launcher is killed #28 does), the instance is terminated. Otherwise it is stopped. The host script deletes any old flag file when it starts, so a file left by an earlier build cannot stop a new one.Timing: the key expires at most 120 s after the launcher's last refresh, and 3 misses take up to another 180 s. So the instance starts shutting down about 5 minutes after its launcher dies.
Safety properties
0reply fromEXISTSdoes. An unreachable or slow Redis leaves the build alone.When it is off
CI3_LAUNCHER_WATCHDOG=1. Someone launching from a laptop may close the lid and still expect the build to finish. The setting keeps the mechanism vendor-neutral: another CI system can turn it on withCI3_LAUNCHER_WATCHDOG=1, and a GitHub Actions job can turn it off withCI3_LAUNCHER_WATCHDOG=0.NO_TERMINATE=1, whose instances are meant to outlive their launcher.REF_NAMEis a semver).SETEXdoes not replyOK.Tests
tests/launcher_watchdog_testneeds no Redis server and makes no AWS calls; it unsets AWS credentials. It driveswatch_launcherwith a stub check command, checks 0.01 s apart, and checks:It also checks when the launcher turns the watchdog on (GitHub Actions,
NO_TERMINATE, releases,CI3_LAUNCHER_WATCHDOG=0/1). It checks that the heartbeat starts only if Redis is available and the first write returnsOK, and thatstop_launcher_heartbeatstops the loop. Finally, it checks the wiring inbootstrap_ec2by reading the source.All 21 checks pass, in about 2 s. I also broke the watcher on purpose (acting before the key is seen; counting errors as misses), and the relevant checks failed.
bash -npasses on every changed file.Not tested here: a real launch.
Checks a maintainer should do on a real run
LAUNCHER WATCHDOGandHOST:lines. Without fix(aws): bound the lifetime of build instances whose launcher is killed #28 the instance ends stopped, not terminated.ci3-launcher-<CI_LOG_ID>key is refreshed throughout, and no/tmp/ci3-launcher-goneappears on the instance.NO_TERMINATE=1build, and confirm the container getsCI3_LAUNCHER_WATCHDOG=0.Related