Lightweight server monitoring with historical data, docker stats, and alerts.
fix(agent): back off repeated GPU collector failure (#2709)
yi11196062f057cde
diff --git a/agent/gpu.go b/agent/gpu.goindex 6e3888136..2655c4f6c 100644--- a/agent/gpu.go+++ b/agent/gpu.go@@ -4,7 +4,6 @@ import ("bufio""bytes""encoding/json"- "errors""fmt""log/slog""maps"@@ -32,6 +31,7 @@ const (// Command retry and timeout constantsretryWaitTime time.Duration = 5 * time.Second+ maxRetryBackoff time.Duration = 5 * time.MinutemaxFailureRetries int = 5// Unit Conversions@@ -143,41 +143,41 @@ type collectorDefinition struct {}// starts and manages the ongoing collection of GPU data for the specified GPU management utility.-// A run without valid data is retried with a growing delay, since the tool may have started-// before the GPU driver was ready (e.g. right after boot). Collection stops after-// maxFailureRetries consecutive runs without valid data; a run that produces valid data resets-// the count. Any other run is restarted after retryWaitTime, so a tool that keeps exiting right-// away can't turn this into a busy loop.+// The tool is restarted whenever it exits. A run without valid data counts as a failure, since the+// tool may have started before the GPU driver was ready (e.g. right after boot) or may be broken,+// and each consecutive failure doubles the delay up to maxRetryBackoff. A run that produces valid+// data resets the count whatever its exit status, because tools started with -l or --interval+// normally only exit when something ends them. Collection never stops for good, so it recovers+// without an agent restart.func (c *gpuCollector) start() {sleep := c.sleepif sleep == nil {sleep = time.Sleep}- noDataRuns := 0+ consecutiveFailures := 0for {hasValidData, err := c.collect()if hasValidData {- noDataRuns = 0- }- if err == nil {- // the tool exited cleanly; tools started with -l or --interval normally never do- sleep(retryWaitTime)- continue+ consecutiveFailures = 0+ } else {+ consecutiveFailures++}- if errors.Is(err, errNoValidData) {- noDataRuns++- if noDataRuns > maxFailureRetries {- slog.Warn(c.name+" found no valid GPU data, stopping", "err", err)- return- }- delay := retryWaitTime * time.Duration(noDataRuns)- slog.Warn(c.name+" found no valid GPU data, retrying", "err", err, "attempt", noDataRuns, "delay", delay)- sleep(delay)- continue+ wait := retryDelay(retryWaitTime, consecutiveFailures)+ slog.Warn(c.name+" stopped, restarting", "err", err, "failures", consecutiveFailures, "wait", wait.String())+ sleep(wait)+ }+}++// retryDelay doubles the base wait after each consecutive failure, capping at maxRetryBackoff+func retryDelay(base time.Duration, consecutiveFailures int) time.Duration {+ backoff := base+ for range consecutiveFailures - 1 {+ backoff *= 2+ if backoff >= maxRetryBackoff {+ return maxRetryBackoff}- slog.Warn(c.name+" failed, restarting", "err", err)- sleep(retryWaitTime)}+ return backoff}// maxLoggedLineLen limits how much of an unparseable output line is included in errorsdiff --git a/agent/gpu_fixture_test.go b/agent/gpu_fixture_test.goindex ed7ab801b..869bbecfb 100644--- a/agent/gpu_fixture_test.go+++ b/agent/gpu_fixture_test.go@@ -44,6 +44,10 @@ func TestMain(m *testing.M) {os.Exit(1)}fmt.Print(string(output))+ // A .fail file makes the command exit non-zero after printing its output.+ if _, err := os.Stat(executable + ".fail"); err == nil {+ os.Exit(1)+ }os.Exit(0)}os.Exit(m.Run())diff --git a/agent/gpu_test.go b/agent/gpu_test.goindex 8eaca9bb2..c1cbf1eb8 100644--- a/agent/gpu_test.go+++ b/agent/gpu_test.go@@ -2165,35 +2165,92 @@ func TestGPUCollectorRetriesAfterNoValidData(t *testing.T) {assert.Equal(t, []time.Duration{retryWaitTime, retryWaitTime, retryWaitTime}, waits)}-// A host with nvidia-smi but no usable GPU never produces data; the collector must still give up.-func TestGPUCollectorStopsAfterRepeatedNoValidData(t *testing.T) {- dir := t.TempDir()- t.Setenv("PATH", dir)- gpuCommandFixture(t, dir, nvidiaSmiCmd, "No devices were found\n")-+// runGPUCollector runs start() until it has waited count times and returns those waits.+func runGPUCollector(t *testing.T, collector *gpuCollector, count int) []time.Duration {+ t.Helper()var waits []time.Duration- gm := &GPUManager{GpuDataMap: make(map[string]*system.GPUData)}- collector := &gpuCollector{- name: nvidiaSmiCmd,- bufSize: 10 * 1024,- parse: gm.parseNvidiaData,- sleep: func(d time.Duration) { waits = append(waits, d) },+ collector.sleep = func(d time.Duration) {+ waits = append(waits, d)+ if len(waits) == count {+ // start() has no stop signal, so end its goroutine here, before the+ // test's cleanup removes the fixture+ runtime.Goexit()+ }}done := make(chan struct{})go func() {+ defer close(done)collector.start()- close(done)}()select {case <-done:case <-time.After(10 * time.Second):- t.Fatal("collector kept retrying a tool that never produces valid data")+ t.Fatalf("collector did not get through %d runs", count)+ }+ return waits+}++// A broken tool (#2707) or a host with nvidia-smi but no usable GPU never produces data.+// The collector must back off, however the run ended, and keep retrying at the cap.+func TestGPUCollectorBacksOffWithoutValidData(t *testing.T) {+ tests := []struct {+ name string+ output string+ fail bool+ }{+ {name: "tool prints an error", output: "No devices were found\n"},+ {name: "tool prints nothing and fails", fail: true},+ {name: "tool prints nothing and exits cleanly"},+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ dir := t.TempDir()+ t.Setenv("PATH", dir)+ argsFile := gpuCommandFixture(t, dir, nvidiaSmiCmd, tt.output)+ if tt.fail {+ require.NoError(t, os.WriteFile(strings.TrimSuffix(argsFile, ".args")+".fail", nil, 0600))+ }++ gm := &GPUManager{GpuDataMap: make(map[string]*system.GPUData)}+ collector := &gpuCollector{name: nvidiaSmiCmd, bufSize: 10 * 1024, parse: gm.parseNvidiaData}+ waits := runGPUCollector(t, collector, 8)++ assert.Equal(t, []time.Duration{+ 5 * time.Second, 10 * time.Second, 20 * time.Second, 40 * time.Second,+ 80 * time.Second, 160 * time.Second, maxRetryBackoff, maxRetryBackoff,+ }, waits)+ })}+}- // the delay grows with each consecutive run without data: 5s, 10s, ... (75s in total by default)- want := make([]time.Duration, maxFailureRetries)- for i := range want {- want[i] = retryWaitTime * time.Duration(i+1)+// With -l a healthy nvidia-smi only exits when something ends it, usually with a+// non-zero status, so valid data must reset the backoff even though the run failed.+func TestGPUCollectorValidDataResetsBackoffOnFailedExit(t *testing.T) {+ dir := t.TempDir()+ t.Setenv("PATH", dir)+ argsFile := gpuCommandFixture(t, dir, nvidiaSmiCmd, "0, NVIDIA Test GPU, 50, 1024, 4096, 25, 100\n")+ require.NoError(t, os.WriteFile(strings.TrimSuffix(argsFile, ".args")+".fail", nil, 0600))++ gm := &GPUManager{GpuDataMap: make(map[string]*system.GPUData)}+ collector := &gpuCollector{name: nvidiaSmiCmd, bufSize: 10 * 1024, parse: gm.parseNvidiaData}+ waits := runGPUCollector(t, collector, 4)++ assert.Equal(t, []time.Duration{retryWaitTime, retryWaitTime, retryWaitTime, retryWaitTime}, waits)+}++func TestGPURetryDelay(t *testing.T) {+ tests := []struct {+ failures int+ want time.Duration+ }{+ {0, 5 * time.Second},+ {1, 5 * time.Second},+ {2, 10 * time.Second},+ {3, 20 * time.Second},+ {8, 5 * time.Minute},+ {20, 5 * time.Minute},+ }+ for _, tt := range tests {+ assert.Equal(t, tt.want, retryDelay(retryWaitTime, tt.failures), tt.failures)}- assert.Equal(t, want, waits)}