Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Fix the issue of missing container information caused by the event sequence when docker compose is repeatedly up. (#1875)" #1962

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 23 additions & 32 deletions pkg/helper/docker_center.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package helper

import (
"context"
"errors"
"hash/fnv"
"path"
"regexp"
Expand Down Expand Up @@ -1023,35 +1022,6 @@ func (dc *DockerCenter) updateContainer(id string, container *DockerInfoDetail)
dc.refreshLastUpdateMapTime()
}

func (dc *DockerCenter) inspectOneContainer(containerID string) (types.ContainerJSON, error) {
var err error
var containerDetail types.ContainerJSON
for idx := 0; idx < 3; idx++ {
if containerDetail, err = dc.client.ContainerInspect(context.Background(), containerID); err == nil {
break
}
time.Sleep(time.Second * 5)
}
if err != nil {
dc.setLastError(err, "inspect container error "+containerID)
return types.ContainerJSON{}, err
}
if !ContainerProcessAlive(containerDetail.State.Pid) {
containerDetail.State.Status = ContainerStatusExited
finishedAt := containerDetail.State.FinishedAt
finishedAtTime, _ := time.Parse(time.RFC3339, finishedAt)
now := time.Now()
duration := now.Sub(finishedAtTime)
if duration >= ContainerInfoDeletedTimeout {
errMsg := "inspect time out container " + containerID
err = errors.New(errMsg)
dc.setLastError(err, errMsg)
return types.ContainerJSON{}, err
}
}
return containerDetail, nil
}

func (dc *DockerCenter) fetchAll() error {
dc.containerStateLock.Lock()
defer dc.containerStateLock.Unlock()
Expand All @@ -1065,9 +1035,26 @@ func (dc *DockerCenter) fetchAll() error {

for _, container := range containers {
var containerDetail types.ContainerJSON
containerDetail, err = dc.inspectOneContainer(container.ID)
for idx := 0; idx < 3; idx++ {
if containerDetail, err = dc.client.ContainerInspect(context.Background(), container.ID); err == nil {
break
}
time.Sleep(time.Second * 5)
}
if err == nil {
if !ContainerProcessAlive(containerDetail.State.Pid) {
containerDetail.State.Status = ContainerStatusExited
finishedAt := containerDetail.State.FinishedAt
finishedAtTime, _ := time.Parse(time.RFC3339, finishedAt)
now := time.Now()
duration := now.Sub(finishedAtTime)
if duration >= ContainerInfoDeletedTimeout {
continue
}
}
containerMap[container.ID] = dc.CreateInfoDetail(containerDetail, envConfigPrefix, false)
} else {
dc.setLastError(err, "inspect container error "+container.ID)
}
}
dc.updateContainers(containerMap)
Expand All @@ -1078,10 +1065,14 @@ func (dc *DockerCenter) fetchAll() error {
func (dc *DockerCenter) fetchOne(containerID string, tryFindSandbox bool) error {
dc.containerStateLock.Lock()
defer dc.containerStateLock.Unlock()
containerDetail, err := dc.inspectOneContainer(containerID)
containerDetail, err := dc.client.ContainerInspect(context.Background(), containerID)
if err != nil {
dc.setLastError(err, "inspect container error "+containerID)
return err
}
if containerDetail.State.Status == ContainerStatusRunning && !ContainerProcessAlive(containerDetail.State.Pid) {
containerDetail.State.Status = ContainerStatusExited
}
// docker 场景下
// tryFindSandbox如果是false, 那么fetchOne的地方应该会调用两次,一次是sandbox的id,一次是业务容器的id
// tryFindSandbox如果是true, 调用的地方只会有一个业务容器的id,然后依赖fetchOne内部把sandbox信息补全
Expand Down
Loading