mirror of
https://github.com/mudler/LocalAI.git
synced 2026-09-12 22:33:54 -04:00
* fix(distributed): evict only when a node is known to be full scheduleNewModel asked the registry for a free replica slot and treated every error as "this node is full", so a control-plane database slow enough to time out the lookup evicted a healthy loaded model. The evicted process died, a peer frontend still holding its address dialled the dead port and retried, and the model thrashed between nodes. The comment on the branch already said it meant a full node; the code never tested for it. Evict only on ErrNoFreeSlot. Any other error now returns and names the lookup that failed, so a slow database degrades into a diagnosable load failure instead of into lost work. An audit of the rest of the router found one branch of the same shape: node selection discarded the error from its last-resort finder, so a database timeout there also produced a nil node and evicted for it. That path now returns unless the finder said gorm.ErrRecordNotFound, which is the only answer that means the cluster had no node to give. No other destructive branch in router.go fires on a generic error. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(distributed): checkpoint heartbeat writes instead of writing every beat Every heartbeat UPDATEd backend_nodes. Six nodes at a ten second beat is roughly 52,000 writes a day against a six-row table, and that churn is what turned a blocked autovacuum into a 460 MB table whose six-row scan cost 867 ms and timed out the queries that place models. A beat carrying only a fresher timestamp now waits for the checkpoint interval. Each reported field is compared against the value last persisted rather than tested for presence, because a worker sends its disk figures on every beat and presence alone would suppress nothing. A node's first beat, a changed total VRAM, total disk or GPU vendor, and a free VRAM, RAM or disk reading that has moved more than 256 MiB from the persisted value all still write at once. A node that is not active is never suppressed, because it recovers only when the health monitor sees a fresh timestamp. The persisted column is up to one interval stale by design, so the stale-node threshold moves from 60s to 5m to cover it. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(distributed): fail worker readiness when a held backend is unreachable The readiness gate tracked only the NATS link, so a worker whose backend processes had died still answered /readyz with 200 and kept receiving loads. One node did exactly that during an incident: it reported healthy while its backend port refused connections, and every load routed to it failed. Readiness is now the NATS link and, for each backend process the worker believes it is running, a short dial of its recorded address. A worker holding no backends stays ready, because idle is a healthy state. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(distributed): keep a starting backend out of the readiness dial set A backend process is inserted into the supervisor map with its gRPC address already recorded, but the address refuses connections until the gRPC server binds, which the startup poll allows up to 30 seconds for and which takes 10 to 15 seconds on a slow node. The new data-path readiness probe dialled that address straight away, so a worker answered /readyz with 503 for the whole of every cold backend start. The container HEALTHCHECK absorbs that, but a Kubernetes readinessProbe at 10s does not, and the worker would leave rotation each time it loaded a model. The skip for a stopping process had no counterpart at the other end of the lifecycle. Backend processes now carry a serving flag, set where the startup health-check gate succeeds, and the probe dials only processes that are serving and not yet stopping. backendStartStillValid becomes markBackendServing: the check and the mark must share one lock hold, so the flag can only ever land on the entry the key currently owns. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(distributed): export control-plane database health gauges Four transactions wedged on a corrupt index held the vacuum horizon open for 42 days. Nothing measured it, so the first symptom anyone saw was models failing to load six weeks later, by which time a six-row table had grown to 460 MB. Export the oldest xmin age, the longest open transaction, and the dead tuple ratio on the registry tables. The first is the number that would have caught it: it sits near zero in health and was 21,002,291. Sampling is scrape-driven behind a cache, and a failed sample reports the last good values rather than failing the scrape, because these gauges matter most when the database is already struggling. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(distributed): rate-limit failed control-plane database samples The cache advanced its clock only on a successful sample, so once the database started failing every scrape retried the query immediately. That turned the cache off in the one regime it exists for: a retry storm at scrape cadence aimed at a database already in trouble. A catalog read that consistently exceeds the 5 second timeout also paid that cost on every scrape, with all scrapes serialised behind the sampler mutex. Time every attempt rather than every success, so failures and timeouts cost the same interval as good samples. Whether a good sample exists moves to its own field, keeping the gauges absent until the first success and holding the last good values through later failures. Also note in the runbook that pg_stat_activity cannot see prepared transactions or replication slot xmins, so a healthy-looking xmin age does not by itself rule out a blocked horizon. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(distributed): pin that a failing database evicts nothing Exercises the real distributed stack against a control-plane database that refuses the router's slot lookup, and asserts the scheduler reports the lookup it could not answer instead of falling through to eviction. The failure is injected with privileges rather than a statement timeout. A timeout set with ALTER DATABASE also breaks AutoMigrate, and it leaks into every later spec in the suite unless it is reset, so the spec would end up testing the migration rather than the scheduler. Instead the spec creates a dedicated login role, points a second gorm handle at it, and revokes that role's SELECT on node_models.replica_index. This has to be a separate role: the test container's owner is a PostgreSQL superuser, and superusers bypass every privilege check, so revoking from CURRENT_USER is recorded and then ignored. The revoke is scoped to one column on purpose. Revoking the whole table would also blind node selection, which runs first and has a guard of its own, so the scheduler would never reach the slot lookup this spec is about. Leaving every other column readable lets selection succeed and lands the refusal exactly on NextFreeReplicaIndex, which plucks replica_index. The grant is restored from BeforeEach via DeferCleanup, so a failing assertion or a panic cannot hand the next spec a role that cannot read. Reverting the eviction guard fails this spec, which is the point of it: the router then reports "no replica slot on keeper and eviction failed" for an error that was never evidence the node was full. The surviving-row assertions are secondary under this injection, because the eviction path reads whole node_models rows and the same revoke blinds it too; a comment in the spec says so, so nobody mistakes them for the load-bearing ones. Also documents why the vector store and the control plane must not share a database: the removable-tuple cutoff is per database, not per table, so one transaction left open anywhere stops autovacuum reclaiming the node registry, and a six-row table bloats into hundreds of megabytes. The note names LOCALAI_AUTH_DATABASE_URL and LOCALAI_AGENT_POOL_DATABASE_URL as the two knobs that must differ, and the localai_control_plane_oldest_xmin_age gauge as the way to see it coming. grep for StaleNodeThreshold and HealthCheckInterval in core/config/runtime_settings_registry.go returns no matches: the distributed duration knobs are not exposed as runtime settings, so the new heartbeat checkpoint interval follows them and needs no registry entry. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(distributed): close the review gaps in the heartbeat and health path The stale-node threshold moved from 60 seconds to 5 minutes in this branch because checkpointing makes last_heartbeat up to one checkpoint interval behind by design. Two things were left inconsistent with that. NewHealthMonitor still fell back to a hardcoded 60 seconds when handed a zero threshold, so any future caller that stopped passing the configured value would mark every healthy, beating node offline on every cycle. And the threshold itself had a flag-name constant but no AppOption, no CLI field and no env binding, so an operator who widened --node-heartbeat-checkpoint had no way to widen the threshold to match. The fallback now tracks config.DefaultStaleNodeThreshold, and --stale-node-threshold / LOCALAI_STALE_NODE_THRESHOLD is wired the same way its sibling is. Heartbeat suppression compared the RAW reported free VRAM against the snapshot, but the column persists capAvailable(raw, ceiling). On any node with a VRAM budget set, whose actual free VRAM oscillates above that ceiling, every beat looked material while the persisted value never moved: suppression was defeated on exactly the nodes an operator had configured, and the write amplification this branch exists to remove came straight back there. The comparison and the snapshot now both hold the capped figure, so they measure the same quantity as the column. Fixing that needs the ceiling, and reading it cost a SELECT on every beat, including suppressed ones. The skip decision therefore moved ahead of the updates map and now reuses the ceiling cached on the last durable write, while the write path still re-reads it before capping anything. A ceiling that changed inside the checkpoint window can cost one extra or one late write; it cannot persist a wrong figure. A suppressed beat now costs no query at all. Also: the operations section now says to grant pg_read_all_stats to the LocalAI role, because PostgreSQL blanks backend_xmin and xact_start for sessions owned by other roles, and the transaction that wedged the horizon in the incident was a co-located vector store connecting as a different role, so without the grant the new gauge sees only our own sessions. The compose healthcheck comment now describes readiness covering the backend data path, and the control-plane gauge registration records the otel.SetMeterProvider ordering it depends on. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(distributed): resolve the gauge's table names through gorm The dead-tuple gauge queried pg_stat_user_tables against a hardcoded list of three table names. Those three do not agree on where their name comes from: BackendNode and NodeModel take gorm's default pluralisation, while GalleryOperationRecord overrides TableName, and gallery_operations already had a constant of its own that the list duplicated. A literal list keeps compiling after any of that moves, and the query then matches nothing. The failure is silent and it points the wrong way: a dead-tuple ratio that matched no rows reports the same numbers as a cluster with no bloat, so the gauge would look healthiest exactly when it had stopped working. Ask gorm what each model is stored as instead, which follows a TableName override and the default pluralisation alike. A spec pins that the override really is consulted: naive pluralisation of the type would give gallery_operation_records, so the resolution cannot quietly stop asking the model. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
569 lines
24 KiB
Go
569 lines
24 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 5m)
|
|
NodeHeartbeatCheckpoint time.Duration // Minimum gap between durable heartbeat writes (default 60s, 0 = every beat)
|
|
// 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,
|
|
FlagNodeHeartbeatCheckpoint: c.NodeHeartbeatCheckpoint,
|
|
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
|
|
}
|
|
}
|
|
|
|
// WithStaleNodeThreshold sets how long a node may go without a durable
|
|
// heartbeat before the health monitor marks it offline. It has to be raised
|
|
// alongside WithNodeHeartbeatCheckpoint: a checkpoint interval wider than this
|
|
// threshold makes every healthy node look dead the moment its beats start
|
|
// being suppressed.
|
|
func WithStaleNodeThreshold(d time.Duration) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.StaleNodeThreshold = d
|
|
}
|
|
}
|
|
|
|
// WithNodeHeartbeatCheckpoint bounds durable heartbeat writes. A zero d is
|
|
// deliberately not special-cased into "unbounded": NodeHeartbeatCheckpointOrDefault
|
|
// reads zero as unset, and an operator who wants a write per beat sets a value
|
|
// below the worker's heartbeat interval instead.
|
|
func WithNodeHeartbeatCheckpoint(d time.Duration) AppOption {
|
|
return func(o *ApplicationConfig) {
|
|
o.Distributed.NodeHeartbeatCheckpoint = 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"
|
|
FlagNodeHeartbeatCheckpoint = "node-heartbeat-checkpoint"
|
|
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
|
|
// A beat that only refreshes the timestamp is now dropped until the
|
|
// checkpoint interval elapses, so the persisted column is up to one
|
|
// interval stale by design. The threshold covers that plus jitter.
|
|
// A genuinely dead node is still caught sooner by the per-model gRPC
|
|
// health check and by request-time failure, neither of which reads this.
|
|
DefaultStaleNodeThreshold = 5 * time.Minute
|
|
DefaultNodeHeartbeatCheckpoint = 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)
|
|
}
|
|
|
|
// NodeHeartbeatCheckpointOrDefault returns the configured interval or the
|
|
// default. A configured zero is indistinguishable from unset here, which is
|
|
// intentional: cmp.Or falls back to the default, and an operator who wants a
|
|
// write per beat sets a value below the heartbeat interval instead.
|
|
func (c DistributedConfig) NodeHeartbeatCheckpointOrDefault() time.Duration {
|
|
return cmp.Or(c.NodeHeartbeatCheckpoint, DefaultNodeHeartbeatCheckpoint)
|
|
}
|
|
|
|
// 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)
|
|
}
|