Skip to content

Modern Docker Management, Designed for Everyone

perf: filter unhealthy containers at the daemon in auto-heal (#3605)

Kyle Mendell9f8c4a22fc10
diff --git a/backend/pkg/scheduler/auto_heal_job.go b/backend/pkg/scheduler/auto_heal_job.go
index 605d38087..317056ddc 100644
--- a/backend/pkg/scheduler/auto_heal_job.go
+++ b/backend/pkg/scheduler/auto_heal_job.go
@@ -210,6 +210,9 @@ func (j *AutoHealJob) processCandidateInternal(
containerID := candidate.ID
containerName := dockerutil.ContainerNameFromNames(candidate.Names)
+ // The daemon-side health filter already selected unhealthy containers; the
+ // inspect re-confirms right before restarting so a container that recovered
+ // (or was restarted by someone else) since the list isn't bounced again.
inspect, err := j.inspectContainerInternal(ctx, dockerClient, containerID)
if err != nil {
slog.WarnContext(ctx, "auto-heal failed to inspect container", "container", containerName, "error", err)
@@ -422,12 +425,21 @@ func (j *AutoHealJob) getDockerClientInternal(ctx context.Context) (*client.Clie
return j.dockerClientService.GetClient(ctx)
}
+// autoHealListOptionsInternal filters unhealthy containers at the daemon, so
+// the common all-healthy case returns an empty list and nothing is inspected.
+func autoHealListOptionsInternal() client.ContainerListOptions {
+ return client.ContainerListOptions{
+ All: false,
+ Filters: make(client.Filters).Add("health", string(container.Unhealthy)),
+ }
+}
+
func (j *AutoHealJob) ListContainers(ctx context.Context, dockerClient *client.Client) ([]container.Summary, error) {
if j.listContainers != nil {
return j.listContainers(ctx, dockerClient)
}
- containerList, err := dockerClient.ContainerList(ctx, client.ContainerListOptions{All: false})
+ containerList, err := dockerClient.ContainerList(ctx, autoHealListOptionsInternal())
if err != nil {
return nil, err
}
diff --git a/backend/pkg/scheduler/auto_heal_job_test.go b/backend/pkg/scheduler/auto_heal_job_test.go
index 925a6dee5..44c90c01c 100644
--- a/backend/pkg/scheduler/auto_heal_job_test.go
+++ b/backend/pkg/scheduler/auto_heal_job_test.go
@@ -21,6 +21,12 @@ func newTestAutoHealJob() *AutoHealJob {
}
}
+func TestAutoHeal_ListOptionsFilterUnhealthyAtDaemon(t *testing.T) {
+ opts := autoHealListOptionsInternal()
+ require.False(t, opts.All)
+ require.Equal(t, make(client.Filters).Add("health", string(container.Unhealthy)), opts.Filters)
+}
+
func TestAutoHeal_FilterCandidates_SkipsSelfContainer(t *testing.T) {
job := newTestAutoHealJob()