Skip to content

fix(distributed): run cold model loads as durable jobs instead of holding the advisory lock - #11514

Merged
mudler merged 7 commits into
masterfrom
feat/distributed-cold-load-jobs
Aug 15, 2026
Merged

fix(distributed): run cold model loads as durable jobs instead of holding the advisory lock#11514
mudler merged 7 commits into
masterfrom
feat/distributed-cold-load-jobs

Conversation

@localai-bot

Copy link
Copy Markdown
Collaborator

The incident

On a localai-org-development frontend (2 replicas), loading a 35.7 GB GGUF onto a newly added Jetson Thor worker made the model permanently unloadable from the operator's seat, while staging was in fact progressing normally underneath.

Replica A acquired the per-model advisory lock model-load:Qwen3.6-27B-MTP-GGUF and began staging — ~20 minutes of transfer. Replica B received a request for the same model, blocked on pg_advisory_lock, and was killed at 60s by the localai role's statement_timeout:

routing model llama-cpp/models/Qwen3.6-27B-MTP-GGUF/Qwen3.6-27B-UD-Q8_K_XL.gguf:
loading model Qwen3.6-27B-MTP-GGUF: advisorylock: acquiring lock 9003261067483446873:
ERROR: canceling statement due to statement timeout (SQLSTATE 57014)

Every UI retry reproduced it. Three defects sit behind that one symptom:

  1. The lock's lifetime was the transfer's lifetime. Route wrapped the whole cold load — backend install, multi-GB staging, checkpoint load — in advisorylock.WithLockCtx. The lock's job is to de-duplicate concurrent loaders, a decision that takes milliseconds; holding it for tens of minutes turns a dedup mechanism into a cluster-wide outage for that model.
  2. WithLockCtx defended against lock_timeout but not statement_timeout. Both abort the same blocking pg_advisory_lock($1); only the former was overridden. Latent for every blocking caller, not just model loads.
  3. There was no caller contract for "this model is staging." The request either blocked or failed. StagingTracker already tracked per-file byte progress and broadcast it over NATS, but nothing on the inference path consumed any of it.

The change: claim / run split

The cold load becomes a durable job (model_load_jobs), with waiters attached by broadcast.

Request ─► Route
             ├─ warm path: FindAndLockNodeWithModel ──────────────► serve (unchanged)
             │
             └─ cold path:
                  ClaimLoadJob(trackingKey)          ◄── advisory lock held ~ms
                     │
                     ├─ claimed ─► run the job in the background
                     │               install ─► stage ─► LoadModel
                     │
                     └─ already active ────► attach as waiter (no lock held)
                                                │
                  ┌─────────────────────────────┘
                  ▼
             wait for: job ready ─► retry warm path ─► serve
                       job failed ─► return the job's real error
                       wait budget elapsed ─► 503 + progress + Retry-After
                       client cancelled ─► return, the job continues

The advisory lock is kept — it is the right primitive — but its guarded section shrinks to the claim: a SELECT, possibly a DELETE of an orphan, an INSERT. No network, file or gRPC I/O inside it. Uniqueness of the primary key on tracking_key is the real guard; the lock only makes the read-then-write non-racy.

Waiters share one broadcast, not an ordered queue: every waiter for a model wants the identical outcome, so ordering them would add fairness machinery that changes no result. Local waiters wake on a closed channel; a 2s DB poll is the authority, because a waiter on another replica has no channel to close and NATS broadcasts are fire-and-forget.

Liveness is a heartbeat, not a byte counter. The runner touches last_progress on a fixed 1s interval whether or not bytes move — a checkpoint load legitimately transfers zero bytes for many minutes, and a reaper keyed on byte movement would reclaim a healthy job mid-load. Byte progress stays the concern of load_deadline.go, which is untouched. A job whose heartbeat stops past the orphan window is reclaimable, so a replica killed mid-load cannot wedge a model permanently.

Caller contract

  • Within the wait budget, the request is served the moment the model is ready — no client retry. This is the common case for a model already most of the way staged.
  • A failure propagates the real cause (worker out of disk) to every waiter. The failed job row is retained briefly so a request arriving right after does not read "no job" as "not loading" and start a duplicate load of a model that just failed.
  • A client that disconnects returns immediately; the load keeps running. It is owned by the job record, not by the request.
  • On budget expiry: 503 with Retry-After and
{
  "error": { "message": "model Qwen3.6-27B-MTP-GGUF is staging on node nvidia-thor (41%, ETA ~11m)",
             "type": "model_loading", "code": "model_loading" },
  "loading": { "model": "Qwen3.6-27B-MTP-GGUF", "state": "staging", "node": "nvidia-thor",
               "progress": 41.2, "bytes_sent": 14730000000, "total_bytes": 35776484480,
               "file_index": 1, "total_files": 2, "eta_seconds": 660 }
}

The error envelope keeps OpenAI-client compatibility; loading is additive, so existing clients ignore it. eta_seconds comes from the job's own observed rate and is omitted, not guessed, until enough bytes have moved for that rate to mean anything — a confidently wrong ETA on a twenty-minute wait is worse than none. Retry-After is the ETA when known, clamped to [5s, 300s], and the wait budget otherwise.

New config key

LOCALAI_MODEL_LOAD_WAIT (default 60s) bounds how long a request waits for a running cold load. It bounds the caller, never the load — the job runs on either way. A held HTTP request cannot survive real infrastructure: an ingress or LB idle timeout kills a twenty-minute request regardless of what LocalAI does, so the answer should come from LocalAI with progress attached rather than from a proxy dropping the connection. LOCALAI_MODEL_LOAD_WAIT=0 restores unbounded waiting for deployments with no proxy in front. (Zero in the config struct still means "unset, use the default", so the CLI records the operator's explicit zero as ModelLoadWaitUnbounded rather than losing the distinction.)

New endpoint

GET /api/models/{id}/load-status → the same loading object, or 404 when no load is running. Deliberately neither admin- nor feature-gated: it explains a 503 the caller just received, and hiding that behind a per-modality feature would make the explanation for a failed image request depend on chat permissions. No MCP tool — there is nothing here an admin would manage conversationally.

UI

Chat renders a 503 with type: "model_loading" as inline progress (phase, node, percent, ETA), polls load-status, and re-sends the request once the model is ready. It reuses the staging progress idiom the page already had rather than inventing a second one; the load job wins over the /api/operations staging row because it is authoritative across replicas and names the phase, where the operation only knows about a byte transfer this replica happens to be performing.

Commits

  1. fix(advisorylock) — set statement_timeout alongside lock_timeout. Independently backportable; it fixes a latent bug for every blocking WithLockCtx caller (schema migration, reconciler, health check), not just model loads.
  2. feat(distributed) — the ModelLoadJob row + ClaimLoadJob.
  3. refactor(distributed) — move staging/load out of the lock into the job runner, with waiter fan-out.
  4. feat(distributed) — the wait budget and the 503 contract.
  5. feat(api)load-status + registration surfaces + docs.
  6. feat(ui) — Chat staging state and auto-retry.

Each builds and passes on its own.

Testing

New specs (Ginkgo + testcontainers PostgreSQL, and Playwright for the UI):

  • a waiter survives a short server-side statement_timeout instead of failing 57014 (mirrors the existing lock_timeout spec);
  • exactly one of eight concurrent claimers wins;
  • a claim returns in <100ms while another replica's job is running;
  • an orphaned job (heartbeat stopped) is reclaimable;
  • a concurrent request for a loading model is served from one load, with no duplicate install;
  • a load failure reaches every waiter with its real cause;
  • cancelling a waiter returns immediately and leaves the job running;
  • the runner heartbeats while no bytes move;
  • budget expiry yields a structured ModelLoadingError with Retry-After, and LOCALAI_MODEL_LOAD_WAIT=0 waits unbounded;
  • load-status 404s with no job and reports progress with one;
  • Chat renders the staging state on a 503 and auto-retries on ready (verified failing before the change).

Notes

  • pkg/model/loader.go wrapped the router's error with %s, flattening the chain; changed to %w so the typed loading error survives to the HTTP layer.
  • Non-distributed (no-DB) mode keeps the inline load byte-for-byte as it was.
  • load_deadline.go (the progress-extended cold-load hold) and file_stager_http.go are untouched.

🤖 Generated with Claude Code

mudler added 6 commits August 13, 2026 20:40
WithLockCtx already overrides a deployment-wide lock_timeout on its
dedicated connection so a blocking pg_advisory_lock() waits its turn
instead of failing with 55P03. statement_timeout aborts that exact same
statement independently, with SQLSTATE 57014, and was not overridden.

Production roles commonly carry statement_timeout=60s. Any guarded
section longer than that (a cold model load stages for tens of minutes)
therefore killed every concurrent waiter:

  advisorylock: acquiring lock 9003261067483446873: ERROR: canceling
  statement due to statement timeout (SQLSTATE 57014)

Derive it from the same context budget as lock_timeout, with a matching
RESET so the pooled connection is returned clean.

Assisted-by: Claude Opus 5 [claude-code]
A cold load in distributed mode is a long-running background job, but it
was modelled as a synchronous side effect of an inference request: the
whole of it (backend install, multi-GB staging, checkpoint load) ran
inside the per-model advisory lock. Loading a 35.7 GB GGUF held that lock
for ~20 minutes, so every concurrent request for the same model blocked
on pg_advisory_lock and died at the role's 60s statement_timeout.

Introduce the row that lets the lock shrink to a decision. Exactly one
ModelLoadJob may be active per tracking key; that uniqueness — not the
lifetime of a lock — is what de-duplicates concurrent loaders across
replicas. ClaimLoadJob does its read-then-write under the advisory lock
and nothing else: no network, file or gRPC I/O inside the guarded
section, so a claim costs milliseconds no matter how long the resulting
load takes.

LastProgress is a heartbeat rather than a byte counter. A checkpoint load
legitimately moves zero bytes for many minutes, so a reaper keyed on byte
movement would reclaim a healthy job mid-load; byte progress stays the
concern of load_deadline.go. A job whose heartbeat stops for longer than
the orphan window is reclaimable, so a replica killed mid-load cannot
wedge a model permanently.

Failed jobs keep their row for a short grace so an immediately-following
request reports the real cause instead of silently starting a fresh load
of a model that just failed.

No caller yet — the router moves onto this in the next commit.

Assisted-by: Claude Opus 5 [claude-code]
Route wrapped the entire cold load — node selection, backend install,
multi-GB staging and the remote LoadModel — in the per-model advisory
lock. The lock's job is to de-duplicate concurrent loaders, a decision
that takes milliseconds; holding it for the tens of minutes the resulting
work takes is what turned a dedup mechanism into a cluster-wide outage
for that model.

Split it into a claim and a run. The claim is the only thing left inside
the lock. The run is a background job owned by the claiming replica and
bounded by the same progress-extended deadline as before; every other
request for that model — local or on another replica — attaches as a
waiter and is served the moment the model is ready, with no duplicate
load and no lock contention.

Waiters share one broadcast rather than an ordered queue: they all want
the identical outcome, so ordering them would add fairness machinery that
changes no result. The local channel wakes same-replica waiters instantly
and a 2s DB poll is the authority, because a waiter on another replica
has no channel to close. On wake a waiter re-runs the warm path rather
than trusting the signal — the model may have been evicted in between.

A waiter whose client disconnects returns immediately and the job keeps
running; it belongs to the job record, not to the request. A failure is
recorded on the row so every waiter reports the real cause, and the row
survives briefly so the next request does not read "no job" as "not
loading" and start a duplicate load of a model that just failed.

The runner heartbeats the row on a fixed interval whether or not bytes
are moving, which is what keeps a legitimately silent checkpoint load
from being reclaimed as an orphan. Phase (installing/staging/loading) and
placement ride to the heartbeat on the context, the same seam
load_deadline.go already uses, so single-host paths are untouched.

Non-distributed mode (no DB) keeps the inline load exactly as it was.

Assisted-by: Claude Opus 5 [claude-code]
… progress

A request whose model is cold-loading now attaches to the running job and
is served the moment the model is ready. That wait has to be bounded: a
held HTTP request cannot survive real infrastructure, and an ingress or LB
idle timeout kills a twenty-minute request regardless of what LocalAI
does.

New LOCALAI_MODEL_LOAD_WAIT (default 60s) bounds the CALLER, never the
load — the job keeps running either way. On expiry the request gets 503
with Retry-After and a structured body naming the model, the node, the
phase, byte progress and an ETA. The `error` envelope keeps OpenAI
clients working; `loading` is additive so they ignore it.

The ETA comes from the job's own observed rate and is omitted rather than
guessed until enough bytes have moved for that rate to mean anything: a
confidently wrong ETA on a twenty-minute wait is worse than none.
Retry-After is that ETA when known, clamped to [5s, 300s], and the wait
budget otherwise.

LOCALAI_MODEL_LOAD_WAIT=0 waits unbounded, for deployments with no proxy
in front. Zero in the config struct still means "unset, use the default",
so the CLI records the operator's zero as ModelLoadWaitUnbounded rather
than losing the distinction.

The distributed branch of ModelLoader.loadModel wrapped the router's
error with %s, which flattened it to a string. Use %w: the typed error is
what the HTTP layer keys the 503 off.

Assisted-by: Claude Opus 5 [claude-code]
A client that receives 503 while a model stages onto a worker needs
somewhere to poll. This returns the same `loading` object the 503 carries
— phase, node, byte progress and ETA — or 404 when no load is running.

Read-only and observability-shaped, so it is deliberately neither
admin-gated nor feature-gated: it explains a 503 the caller just
received, and hiding that behind a per-modality feature would make the
explanation for a failed image request depend on chat permissions. It
also gets no MCP tool, since there is nothing here an admin would manage
conversationally.

Registered on the surfaces from .agents/api-endpoints-and-auth.md: the
swagger block (existing `models` tag, so /api/instructions needs no new
area), the endpoint discovery maps in RegisterLocalAIRoutes, regenerated
swagger, and the distributed-mode docs page. No FLAG_* usecase is
involved, so capabilities.js is unchanged.

Assisted-by: Claude Opus 5 [claude-code]
… ready

A chat request for a model that is still staging onto a worker now gets a
503 carrying live progress instead of an error. Render it: the composer
shows the phase (installing / staging / loading), the node, the percent
and the ETA, then polls load-status and re-sends the request the moment
the model is ready.

Reuses the staging progress idiom the page already had rather than
inventing a second one — the two sources are folded into one
loadProgress, with the load job winning because it is authoritative
across frontend replicas and knows the phase, where the staging operation
only knows about a byte transfer this replica happens to be performing.

Waiting is bounded (three send attempts, ~30 min of polling each), so a
load that never finishes still surfaces as an error rather than as a
spinner nobody questions. An aborted generation stops the polling too.

Assisted-by: Claude Opus 5 [claude-code]
Comment thread core/services/nodes/router.go Fixed
Comment thread core/services/nodes/router.go Fixed
Comment thread core/services/nodes/router.go Fixed
The router moved legacy cleanup calls onto newly linted lines. Report
cleanup failures while preserving the fallback to a cold load.

Assisted-by: Codex:gpt-5 [golangci-lint]
@mudler
mudler merged commit 88edd7f into master Aug 15, 2026
70 of 71 checks passed
@mudler
mudler deleted the feat/distributed-cold-load-jobs branch August 15, 2026 11:20
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.

4 participants