Skip to content

Commit

Permalink
Retry containerd task api request when task is in unknown state
Browse files Browse the repository at this point in the history
In some cases, it appears that containerd task api can return a zero pid
when the task is not ready yet. In these cases, the process task state
is reported to be in unknown state. In this case, we should retry the
containerd task api request as the task can take some time to move away
from unknown state and be initialized.

Signed-off-by: David Porter <porterdavid@google.com>
  • Loading branch information
bobbypage committed Apr 25, 2022
1 parent 808d2f5 commit 6e73251
Show file tree
Hide file tree
Showing 2 changed files with 15 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 containerd task api is in nUnknownState
)

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
8 changes: 6 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,13 @@ func newContainerdContainerHandler(
if err == nil {
break
}
retry--
if !errdefs.IsNotFound(err) || retry == 0 {

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 6e73251

Please sign in to comment.