mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #34004 from yummypeng/fix-docker-stats-hang
Return an empty stats if "container not found"
This commit is contained in:
commit
c8a2596d67
4 changed files with 36 additions and 12 deletions
|
@ -1165,6 +1165,9 @@ func (daemon *Daemon) stats(c *container.Container) (*types.StatsJSON, error) {
|
||||||
}
|
}
|
||||||
stats, err := daemon.containerd.Stats(c.ID)
|
stats, err := daemon.containerd.Stats(c.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if strings.Contains(err.Error(), "container not found") {
|
||||||
|
return nil, errNotFound{c.ID}
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
s := &types.StatsJSON{}
|
s := &types.StatsJSON{}
|
||||||
|
|
|
@ -525,6 +525,9 @@ func (daemon *Daemon) stats(c *container.Container) (*types.StatsJSON, error) {
|
||||||
// Obtain the stats from HCS via libcontainerd
|
// Obtain the stats from HCS via libcontainerd
|
||||||
stats, err := daemon.containerd.Stats(c.ID)
|
stats, err := daemon.containerd.Stats(c.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if strings.Contains(err.Error(), "container not found") {
|
||||||
|
return nil, errNotFound{c.ID}
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,3 +39,15 @@ func errExecPaused(id string) error {
|
||||||
err := fmt.Errorf("Container %s is paused, unpause the container before exec", id)
|
err := fmt.Errorf("Container %s is paused, unpause the container before exec", id)
|
||||||
return errors.NewRequestConflictError(err)
|
return errors.NewRequestConflictError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type errNotFound struct {
|
||||||
|
containerID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e errNotFound) Error() string {
|
||||||
|
return fmt.Sprintf("Container %s is not found", e.containerID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e errNotFound) ContainerNotFound() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
|
@ -88,24 +88,25 @@ func (s *Collector) Run() {
|
||||||
|
|
||||||
for _, pair := range pairs {
|
for _, pair := range pairs {
|
||||||
stats, err := s.supervisor.GetContainerStats(pair.container)
|
stats, err := s.supervisor.GetContainerStats(pair.container)
|
||||||
if err != nil {
|
|
||||||
if _, ok := err.(notRunningErr); !ok {
|
|
||||||
logrus.Errorf("collecting stats for %s: %v", pair.container.ID, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// publish empty stats containing only name and ID if not running
|
switch err.(type) {
|
||||||
pair.publisher.Publish(types.StatsJSON{
|
case nil:
|
||||||
Name: pair.container.Name,
|
|
||||||
ID: pair.container.ID,
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// FIXME: move to containerd on Linux (not Windows)
|
// FIXME: move to containerd on Linux (not Windows)
|
||||||
stats.CPUStats.SystemUsage = systemUsage
|
stats.CPUStats.SystemUsage = systemUsage
|
||||||
stats.CPUStats.OnlineCPUs = onlineCPUs
|
stats.CPUStats.OnlineCPUs = onlineCPUs
|
||||||
|
|
||||||
pair.publisher.Publish(*stats)
|
pair.publisher.Publish(*stats)
|
||||||
|
|
||||||
|
case notRunningErr, notFoundErr:
|
||||||
|
// publish empty stats containing only name and ID if not running or not found
|
||||||
|
pair.publisher.Publish(types.StatsJSON{
|
||||||
|
Name: pair.container.Name,
|
||||||
|
ID: pair.container.ID,
|
||||||
|
})
|
||||||
|
|
||||||
|
default:
|
||||||
|
logrus.Errorf("collecting stats for %s: %v", pair.container.ID, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -114,3 +115,8 @@ type notRunningErr interface {
|
||||||
error
|
error
|
||||||
ContainerIsRunning() bool
|
ContainerIsRunning() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type notFoundErr interface {
|
||||||
|
error
|
||||||
|
ContainerNotFound() bool
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue