mirror of
https://github.com/mudler/LocalAI.git
synced 2026-09-12 22:33:54 -04:00
* test: make coverage failures observable Keep per-root logs, reject concurrent coverage runs, and avoid relying on /bin/sleep in the worker timeout test. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * test: parallelize coverage without remote fixtures Assisted-by: Codex:gpt-5 [apply_patch] [exec_command] Signed-off-by: Richard Palethorpe <io@richiejp.com> * test: add offline resource infrastructure Introduce versioned resource manifests, a checksum-verified CAS preparer, offline test wrappers, and a guarded network transport. Replace live Hugging Face, GitHub, and OCI cases with deterministic fixtures and inject fixture metadata into importer discovery. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * test: enforce offline resource replay Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * test: harden offline resource refresh Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * test: expose slow coverage waits Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * test: eliminate avoidable wall-clock waits Inject a clock into Hugging Face retry handling, reuse a process-scoped PostgreSQL container with per-spec schemas in the nodes suite, and poll local import jobs promptly. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * test: remove repeated fixture startup waits Share PostgreSQL fixtures across parallel endpoint and agent suite workers, and make the worker Free deadline injectable so the wedged-backend test does not spend five seconds on wall-clock time. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * test: fix offline resource CI portability Normalize Docker archive metadata before content addressing, derive archive checksums during explicit refreshes, make network lint portable to macOS, and prepare distributed images before running their offline suite. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * ci: cache Go modules before offline tests Warm the complete module graph before the Linux and macOS test jobs enter offline replay mode, so tool dependencies such as Ginkgo are not fetched through the guarded proxy. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * test: drop the static network lint in favour of real isolation The offline test suite already prevents tests from reaching the network twice over: run-test-linux-offline.sh puts the test process in a cgroup and REJECTs egress outside the private ranges, and HardenedTransport installs testnetwork.LocalGuard to refuse dials that resolve to a public address. Both fail the test with a precise error at the moment of the dial. test-network-lint.sh added neither. Its diff stage defaulted to a HEAD base, so on a clean checkout it compared the tree against itself and inspected nothing; the branch's own commits were never examined. It only produced output when an earlier job step dirtied the tree, and then it matched a bare https?:// against whatever changed. make react-ui runs npm install rather than npm ci, so CI rewrote core/http/react-ui/package-lock.json and the lint reported an npm registry URL as forbidden test network access: + "resolved": "https://registry.npmjs.org/hono/-/hono-4.12.25.tgz", Its fingerprint stage was self-defeating in a quieter way: hashing the whole tree's network-mechanism inventory meant every rebase onto a master that touched any _test.go needed a manual baseline bump, so the check mostly caught its own staleness. Remove the script, its make target and the two prerequisite edges, along with the test-network: fixture markers that existed only to suppress it. The isolation itself is untouched. Assisted-by: Claude:claude-opus-5 [go vet] Signed-off-by: Richard Palethorpe <io@richiejp.com> * ci: keep hidden files in the offline test bundle artifact Cherry-picked from15a37b0acon the remote branch. The offline bundle lives under .cache/, which actions/upload-artifact skips by default, so the Linux job packed an artifact missing the very file the next step restores. The other half of15a37b0acmoved test-network-lint out of the `test` and `test-coverage` prerequisite lists into a recipe line, so parallel make could not fingerprint the tree while generated fixtures were still changing. That is dropped: the preceding commit removes the lint entirely, and the race it worked around is one more reason a whole-tree fingerprint was the wrong mechanism. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * refactor: share bounded exponential backoff Use overflow-safe saturating arithmetic for retry delays across model import polling, downloads, registration, node operations, and model loading. Keep model import status checks responsive initially while capping their interval at 500ms. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * ci: mirror Jetson Python wheels Keep the CUDA aarch64 wheel subset in GHCR and serve it as a local PEP 503 index during L4T backend builds, preserving last-known-good packages through upstream outages. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * docs(agents): index the Jetson wheels mirror Mention the GHCR-hosted L4T wheel mirror in the CI caching guide summary so maintainers can find its outage and cache documentation. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * ci: add defensive build network proxy Record build destinations and byte counts, retry observable idempotent HTTP downloads, and isolate explorer database tests that race under coverage. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * fix(kokoros): implement updated backend trait Return unimplemented for image upscaling, matching the backend's other unsupported modalities after the protobuf API update. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * fix(ci): clear recovered proxy errors Do not mark a request failed when a later safe retry succeeds. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * ci: require HTTPS build interception Inject a short-lived proxy CA into BuildKit and Dockerfile RUN steps, reject plain HTTP and opaque tunnels, and retain method/status/byte telemetry for verified HTTPS traffic. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * fix(ci): preserve system trust in unproxied builds Mount the generated interception CA at a dedicated secret path and add it to the trust bundle only in proxy-aware dependency stages. This prevents optional secret mounts from masking the system CA bundle in ordinary backend test builds. Install the requested Go toolchain before starting the proxy and satisfy cleanup error checks found by CI lint. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * fix(ci): persist build proxy trust Install the generated proxy CA through the system-managed local certificate directory so ca-certificates upgrades retain it. Avoid turning canceled matrix jobs into proxy cleanup failures. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * fix(ci): trust proxy in nested build scripts Install the build proxy CA before nested source fetches, route the DS4 package setup through the HTTPS mirror helper, and avoid repeated OCI setup in gallery behavior tests. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * fix(ci): use HTTPS apt sources for Bonsai Rewrite ARM64 package sources before installing GCC and check gallery fixture cleanup errors so the optimized tests satisfy errcheck. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * fix(privacy-filter): trust build proxy CA Install the mounted build proxy certificate before privacy-filter's make target fetches its HTTPS sources, for both source and prebuilt builder paths.\n\nAssisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * test: fail on hidden offline egress Count cgroup-scoped firewall rejects and fail the offline test harness with bounded aggregate diagnostics. Inject the gen-audio GGUF probe so fixture-backed importer tests do not attempt real network access. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> * fix(ci): preserve system CA trust Build a combined runner certificate bundle instead of replacing public roots with the generated proxy CA. Centralize additive container installation in the shared proxy CA helper. Assisted-by: Codex:gpt-5 Signed-off-by: Richard Palethorpe <io@richiejp.com> --------- Signed-off-by: Richard Palethorpe <io@richiejp.com> Co-authored-by: localai-org-maint-bot <306269227+localai-org-maint-bot@users.noreply.github.com>
602 lines
20 KiB
Go
602 lines
20 KiB
Go
package model_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
grpcPkg "github.com/mudler/LocalAI/pkg/grpc"
|
|
"github.com/mudler/LocalAI/pkg/model"
|
|
"github.com/mudler/LocalAI/pkg/system"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
// lifecycleBackend embeds the full backend interface so these tests only need
|
|
// to override the lifecycle methods they exercise. A nil embedded backend is
|
|
// safe because no inference method is called.
|
|
type lifecycleBackend struct {
|
|
grpcPkg.Backend
|
|
busy atomic.Bool
|
|
freeOnce sync.Once
|
|
freeStarted chan struct{}
|
|
freeRelease chan struct{}
|
|
}
|
|
|
|
type failingRemoteUnloader struct {
|
|
err error
|
|
}
|
|
|
|
func (u failingRemoteUnloader) UnloadRemoteModel(string) error {
|
|
return u.err
|
|
}
|
|
|
|
func newLifecycleBackend() *lifecycleBackend {
|
|
return &lifecycleBackend{
|
|
freeStarted: make(chan struct{}),
|
|
freeRelease: make(chan struct{}),
|
|
}
|
|
}
|
|
|
|
func (b *lifecycleBackend) IsBusy() bool {
|
|
return b.busy.Load()
|
|
}
|
|
|
|
func (b *lifecycleBackend) Free(ctx context.Context) error {
|
|
b.freeOnce.Do(func() { close(b.freeStarted) })
|
|
select {
|
|
case <-b.freeRelease:
|
|
return nil
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
}
|
|
|
|
var _ = Describe("ModelLoader", func() {
|
|
var (
|
|
modelLoader *model.ModelLoader
|
|
modelPath string
|
|
mockModel *model.Model
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
// Setup the model loader with a test directory
|
|
modelPath = GinkgoT().TempDir()
|
|
|
|
systemState, err := system.GetSystemState(
|
|
system.WithModelPath(modelPath),
|
|
)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
modelLoader = model.NewModelLoader(systemState)
|
|
})
|
|
|
|
Context("NewModelLoader", func() {
|
|
It("should create a new ModelLoader with an empty model map", func() {
|
|
Expect(modelLoader).ToNot(BeNil())
|
|
Expect(modelLoader.ModelPath).To(Equal(modelPath))
|
|
Expect(modelLoader.ListLoadedModels()).To(BeEmpty())
|
|
})
|
|
})
|
|
|
|
Context("ExistsInModelPath", func() {
|
|
It("should return true if a file exists in the model path", func() {
|
|
testFile := filepath.Join(modelPath, "test.model")
|
|
os.Create(testFile)
|
|
Expect(modelLoader.ExistsInModelPath("test.model")).To(BeTrue())
|
|
})
|
|
|
|
It("should return false if a file does not exist in the model path", func() {
|
|
Expect(modelLoader.ExistsInModelPath("nonexistent.model")).To(BeFalse())
|
|
})
|
|
})
|
|
|
|
Context("ListFilesInModelPath", func() {
|
|
It("should list all valid model files in the model path", func() {
|
|
os.Create(filepath.Join(modelPath, "test.model"))
|
|
os.Create(filepath.Join(modelPath, "model.gguf"))
|
|
os.Create(filepath.Join(modelPath, "README.md"))
|
|
|
|
files, err := modelLoader.ListFilesInModelPath()
|
|
Expect(err).To(BeNil())
|
|
Expect(files).To(ContainElement("test.model"))
|
|
Expect(files).ToNot(ContainElement("model.gguf"))
|
|
Expect(files).ToNot(ContainElement("README.md"))
|
|
})
|
|
})
|
|
|
|
Context("LoadModel", func() {
|
|
It("passes a logical model and managed model file independently", func() {
|
|
const relative = ".artifacts/huggingface/0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef/snapshot"
|
|
var receivedName, receivedFile string
|
|
mockModel = model.NewModel("managed", "test.model", nil)
|
|
mockModel.MarkHealthy()
|
|
mockLoader := func(_ string, modelName, modelFile string) (*model.Model, error) {
|
|
receivedName, receivedFile = modelName, modelFile
|
|
return mockModel, nil
|
|
}
|
|
|
|
_, err := modelLoader.LoadModelWithFile("managed", "owner/repo", relative, mockLoader)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(receivedName).To(Equal("owner/repo"))
|
|
Expect(receivedFile).To(Equal(filepath.Join(modelPath, filepath.FromSlash(relative))))
|
|
})
|
|
|
|
It("should load a model and keep it in memory", func() {
|
|
mockModel = model.NewModel("foo", "test.model", nil)
|
|
mockModel.MarkHealthy() // skip gRPC health check (no real server)
|
|
|
|
mockLoader := func(modelID, modelName, modelFile string) (*model.Model, error) {
|
|
return mockModel, nil
|
|
}
|
|
|
|
model, err := modelLoader.LoadModel("foo", "test.model", mockLoader)
|
|
Expect(err).To(BeNil())
|
|
Expect(model).To(Equal(mockModel))
|
|
Expect(modelLoader.CheckIsLoaded("foo")).To(Equal(mockModel))
|
|
})
|
|
|
|
It("should return an error if loading the model fails", func() {
|
|
mockLoader := func(modelID, modelName, modelFile string) (*model.Model, error) {
|
|
return nil, errors.New("failed to load model")
|
|
}
|
|
|
|
model, err := modelLoader.LoadModel("foo", "test.model", mockLoader)
|
|
Expect(err).To(HaveOccurred())
|
|
Expect(model).To(BeNil())
|
|
})
|
|
})
|
|
|
|
Context("Remote model eviction", func() {
|
|
It("should evict unreachable remote models from cache on health check", func() {
|
|
// Create a remote model (process=nil) with an unreachable address
|
|
remoteModel := model.NewModel("remote-test", "127.0.0.1:1", nil)
|
|
|
|
mockLoader := func(modelID, modelName, modelFile string) (*model.Model, error) {
|
|
return remoteModel, nil
|
|
}
|
|
|
|
_, err := modelLoader.LoadModel("remote-test", "test.model", mockLoader)
|
|
Expect(err).To(BeNil())
|
|
|
|
// CheckIsLoaded should detect the connection error and evict
|
|
result := modelLoader.CheckIsLoaded("remote-test")
|
|
Expect(result).To(BeNil(), "unreachable remote model should be evicted from cache")
|
|
})
|
|
|
|
It("should keep recently-healthy remote models in cache", func() {
|
|
remoteModel := model.NewModel("healthy-remote", "127.0.0.1:1", nil)
|
|
remoteModel.MarkHealthy() // simulate a recent successful health check
|
|
|
|
mockLoader := func(modelID, modelName, modelFile string) (*model.Model, error) {
|
|
return remoteModel, nil
|
|
}
|
|
|
|
loaded, err := modelLoader.LoadModel("healthy-remote", "test.model", mockLoader)
|
|
Expect(err).To(BeNil())
|
|
|
|
// Within TTL, should return the model without health check
|
|
result := modelLoader.CheckIsLoaded("healthy-remote")
|
|
Expect(result).To(Equal(loaded), "recently-healthy model should be returned from cache")
|
|
})
|
|
})
|
|
|
|
Context("ShutdownModel", func() {
|
|
It("should shutdown a loaded model", func() {
|
|
mockLoader := func(modelID, modelName, modelFile string) (*model.Model, error) {
|
|
return model.NewModel("foo", "test.model", nil), nil
|
|
}
|
|
|
|
_, err := modelLoader.LoadModel("foo", "test.model", mockLoader)
|
|
Expect(err).To(BeNil())
|
|
|
|
err = modelLoader.ShutdownModel("foo")
|
|
Expect(err).To(BeNil())
|
|
Expect(modelLoader.CheckIsLoaded("foo")).To(BeNil())
|
|
})
|
|
|
|
It("evicts the local remote-model entry when remote unload fails", func() {
|
|
remote := model.NewModel("remote", "worker.example:50051", nil)
|
|
remote.MarkHealthy()
|
|
_, err := modelLoader.LoadModel("remote", "remote", func(_, _, _ string) (*model.Model, error) {
|
|
return remote, nil
|
|
})
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
unloadErr := errors.New("worker unreachable")
|
|
modelLoader.SetRemoteUnloader(failingRemoteUnloader{err: unloadErr})
|
|
Expect(modelLoader.ShutdownModel("remote")).To(MatchError(unloadErr))
|
|
Expect(modelLoader.ListLoadedModels()).To(BeEmpty())
|
|
|
|
replacement := model.NewModel("remote", "replacement.example:50051", nil)
|
|
replacement.MarkHealthy()
|
|
var reloads atomic.Int32
|
|
loaded, err := modelLoader.LoadModel("remote", "remote", func(_, _, _ string) (*model.Model, error) {
|
|
reloads.Add(1)
|
|
return replacement, nil
|
|
})
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(loaded).To(BeIdenticalTo(replacement))
|
|
Expect(reloads.Load()).To(Equal(int32(1)))
|
|
})
|
|
})
|
|
|
|
Context("Shutdown lifecycle conformance", func() {
|
|
loadBackend := func(id string, backend grpcPkg.Backend) {
|
|
_, err := modelLoader.LoadModel(id, id, func(_, _, _ string) (*model.Model, error) {
|
|
return model.NewModelWithClient(id, id, backend), nil
|
|
})
|
|
Expect(err).NotTo(HaveOccurred())
|
|
}
|
|
|
|
It("force shutdown bypasses a permanently busy backend and leaves the loader available", func() {
|
|
stuck := newLifecycleBackend()
|
|
stuck.busy.Store(true)
|
|
loadBackend("stuck", stuck)
|
|
|
|
shutdownDone := make(chan error, 1)
|
|
go func() { shutdownDone <- modelLoader.ShutdownModelForce("stuck") }()
|
|
|
|
var shutdownErr error
|
|
Eventually(shutdownDone, "500ms").Should(Receive(&shutdownErr))
|
|
Expect(shutdownErr).NotTo(HaveOccurred())
|
|
Consistently(stuck.freeStarted, "50ms").ShouldNot(Receive(), "force shutdown must skip Free on a stuck backend")
|
|
|
|
other := newLifecycleBackend()
|
|
loadBackend("unrelated", other)
|
|
Expect(modelLoader.ListLoadedModels()).To(ConsistOf(HaveField("ID", "unrelated")))
|
|
})
|
|
|
|
It("keeps unrelated loads available while graceful Free is blocked", func() {
|
|
blocked := newLifecycleBackend()
|
|
loadBackend("blocked", blocked)
|
|
|
|
shutdownDone := make(chan error, 1)
|
|
go func() { shutdownDone <- modelLoader.ShutdownModel("blocked") }()
|
|
Eventually(blocked.freeStarted, "500ms").Should(BeClosed())
|
|
|
|
otherDone := make(chan error, 1)
|
|
go func() {
|
|
_, err := modelLoader.LoadModel("unrelated", "unrelated", func(_, _, _ string) (*model.Model, error) {
|
|
return model.NewModelWithClient("unrelated", "unrelated", newLifecycleBackend()), nil
|
|
})
|
|
otherDone <- err
|
|
}()
|
|
|
|
var otherErr error
|
|
Eventually(otherDone, "500ms").Should(Receive(&otherErr))
|
|
Expect(otherErr).NotTo(HaveOccurred())
|
|
Consistently(shutdownDone, "50ms").ShouldNot(Receive(), "shutdown must still be waiting for Free")
|
|
close(blocked.freeRelease)
|
|
|
|
var shutdownErr error
|
|
Eventually(shutdownDone, "500ms").Should(Receive(&shutdownErr))
|
|
Expect(shutdownErr).NotTo(HaveOccurred())
|
|
})
|
|
|
|
It("serializes a same-model reload behind shutdown", func() {
|
|
blocked := newLifecycleBackend()
|
|
loadBackend("replace-me", blocked)
|
|
|
|
shutdownDone := make(chan error, 1)
|
|
go func() { shutdownDone <- modelLoader.ShutdownModel("replace-me") }()
|
|
Eventually(blocked.freeStarted, "500ms").Should(BeClosed())
|
|
|
|
reloadStarted := make(chan struct{})
|
|
reloadDone := make(chan error, 1)
|
|
go func() {
|
|
_, err := modelLoader.LoadModel("replace-me", "replace-me", func(_, _, _ string) (*model.Model, error) {
|
|
close(reloadStarted)
|
|
return model.NewModelWithClient("replace-me", "replace-me", newLifecycleBackend()), nil
|
|
})
|
|
reloadDone <- err
|
|
}()
|
|
|
|
Consistently(reloadStarted, "50ms").ShouldNot(BeClosed())
|
|
close(blocked.freeRelease)
|
|
|
|
Eventually(shutdownDone, "500ms").Should(Receive(Succeed()))
|
|
Eventually(reloadStarted, "500ms").Should(BeClosed())
|
|
Eventually(reloadDone, "500ms").Should(Receive(Succeed()))
|
|
})
|
|
|
|
It("bounds a graceful wait for a permanently busy backend without blocking other models", func() {
|
|
stuck := newLifecycleBackend()
|
|
stuck.busy.Store(true)
|
|
loadBackend("stuck", stuck)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
|
defer cancel()
|
|
shutdownDone := make(chan error, 1)
|
|
go func() { shutdownDone <- modelLoader.ShutdownModelContext(ctx, "stuck", false) }()
|
|
|
|
other := newLifecycleBackend()
|
|
loadBackend("unrelated", other)
|
|
|
|
var shutdownErr error
|
|
Eventually(shutdownDone, "500ms").Should(Receive(&shutdownErr))
|
|
Expect(errors.Is(shutdownErr, model.ErrModelBusy)).To(BeTrue())
|
|
Expect(modelLoader.ListLoadedModels()).To(ConsistOf(
|
|
HaveField("ID", "stuck"),
|
|
HaveField("ID", "unrelated"),
|
|
))
|
|
})
|
|
|
|
It("honors cancellation while waiting for an in-progress load of the same model", func() {
|
|
loadStarted := make(chan struct{})
|
|
loadRelease := make(chan struct{})
|
|
loadDone := make(chan error, 1)
|
|
go func() {
|
|
_, err := modelLoader.LoadModel("loading", "loading", func(_, _, _ string) (*model.Model, error) {
|
|
close(loadStarted)
|
|
<-loadRelease
|
|
return model.NewModelWithClient("loading", "loading", newLifecycleBackend()), nil
|
|
})
|
|
loadDone <- err
|
|
}()
|
|
Eventually(loadStarted, "500ms").Should(BeClosed())
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
|
defer cancel()
|
|
err := modelLoader.ShutdownModelContext(ctx, "loading", false)
|
|
Expect(errors.Is(err, context.DeadlineExceeded)).To(BeTrue())
|
|
|
|
close(loadRelease)
|
|
Eventually(loadDone, "500ms").Should(Receive(Succeed()))
|
|
})
|
|
})
|
|
|
|
Context("Concurrent Loading", func() {
|
|
It("should handle concurrent requests for the same model", func() {
|
|
var loadCount int32
|
|
mockLoader := func(modelID, modelName, modelFile string) (*model.Model, error) {
|
|
atomic.AddInt32(&loadCount, 1)
|
|
time.Sleep(100 * time.Millisecond) // Simulate loading time
|
|
m := model.NewModel(modelID, modelName, nil)
|
|
m.MarkHealthy() // skip gRPC health check (no real server)
|
|
return m, nil
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
results := make([]*model.Model, 5)
|
|
errs := make([]error, 5)
|
|
|
|
// Start 5 concurrent requests for the same model
|
|
for i := range 5 {
|
|
wg.Go(func() {
|
|
results[i], errs[i] = modelLoader.LoadModel("concurrent-model", "test.model", mockLoader)
|
|
})
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
// All requests should succeed
|
|
for i := range 5 {
|
|
Expect(errs[i]).To(BeNil())
|
|
Expect(results[i]).ToNot(BeNil())
|
|
}
|
|
|
|
// The loader should only have been called once
|
|
Expect(atomic.LoadInt32(&loadCount)).To(Equal(int32(1)))
|
|
|
|
// All results should be the same model instance
|
|
for i := 1; i < 5; i++ {
|
|
Expect(results[i]).To(Equal(results[0]))
|
|
}
|
|
})
|
|
|
|
It("should handle concurrent requests for different models", func() {
|
|
var loadCount int32
|
|
mockLoader := func(modelID, modelName, modelFile string) (*model.Model, error) {
|
|
atomic.AddInt32(&loadCount, 1)
|
|
time.Sleep(50 * time.Millisecond) // Simulate loading time
|
|
m := model.NewModel(modelID, modelName, nil)
|
|
m.MarkHealthy() // skip gRPC health check (no real server)
|
|
return m, nil
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
modelCount := 3
|
|
|
|
// Start concurrent requests for different models
|
|
for i := range modelCount {
|
|
wg.Go(func() {
|
|
modelID := "model-" + string(rune('A'+i))
|
|
_, err := modelLoader.LoadModel(modelID, "test.model", mockLoader)
|
|
Expect(err).To(BeNil())
|
|
})
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
// Each model should be loaded exactly once
|
|
Expect(atomic.LoadInt32(&loadCount)).To(Equal(int32(modelCount)))
|
|
|
|
// All models should be loaded
|
|
Expect(modelLoader.CheckIsLoaded("model-A")).ToNot(BeNil())
|
|
Expect(modelLoader.CheckIsLoaded("model-B")).ToNot(BeNil())
|
|
Expect(modelLoader.CheckIsLoaded("model-C")).ToNot(BeNil())
|
|
})
|
|
|
|
It("should track loading count correctly", func() {
|
|
loadStarted := make(chan struct{})
|
|
loadComplete := make(chan struct{})
|
|
|
|
mockLoader := func(modelID, modelName, modelFile string) (*model.Model, error) {
|
|
close(loadStarted)
|
|
<-loadComplete // Wait until we're told to complete
|
|
return model.NewModel(modelID, modelName, nil), nil
|
|
}
|
|
|
|
// Start loading in background
|
|
go func() {
|
|
modelLoader.LoadModel("slow-model", "test.model", mockLoader)
|
|
}()
|
|
|
|
// Wait for loading to start
|
|
<-loadStarted
|
|
|
|
// Loading count should be 1
|
|
Expect(modelLoader.GetLoadingCount()).To(Equal(1))
|
|
|
|
// Complete the loading
|
|
close(loadComplete)
|
|
|
|
// Wait a bit for cleanup
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
// Loading count should be back to 0
|
|
Expect(modelLoader.GetLoadingCount()).To(Equal(0))
|
|
})
|
|
|
|
It("should retry loading if first attempt fails", func() {
|
|
var attemptCount int32
|
|
mockLoader := func(modelID, modelName, modelFile string) (*model.Model, error) {
|
|
count := atomic.AddInt32(&attemptCount, 1)
|
|
if count == 1 {
|
|
// Hold the loading slot so the second request coalesces as a
|
|
// follower before this leader fails. That follower then gets
|
|
// the one in-burst retry, which bypasses the failure cooldown
|
|
// (the cooldown only gates fresh, independent load triggers).
|
|
time.Sleep(50 * time.Millisecond)
|
|
return nil, errors.New("first attempt fails")
|
|
}
|
|
return model.NewModel(modelID, modelName, nil), nil
|
|
}
|
|
|
|
// First goroutine will fail
|
|
var wg sync.WaitGroup
|
|
|
|
var err1, err2 error
|
|
var m1, m2 *model.Model
|
|
|
|
wg.Go(func() {
|
|
m1, err1 = modelLoader.LoadModel("retry-model", "test.model", mockLoader)
|
|
})
|
|
|
|
// Give first goroutine a head start so it owns the loading slot.
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
wg.Go(func() {
|
|
m2, err2 = modelLoader.LoadModel("retry-model", "test.model", mockLoader)
|
|
})
|
|
|
|
wg.Wait()
|
|
|
|
// At least one should succeed (the second attempt after retry)
|
|
successCount := 0
|
|
if err1 == nil && m1 != nil {
|
|
successCount++
|
|
}
|
|
if err2 == nil && m2 != nil {
|
|
successCount++
|
|
}
|
|
Expect(successCount).To(BeNumerically(">=", 1))
|
|
})
|
|
})
|
|
|
|
Context("GetLoadingCount", func() {
|
|
It("should return 0 when nothing is loading", func() {
|
|
Expect(modelLoader.GetLoadingCount()).To(Equal(0))
|
|
})
|
|
})
|
|
|
|
Context("LRU Eviction Retry Settings", func() {
|
|
It("should allow updating retry settings", func() {
|
|
modelLoader.SetLRUEvictionRetrySettings(50, 2*time.Second)
|
|
// Settings are updated - we can verify through behavior if needed
|
|
// For now, just verify the call doesn't panic
|
|
Expect(modelLoader).ToNot(BeNil())
|
|
})
|
|
})
|
|
|
|
Context("Load failure cooldown", func() {
|
|
It("refuses a fresh load within the cooldown window without re-invoking the loader", func() {
|
|
modelLoader.SetLoadFailureCooldown(60*time.Millisecond, 240*time.Millisecond)
|
|
|
|
var loadCount int32
|
|
failing := func(modelID, modelName, modelFile string) (*model.Model, error) {
|
|
atomic.AddInt32(&loadCount, 1)
|
|
return nil, errors.New("boom")
|
|
}
|
|
|
|
// First attempt runs the loader and fails (not a cooldown error).
|
|
_, err := modelLoader.LoadModel("broken", "test.model", failing)
|
|
Expect(err).To(HaveOccurred())
|
|
var coolErr *model.ModelLoadCooldownError
|
|
Expect(errors.As(err, &coolErr)).To(BeFalse())
|
|
Expect(atomic.LoadInt32(&loadCount)).To(Equal(int32(1)))
|
|
|
|
// An immediate retry is short-circuited: cooldown error, loader untouched.
|
|
_, err = modelLoader.LoadModel("broken", "test.model", failing)
|
|
Expect(errors.As(err, &coolErr)).To(BeTrue())
|
|
Expect(coolErr.ModelID).To(Equal("broken"))
|
|
Expect(coolErr.RetryAfter).To(BeNumerically(">", time.Duration(0)))
|
|
Expect(atomic.LoadInt32(&loadCount)).To(Equal(int32(1)))
|
|
|
|
// Once the window elapses the loader is attempted again.
|
|
Eventually(func() int32 {
|
|
_, _ = modelLoader.LoadModel("broken", "test.model", failing)
|
|
return atomic.LoadInt32(&loadCount)
|
|
}, "1s", "20ms").Should(BeNumerically(">=", 2))
|
|
})
|
|
|
|
It("clears the cooldown after a successful load", func() {
|
|
modelLoader.SetLoadFailureCooldown(60*time.Millisecond, 240*time.Millisecond)
|
|
|
|
var attempts int32
|
|
loader := func(modelID, modelName, modelFile string) (*model.Model, error) {
|
|
if atomic.AddInt32(&attempts, 1) == 1 {
|
|
return nil, errors.New("boom")
|
|
}
|
|
m := model.NewModel(modelID, modelName, nil)
|
|
m.MarkHealthy()
|
|
return m, nil
|
|
}
|
|
|
|
_, err := modelLoader.LoadModel("flaky", "test.model", loader)
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
// After the window, the retry succeeds and resets the failure state.
|
|
var m *model.Model
|
|
Eventually(func() error {
|
|
m, err = modelLoader.LoadModel("flaky", "test.model", loader)
|
|
return err
|
|
}, "1s", "20ms").Should(Succeed())
|
|
Expect(m).ToNot(BeNil())
|
|
|
|
// A subsequent load returns the cached model, never a cooldown error.
|
|
m2, err := modelLoader.LoadModel("flaky", "test.model", loader)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(m2).To(Equal(m))
|
|
})
|
|
|
|
It("grows the cooldown on consecutive failures", func() {
|
|
modelLoader.SetLoadFailureCooldown(50*time.Millisecond, 10*time.Second)
|
|
failing := func(modelID, modelName, modelFile string) (*model.Model, error) {
|
|
return nil, errors.New("boom")
|
|
}
|
|
|
|
// Failure 1, then read its cooldown.
|
|
_, err := modelLoader.LoadModel("bad", "test.model", failing)
|
|
Expect(err).To(HaveOccurred())
|
|
_, err = modelLoader.LoadModel("bad", "test.model", failing)
|
|
var c1 *model.ModelLoadCooldownError
|
|
Expect(errors.As(err, &c1)).To(BeTrue())
|
|
|
|
// Wait out the first window, trigger failure 2, read its (larger) cooldown.
|
|
time.Sleep(70 * time.Millisecond)
|
|
_, err = modelLoader.LoadModel("bad", "test.model", failing)
|
|
Expect(err).To(HaveOccurred())
|
|
_, err = modelLoader.LoadModel("bad", "test.model", failing)
|
|
var c2 *model.ModelLoadCooldownError
|
|
Expect(errors.As(err, &c2)).To(BeTrue())
|
|
|
|
Expect(c2.RetryAfter).To(BeNumerically(">", c1.RetryAfter))
|
|
})
|
|
})
|
|
})
|