mirror of
https://github.com/mudler/LocalAI.git
synced 2026-09-13 06:45:26 -04:00
* fix(advisorylock): set statement_timeout alongside lock_timeout
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]
* feat(distributed): add ModelLoadJob, the durable cold-load record
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]
* refactor(distributed): run cold loads as jobs, outside the advisory lock
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]
* feat(distributed): bound the wait for a loading model and answer with 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]
* feat(api): add GET /api/models/{id}/load-status
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]
* feat(ui): show cold-load progress in Chat and retry when the model is 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]
* fix(distributed): check warm-path cleanup errors
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]
---------
Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
Co-authored-by: localai-org-maint-bot <306269227+localai-org-maint-bot@users.noreply.github.com>
531 lines
22 KiB
Go
531 lines
22 KiB
Go
package config
|
|
|
|
import (
|
|
"cmp"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/mudler/LocalAI/core/services/messaging"
|
|
"github.com/mudler/LocalAI/pkg/natsauth"
|
|
"github.com/mudler/xlog"
|
|
)
|
|
|
|
// DistributedConfig holds configuration for horizontal scaling mode.
|
|
// When Enabled is true, PostgreSQL and NATS are required.
|
|
type DistributedConfig struct {
|
|
Enabled bool // --distributed / LOCALAI_DISTRIBUTED
|
|
InstanceID string // --instance-id / LOCALAI_INSTANCE_ID (auto-generated UUID if empty)
|
|
NatsURL string // --nats-url / LOCALAI_NATS_URL
|
|
StorageURL string // --storage-url / LOCALAI_STORAGE_URL (S3 endpoint)
|
|
RegistrationToken string // --registration-token / LOCALAI_REGISTRATION_TOKEN (required token for node registration)
|
|
// RegistrationRequireAuth fails startup when distributed mode is enabled but
|
|
// RegistrationToken is empty. The default (false) keeps the historical
|
|
// fail-open behavior with a loud warning; production should set it so the
|
|
// node-register endpoints and the worker file-transfer server cannot run
|
|
// unauthenticated. Mirrors NatsRequireAuth for the NATS bus.
|
|
RegistrationRequireAuth bool // LOCALAI_REGISTRATION_REQUIRE_AUTH
|
|
// RequireAuth is the umbrella switch (LOCALAI_DISTRIBUTED_REQUIRE_AUTH) for
|
|
// distributed-mode auth: when true it implies BOTH NatsRequireAuth and
|
|
// RegistrationRequireAuth, so a single knob locks down the bus and the
|
|
// registration/file-transfer layer together. The granular flags remain
|
|
// available to enforce just one layer.
|
|
RequireAuth bool // LOCALAI_DISTRIBUTED_REQUIRE_AUTH
|
|
AutoApproveNodes bool // --auto-approve-nodes / LOCALAI_AUTO_APPROVE_NODES (skip admin approval for new workers)
|
|
// SharedModels asserts that every node (frontend and workers) mounts the
|
|
// SAME models directory at the SAME path (e.g. a shared volume, as in
|
|
// docker-compose.distributed.yaml). When true, the router skips staging
|
|
// model files to workers entirely: the frontend's absolute model paths are
|
|
// already valid on the worker, so re-uploading them into a per-model
|
|
// subdirectory only re-downloads what is already present (#10556). Default
|
|
// false preserves the historical per-node staging behavior.
|
|
SharedModels bool // --distributed-shared-models / LOCALAI_DISTRIBUTED_SHARED_MODELS
|
|
|
|
// NATS JWT auth (optional; see pkg/natsauth and docs/features/distributed-mode.md)
|
|
NatsAccountSeed string // LOCALAI_NATS_ACCOUNT_SEED — account signing seed to mint per-node worker JWTs
|
|
NatsServiceJWT string // LOCALAI_NATS_SERVICE_JWT — user JWT for frontends / agent workers
|
|
NatsServiceSeed string // LOCALAI_NATS_SERVICE_SEED — signing seed paired with service JWT
|
|
NatsWorkerJWTTTL time.Duration // LOCALAI_NATS_WORKER_JWT_TTL — minted worker JWT lifetime (default 24h)
|
|
NatsRequireAuth bool // LOCALAI_NATS_REQUIRE_AUTH — fail startup if NATS credentials are missing
|
|
NatsTLSCA string // LOCALAI_NATS_TLS_CA — PEM file for private CA (server verify)
|
|
NatsTLSCert string // LOCALAI_NATS_TLS_CERT — client cert for NATS mTLS
|
|
NatsTLSKey string // LOCALAI_NATS_TLS_KEY — client key paired with NatsTLSCert
|
|
|
|
// S3 configuration (used when StorageURL is set)
|
|
StorageBucket string // --storage-bucket / LOCALAI_STORAGE_BUCKET
|
|
StorageRegion string // --storage-region / LOCALAI_STORAGE_REGION
|
|
StorageAccessKey string // --storage-access-key / LOCALAI_STORAGE_ACCESS_KEY
|
|
StorageSecretKey string // --storage-secret-key / LOCALAI_STORAGE_SECRET_KEY
|
|
|
|
// Timeout configuration (all have sensible defaults — zero means use default)
|
|
MCPToolTimeout time.Duration // MCP tool execution timeout (default 360s)
|
|
MCPDiscoveryTimeout time.Duration // MCP discovery timeout (default 60s)
|
|
WorkerWaitTimeout time.Duration // Max wait for healthy worker at startup (default 5m)
|
|
DrainTimeout time.Duration // Time to wait for in-flight requests during drain (default 30s)
|
|
HealthCheckInterval time.Duration // Health monitor check interval (default 15s)
|
|
StaleNodeThreshold time.Duration // Time before a node is considered stale (default 60s)
|
|
// DisablePerModelHealthCheck turns off the health monitor's per-model
|
|
// gRPC probe. When enabled (the default), the monitor pings each model's
|
|
// gRPC address and removes stale node_models rows whose backend has
|
|
// crashed even though the worker's node-level heartbeat is still arriving.
|
|
// Without per-model probing, /embeddings and /completions can be dispatched
|
|
// to a backend that silently returns garbage (see also the cascading
|
|
// model-row cleanup on MarkUnhealthy / MarkDraining).
|
|
DisablePerModelHealthCheck bool
|
|
|
|
MCPCIJobTimeout time.Duration // MCP CI job execution timeout (default 10m)
|
|
|
|
BackendInstallTimeout time.Duration // NATS round-trip timeout for backend.install (default 15m)
|
|
BackendUpgradeTimeout time.Duration // NATS round-trip timeout for backend.upgrade (default 15m)
|
|
// ModelLoadTimeout is the gRPC deadline for the remote LoadModel call the
|
|
// router issues once a worker has the backend installed and the model files
|
|
// staged. It therefore covers only the backend's own checkpoint load and
|
|
// pipeline init, which for a multi-tens-of-GB diffusion/video checkpoint on
|
|
// unified memory can far exceed the 5m default.
|
|
ModelLoadTimeout time.Duration // gRPC deadline for remote LoadModel (default 5m)
|
|
// ModelLoadWait bounds how long an inference request waits for a model that
|
|
// is being cold-loaded before it is answered with 503 plus live progress. A
|
|
// held HTTP request cannot survive real infrastructure — an ingress or LB
|
|
// idle timeout kills a 20-minute request regardless of what LocalAI does —
|
|
// so the wait is bounded by default.
|
|
//
|
|
// Zero means unset (DefaultModelLoadWait applies); ModelLoadWaitUnbounded
|
|
// records the operator asking for unbounded waiting with
|
|
// LOCALAI_MODEL_LOAD_WAIT=0.
|
|
ModelLoadWait time.Duration
|
|
|
|
MaxUploadSize int64 // Maximum upload body size in bytes (default 50 GB)
|
|
|
|
AgentWorkerConcurrency int `yaml:"agent_worker_concurrency" json:"agent_worker_concurrency" env:"LOCALAI_AGENT_WORKER_CONCURRENCY"`
|
|
JobWorkerConcurrency int `yaml:"job_worker_concurrency" json:"job_worker_concurrency" env:"LOCALAI_JOB_WORKER_CONCURRENCY"`
|
|
|
|
// DiskHeadroomDisabled turns off the scheduler's free-disk admission check,
|
|
// restoring the pre-#11054 behaviour where node selection ignores whether a
|
|
// node can actually store the model. The check is ON by default because it
|
|
// prevents a measured failure (a node with 0 bytes free accepted a 70GB
|
|
// model and failed 16 minutes into staging); this is the escape hatch for
|
|
// setups where our size estimate is wrong (deduplicating filesystems, a
|
|
// worker that fetches its own weights), not the norm.
|
|
//
|
|
// Disabling does NOT silence the check: it still runs and warns when it
|
|
// would have rejected every node, so the operator keeps the diagnosis
|
|
// without being blocked. See SmartRouter.scheduleNewModel.
|
|
//
|
|
// Stored as the negation of the CLI/runtime flag so the zero value is
|
|
// "enabled" (mirrors PrefixCacheDisabled).
|
|
DiskHeadroomDisabled bool
|
|
|
|
// PrefixCacheDisabled turns off prefix-cache-aware routing, falling back to
|
|
// round-robin (the floor). Prefix-cache routing is ON by default in
|
|
// distributed mode; this flag exists so operators can opt out. The CLI
|
|
// surfaces a default-true --distributed-prefix-cache enable flag and sets
|
|
// this when the operator passes --distributed-prefix-cache=false.
|
|
PrefixCacheDisabled bool
|
|
// PrefixCacheTTL is the idle-timeout for prefix-cache index entries and
|
|
// drives the background eviction cadence (eviction runs every TTL/2). Zero
|
|
// means use the prefixcache package default (5m).
|
|
PrefixCacheTTL time.Duration
|
|
// ModelSchedulingJSON is an inline JSON list of per-model scheduling configs
|
|
// applied authoritatively at startup (LOCALAI_MODEL_SCHEDULING).
|
|
ModelSchedulingJSON string
|
|
// ModelSchedulingConfigPath is a path to a YAML file with the same list
|
|
// (LOCALAI_MODEL_SCHEDULING_CONFIG).
|
|
ModelSchedulingConfigPath string
|
|
}
|
|
|
|
// Validate checks that the distributed configuration is internally consistent.
|
|
// It returns nil if distributed mode is disabled.
|
|
func (c DistributedConfig) Validate() error {
|
|
if !c.Enabled {
|
|
return nil
|
|
}
|
|
if c.NatsURL == "" {
|
|
return fmt.Errorf("distributed mode requires --nats-url / LOCALAI_NATS_URL")
|
|
}
|
|
// S3 credentials must be paired
|
|
if (c.StorageAccessKey != "" && c.StorageSecretKey == "") ||
|
|
(c.StorageAccessKey == "" && c.StorageSecretKey != "") {
|
|
return fmt.Errorf("storage-access-key and storage-secret-key must both be set or both empty")
|
|
}
|
|
// The registration token guards both the node HTTP register/heartbeat
|
|
// endpoints and the worker file-transfer server (which fails open on an
|
|
// empty token). Enforce it when registration auth is required (the granular
|
|
// flag or the umbrella); otherwise warn.
|
|
if c.RegistrationToken == "" {
|
|
if c.RegistrationAuthRequired() {
|
|
return fmt.Errorf("registration auth is required (LOCALAI_REGISTRATION_REQUIRE_AUTH or LOCALAI_DISTRIBUTED_REQUIRE_AUTH) but LOCALAI_REGISTRATION_TOKEN is empty")
|
|
}
|
|
xlog.Warn("distributed mode running without registration token — node endpoints and the worker file-transfer server are unprotected; set LOCALAI_REGISTRATION_TOKEN, or LOCALAI_DISTRIBUTED_REQUIRE_AUTH=true to fail closed")
|
|
}
|
|
if err := c.NatsAuthConfig().Validate(); err != nil {
|
|
return err
|
|
}
|
|
if err := c.NatsTLSFiles().Validate(); err != nil {
|
|
return err
|
|
}
|
|
c.NatsAuthConfig().WarnIfInsecure(true)
|
|
// Check for negative durations
|
|
for name, d := range map[string]time.Duration{
|
|
FlagMCPToolTimeout: c.MCPToolTimeout,
|
|
FlagMCPDiscoveryTimeout: c.MCPDiscoveryTimeout,
|
|
FlagWorkerWaitTimeout: c.WorkerWaitTimeout,
|
|
FlagDrainTimeout: c.DrainTimeout,
|
|
FlagHealthCheckInterval: c.HealthCheckInterval,
|
|
FlagStaleNodeThreshold: c.StaleNodeThreshold,
|
|
FlagMCPCIJobTimeout: c.MCPCIJobTimeout,
|
|
FlagBackendInstallTimeout: c.BackendInstallTimeout,
|
|
FlagBackendUpgradeTimeout: c.BackendUpgradeTimeout,
|
|
FlagModelLoadTimeout: c.ModelLoadTimeout,
|
|
} {
|
|
if d < 0 {
|
|
return fmt.Errorf("%s must not be negative", name)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Distributed config options
|
|
|
|
var EnableDistributed = func(o *ApplicationConfig) {
|
|
o.Distributed.Enabled = true
|
|
}
|
|
|
|
func WithDistributedInstanceID(id string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.InstanceID = id
|
|
}
|
|
}
|
|
|
|
func WithNatsURL(url string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.NatsURL = url
|
|
}
|
|
}
|
|
|
|
func WithRegistrationToken(token string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.RegistrationToken = token
|
|
}
|
|
}
|
|
|
|
func WithNatsAccountSeed(seed string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.NatsAccountSeed = seed
|
|
}
|
|
}
|
|
|
|
func WithNatsServiceJWT(jwt string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.NatsServiceJWT = jwt
|
|
}
|
|
}
|
|
|
|
func WithNatsServiceSeed(seed string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.NatsServiceSeed = seed
|
|
}
|
|
}
|
|
|
|
func WithNatsWorkerJWTTTL(d time.Duration) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.NatsWorkerJWTTTL = d
|
|
}
|
|
}
|
|
|
|
var EnableNatsRequireAuth = func(o *ApplicationConfig) {
|
|
o.Distributed.NatsRequireAuth = true
|
|
}
|
|
|
|
// EnableRegistrationRequireAuth makes an empty registration token a hard error
|
|
// in distributed mode (see DistributedConfig.RegistrationRequireAuth).
|
|
var EnableRegistrationRequireAuth = func(o *ApplicationConfig) {
|
|
o.Distributed.RegistrationRequireAuth = true
|
|
}
|
|
|
|
// EnableDistributedRequireAuth is the umbrella switch implying both
|
|
// NatsRequireAuth and RegistrationRequireAuth (see DistributedConfig.RequireAuth).
|
|
var EnableDistributedRequireAuth = func(o *ApplicationConfig) {
|
|
o.Distributed.RequireAuth = true
|
|
}
|
|
|
|
// RegistrationAuthRequired reports whether an empty registration token must be
|
|
// treated as a fatal misconfiguration — the granular flag or the umbrella.
|
|
func (c DistributedConfig) RegistrationAuthRequired() bool {
|
|
return c.RegistrationRequireAuth || c.RequireAuth
|
|
}
|
|
|
|
// NatsAuthRequired reports whether NATS JWT credentials must be present — the
|
|
// granular flag or the umbrella.
|
|
func (c DistributedConfig) NatsAuthRequired() bool {
|
|
return c.NatsRequireAuth || c.RequireAuth
|
|
}
|
|
|
|
func WithNatsTLSCA(path string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.NatsTLSCA = path
|
|
}
|
|
}
|
|
|
|
func WithNatsTLSCert(path string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.NatsTLSCert = path
|
|
}
|
|
}
|
|
|
|
func WithNatsTLSKey(path string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.NatsTLSKey = path
|
|
}
|
|
}
|
|
|
|
func WithStorageURL(url string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.StorageURL = url
|
|
}
|
|
}
|
|
|
|
func WithStorageBucket(bucket string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.StorageBucket = bucket
|
|
}
|
|
}
|
|
|
|
func WithStorageRegion(region string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.StorageRegion = region
|
|
}
|
|
}
|
|
|
|
func WithStorageAccessKey(key string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.StorageAccessKey = key
|
|
}
|
|
}
|
|
|
|
func WithStorageSecretKey(key string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.StorageSecretKey = key
|
|
}
|
|
}
|
|
|
|
func WithBackendInstallTimeout(d time.Duration) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.BackendInstallTimeout = d
|
|
}
|
|
}
|
|
|
|
func WithBackendUpgradeTimeout(d time.Duration) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.BackendUpgradeTimeout = d
|
|
}
|
|
}
|
|
|
|
func WithModelLoadTimeout(d time.Duration) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.ModelLoadTimeout = d
|
|
}
|
|
}
|
|
|
|
// WithModelLoadWait sets how long a request waits for a cold-loading model. A
|
|
// zero d records the operator asking for unbounded waiting: "set the knob to
|
|
// zero" cannot sensibly mean "use the default".
|
|
func WithModelLoadWait(d time.Duration) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
if d == 0 {
|
|
d = ModelLoadWaitUnbounded
|
|
}
|
|
o.Distributed.ModelLoadWait = d
|
|
}
|
|
}
|
|
|
|
var EnableAutoApproveNodes = func(o *ApplicationConfig) {
|
|
o.Distributed.AutoApproveNodes = true
|
|
}
|
|
|
|
// EnableDistributedSharedModels marks the cluster as sharing one models
|
|
// directory across all nodes, so the router skips staging model files to
|
|
// workers (see DistributedConfig.SharedModels).
|
|
var EnableDistributedSharedModels = func(o *ApplicationConfig) {
|
|
o.Distributed.SharedModels = true
|
|
}
|
|
|
|
// DisableDiskHeadroomCheck turns off the scheduler's free-disk admission
|
|
// check (see DistributedConfig.DiskHeadroomDisabled). The check is enabled by
|
|
// default in distributed mode.
|
|
var DisableDiskHeadroomCheck = func(o *ApplicationConfig) {
|
|
o.Distributed.DiskHeadroomDisabled = true
|
|
}
|
|
|
|
// DisablePrefixCache turns off prefix-cache-aware routing (falls back to
|
|
// round-robin). Prefix-cache routing is enabled by default in distributed mode.
|
|
var DisablePrefixCache = func(o *ApplicationConfig) {
|
|
o.Distributed.PrefixCacheDisabled = true
|
|
}
|
|
|
|
// WithPrefixCacheTTL sets the prefix-cache index idle-timeout (and the
|
|
// background eviction cadence, which runs every TTL/2).
|
|
func WithPrefixCacheTTL(d time.Duration) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.PrefixCacheTTL = d
|
|
}
|
|
}
|
|
|
|
// WithModelSchedulingJSON sets the inline-JSON declarative scheduling config.
|
|
func WithModelSchedulingJSON(s string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.ModelSchedulingJSON = s
|
|
}
|
|
}
|
|
|
|
// WithModelSchedulingConfigPath sets the path to a YAML declarative scheduling
|
|
// config file.
|
|
func WithModelSchedulingConfigPath(path string) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.ModelSchedulingConfigPath = path
|
|
}
|
|
}
|
|
|
|
// Flag names for distributed timeout / interval configuration. These are
|
|
// the kebab-case identifiers kong derives from the matching RunCMD struct
|
|
// fields; they appear in Validate error messages and any other operator-
|
|
// facing surface that needs to reference a specific knob by name. Keeping
|
|
// them as constants prevents the string from drifting from the actual
|
|
// flag a future rename would produce.
|
|
const (
|
|
FlagMCPToolTimeout = "mcp-tool-timeout"
|
|
FlagMCPDiscoveryTimeout = "mcp-discovery-timeout"
|
|
FlagWorkerWaitTimeout = "worker-wait-timeout"
|
|
FlagDrainTimeout = "drain-timeout"
|
|
FlagHealthCheckInterval = "health-check-interval"
|
|
FlagStaleNodeThreshold = "stale-node-threshold"
|
|
FlagMCPCIJobTimeout = "mcp-ci-job-timeout"
|
|
FlagBackendInstallTimeout = "backend-install-timeout"
|
|
FlagBackendUpgradeTimeout = "backend-upgrade-timeout"
|
|
FlagModelLoadTimeout = "model-load-timeout"
|
|
FlagModelLoadWait = "model-load-wait"
|
|
// FlagDiskHeadroomCheck names the disk-headroom toggle. It is quoted in
|
|
// the warning the check emits while disabled, so the operator reading a
|
|
// log line knows exactly which knob produced it.
|
|
FlagDiskHeadroomCheck = "distributed-disk-headroom-check"
|
|
)
|
|
|
|
// Defaults for distributed timeouts.
|
|
const (
|
|
DefaultMCPToolTimeout = 360 * time.Second
|
|
DefaultMCPDiscoveryTimeout = 60 * time.Second
|
|
DefaultWorkerWaitTimeout = 5 * time.Minute
|
|
DefaultDrainTimeout = 30 * time.Second
|
|
DefaultHealthCheckInterval = 15 * time.Second
|
|
DefaultStaleNodeThreshold = 60 * time.Second
|
|
DefaultMCPCIJobTimeout = 10 * time.Minute
|
|
DefaultBackendInstallTimeout = 15 * time.Minute
|
|
DefaultBackendUpgradeTimeout = 15 * time.Minute
|
|
DefaultModelLoadTimeout = 5 * time.Minute
|
|
// DefaultModelLoadWait is how long a request waits for a cold-loading model
|
|
// before it is answered with 503 and live progress. Chosen to sit under the
|
|
// idle timeout of typical ingress/LB defaults, so the answer comes from
|
|
// LocalAI (with progress the client can act on) rather than from a proxy
|
|
// dropping the connection.
|
|
DefaultModelLoadWait = 60 * time.Second
|
|
)
|
|
|
|
// ModelLoadWaitUnbounded records LOCALAI_MODEL_LOAD_WAIT=0 — "wait as long as
|
|
// it takes" — which a plain zero cannot express, since zero also means "unset,
|
|
// use the default". Only deployments with no proxy in front should use it.
|
|
const ModelLoadWaitUnbounded = -1 * time.Second
|
|
|
|
// DefaultMaxUploadSize is the default maximum upload body size (50 GB).
|
|
const DefaultMaxUploadSize int64 = 50 << 30
|
|
|
|
// NatsTLSFiles returns NATS TLS/mTLS PEM paths for the messaging client.
|
|
func (c DistributedConfig) NatsTLSFiles() messaging.TLSFiles {
|
|
return messaging.TLSFiles{
|
|
CA: c.NatsTLSCA,
|
|
Cert: c.NatsTLSCert,
|
|
Key: c.NatsTLSKey,
|
|
}
|
|
}
|
|
|
|
// NatsMessagingOptions builds messaging client options (JWT + TLS) for distributed components.
|
|
// Pass explicit userJWT/userSeed when set (e.g. worker overrides); empty uses service JWT from config.
|
|
func (c DistributedConfig) NatsMessagingOptions(userJWT, userSeed string) []messaging.Option {
|
|
var opts []messaging.Option
|
|
jwt, seed := userJWT, userSeed
|
|
if jwt == "" && seed == "" {
|
|
auth := c.NatsAuthConfig()
|
|
jwt, seed = auth.ServiceUserJWT, auth.ServiceUserSeed
|
|
}
|
|
if jwt != "" && seed != "" {
|
|
opts = append(opts, messaging.WithUserJWT(jwt, seed))
|
|
}
|
|
if tls := c.NatsTLSFiles(); tls.Enabled() {
|
|
opts = append(opts, messaging.WithTLS(tls))
|
|
}
|
|
return opts
|
|
}
|
|
|
|
// NatsAuthConfig builds pkg/natsauth settings from distributed configuration.
|
|
func (c DistributedConfig) NatsAuthConfig() natsauth.Config {
|
|
return natsauth.Config{
|
|
AccountSeed: c.NatsAccountSeed,
|
|
ServiceUserJWT: c.NatsServiceJWT,
|
|
ServiceUserSeed: c.NatsServiceSeed,
|
|
WorkerJWTTTL: c.NatsWorkerJWTTTL,
|
|
RequireAuth: c.NatsAuthRequired(),
|
|
}
|
|
}
|
|
|
|
// BackendInstallTimeoutOrDefault returns the configured timeout or the default.
|
|
func (c DistributedConfig) BackendInstallTimeoutOrDefault() time.Duration {
|
|
return cmp.Or(c.BackendInstallTimeout, DefaultBackendInstallTimeout)
|
|
}
|
|
|
|
// BackendUpgradeTimeoutOrDefault returns the configured timeout or the default.
|
|
func (c DistributedConfig) BackendUpgradeTimeoutOrDefault() time.Duration {
|
|
return cmp.Or(c.BackendUpgradeTimeout, DefaultBackendUpgradeTimeout)
|
|
}
|
|
|
|
// ModelLoadTimeoutOrDefault returns the configured timeout or the default.
|
|
func (c DistributedConfig) ModelLoadTimeoutOrDefault() time.Duration {
|
|
return cmp.Or(c.ModelLoadTimeout, DefaultModelLoadTimeout)
|
|
}
|
|
|
|
// MCPToolTimeoutOrDefault returns the configured timeout or the default.
|
|
func (c DistributedConfig) MCPToolTimeoutOrDefault() time.Duration {
|
|
return cmp.Or(c.MCPToolTimeout, DefaultMCPToolTimeout)
|
|
}
|
|
|
|
// MCPDiscoveryTimeoutOrDefault returns the configured timeout or the default.
|
|
func (c DistributedConfig) MCPDiscoveryTimeoutOrDefault() time.Duration {
|
|
return cmp.Or(c.MCPDiscoveryTimeout, DefaultMCPDiscoveryTimeout)
|
|
}
|
|
|
|
// WorkerWaitTimeoutOrDefault returns the configured timeout or the default.
|
|
func (c DistributedConfig) WorkerWaitTimeoutOrDefault() time.Duration {
|
|
return cmp.Or(c.WorkerWaitTimeout, DefaultWorkerWaitTimeout)
|
|
}
|
|
|
|
// DrainTimeoutOrDefault returns the configured timeout or the default.
|
|
func (c DistributedConfig) DrainTimeoutOrDefault() time.Duration {
|
|
return cmp.Or(c.DrainTimeout, DefaultDrainTimeout)
|
|
}
|
|
|
|
// HealthCheckIntervalOrDefault returns the configured interval or the default.
|
|
func (c DistributedConfig) HealthCheckIntervalOrDefault() time.Duration {
|
|
return cmp.Or(c.HealthCheckInterval, DefaultHealthCheckInterval)
|
|
}
|
|
|
|
// StaleNodeThresholdOrDefault returns the configured threshold or the default.
|
|
func (c DistributedConfig) StaleNodeThresholdOrDefault() time.Duration {
|
|
return cmp.Or(c.StaleNodeThreshold, DefaultStaleNodeThreshold)
|
|
}
|
|
|
|
// MCPCIJobTimeoutOrDefault returns the configured MCP CI job timeout or the default.
|
|
func (c DistributedConfig) MCPCIJobTimeoutOrDefault() time.Duration {
|
|
return cmp.Or(c.MCPCIJobTimeout, DefaultMCPCIJobTimeout)
|
|
}
|
|
|
|
// MaxUploadSizeOrDefault returns the configured max upload size or the default.
|
|
func (c DistributedConfig) MaxUploadSizeOrDefault() int64 {
|
|
return cmp.Or(c.MaxUploadSize, DefaultMaxUploadSize)
|
|
}
|