浏览代码

Merge pull request #18285 from hqhq/hq_fix_swappiness

Set default MemorySwappiness when adapt
Brian Goff 9 年之前
父节点
当前提交
f411b101ac
共有 6 个文件被更改,包括 31 次插入24 次删除
  1. 1 5
      daemon/container_unix.go
  2. 7 10
      daemon/create.go
  3. 14 5
      daemon/daemon_unix.go
  4. 4 2
      daemon/daemon_windows.go
  5. 3 0
      daemon/start.go
  6. 2 2
      integration-cli/docker_api_containers_test.go

+ 1 - 5
daemon/container_unix.go

@@ -301,11 +301,7 @@ func (daemon *Daemon) populateCommand(c *Container, env []string) error {
 		Rlimits:           rlimits,
 		Rlimits:           rlimits,
 		BlkioWeightDevice: weightDevices,
 		BlkioWeightDevice: weightDevices,
 		OomKillDisable:    c.hostConfig.OomKillDisable,
 		OomKillDisable:    c.hostConfig.OomKillDisable,
-		MemorySwappiness:  -1,
-	}
-
-	if c.hostConfig.MemorySwappiness != nil {
-		resources.MemorySwappiness = *c.hostConfig.MemorySwappiness
+		MemorySwappiness:  *c.hostConfig.MemorySwappiness,
 	}
 	}
 
 
 	processConfig := execdriver.ProcessConfig{
 	processConfig := execdriver.ProcessConfig{

+ 7 - 10
daemon/create.go

@@ -31,7 +31,13 @@ func (daemon *Daemon) ContainerCreate(params *ContainerCreateConfig) (types.Cont
 		return types.ContainerCreateResponse{ID: "", Warnings: warnings}, err
 		return types.ContainerCreateResponse{ID: "", Warnings: warnings}, err
 	}
 	}
 
 
-	daemon.adaptContainerSettings(params.HostConfig, params.AdjustCPUShares)
+	if params.HostConfig == nil {
+		params.HostConfig = &runconfig.HostConfig{}
+	}
+	err = daemon.adaptContainerSettings(params.HostConfig, params.AdjustCPUShares)
+	if err != nil {
+		return types.ContainerCreateResponse{ID: "", Warnings: warnings}, err
+	}
 
 
 	container, err := daemon.create(params)
 	container, err := daemon.create(params)
 	if err != nil {
 	if err != nil {
@@ -62,15 +68,6 @@ func (daemon *Daemon) create(params *ContainerCreateConfig) (retC *Container, re
 		return nil, err
 		return nil, err
 	}
 	}
 
 
-	if params.HostConfig == nil {
-		params.HostConfig = &runconfig.HostConfig{}
-	}
-	if params.HostConfig.SecurityOpt == nil {
-		params.HostConfig.SecurityOpt, err = daemon.generateSecurityOpt(params.HostConfig.IpcMode, params.HostConfig.PidMode)
-		if err != nil {
-			return nil, err
-		}
-	}
 	if container, err = daemon.newContainer(params.Name, params.Config, imgID); err != nil {
 	if container, err = daemon.newContainer(params.Name, params.Config, imgID); err != nil {
 		return nil, err
 		return nil, err
 	}
 	}

+ 14 - 5
daemon/daemon_unix.go

@@ -112,11 +112,7 @@ func checkKernel() error {
 
 
 // adaptContainerSettings is called during container creation to modify any
 // adaptContainerSettings is called during container creation to modify any
 // settings necessary in the HostConfig structure.
 // settings necessary in the HostConfig structure.
-func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) {
-	if hostConfig == nil {
-		return
-	}
-
+func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) error {
 	if adjustCPUShares && hostConfig.CPUShares > 0 {
 	if adjustCPUShares && hostConfig.CPUShares > 0 {
 		// Handle unsupported CPUShares
 		// Handle unsupported CPUShares
 		if hostConfig.CPUShares < linuxMinCPUShares {
 		if hostConfig.CPUShares < linuxMinCPUShares {
@@ -135,6 +131,19 @@ func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, a
 		shmSize := DefaultSHMSize
 		shmSize := DefaultSHMSize
 		hostConfig.ShmSize = &shmSize
 		hostConfig.ShmSize = &shmSize
 	}
 	}
+	var err error
+	if hostConfig.SecurityOpt == nil {
+		hostConfig.SecurityOpt, err = daemon.generateSecurityOpt(hostConfig.IpcMode, hostConfig.PidMode)
+		if err != nil {
+			return err
+		}
+	}
+	if hostConfig.MemorySwappiness == nil {
+		defaultSwappiness := int64(-1)
+		hostConfig.MemorySwappiness = &defaultSwappiness
+	}
+
+	return nil
 }
 }
 
 
 // verifyPlatformContainerSettings performs platform-specific validation of the
 // verifyPlatformContainerSettings performs platform-specific validation of the

+ 4 - 2
daemon/daemon_windows.go

@@ -48,9 +48,9 @@ func checkKernel() error {
 
 
 // adaptContainerSettings is called during container creation to modify any
 // adaptContainerSettings is called during container creation to modify any
 // settings necessary in the HostConfig structure.
 // settings necessary in the HostConfig structure.
-func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) {
+func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) error {
 	if hostConfig == nil {
 	if hostConfig == nil {
-		return
+		return nil
 	}
 	}
 
 
 	if hostConfig.CPUShares < 0 {
 	if hostConfig.CPUShares < 0 {
@@ -60,6 +60,8 @@ func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, a
 		logrus.Warnf("Changing requested CPUShares of %d to maximum allowed of %d", hostConfig.CPUShares, windowsMaxCPUShares)
 		logrus.Warnf("Changing requested CPUShares of %d to maximum allowed of %d", hostConfig.CPUShares, windowsMaxCPUShares)
 		hostConfig.CPUShares = windowsMaxCPUShares
 		hostConfig.CPUShares = windowsMaxCPUShares
 	}
 	}
+
+	return nil
 }
 }
 
 
 // verifyPlatformContainerSettings performs platform-specific validation of the
 // verifyPlatformContainerSettings performs platform-specific validation of the

+ 3 - 0
daemon/start.go

@@ -36,6 +36,9 @@ func (daemon *Daemon) ContainerStart(name string, hostConfig *runconfig.HostConf
 				return err
 				return err
 			}
 			}
 			container.Unlock()
 			container.Unlock()
+			if err := daemon.adaptContainerSettings(hostConfig, false); err != nil {
+				return err
+			}
 			if err := daemon.setHostConfig(container, hostConfig); err != nil {
 			if err := daemon.setHostConfig(container, hostConfig); err != nil {
 				return err
 				return err
 			}
 			}

+ 2 - 2
integration-cli/docker_api_containers_test.go

@@ -1434,7 +1434,7 @@ func (s *DockerSuite) TestPostContainersCreateShmSizeHostConfigOmitted(c *check.
 	var containerJSON types.ContainerJSON
 	var containerJSON types.ContainerJSON
 	c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
 	c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
 
 
-	c.Assert(containerJSON.HostConfig.ShmSize, check.IsNil)
+	c.Assert(*containerJSON.HostConfig.ShmSize, check.Equals, runconfig.DefaultSHMSize)
 
 
 	out, _ := dockerCmd(c, "start", "-i", containerJSON.ID)
 	out, _ := dockerCmd(c, "start", "-i", containerJSON.ID)
 	shmRegexp := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
 	shmRegexp := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
@@ -1522,5 +1522,5 @@ func (s *DockerSuite) TestPostContainersCreateMemorySwappinessHostConfigOmitted(
 	var containerJSON types.ContainerJSON
 	var containerJSON types.ContainerJSON
 	c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
 	c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
 
 
-	c.Assert(containerJSON.HostConfig.MemorySwappiness, check.IsNil)
+	c.Assert(*containerJSON.HostConfig.MemorySwappiness, check.Equals, int64(-1))
 }
 }