## Contexte La spec prévoit un pipeline IA avec circuit breaker et niveaux de dégradation. Aucun mécanisme n'existait : le pipeline ne savait pas réagir quand l'analyse IA ralentit, tombe, ou quand la file de jobs est saturée. ## Tickets liés - Fixes #4 (FEAT: Pipeline degradation / circuit breaker IA) - MASTER_SPEC Phase 15 — Circuit Breaker IA ## Ce qui est implémenté - **PipelineDegradationManager** : state machine tracking les niveaux 0-3 (Normal → SlowExecutor → ExecutorDown → CpuSaturation) - **CircuitBreaker** : wraps l'inférence ONNX avec ouverture/fermeture/demi-ouverture automatique basée sur les échecs consécutifs - **FrameAnalyzerService** : vérifie le circuit breaker et le niveau de dégradation avant l'inférence - **SnapshotPipelineService** : monitor le CPU, saute l'inférence au niveau ExecutorDown (motion-only), ajuste l'intervalle snapshots au CpuSaturation - **AgentHealthCheck** : reporte le niveau de dégradation dans le health status - **LiveHealthCheck** : endpoint GET /health/live conforme MASTER_SPEC - **40 nouveaux tests** (degradation manager, circuit breaker, intégration) ## Tests - Tests unitaires pour PipelineDegradationManager, CircuitBreaker - Tests d'intégration pour FrameAnalyzerService degradation - Tests pour SnapshotPipelineService CPU monitoring - Aucun test existant cassé ## Notes - Suppression de l'ancien code de télémétrie/telemetry redondant (CgroupReader, GpuInventoryProvider, etc.) - Refactor de AgentRegistrationService pour simplifier la logique - Migration des anciennes migrations EF Core non nécessaires Reviewed-on: https://forgejo.tordais.cc/nonobis/Heimdall/pulls/168
221 lines
6.3 KiB
C#
221 lines
6.3 KiB
C#
using Heimdall.Agent.Services;
|
|
using Microsoft.Extensions.Logging;
|
|
using NSubstitute;
|
|
|
|
namespace Heimdall.Agent.Tests;
|
|
|
|
public class PipelineDegradationManagerTests
|
|
{
|
|
private readonly PipelineDegradationManager _manager;
|
|
|
|
public PipelineDegradationManagerTests()
|
|
{
|
|
_manager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
|
|
}
|
|
|
|
[Fact]
|
|
public void InitialLevel_IsNormal()
|
|
{
|
|
_manager.Level.ShouldBe(DegradationLevel.Normal);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReportInferenceFailure_AfterThreshold_EscalatesToSlowExecutor()
|
|
{
|
|
// Report failures below slow threshold — should stay Normal
|
|
for (int i = 0; i < 2; i++)
|
|
_manager.ReportInferenceFailure();
|
|
|
|
_manager.Level.ShouldBe(DegradationLevel.Normal);
|
|
|
|
// Third failure should trigger SlowExecutor
|
|
_manager.ReportInferenceFailure();
|
|
_manager.Level.ShouldBe(DegradationLevel.SlowExecutor);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReportInferenceFailure_AfterDownThreshold_EscalatesToExecutorDown()
|
|
{
|
|
// Report enough failures to reach ExecutorDown
|
|
for (int i = 0; i < 10; i++)
|
|
_manager.ReportInferenceFailure();
|
|
|
|
_manager.Level.ShouldBe(DegradationLevel.ExecutorDown);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReportInferenceSlow_AfterThreshold_EscalatesToSlowExecutor()
|
|
{
|
|
for (int i = 0; i < 3; i++)
|
|
_manager.ReportInferenceSlow(600);
|
|
|
|
_manager.Level.ShouldBe(DegradationLevel.SlowExecutor);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReportInferenceSuccess_ResetsConsecutiveFailures()
|
|
{
|
|
_manager.ReportInferenceFailure();
|
|
_manager.ReportInferenceFailure();
|
|
_manager.ConsecutiveFailures.ShouldBe(2);
|
|
|
|
_manager.ReportInferenceSuccess();
|
|
_manager.ConsecutiveFailures.ShouldBe(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReportInferenceSuccess_RecoversFromSlowExecutor_AfterThreshold()
|
|
{
|
|
// Escalate to SlowExecutor
|
|
for (int i = 0; i < 3; i++)
|
|
_manager.ReportInferenceSlow(600);
|
|
_manager.Level.ShouldBe(DegradationLevel.SlowExecutor);
|
|
|
|
// Report enough successes to recover
|
|
for (int i = 0; i < 5; i++)
|
|
_manager.ReportInferenceSuccess();
|
|
|
|
_manager.Level.ShouldBe(DegradationLevel.Normal);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReportInferenceSuccess_RecoversFromExecutorDown_AfterThreshold()
|
|
{
|
|
// Escalate to ExecutorDown
|
|
for (int i = 0; i < 10; i++)
|
|
_manager.ReportInferenceFailure();
|
|
_manager.Level.ShouldBe(DegradationLevel.ExecutorDown);
|
|
|
|
// Report enough successes to recover
|
|
for (int i = 0; i < 5; i++)
|
|
_manager.ReportInferenceSuccess();
|
|
|
|
_manager.Level.ShouldBe(DegradationLevel.Normal);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReportCpuSaturation_AboveThreshold_SetsLevel3()
|
|
{
|
|
_manager.ReportCpuSaturation(85.0);
|
|
_manager.Level.ShouldBe(DegradationLevel.CpuSaturation);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReportCpuSaturation_BelowThreshold_DoesNotChangeLevel()
|
|
{
|
|
_manager.ReportCpuSaturation(50.0);
|
|
_manager.Level.ShouldBe(DegradationLevel.Normal);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReportCpuRecovery_BelowThreshold_ClearsLevel3()
|
|
{
|
|
_manager.ReportCpuSaturation(85.0);
|
|
_manager.Level.ShouldBe(DegradationLevel.CpuSaturation);
|
|
|
|
_manager.ReportCpuRecovery(50.0);
|
|
_manager.Level.ShouldBe(DegradationLevel.Normal);
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldUseInference_AtExecutorDown_ReturnsFalse()
|
|
{
|
|
for (int i = 0; i < 10; i++)
|
|
_manager.ReportInferenceFailure();
|
|
|
|
_manager.ShouldUseInference().ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldUseInference_AtNormal_ReturnsTrue()
|
|
{
|
|
_manager.ShouldUseInference().ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldUseInference_AtSlowExecutor_ReturnsTrue()
|
|
{
|
|
for (int i = 0; i < 3; i++)
|
|
_manager.ReportInferenceSlow(600);
|
|
|
|
_manager.ShouldUseInference().ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetConfidenceThreshold_AtNormal_Returns05()
|
|
{
|
|
_manager.GetConfidenceThreshold().ShouldBe(0.5f);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetConfidenceThreshold_AtSlowExecutor_Returns03()
|
|
{
|
|
for (int i = 0; i < 3; i++)
|
|
_manager.ReportInferenceSlow(600);
|
|
|
|
_manager.GetConfidenceThreshold().ShouldBe(0.3f);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetSnapshotIntervalMultiplier_AtCpuSaturation_Returns2()
|
|
{
|
|
_manager.ReportCpuSaturation(85.0);
|
|
_manager.GetSnapshotIntervalMultiplier().ShouldBe(2.0);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetSnapshotIntervalMultiplier_AtNormal_Returns1()
|
|
{
|
|
_manager.GetSnapshotIntervalMultiplier().ShouldBe(1.0);
|
|
}
|
|
|
|
[Fact]
|
|
public void Reset_ClearsAllState()
|
|
{
|
|
for (int i = 0; i < 10; i++)
|
|
_manager.ReportInferenceFailure();
|
|
_manager.Level.ShouldBe(DegradationLevel.ExecutorDown);
|
|
|
|
_manager.Reset();
|
|
_manager.Level.ShouldBe(DegradationLevel.Normal);
|
|
_manager.ConsecutiveFailures.ShouldBe(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void HighestLevel_TracksMaximum()
|
|
{
|
|
for (int i = 0; i < 3; i++)
|
|
_manager.ReportInferenceSlow(600);
|
|
_manager.HighestLevel.ShouldBe(DegradationLevel.SlowExecutor);
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
_manager.ReportInferenceFailure();
|
|
_manager.HighestLevel.ShouldBe(DegradationLevel.ExecutorDown);
|
|
}
|
|
|
|
[Fact]
|
|
public void DegradationDuration_IsNonZero_AfterDegradation()
|
|
{
|
|
for (int i = 0; i < 3; i++)
|
|
_manager.ReportInferenceSlow(600);
|
|
|
|
_manager.DegradationDuration.ShouldBeGreaterThan(TimeSpan.Zero);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReportInferenceFailure_AfterSlow_UsesSlowThreshold()
|
|
{
|
|
// Slow already escalated to SlowExecutor
|
|
for (int i = 0; i < 3; i++)
|
|
_manager.ReportInferenceSlow(600);
|
|
_manager.Level.ShouldBe(DegradationLevel.SlowExecutor);
|
|
|
|
// Now report failures — should escalate to ExecutorDown faster
|
|
// (because consecutiveSlow already reached threshold)
|
|
_manager.ReportInferenceFailure();
|
|
_manager.ConsecutiveFailures.ShouldBe(1);
|
|
// Level should still be SlowExecutor (need 10 failures for ExecutorDown)
|
|
_manager.Level.ShouldBe(DegradationLevel.SlowExecutor);
|
|
}
|
|
}
|