wasm_cleanup_manager (wasm v0.16.0)

View Source

Internal: the node-wide cleanup manager.

It performs no filesystem I/O and runs no cleanup callback, so the reaper being slow or absent can never stall it. What it does today:

  • The operator view. The reaper pushes its per-state counts and live requests here, and stats/0 and requests/0 serve that copy, which wasm_script_worker:cleanup_stats/0 and wasm_script_worker:cleanup_requests/0 read. They answer while the reaper is busy with its journal, and answer an empty view between a reaper's death and its replacement's first push.
  • Job leases for cleanup the reaper cannot own. start_local_cleanup/2 and run_bounded_action/3 run under at most max_cleanup_jobs leases, with the overflow queued. This count is separate from the reaper's own jobs.
  • Whether a reaper can be reached, for a steward whose reaper died: reaper/0.
  • Admission capacity. capacity/0 is the same ceiling the reaper refuses requests at (max_cleanup_jobs + cleanup_queue_len), and admit/1 and release/1 keep a count against it, keyed by request id. Nothing in src/ calls admit/1 or release/1 yet: the reaper still decides admission and answers cleanup_saturated itself. Only wasm_cleanup_manager_SUITE uses them.

The cleanup work is moving here from the reaper in stages, and the move is not finished. test/audit/CLEANUP_STEWARD.md is the design record for where it is going; "Who owns what while the cleanup migration is in progress" in docs/worker-internals.md says who owns each job today.

The manager is recovering until it has learned the current reaper's generation. The reaper announces its generation and recovered record count by sending the manager a message addressed to its pid, so nothing here calls into the reaper and no module cycle is formed. A reaper-only crash restarts the reaper beneath the manager (the supervisor is rest_for_one); the manager sees the monitored reaper go down, returns to recovering, and reaches ready again on the replacement's announcement. An announcement from an older generation is ignored.

Summary

Functions

Reserve admission for a request. cleanup_saturated when the node is already at capacity. Admission is idempotent per request id.

How many requests currently hold admission.

How many requests may hold cleanup state at once.

Whether the manager has learned the current reaper's generation. recovering until it has, ready after.

The hold-vs-fail decision a steward needs when its pinned reaper dies: whether a reaper can be reached now. {ok, Pid} when one is registered (a supervised restart is established first through wasm_worker_sup:ensure_reaper/0); gone when none can be reached, so the steward stops holding and fails the operation.

The generation of the reaper the manager tracks, or 0 if it tracks none.

Release a request's admission. A no-op if it held none.

The operator view's live requests, served from the reaper's last push.

Run one unaccepted cleanup action under a job lease, off the caller.

Run a request's local cleanup fallback under a job lease, off the guardian.

The operator view's per-state counts, served from the reaper's last push so it answers even while the reaper is wedged in journal I/O.

Functions

admit(RequestId)

-spec admit(term()) -> ok | {error, cleanup_saturated}.

Reserve admission for a request. cleanup_saturated when the node is already at capacity. Admission is idempotent per request id.

admitted()

-spec admitted() -> non_neg_integer().

How many requests currently hold admission.

capacity()

-spec capacity() -> non_neg_integer().

How many requests may hold cleanup state at once.

handle_call/3

handle_cast(Msg, S)

handle_info/2

init/1

phase()

-spec phase() -> recovering | ready.

Whether the manager has learned the current reaper's generation. recovering until it has, ready after.

reaper()

-spec reaper() -> {ok, pid()} | gone.

The hold-vs-fail decision a steward needs when its pinned reaper dies: whether a reaper can be reached now. {ok, Pid} when one is registered (a supervised restart is established first through wasm_worker_sup:ensure_reaper/0); gone when none can be reached, so the steward stops holding and fails the operation.

reaper_generation()

-spec reaper_generation() -> non_neg_integer().

The generation of the reaper the manager tracks, or 0 if it tracks none.

release(RequestId)

-spec release(term()) -> ok.

Release a request's admission. A no-op if it held none.

requests()

-spec requests() -> [map()].

The operator view's live requests, served from the reaper's last push.

run_bounded_action(Action, ReplyTo, Tag)

-spec run_bounded_action(fun(() -> any()), pid(), term()) -> ok.

Run one unaccepted cleanup action under a job lease, off the caller.

For a register the steward could not have the reaper own -- past the per-request operation ceiling -- the action still has to run rather than leak. It runs under the same max_cleanup_jobs bound, and {Tag, released | cleanup_failed} is sent to ReplyTo when it is done, so the steward answers the guardian without ever running a callback itself.

start_link()

-spec start_link() -> {ok, pid()}.

start_local_cleanup(RequestId, Mirror)

-spec start_local_cleanup(term(), map()) -> ok.

Run a request's local cleanup fallback under a job lease, off the guardian.

The guardian hands over the request's complete mirror when no reaper can own the cleanup. The manager starts a terminal replacement steward to run it and holds a lease, so local fallback jobs share the node's max_cleanup_jobs bound with the reaper's own jobs and a reaper outage cannot create an unbounded burst. Returns once the job is leased or queued; the manager owns it from there.

stats()

-spec stats() -> map().

The operator view's per-state counts, served from the reaper's last push so it answers even while the reaper is wedged in journal I/O.