Skip to content

Commit

Permalink
fix: nil cache when get container
Browse files Browse the repository at this point in the history
Signed-off-by: zhuangqh <zhuangqhc@gmail.com>
  • Loading branch information
zhuangqh authored and fuweid committed Oct 25, 2019
1 parent 93cb0b1 commit ee4faa2
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions daemon/mgr/container_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/ctrd"
networktypes "github.com/alibaba/pouch/network/types"
"github.com/alibaba/pouch/pkg/collect"
"github.com/alibaba/pouch/pkg/errtypes"
"github.com/alibaba/pouch/pkg/meta"
"github.com/alibaba/pouch/pkg/randomid"
Expand Down Expand Up @@ -56,10 +57,24 @@ func (mgr *ContainerManager) containerID(nameOrPrefix string) (string, error) {
return con.ID, nil
}

func containerFromCache(cache *collect.SafeMap, key string) (*Container, error) {
res, exists := cache.Get(key).Result()
if exists {
// we may put nil to cache to hold the containerID when creating container.
v, ok := res.(*Container)
if ok {
return v, nil
}
return nil, errors.Wrapf(errtypes.ErrNotfound, "container %s creating", key)
}

return nil, errors.Wrapf(errtypes.ErrNotfound, "container %s", key)
}

func (mgr *ContainerManager) container(nameOrPrefix string) (*Container, error) {
res, ok := mgr.cache.Get(nameOrPrefix).Result()
if ok {
return res.(*Container), nil
res, err := containerFromCache(mgr.cache, nameOrPrefix)
if err == nil {
return res, nil
}

id, err := mgr.containerID(nameOrPrefix)
Expand All @@ -68,12 +83,7 @@ func (mgr *ContainerManager) container(nameOrPrefix string) (*Container, error)
}

// lookup again
res, ok = mgr.cache.Get(id).Result()
if ok {
return res.(*Container), nil
}

return nil, errors.Wrapf(errtypes.ErrNotfound, "container %s", nameOrPrefix)
return containerFromCache(mgr.cache, id)
}

// generateID generates an ID for newly created container. We must ensure that
Expand Down

0 comments on commit ee4faa2

Please sign in to comment.