Browse code

Merge pull request #18285 from hqhq/hq_fix_swappiness

Set default MemorySwappiness when adapt

Brian Goff authored on 2015/12/03 04:25:08
Showing 6 changed files
... ...
@@ -301,11 +301,7 @@ func (daemon *Daemon) populateCommand(c *Container, env []string) error {
301 301
 		Rlimits:           rlimits,
302 302
 		BlkioWeightDevice: weightDevices,
303 303
 		OomKillDisable:    c.hostConfig.OomKillDisable,
304
-		MemorySwappiness:  -1,
305
-	}
306
-
307
-	if c.hostConfig.MemorySwappiness != nil {
308
-		resources.MemorySwappiness = *c.hostConfig.MemorySwappiness
304
+		MemorySwappiness:  *c.hostConfig.MemorySwappiness,
309 305
 	}
310 306
 
311 307
 	processConfig := execdriver.ProcessConfig{
... ...
@@ -31,7 +31,13 @@ func (daemon *Daemon) ContainerCreate(params *ContainerCreateConfig) (types.Cont
31 31
 		return types.ContainerCreateResponse{ID: "", Warnings: warnings}, err
32 32
 	}
33 33
 
34
-	daemon.adaptContainerSettings(params.HostConfig, params.AdjustCPUShares)
34
+	if params.HostConfig == nil {
35
+		params.HostConfig = &runconfig.HostConfig{}
36
+	}
37
+	err = daemon.adaptContainerSettings(params.HostConfig, params.AdjustCPUShares)
38
+	if err != nil {
39
+		return types.ContainerCreateResponse{ID: "", Warnings: warnings}, err
40
+	}
35 41
 
36 42
 	container, err := daemon.create(params)
37 43
 	if err != nil {
... ...
@@ -62,15 +68,6 @@ func (daemon *Daemon) create(params *ContainerCreateConfig) (retC *Container, re
62 62
 		return nil, err
63 63
 	}
64 64
 
65
-	if params.HostConfig == nil {
66
-		params.HostConfig = &runconfig.HostConfig{}
67
-	}
68
-	if params.HostConfig.SecurityOpt == nil {
69
-		params.HostConfig.SecurityOpt, err = daemon.generateSecurityOpt(params.HostConfig.IpcMode, params.HostConfig.PidMode)
70
-		if err != nil {
71
-			return nil, err
72
-		}
73
-	}
74 65
 	if container, err = daemon.newContainer(params.Name, params.Config, imgID); err != nil {
75 66
 		return nil, err
76 67
 	}
... ...
@@ -112,11 +112,7 @@ func checkKernel() error {
112 112
 
113 113
 // adaptContainerSettings is called during container creation to modify any
114 114
 // settings necessary in the HostConfig structure.
115
-func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) {
116
-	if hostConfig == nil {
117
-		return
118
-	}
119
-
115
+func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) error {
120 116
 	if adjustCPUShares && hostConfig.CPUShares > 0 {
121 117
 		// Handle unsupported CPUShares
122 118
 		if hostConfig.CPUShares < linuxMinCPUShares {
... ...
@@ -135,6 +131,19 @@ func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, a
135 135
 		shmSize := DefaultSHMSize
136 136
 		hostConfig.ShmSize = &shmSize
137 137
 	}
138
+	var err error
139
+	if hostConfig.SecurityOpt == nil {
140
+		hostConfig.SecurityOpt, err = daemon.generateSecurityOpt(hostConfig.IpcMode, hostConfig.PidMode)
141
+		if err != nil {
142
+			return err
143
+		}
144
+	}
145
+	if hostConfig.MemorySwappiness == nil {
146
+		defaultSwappiness := int64(-1)
147
+		hostConfig.MemorySwappiness = &defaultSwappiness
148
+	}
149
+
150
+	return nil
138 151
 }
139 152
 
140 153
 // verifyPlatformContainerSettings performs platform-specific validation of the
... ...
@@ -48,9 +48,9 @@ func checkKernel() error {
48 48
 
49 49
 // adaptContainerSettings is called during container creation to modify any
50 50
 // settings necessary in the HostConfig structure.
51
-func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) {
51
+func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) error {
52 52
 	if hostConfig == nil {
53
-		return
53
+		return nil
54 54
 	}
55 55
 
56 56
 	if hostConfig.CPUShares < 0 {
... ...
@@ -60,6 +60,8 @@ func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, a
60 60
 		logrus.Warnf("Changing requested CPUShares of %d to maximum allowed of %d", hostConfig.CPUShares, windowsMaxCPUShares)
61 61
 		hostConfig.CPUShares = windowsMaxCPUShares
62 62
 	}
63
+
64
+	return nil
63 65
 }
64 66
 
65 67
 // verifyPlatformContainerSettings performs platform-specific validation of the
... ...
@@ -36,6 +36,9 @@ func (daemon *Daemon) ContainerStart(name string, hostConfig *runconfig.HostConf
36 36
 				return err
37 37
 			}
38 38
 			container.Unlock()
39
+			if err := daemon.adaptContainerSettings(hostConfig, false); err != nil {
40
+				return err
41
+			}
39 42
 			if err := daemon.setHostConfig(container, hostConfig); err != nil {
40 43
 				return err
41 44
 			}
... ...
@@ -1434,7 +1434,7 @@ func (s *DockerSuite) TestPostContainersCreateShmSizeHostConfigOmitted(c *check.
1434 1434
 	var containerJSON types.ContainerJSON
1435 1435
 	c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
1436 1436
 
1437
-	c.Assert(containerJSON.HostConfig.ShmSize, check.IsNil)
1437
+	c.Assert(*containerJSON.HostConfig.ShmSize, check.Equals, runconfig.DefaultSHMSize)
1438 1438
 
1439 1439
 	out, _ := dockerCmd(c, "start", "-i", containerJSON.ID)
1440 1440
 	shmRegexp := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
... ...
@@ -1522,5 +1522,5 @@ func (s *DockerSuite) TestPostContainersCreateMemorySwappinessHostConfigOmitted(
1522 1522
 	var containerJSON types.ContainerJSON
1523 1523
 	c.Assert(json.Unmarshal(body, &containerJSON), check.IsNil)
1524 1524
 
1525
-	c.Assert(containerJSON.HostConfig.MemorySwappiness, check.IsNil)
1525
+	c.Assert(*containerJSON.HostConfig.MemorySwappiness, check.Equals, int64(-1))
1526 1526
 }