From 56b0b08a5b832c7a016c753c95f7caa3578c7838 Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Mon, 16 Apr 2018 03:27:39 +0000 Subject: [PATCH] Change the format of memory field In before, the container's memory field is displayed as '512M'. The original intent is to support other memory unit, such as 'K', 'G', etc. However, this will make the code more complicated since it needs to handle many possible inputs. Let's use 'M' as the only memory unit, then remove the tailing 'M' since the information is redundant. Depends-On: Ief00d4899a9628967aa6ea875aa4195cc4f2bfc4 Closes-Bug: #1764923 Change-Id: I5f586ccc73f53a5f6e6e4d341251a5288a8687f2 --- zun/api/controllers/v1/capsules.py | 4 ++-- zun/api/controllers/v1/containers.py | 4 ++-- zun/compute/claims.py | 13 +++---------- zun/compute/compute_node_tracker.py | 2 +- zun/container/docker/driver.py | 4 ++-- zun/scheduler/filters/ram_filter.py | 2 +- .../unit/api/controllers/v1/test_capsules.py | 10 +++++----- .../unit/api/controllers/v1/test_containers.py | 16 ++++++++-------- .../unit/container/docker/test_docker_driver.py | 4 ++-- zun/tests/unit/db/utils.py | 4 ++-- .../unit/scheduler/filters/test_ram_filter.py | 4 ++-- 11 files changed, 30 insertions(+), 37 deletions(-) diff --git a/zun/api/controllers/v1/capsules.py b/zun/api/controllers/v1/capsules.py index 4d14dcadc..e69a24be5 100644 --- a/zun/api/controllers/v1/capsules.py +++ b/zun/api/controllers/v1/capsules.py @@ -222,7 +222,7 @@ class CapsuleController(base.Controller): container_dict['cpu'] = allocation.get('cpu') if allocation.get('memory'): capsule_need_memory += allocation.get('memory') - container_dict['memory'] = str(allocation['memory']) + 'M' + container_dict['memory'] = str(allocation['memory']) container_dict.pop('resources') container_dict['restart_policy'] = container_restart_policy @@ -247,7 +247,7 @@ class CapsuleController(base.Controller): container_volume_requests, new_capsule) new_capsule.cpu = capsule_need_cpu - new_capsule.memory = str(capsule_need_memory) + 'M' + new_capsule.memory = str(capsule_need_memory) new_capsule.save(context) compute_api.capsule_create(context, new_capsule, requested_networks, requested_volumes, extra_spec) diff --git a/zun/api/controllers/v1/containers.py b/zun/api/controllers/v1/containers.py index aced05acc..5eca510f6 100644 --- a/zun/api/controllers/v1/containers.py +++ b/zun/api/controllers/v1/containers.py @@ -367,7 +367,7 @@ class ContainersController(base.Controller): container_dict['disk'] = container_dict.get('disk') container_dict['memory'] = container_dict.get( 'memory', CONF.default_memory) - container_dict['memory'] = str(container_dict['memory']) + 'M' + container_dict['memory'] = str(container_dict['memory']) container_dict['cpu'] = container_dict.get( 'cpu', CONF.default_cpu) @@ -553,7 +553,7 @@ class ContainersController(base.Controller): check_policy_on_container(container.as_dict(), "container:update") utils.validate_container_state(container, 'update') if 'memory' in patch: - patch['memory'] = str(patch['memory']) + 'M' + patch['memory'] = str(patch['memory']) if 'cpu' in patch: patch['cpu'] = float(patch['cpu']) if 'name' in patch: diff --git a/zun/compute/claims.py b/zun/compute/claims.py index a806f073a..80c5e8307 100644 --- a/zun/compute/claims.py +++ b/zun/compute/claims.py @@ -88,10 +88,7 @@ class Claim(NopClaim): @property def memory(self): - mem_str = "0" - if self.container.memory: - mem_str = self.container.memory[:-1] - return int(mem_str) + return int(self.container.memory or "0") @property def cpu(self): @@ -224,12 +221,8 @@ class UpdateClaim(Claim): @property def memory(self): - new_mem_str = "0" - if self.new_container.memory: - new_mem_str = self.new_container.memory[:-1] - old_mem_str = "0" - if self.old_container.memory: - old_mem_str = self.old_container.memory[:-1] + new_mem_str = self.new_container.memory or "0" + old_mem_str = self.old_container.memory or "0" return int(new_mem_str) - int(old_mem_str) @property diff --git a/zun/compute/compute_node_tracker.py b/zun/compute/compute_node_tracker.py index 90292303a..0fcd21a5e 100644 --- a/zun/compute/compute_node_tracker.py +++ b/zun/compute/compute_node_tracker.py @@ -297,7 +297,7 @@ class ComputeNodeTracker(object): # (Fixme): The Container.memory is string. memory = 0 if container.memory: - memory = int(container.memory[:-1]) + memory = int(container.memory) usage = {'memory': memory, 'cpu': container.cpu or 0, 'disk': container.disk or 0} diff --git a/zun/container/docker/driver.py b/zun/container/docker/driver.py index 02a58de3e..ecbc768fd 100644 --- a/zun/container/docker/driver.py +++ b/zun/container/docker/driver.py @@ -245,7 +245,7 @@ class DockerDriver(driver.ContainerDriver): if container.auto_remove: host_config['auto_remove'] = container.auto_remove if container.memory is not None: - host_config['mem_limit'] = container.memory + host_config['mem_limit'] = str(container.memory) + 'M' if container.cpu is not None: host_config['cpu_quota'] = int(100000 * container.cpu) host_config['cpu_period'] = 100000 @@ -784,7 +784,7 @@ class DockerDriver(driver.ContainerDriver): args = {} memory = patch.get('memory') if memory is not None: - args['mem_limit'] = memory + args['mem_limit'] = str(memory) + 'M' args['memswap_limit'] = CONF.default_memory_swap cpu = patch.get('cpu') if cpu is not None: diff --git a/zun/scheduler/filters/ram_filter.py b/zun/scheduler/filters/ram_filter.py index acc4a1c94..7f0da34b7 100644 --- a/zun/scheduler/filters/ram_filter.py +++ b/zun/scheduler/filters/ram_filter.py @@ -29,7 +29,7 @@ class RamFilter(filters.BaseHostFilter): if not container.memory: return True - request_ram = int(container.memory[:-1]) + request_ram = int(container.memory) usable_ram = host_state.mem_total - host_state.mem_used if usable_ram < request_ram: LOG.debug("%(host_state)s does not have %(request_ram)d " diff --git a/zun/tests/unit/api/controllers/v1/test_capsules.py b/zun/tests/unit/api/controllers/v1/test_capsules.py index 52bcbf3ba..1b5dd6a7c 100644 --- a/zun/tests/unit/api/controllers/v1/test_capsules.py +++ b/zun/tests/unit/api/controllers/v1/test_capsules.py @@ -48,7 +48,7 @@ class TestCapsuleController(api_base.FunctionalTest): return_value = response.json expected_meta_name = "capsule-example" expected_meta_labels = {"foo0": "bar0", "foo1": "bar1"} - expected_memory = '1024M' + expected_memory = '1024' expected_cpu = 1.0 expected_container_num = 2 self.assertEqual(len(return_value["containers_uuids"]), @@ -85,7 +85,7 @@ class TestCapsuleController(api_base.FunctionalTest): return_value = response.json expected_meta_name = "capsule-example" expected_meta_labels = {"foo0": "bar0"} - expected_memory = '2048M' + expected_memory = '2048' expected_cpu = 2.0 expected_container_num = 3 self.assertEqual(len(return_value["containers_uuids"]), @@ -199,7 +199,7 @@ class TestCapsuleController(api_base.FunctionalTest): return_value = response.json expected_meta_name = "capsule-example" expected_meta_labels = {"foo0": "bar0", "foo1": "bar1"} - expected_memory = '1024M' + expected_memory = '1024' expected_cpu = 1.0 expected_container_num = 2 self.assertEqual(len(return_value["containers_uuids"]), @@ -253,7 +253,7 @@ class TestCapsuleController(api_base.FunctionalTest): return_value = response.json expected_meta_name = "capsule-example" expected_meta_labels = {"foo0": "bar0", "foo1": "bar1"} - expected_memory = '1024M' + expected_memory = '1024' expected_cpu = 1.0 expected_container_num = 2 self.assertEqual(len(return_value["containers_uuids"]), @@ -316,7 +316,7 @@ class TestCapsuleController(api_base.FunctionalTest): return_value = response.json expected_meta_name = "capsule-example" expected_meta_labels = {"foo0": "bar0", "foo1": "bar1"} - expected_memory = '1024M' + expected_memory = '1024' expected_cpu = 1.0 expected_container_num = 2 self.assertEqual(len(return_value["containers_uuids"]), diff --git a/zun/tests/unit/api/controllers/v1/test_containers.py b/zun/tests/unit/api/controllers/v1/test_containers.py index a4192f5be..274d3f498 100644 --- a/zun/tests/unit/api/controllers/v1/test_containers.py +++ b/zun/tests/unit/api/controllers/v1/test_containers.py @@ -261,7 +261,7 @@ class TestContainerController(api_base.FunctionalTest): self.assertIsNotNone(c.get('uuid')) self.assertEqual('MyDocker', c.get('name')) self.assertEqual('env', c.get('command')) - self.assertEqual('512M', c.get('memory')) + self.assertEqual('512', c.get('memory')) self.assertEqual({"key1": "val1", "key2": "val2"}, c.get('environment')) self.assertEqual('runc', c.get('runtime')) @@ -318,7 +318,7 @@ class TestContainerController(api_base.FunctionalTest): self.assertIsNotNone(c.get('uuid')) self.assertIsNotNone(c.get('name')) self.assertIsNone(c.get('command')) - self.assertEqual('512M', c.get('memory')) + self.assertEqual('512', c.get('memory')) self.assertEqual(1.0, c.get('cpu')) # TODO(kiennt): Uncomment it when bug [1] be resolved. # At this time, limit disk size feature will be ready. @@ -358,7 +358,7 @@ class TestContainerController(api_base.FunctionalTest): self.assertIsNotNone(c.get('uuid')) self.assertEqual('MyDocker', c.get('name')) self.assertEqual('env', c.get('command')) - self.assertEqual('512M', c.get('memory')) + self.assertEqual('512', c.get('memory')) self.assertEqual(1.0, c.get('cpu')) # TODO(kiennt): Uncomment it when bug [1] be resolved. # At this time, limit disk size feature will be ready. @@ -397,7 +397,7 @@ class TestContainerController(api_base.FunctionalTest): self.assertIsNotNone(c.get('uuid')) self.assertEqual('MyDocker', c.get('name')) self.assertEqual('env', c.get('command')) - self.assertEqual('512M', c.get('memory')) + self.assertEqual('512', c.get('memory')) self.assertEqual({"Name": "no", "MaximumRetryCount": "0"}, c.get('restart_policy')) mock_neutron_get_network.assert_called_once() @@ -433,7 +433,7 @@ class TestContainerController(api_base.FunctionalTest): self.assertIsNotNone(c.get('uuid')) self.assertEqual('MyDocker', c.get('name')) self.assertEqual('env', c.get('command')) - self.assertEqual('512M', c.get('memory')) + self.assertEqual('512', c.get('memory')) self.assertEqual({"Name": "no", "MaximumRetryCount": "0"}, c.get('restart_policy')) mock_neutron_get_network.assert_called_once() @@ -469,7 +469,7 @@ class TestContainerController(api_base.FunctionalTest): self.assertIsNotNone(c.get('uuid')) self.assertEqual('MyDocker', c.get('name')) self.assertEqual('env', c.get('command')) - self.assertEqual('512M', c.get('memory')) + self.assertEqual('512', c.get('memory')) self.assertEqual({"Name": "unless-stopped", "MaximumRetryCount": "0"}, c.get('restart_policy')) mock_neutron_get_network.assert_called_once() @@ -513,7 +513,7 @@ class TestContainerController(api_base.FunctionalTest): self.assertIsNotNone(c.get('uuid')) self.assertEqual('MyDocker', c.get('name')) self.assertEqual('env', c.get('command')) - self.assertEqual('512M', c.get('memory')) + self.assertEqual('512', c.get('memory')) self.assertEqual({"key1": "val1", "key2": "val2"}, c.get('environment')) requested_networks = \ @@ -637,7 +637,7 @@ class TestContainerController(api_base.FunctionalTest): self.assertEqual('MyDocker', c.get('name')) self.assertEqual('env', c.get('command')) self.assertEqual('Creating', c.get('status')) - self.assertEqual('512M', c.get('memory')) + self.assertEqual('512', c.get('memory')) requested_networks = \ mock_container_create.call_args[1]['requested_networks'] self.assertEqual(1, len(requested_networks)) diff --git a/zun/tests/unit/container/docker/test_docker_driver.py b/zun/tests/unit/container/docker/test_docker_driver.py index 6f32975f8..931232b89 100644 --- a/zun/tests/unit/container/docker/test_docker_driver.py +++ b/zun/tests/unit/container/docker/test_docker_driver.py @@ -124,7 +124,7 @@ class TestDockerDriver(base.DriverTestCase): result_container = self.driver.create(self.context, mock_container, image, networks, volumes) host_config = {} - host_config['mem_limit'] = '512m' + host_config['mem_limit'] = '512M' host_config['cpu_quota'] = 100000 host_config['cpu_period'] = 100000 host_config['restart_policy'] = {'Name': 'no', 'MaximumRetryCount': 0} @@ -190,7 +190,7 @@ class TestDockerDriver(base.DriverTestCase): result_container = self.driver.create(self.context, mock_container, image, networks, volumes) host_config = {} - host_config['mem_limit'] = '512m' + host_config['mem_limit'] = '512M' host_config['cpu_quota'] = 100000 host_config['cpu_period'] = 100000 host_config['restart_policy'] = {'Name': 'no', 'MaximumRetryCount': 0} diff --git a/zun/tests/unit/db/utils.py b/zun/tests/unit/db/utils.py index 76e05b8ec..bd345f30a 100644 --- a/zun/tests/unit/db/utils.py +++ b/zun/tests/unit/db/utils.py @@ -61,7 +61,7 @@ def get_test_container(**kwargs): 'environment': kwargs.get('environment', {'key1': 'val1', 'key2': 'val2'}), 'cpu': kwargs.get('cpu', 1.0), - 'memory': kwargs.get('memory', '512m'), + 'memory': kwargs.get('memory', '512'), 'workdir': kwargs.get('workdir', '/home/ubuntu'), 'ports': kwargs.get('ports', [80, 443]), 'hostname': kwargs.get('hostname', 'testhost'), @@ -395,7 +395,7 @@ def get_test_capsule(**kwargs): 'status': kwargs.get('status', 'Running'), 'status_reason': kwargs.get('status_reason', 'Created Successfully'), 'cpu': kwargs.get('cpu', 1.0), - 'memory': kwargs.get('memory', '512m'), + 'memory': kwargs.get('memory', '512'), 'spec': kwargs.get('spec', CAPSULE_SPEC), 'meta_name': kwargs.get('meta_name', "fake-meta-name"), 'meta_labels': kwargs.get('meta_labels', {'key1': 'val1', diff --git a/zun/tests/unit/scheduler/filters/test_ram_filter.py b/zun/tests/unit/scheduler/filters/test_ram_filter.py index add926bee..56578bfbe 100644 --- a/zun/tests/unit/scheduler/filters/test_ram_filter.py +++ b/zun/tests/unit/scheduler/filters/test_ram_filter.py @@ -26,7 +26,7 @@ class TestRamFilter(base.TestCase): def test_ram_filter_pass(self): self.filt_cls = ram_filter.RamFilter() container = objects.Container(self.context) - container.memory = '1024M' + container.memory = '1024' host = fakes.FakeHostState('testhost') host.mem_total = 1024 * 128 host.mem_used = 1024 @@ -36,7 +36,7 @@ class TestRamFilter(base.TestCase): def test_ram_filter_fail(self): self.filt_cls = ram_filter.RamFilter() container = objects.Container(self.context) - container.memory = '4096M' + container.memory = '4096' host = fakes.FakeHostState('testhost') host.mem_total = 1024 * 128 host.mem_used = 1024 * 127