Skip to content

Commit

Permalink
Merge pull request #3105 from bobbypage/retry-containerd-pid
Browse files Browse the repository at this point in the history
Retry containerd task api request when task is in unknown state
  • Loading branch information
bobbypage authored Apr 26, 2022
2 parents 808d2f5 + 798c75f commit 24e7a98
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 9 additions & 0 deletions container/containerd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package containerd

import (
"context"
"errors"
"fmt"
"net"
"sync"
Expand All @@ -24,6 +25,7 @@ import (
containersapi "github.com/containerd/containerd/api/services/containers/v1"
tasksapi "github.com/containerd/containerd/api/services/tasks/v1"
versionapi "github.com/containerd/containerd/api/services/version/v1"
tasktypes "github.com/containerd/containerd/api/types/task"
ptypes "github.com/gogo/protobuf/types"
"github.com/google/cadvisor/container/containerd/containers"
"github.com/google/cadvisor/container/containerd/errdefs"
Expand All @@ -44,6 +46,10 @@ type ContainerdClient interface {
Version(ctx context.Context) (string, error)
}

var (
ErrTaskIsInUnknownState = errors.New("containerd task is in unknown state") // used when process reported in containerd task is in Unknown State
)

var once sync.Once
var ctrdClient ContainerdClient = nil

Expand Down Expand Up @@ -114,6 +120,9 @@ func (c *client) TaskPid(ctx context.Context, id string) (uint32, error) {
if err != nil {
return 0, errdefs.FromGRPC(err)
}
if response.Process.Status == tasktypes.StatusUnknown {
return 0, ErrTaskIsInUnknownState
}
return response.Process.Pid, nil
}

Expand Down
9 changes: 7 additions & 2 deletions container/containerd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package containerd

import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -101,10 +102,14 @@ func newContainerdContainerHandler(
if err == nil {
break
}
retry--
if !errdefs.IsNotFound(err) || retry == 0 {

// Retry when task is not created yet or task is in unknown state (likely in process of initializing)
isRetriableError := errdefs.IsNotFound(err) || errors.Is(err, ErrTaskIsInUnknownState)
if !isRetriableError || retry == 0 {
return nil, err
}

retry--
time.Sleep(backoff)
backoff *= 2
}
Expand Down

0 comments on commit 24e7a98

Please sign in to comment.