Skip to content

Commit

Permalink
cri: optimize ListPodSandboxStats with parallelism
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Lin <linxiulei@gmail.com>
  • Loading branch information
linxiulei committed Jul 12, 2024
1 parent ac0f34f commit 807f325
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions internal/cri/server/sandbox_stats_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"sync"

sandboxstore "github.com/containerd/containerd/v2/internal/cri/store/sandbox"
"github.com/containerd/errdefs"
Expand All @@ -34,20 +35,33 @@ func (c *criService) ListPodSandboxStats(
r *runtime.ListPodSandboxStatsRequest,
) (*runtime.ListPodSandboxStatsResponse, error) {
sandboxes := c.sandboxesForListPodSandboxStatsRequest(r)
stats, errs := make([]*runtime.PodSandboxStats, len(sandboxes)), make([]error, len(sandboxes))

var wg sync.WaitGroup
for i, sandbox := range sandboxes {
i := i
wg.Add(1)
go func() {
defer wg.Done()
sandboxStats, err := c.podSandboxStats(ctx, sandbox)
switch {
case errdefs.IsUnavailable(err), errdefs.IsNotFound(err):
log.G(ctx).WithField("podsandboxid", sandbox.ID).WithError(err).Debug("failed to get pod sandbox stats, this is likely a transient error")
case errors.Is(err, ttrpc.ErrClosed):
log.G(ctx).WithField("podsandboxid", sandbox.ID).WithError(err).Debug("failed to get pod sandbox stats, connection closed")
case err != nil:
errs[i] = fmt.Errorf("failed to decode sandbox container metrics for sandbox %q: %w", sandbox.ID, err)
default:
stats[i] = sandboxStats
}
}()
}
wg.Wait()

var errs []error
podSandboxStats := new(runtime.ListPodSandboxStatsResponse)
for _, sandbox := range sandboxes {
sandboxStats, err := c.podSandboxStats(ctx, sandbox)
switch {
case errdefs.IsUnavailable(err), errdefs.IsNotFound(err):
log.G(ctx).WithField("podsandboxid", sandbox.ID).Debugf("failed to get pod sandbox stats, this is likely a transient error: %v", err)
case errors.Is(err, ttrpc.ErrClosed):
log.G(ctx).WithField("podsandboxid", sandbox.ID).Debugf("failed to get pod sandbox stats, connection closed: %v", err)
case err != nil:
errs = append(errs, fmt.Errorf("failed to decode sandbox container metrics for sandbox %q: %w", sandbox.ID, err))
default:
podSandboxStats.Stats = append(podSandboxStats.Stats, sandboxStats)
for _, stat := range stats {
if stat != nil {
podSandboxStats.Stats = append(podSandboxStats.Stats, stat)
}
}

Expand Down

0 comments on commit 807f325

Please sign in to comment.