Browse code

Fix update memory without memoryswap

The memory should always be smaller than memoryswap,
we should error out with message that user know how
to do rather than just an invalid argument error if
user update the memory limit bigger than already set
memory swap.

Signed-off-by: Lei Jitang <leijitang@huawei.com>

Lei Jitang authored on 2016/08/08 19:36:03
Showing 3 changed files
... ...
@@ -283,6 +283,11 @@ func (container *Container) UpdateContainer(hostConfig *containertypes.HostConfi
283 283
 		cResources.CpusetMems = resources.CpusetMems
284 284
 	}
285 285
 	if resources.Memory != 0 {
286
+		// if memory limit smaller than already set memoryswap limit and doesn't
287
+		// update the memoryswap limit, then error out.
288
+		if resources.Memory > cResources.MemorySwap && resources.MemorySwap == 0 {
289
+			return fmt.Errorf("Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same time")
290
+		}
286 291
 		cResources.Memory = resources.Memory
287 292
 	}
288 293
 	if resources.MemorySwap != 0 {
... ...
@@ -236,3 +236,17 @@ func (s *DockerSuite) TestUpdateStats(c *check.C) {
236 236
 	c.Assert(preMemLimit, checker.Equals, curMemLimit)
237 237
 
238 238
 }
239
+
240
+func (s *DockerSuite) TestUpdateMemoryWithSwapMemory(c *check.C) {
241
+	testRequires(c, DaemonIsLinux)
242
+	testRequires(c, memoryLimitSupport)
243
+	testRequires(c, swapMemorySupport)
244
+
245
+	name := "test-update-container"
246
+	dockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "busybox", "top")
247
+	out, _, err := dockerCmdWithError("update", "--memory", "800M", name)
248
+	c.Assert(err, checker.NotNil)
249
+	c.Assert(out, checker.Contains, "Memory limit should be smaller than already set memoryswap limit")
250
+
251
+	dockerCmd(c, "update", "--memory", "800M", "--memory-swap", "1000M", name)
252
+}
... ...
@@ -67,6 +67,12 @@ a running container with kernel memory initialized.
67 67
 **-m**, **--memory**=""
68 68
    Memory limit (format: <number><optional unit>, where unit = b, k, m or g)
69 69
 
70
+   Note that the memory should be smaller than the already set swap memory limit.
71
+   If you want update a memory limit bigger than the already set swap memory limit,
72
+   you should update swap memory limit at the same time. If you don't set swap memory 
73
+   limit on docker create/run but only memory limit, the swap memory is double
74
+   the memory limit.
75
+
70 76
 **--memory-reservation**=""
71 77
    Memory soft limit (format: <number>[<unit>], where unit = b, k, m or g)
72 78