Skip to content

Commit 78d3e20

Browse files
authored
Merge pull request containerd#10376 from linxiulei/concurrent_podstats
cri: optimize ListPodSandboxStats with parallelism
2 parents b31d3fd + 807f325 commit 78d3e20

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

internal/cri/server/sandbox_stats_list.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
"sync"
2324

2425
sandboxstore "github.com/containerd/containerd/v2/internal/cri/store/sandbox"
2526
"github.com/containerd/errdefs"
@@ -34,20 +35,33 @@ func (c *criService) ListPodSandboxStats(
3435
r *runtime.ListPodSandboxStatsRequest,
3536
) (*runtime.ListPodSandboxStatsResponse, error) {
3637
sandboxes := c.sandboxesForListPodSandboxStatsRequest(r)
38+
stats, errs := make([]*runtime.PodSandboxStats, len(sandboxes)), make([]error, len(sandboxes))
39+
40+
var wg sync.WaitGroup
41+
for i, sandbox := range sandboxes {
42+
i := i
43+
wg.Add(1)
44+
go func() {
45+
defer wg.Done()
46+
sandboxStats, err := c.podSandboxStats(ctx, sandbox)
47+
switch {
48+
case errdefs.IsUnavailable(err), errdefs.IsNotFound(err):
49+
log.G(ctx).WithField("podsandboxid", sandbox.ID).WithError(err).Debug("failed to get pod sandbox stats, this is likely a transient error")
50+
case errors.Is(err, ttrpc.ErrClosed):
51+
log.G(ctx).WithField("podsandboxid", sandbox.ID).WithError(err).Debug("failed to get pod sandbox stats, connection closed")
52+
case err != nil:
53+
errs[i] = fmt.Errorf("failed to decode sandbox container metrics for sandbox %q: %w", sandbox.ID, err)
54+
default:
55+
stats[i] = sandboxStats
56+
}
57+
}()
58+
}
59+
wg.Wait()
3760

38-
var errs []error
3961
podSandboxStats := new(runtime.ListPodSandboxStatsResponse)
40-
for _, sandbox := range sandboxes {
41-
sandboxStats, err := c.podSandboxStats(ctx, sandbox)
42-
switch {
43-
case errdefs.IsUnavailable(err), errdefs.IsNotFound(err):
44-
log.G(ctx).WithField("podsandboxid", sandbox.ID).Debugf("failed to get pod sandbox stats, this is likely a transient error: %v", err)
45-
case errors.Is(err, ttrpc.ErrClosed):
46-
log.G(ctx).WithField("podsandboxid", sandbox.ID).Debugf("failed to get pod sandbox stats, connection closed: %v", err)
47-
case err != nil:
48-
errs = append(errs, fmt.Errorf("failed to decode sandbox container metrics for sandbox %q: %w", sandbox.ID, err))
49-
default:
50-
podSandboxStats.Stats = append(podSandboxStats.Stats, sandboxStats)
62+
for _, stat := range stats {
63+
if stat != nil {
64+
podSandboxStats.Stats = append(podSandboxStats.Stats, stat)
5165
}
5266
}
5367

0 commit comments

Comments
 (0)