Repository navigation
Conversation
…ion (#987) A hibernated instance resumes the same spored process, which carried its pre-hibernate state into the new run. The pre-stop hook and task record were spent, so the next stop or TTL terminate skipped them. The DNS name Cleanup deleted came back only at the hourly renewal, with the IP from the first boot. lastActivityTime was hours old, so the first idle tick hibernated again within a minute. The plugins Cleanup stopped stayed stopped. Detection runs on each monitor tick (checkResume) and needs no sleep hook: - The primary signal is CLOCK_BOOTTIME - CLOCK_MONOTONIC (the idea from #965) growing by 1s or more. Only a suspend moves it. sysSuspendedTime lives in suspend_linux.go / suspend_other.go, so it does not collide with #965's kernelclock.go. - The backup signal is the wall clock running at least 5 minutes ahead of Go's monotonic clock between ticks. - resumedBetween is pure and table-tested. onResume re-arms preStopDone and taskFlushDone, restarts the idle timer and warning, clears the stop backoff, resets the CPU, network and disk baselines, and marks a completion file already acted on as spent. checkCompletion ignores it until its mtime changes. onResume then re-reads the public IP from IMDS and calls restoreAfterCleanup (renamed from restoreAfterFailedStop), which re-registers DNS, the registry entry and the plugins. The new IP is held in a guarded currentIP field behind publicIP(). identity is shared unguarded with the provider, the plugin runtime and off-loop goroutines, so it is not mutated. The DNS register and delete calls and the ready-url hosts read publicIP(). (#987)
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
Fixes #987. Stacked on #1009 (fix/979) → #1005 → #986 → #977 → #969 → #960 → #955 → #936. The base is
fix/979, so the diff shows only #987.What was wrong
A hibernated instance resumes the same spored process, and spored carried its pre-hibernate state into the new run:
preStopDoneandtaskFlushDonestayed set, so the next stop, hibernate or TTL terminate skipped the user's pre-stop hook and the task record (the data-loss shape of --pre-stop does not reliably flush before TTL-triggered terminate → data loss on ephemeral storage #184/pre-stop: surface a loud signal when the hook transfers nothing / exits non-zero #186).Cleanuphad deleted the DNS name. Only the hourly renewal put it back, and withidentity.PublicIPfrom the first boot.lastActivityTimewas from before the hibernation, so the first idle tick re-hibernated within about a minute.Cleanupstopped stayed stopped.A fifth case the issue doesn't list: after
--on-complete hibernate, the completion file is still on disk, so the first tick after the resume hibernated the instance again.Detection (
pkg/agent/resume.go)checkResumeruns at the start of every monitor tick (the first call inMonitorsets the baseline) and compares this tick's clocks with the last tick's. I chose this over a systemd sleep hook: a hook is another unit to install and only helps if it runs. Two independent signals:CLOCK_BOOTTIME − CLOCK_MONOTONICgrows by ≥ 1 s. This is Exclude hibernated time from compute: measure it on the kernel's monotonic clock (#945) #965's idea. Only a suspend moves the difference, because NTP slews both clocks alike.sysSuspendedTimeis insuspend_linux.go/suspend_other.go, deliberately separate from Exclude hibernated time from compute: measure it on the kernel's monotonic clock (#945) #965'skernelclock.go/sysKernelClocks, so the two stacks don't collide. Once both land they can share one reader.resumedBetweenis a pure function and is table-tested, including the near-misses (clock-read noise, a 30 s NTP step, a backwards step).What a resume resets (
onResume)preStopDoneandtaskFlushDone: the next exit runs the hook and writes the record again.lastActivityTime= now, andidleWarned= false.checkCompletionignores it, and a completion written after the resume is acted on as usual.lookupPublicIPis a seam for tests). It is kept in a guardedcurrentIP, read throughpublicIP().identityis not mutated, because the provider, the plugin runtime and off-loop goroutines share it unguarded. The DNS register and delete calls and the ready-url hosts now readpublicIP().restoreAfterCleanup(renamed fromrestoreAfterFailedStop) then re-registers DNS with the new IP and the job-array entry, and restores the plugins withResumeStopped(A failed stop/hibernate leaves every plugin marked stopped on a running instance (Cleanup's StopAll is not undone) #979). When the hibernate was started outside spored,Cleanupdidn't run and these are idempotent refreshes.Tests (
pkg/agent/resume_test.go)The kernel reading is replaced by a fake, so a hibernation can be simulated.
TestResumedBetween: both signals and the near-misses.TestResumeResetsPerBootState: hibernate on idle (soCleanupruns), then an 8 h suspend. Every flag is re-armed, the idle timer and baselines are reset, there is one UPSERT with the new IP, and the plugin is running again.TestResumedInstanceIsNotReHibernatedAndRunsPreStopAgain: the issue's scenario throughcheckAndAct, with counters faked to read a quiet machine. The tick after the resume does not hibernate. The next real idle hibernate runs the pre-stop hook a second time and deletes the record at the new IP.TestResumeAfterOnCompleteHibernate: the stale file does not re-hibernate, and a rewritten file (new mtime) is announced and acted on.TestNoResumeWithoutASuspend.sys_linux_suspend_test.go: the real kernel read. It passed in an AL2023 container under podman, along with the rest of these tests built for linux/arm64.Revert checks. Each revert compiled, and the test ran and failed:
onResumenever calledResetsPerBootState,NotReHibernated...(re-hibernated on the first tick),AfterOnCompleteHibernateResetsPerBootState,NotReHibernated...(re-hibernated on the first tick)ResetsPerBootState(UPSERT with 1.2.3.4),NotReHibernated...(DELETE at 1.2.3.4)AfterOnCompleteHibernatepreStopDonenot re-armedResetsPerBootState,NotReHibernated...(hook ran once over two hibernates)ResumedBetween(both wall cases)Checks
make check: green except the known Docker-onlyTestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker.make check-nested: all 9 nested modules clean.go test -race ./pkg/agent ./pkg/pluginruntime: ok.Caveats
timekeeping_resumeinjects the sleep intoCLOCK_BOOTTIMEfrom the persistent clock, which is the same assumption Compute clock counts hibernated time after a resume (status, and a same-boot spored restart) #945 makes. The wall-clock signal covers a kernel that doesn't. The podman VM I had has never suspended (suspended = 0 s), so I could only check the read itself.spawn:ready-urlon an instance with no DNS name, plugin{{ instance.ip }}, and an instance whose first boot had no public IP.