Skip to content

Commit

Permalink
Return an empty stats if "container not found"
Browse files Browse the repository at this point in the history
If we get "container not found" error from containerd, it's possibly
because that this container has already been stopped. It will be ok to
ignore this error and just return an empty stats.

fix DTS2017062812251
related upstream PR: moby#34004

Signed-off-by: Yuanhong Peng <pengyuanhong@huawei.com>
  • Loading branch information
Yuanhong Peng committed Jul 10, 2017
1 parent dd719af commit 7690ab3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
3 changes: 3 additions & 0 deletions daemon/daemon_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,9 @@ func (daemon *Daemon) stats(c *container.Container) (*types.StatsJSON, error) {
}
stats, err := daemon.containerd.Stats(c.ID)
if err != nil {
if strings.Contains(err.Error(), "container not found") {
return nil, errNotFound{c.ID}
}
return nil, err
}
s := &types.StatsJSON{}
Expand Down
8 changes: 8 additions & 0 deletions daemon/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ func errExecPaused(id string) error {
err := fmt.Errorf("Container %s is paused, unpause the container before exec", id)
return errors.NewRequestConflictError(err)
}

type errNotFound struct {
containerID string
}

func (e errNotFound) Error() string {
return fmt.Sprintf("Container %s is not found", e.containerID)
}
23 changes: 12 additions & 11 deletions daemon/stats_collector_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,21 @@ func (s *statsCollector) run() {

for _, pair := range pairs {
stats, err := s.supervisor.GetContainerStats(pair.container)
if err != nil {
if _, ok := err.(errNotRunning); !ok {
logrus.Errorf("collecting stats for %s: %v", pair.container.ID, err)
continue
}

// publish empty stats ID if not running
switch err.(type) {
case nil:
// FIXME: move to containerd
stats.CPUStats.SystemUsage = systemUsage

pair.publisher.Publish(*stats)

case errNotRunning, errNotFound:
// publish empty stats ID if not running or not found
pair.publisher.Publish(types.StatsJSON{})
continue
}
// FIXME: move to containerd
stats.CPUStats.SystemUsage = systemUsage

pair.publisher.Publish(*stats)
default:
logrus.Errorf("collecting stats for %s: %v", pair.container.ID, err)
}
}
}
}
Expand Down

0 comments on commit 7690ab3

Please sign in to comment.