Skip to content

Commit

Permalink
optimize lock in containerUpdate function
Browse files Browse the repository at this point in the history
Signed-off-by: allen.wang <allen.wq@alipay.com>
  • Loading branch information
wangforthinker committed Feb 26, 2019
1 parent 9f96857 commit 080430d
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1086,15 +1086,16 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty
logrus.Warnf("warnings update %s: %v", name, warnings)
}

c.Lock()
defer c.Unlock()

restore := false
oldConfig := *c.Config
oldHostconfig := *c.HostConfig
defer func() {
if restore {
c.Lock()
c.Config = &oldConfig
c.HostConfig = &oldHostconfig
c.Unlock()
}
}()

Expand All @@ -1107,8 +1108,6 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty
return errors.Wrapf(err, "failed to update diskquota of container %s", c.ID)
}

c.Lock()

// init Container Labels
if c.Config.Labels == nil {
c.Config.Labels = map[string]string{}
Expand All @@ -1131,20 +1130,17 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty
}
}
}
c.Unlock()

// update Resources of a container.
if err := mgr.updateContainerResources(c, config.Resources); err != nil {
restore = true
return errors.Wrapf(err, "failed to update resource of container %s", c.ID)
}

c.Lock()
// TODO update restartpolicy when container is running.
if config.RestartPolicy != nil && config.RestartPolicy.Name != "" {
c.HostConfig.RestartPolicy = config.RestartPolicy
}
c.Unlock()

// Update Env
newEnvSlice, err := mergeEnvSlice(config.Env, c.Config.Env)
Expand All @@ -1163,11 +1159,9 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty
}
}

c.Lock()
if len(config.SpecAnnotation) > 0 {
c.Config.SpecAnnotation = mergeAnnotation(config.SpecAnnotation, c.Config.SpecAnnotation)
}
c.Unlock()

if mgr.containerPlugin != nil && len(config.Env) > 0 {
if err = mgr.containerPlugin.PostUpdate(c.BaseFS, c.Config.Env); err != nil {
Expand Down Expand Up @@ -1273,18 +1267,15 @@ func (mgr *ContainerManager) updateContainerDiskQuota(ctx context.Context, c *Co
return nil
}

c.Lock()
for dir, quota := range diskQuota {
c.Config.DiskQuota[dir] = quota
}
c.Unlock()

// set mount point disk quota
if err := mgr.setMountPointDiskQuota(ctx, c); err != nil {
return errors.Wrapf(err, "failed to set mount point disk quota")
}

c.Lock()
var qid uint32
if c.Config.QuotaID != "" {
id, err := strconv.Atoi(c.Config.QuotaID)
Expand All @@ -1304,7 +1295,6 @@ func (mgr *ContainerManager) updateContainerDiskQuota(ctx context.Context, c *Co
c.Config.QuotaID = strconv.Itoa(int(qid))
}
}
c.Unlock()

// get rootfs quota
defaultQuota := quota.GetDefaultQuota(c.Config.DiskQuota)
Expand Down Expand Up @@ -1342,8 +1332,6 @@ func (mgr *ContainerManager) updateContainerDiskQuota(ctx context.Context, c *Co

// updateContainerResources update container's resources parameters.
func (mgr *ContainerManager) updateContainerResources(c *Container, resources types.Resources) error {
c.Lock()
defer c.Unlock()
// update resources of container.
cResources := &c.HostConfig.Resources
if resources.BlkioWeight != 0 {
Expand Down

0 comments on commit 080430d

Please sign in to comment.