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
This commit is contained in:
Hongbin Lu 2018-04-16 03:27:39 +00:00
parent 3d3580fe39
commit 56b0b08a5b
11 changed files with 30 additions and 37 deletions

View File

@ -222,7 +222,7 @@ class CapsuleController(base.Controller):
container_dict['cpu'] = allocation.get('cpu') container_dict['cpu'] = allocation.get('cpu')
if allocation.get('memory'): if allocation.get('memory'):
capsule_need_memory += 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.pop('resources')
container_dict['restart_policy'] = container_restart_policy container_dict['restart_policy'] = container_restart_policy
@ -247,7 +247,7 @@ class CapsuleController(base.Controller):
container_volume_requests, container_volume_requests,
new_capsule) new_capsule)
new_capsule.cpu = capsule_need_cpu 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) new_capsule.save(context)
compute_api.capsule_create(context, new_capsule, requested_networks, compute_api.capsule_create(context, new_capsule, requested_networks,
requested_volumes, extra_spec) requested_volumes, extra_spec)

View File

@ -367,7 +367,7 @@ class ContainersController(base.Controller):
container_dict['disk'] = container_dict.get('disk') container_dict['disk'] = container_dict.get('disk')
container_dict['memory'] = container_dict.get( container_dict['memory'] = container_dict.get(
'memory', CONF.default_memory) '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( container_dict['cpu'] = container_dict.get(
'cpu', CONF.default_cpu) 'cpu', CONF.default_cpu)
@ -553,7 +553,7 @@ class ContainersController(base.Controller):
check_policy_on_container(container.as_dict(), "container:update") check_policy_on_container(container.as_dict(), "container:update")
utils.validate_container_state(container, 'update') utils.validate_container_state(container, 'update')
if 'memory' in patch: if 'memory' in patch:
patch['memory'] = str(patch['memory']) + 'M' patch['memory'] = str(patch['memory'])
if 'cpu' in patch: if 'cpu' in patch:
patch['cpu'] = float(patch['cpu']) patch['cpu'] = float(patch['cpu'])
if 'name' in patch: if 'name' in patch:

View File

@ -88,10 +88,7 @@ class Claim(NopClaim):
@property @property
def memory(self): def memory(self):
mem_str = "0" return int(self.container.memory or "0")
if self.container.memory:
mem_str = self.container.memory[:-1]
return int(mem_str)
@property @property
def cpu(self): def cpu(self):
@ -224,12 +221,8 @@ class UpdateClaim(Claim):
@property @property
def memory(self): def memory(self):
new_mem_str = "0" new_mem_str = self.new_container.memory or "0"
if self.new_container.memory: old_mem_str = self.old_container.memory or "0"
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]
return int(new_mem_str) - int(old_mem_str) return int(new_mem_str) - int(old_mem_str)
@property @property

View File

@ -297,7 +297,7 @@ class ComputeNodeTracker(object):
# (Fixme): The Container.memory is string. # (Fixme): The Container.memory is string.
memory = 0 memory = 0
if container.memory: if container.memory:
memory = int(container.memory[:-1]) memory = int(container.memory)
usage = {'memory': memory, usage = {'memory': memory,
'cpu': container.cpu or 0, 'cpu': container.cpu or 0,
'disk': container.disk or 0} 'disk': container.disk or 0}

View File

@ -245,7 +245,7 @@ class DockerDriver(driver.ContainerDriver):
if container.auto_remove: if container.auto_remove:
host_config['auto_remove'] = container.auto_remove host_config['auto_remove'] = container.auto_remove
if container.memory is not None: 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: if container.cpu is not None:
host_config['cpu_quota'] = int(100000 * container.cpu) host_config['cpu_quota'] = int(100000 * container.cpu)
host_config['cpu_period'] = 100000 host_config['cpu_period'] = 100000
@ -784,7 +784,7 @@ class DockerDriver(driver.ContainerDriver):
args = {} args = {}
memory = patch.get('memory') memory = patch.get('memory')
if memory is not None: if memory is not None:
args['mem_limit'] = memory args['mem_limit'] = str(memory) + 'M'
args['memswap_limit'] = CONF.default_memory_swap args['memswap_limit'] = CONF.default_memory_swap
cpu = patch.get('cpu') cpu = patch.get('cpu')
if cpu is not None: if cpu is not None:

View File

@ -29,7 +29,7 @@ class RamFilter(filters.BaseHostFilter):
if not container.memory: if not container.memory:
return True return True
request_ram = int(container.memory[:-1]) request_ram = int(container.memory)
usable_ram = host_state.mem_total - host_state.mem_used usable_ram = host_state.mem_total - host_state.mem_used
if usable_ram < request_ram: if usable_ram < request_ram:
LOG.debug("%(host_state)s does not have %(request_ram)d " LOG.debug("%(host_state)s does not have %(request_ram)d "

View File

@ -48,7 +48,7 @@ class TestCapsuleController(api_base.FunctionalTest):
return_value = response.json return_value = response.json
expected_meta_name = "capsule-example" expected_meta_name = "capsule-example"
expected_meta_labels = {"foo0": "bar0", "foo1": "bar1"} expected_meta_labels = {"foo0": "bar0", "foo1": "bar1"}
expected_memory = '1024M' expected_memory = '1024'
expected_cpu = 1.0 expected_cpu = 1.0
expected_container_num = 2 expected_container_num = 2
self.assertEqual(len(return_value["containers_uuids"]), self.assertEqual(len(return_value["containers_uuids"]),
@ -85,7 +85,7 @@ class TestCapsuleController(api_base.FunctionalTest):
return_value = response.json return_value = response.json
expected_meta_name = "capsule-example" expected_meta_name = "capsule-example"
expected_meta_labels = {"foo0": "bar0"} expected_meta_labels = {"foo0": "bar0"}
expected_memory = '2048M' expected_memory = '2048'
expected_cpu = 2.0 expected_cpu = 2.0
expected_container_num = 3 expected_container_num = 3
self.assertEqual(len(return_value["containers_uuids"]), self.assertEqual(len(return_value["containers_uuids"]),
@ -199,7 +199,7 @@ class TestCapsuleController(api_base.FunctionalTest):
return_value = response.json return_value = response.json
expected_meta_name = "capsule-example" expected_meta_name = "capsule-example"
expected_meta_labels = {"foo0": "bar0", "foo1": "bar1"} expected_meta_labels = {"foo0": "bar0", "foo1": "bar1"}
expected_memory = '1024M' expected_memory = '1024'
expected_cpu = 1.0 expected_cpu = 1.0
expected_container_num = 2 expected_container_num = 2
self.assertEqual(len(return_value["containers_uuids"]), self.assertEqual(len(return_value["containers_uuids"]),
@ -253,7 +253,7 @@ class TestCapsuleController(api_base.FunctionalTest):
return_value = response.json return_value = response.json
expected_meta_name = "capsule-example" expected_meta_name = "capsule-example"
expected_meta_labels = {"foo0": "bar0", "foo1": "bar1"} expected_meta_labels = {"foo0": "bar0", "foo1": "bar1"}
expected_memory = '1024M' expected_memory = '1024'
expected_cpu = 1.0 expected_cpu = 1.0
expected_container_num = 2 expected_container_num = 2
self.assertEqual(len(return_value["containers_uuids"]), self.assertEqual(len(return_value["containers_uuids"]),
@ -316,7 +316,7 @@ class TestCapsuleController(api_base.FunctionalTest):
return_value = response.json return_value = response.json
expected_meta_name = "capsule-example" expected_meta_name = "capsule-example"
expected_meta_labels = {"foo0": "bar0", "foo1": "bar1"} expected_meta_labels = {"foo0": "bar0", "foo1": "bar1"}
expected_memory = '1024M' expected_memory = '1024'
expected_cpu = 1.0 expected_cpu = 1.0
expected_container_num = 2 expected_container_num = 2
self.assertEqual(len(return_value["containers_uuids"]), self.assertEqual(len(return_value["containers_uuids"]),

View File

@ -261,7 +261,7 @@ class TestContainerController(api_base.FunctionalTest):
self.assertIsNotNone(c.get('uuid')) self.assertIsNotNone(c.get('uuid'))
self.assertEqual('MyDocker', c.get('name')) self.assertEqual('MyDocker', c.get('name'))
self.assertEqual('env', c.get('command')) self.assertEqual('env', c.get('command'))
self.assertEqual('512M', c.get('memory')) self.assertEqual('512', c.get('memory'))
self.assertEqual({"key1": "val1", "key2": "val2"}, self.assertEqual({"key1": "val1", "key2": "val2"},
c.get('environment')) c.get('environment'))
self.assertEqual('runc', c.get('runtime')) self.assertEqual('runc', c.get('runtime'))
@ -318,7 +318,7 @@ class TestContainerController(api_base.FunctionalTest):
self.assertIsNotNone(c.get('uuid')) self.assertIsNotNone(c.get('uuid'))
self.assertIsNotNone(c.get('name')) self.assertIsNotNone(c.get('name'))
self.assertIsNone(c.get('command')) self.assertIsNone(c.get('command'))
self.assertEqual('512M', c.get('memory')) self.assertEqual('512', c.get('memory'))
self.assertEqual(1.0, c.get('cpu')) self.assertEqual(1.0, c.get('cpu'))
# TODO(kiennt): Uncomment it when bug [1] be resolved. # TODO(kiennt): Uncomment it when bug [1] be resolved.
# At this time, limit disk size feature will be ready. # 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.assertIsNotNone(c.get('uuid'))
self.assertEqual('MyDocker', c.get('name')) self.assertEqual('MyDocker', c.get('name'))
self.assertEqual('env', c.get('command')) 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')) self.assertEqual(1.0, c.get('cpu'))
# TODO(kiennt): Uncomment it when bug [1] be resolved. # TODO(kiennt): Uncomment it when bug [1] be resolved.
# At this time, limit disk size feature will be ready. # 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.assertIsNotNone(c.get('uuid'))
self.assertEqual('MyDocker', c.get('name')) self.assertEqual('MyDocker', c.get('name'))
self.assertEqual('env', c.get('command')) self.assertEqual('env', c.get('command'))
self.assertEqual('512M', c.get('memory')) self.assertEqual('512', c.get('memory'))
self.assertEqual({"Name": "no", "MaximumRetryCount": "0"}, self.assertEqual({"Name": "no", "MaximumRetryCount": "0"},
c.get('restart_policy')) c.get('restart_policy'))
mock_neutron_get_network.assert_called_once() mock_neutron_get_network.assert_called_once()
@ -433,7 +433,7 @@ class TestContainerController(api_base.FunctionalTest):
self.assertIsNotNone(c.get('uuid')) self.assertIsNotNone(c.get('uuid'))
self.assertEqual('MyDocker', c.get('name')) self.assertEqual('MyDocker', c.get('name'))
self.assertEqual('env', c.get('command')) self.assertEqual('env', c.get('command'))
self.assertEqual('512M', c.get('memory')) self.assertEqual('512', c.get('memory'))
self.assertEqual({"Name": "no", "MaximumRetryCount": "0"}, self.assertEqual({"Name": "no", "MaximumRetryCount": "0"},
c.get('restart_policy')) c.get('restart_policy'))
mock_neutron_get_network.assert_called_once() mock_neutron_get_network.assert_called_once()
@ -469,7 +469,7 @@ class TestContainerController(api_base.FunctionalTest):
self.assertIsNotNone(c.get('uuid')) self.assertIsNotNone(c.get('uuid'))
self.assertEqual('MyDocker', c.get('name')) self.assertEqual('MyDocker', c.get('name'))
self.assertEqual('env', c.get('command')) 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"}, self.assertEqual({"Name": "unless-stopped", "MaximumRetryCount": "0"},
c.get('restart_policy')) c.get('restart_policy'))
mock_neutron_get_network.assert_called_once() mock_neutron_get_network.assert_called_once()
@ -513,7 +513,7 @@ class TestContainerController(api_base.FunctionalTest):
self.assertIsNotNone(c.get('uuid')) self.assertIsNotNone(c.get('uuid'))
self.assertEqual('MyDocker', c.get('name')) self.assertEqual('MyDocker', c.get('name'))
self.assertEqual('env', c.get('command')) self.assertEqual('env', c.get('command'))
self.assertEqual('512M', c.get('memory')) self.assertEqual('512', c.get('memory'))
self.assertEqual({"key1": "val1", "key2": "val2"}, self.assertEqual({"key1": "val1", "key2": "val2"},
c.get('environment')) c.get('environment'))
requested_networks = \ requested_networks = \
@ -637,7 +637,7 @@ class TestContainerController(api_base.FunctionalTest):
self.assertEqual('MyDocker', c.get('name')) self.assertEqual('MyDocker', c.get('name'))
self.assertEqual('env', c.get('command')) self.assertEqual('env', c.get('command'))
self.assertEqual('Creating', c.get('status')) self.assertEqual('Creating', c.get('status'))
self.assertEqual('512M', c.get('memory')) self.assertEqual('512', c.get('memory'))
requested_networks = \ requested_networks = \
mock_container_create.call_args[1]['requested_networks'] mock_container_create.call_args[1]['requested_networks']
self.assertEqual(1, len(requested_networks)) self.assertEqual(1, len(requested_networks))

View File

@ -124,7 +124,7 @@ class TestDockerDriver(base.DriverTestCase):
result_container = self.driver.create(self.context, mock_container, result_container = self.driver.create(self.context, mock_container,
image, networks, volumes) image, networks, volumes)
host_config = {} host_config = {}
host_config['mem_limit'] = '512m' host_config['mem_limit'] = '512M'
host_config['cpu_quota'] = 100000 host_config['cpu_quota'] = 100000
host_config['cpu_period'] = 100000 host_config['cpu_period'] = 100000
host_config['restart_policy'] = {'Name': 'no', 'MaximumRetryCount': 0} 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, result_container = self.driver.create(self.context, mock_container,
image, networks, volumes) image, networks, volumes)
host_config = {} host_config = {}
host_config['mem_limit'] = '512m' host_config['mem_limit'] = '512M'
host_config['cpu_quota'] = 100000 host_config['cpu_quota'] = 100000
host_config['cpu_period'] = 100000 host_config['cpu_period'] = 100000
host_config['restart_policy'] = {'Name': 'no', 'MaximumRetryCount': 0} host_config['restart_policy'] = {'Name': 'no', 'MaximumRetryCount': 0}

View File

@ -61,7 +61,7 @@ def get_test_container(**kwargs):
'environment': kwargs.get('environment', {'key1': 'val1', 'environment': kwargs.get('environment', {'key1': 'val1',
'key2': 'val2'}), 'key2': 'val2'}),
'cpu': kwargs.get('cpu', 1.0), 'cpu': kwargs.get('cpu', 1.0),
'memory': kwargs.get('memory', '512m'), 'memory': kwargs.get('memory', '512'),
'workdir': kwargs.get('workdir', '/home/ubuntu'), 'workdir': kwargs.get('workdir', '/home/ubuntu'),
'ports': kwargs.get('ports', [80, 443]), 'ports': kwargs.get('ports', [80, 443]),
'hostname': kwargs.get('hostname', 'testhost'), 'hostname': kwargs.get('hostname', 'testhost'),
@ -395,7 +395,7 @@ def get_test_capsule(**kwargs):
'status': kwargs.get('status', 'Running'), 'status': kwargs.get('status', 'Running'),
'status_reason': kwargs.get('status_reason', 'Created Successfully'), 'status_reason': kwargs.get('status_reason', 'Created Successfully'),
'cpu': kwargs.get('cpu', 1.0), 'cpu': kwargs.get('cpu', 1.0),
'memory': kwargs.get('memory', '512m'), 'memory': kwargs.get('memory', '512'),
'spec': kwargs.get('spec', CAPSULE_SPEC), 'spec': kwargs.get('spec', CAPSULE_SPEC),
'meta_name': kwargs.get('meta_name', "fake-meta-name"), 'meta_name': kwargs.get('meta_name', "fake-meta-name"),
'meta_labels': kwargs.get('meta_labels', {'key1': 'val1', 'meta_labels': kwargs.get('meta_labels', {'key1': 'val1',

View File

@ -26,7 +26,7 @@ class TestRamFilter(base.TestCase):
def test_ram_filter_pass(self): def test_ram_filter_pass(self):
self.filt_cls = ram_filter.RamFilter() self.filt_cls = ram_filter.RamFilter()
container = objects.Container(self.context) container = objects.Container(self.context)
container.memory = '1024M' container.memory = '1024'
host = fakes.FakeHostState('testhost') host = fakes.FakeHostState('testhost')
host.mem_total = 1024 * 128 host.mem_total = 1024 * 128
host.mem_used = 1024 host.mem_used = 1024
@ -36,7 +36,7 @@ class TestRamFilter(base.TestCase):
def test_ram_filter_fail(self): def test_ram_filter_fail(self):
self.filt_cls = ram_filter.RamFilter() self.filt_cls = ram_filter.RamFilter()
container = objects.Container(self.context) container = objects.Container(self.context)
container.memory = '4096M' container.memory = '4096'
host = fakes.FakeHostState('testhost') host = fakes.FakeHostState('testhost')
host.mem_total = 1024 * 128 host.mem_total = 1024 * 128
host.mem_used = 1024 * 127 host.mem_used = 1024 * 127