update instance data cache structure
This change is adapte for the polling agent changes between kilo and liberty. Change-Id: If01b586004ca37351dcd0bfdb8ee370ebc47c4b2
This commit is contained in:
parent
9513a2ac92
commit
a2f452b5ee
@ -93,7 +93,7 @@ class ZVMInspector(virt_inspector.Inspector):
|
|||||||
'used_cpu_time': used_cpu_time,
|
'used_cpu_time': used_cpu_time,
|
||||||
'used_memory': used_memory}
|
'used_memory': used_memory}
|
||||||
|
|
||||||
self.cache.set(inst_stat)
|
self.cache.set('cpumem', inst_stat)
|
||||||
|
|
||||||
def _update_inst_nic_stat(self, instances):
|
def _update_inst_nic_stat(self, instances):
|
||||||
vsw_dict = zvmutils.virutal_network_vswitch_query_iuo_stats(
|
vsw_dict = zvmutils.virutal_network_vswitch_query_iuo_stats(
|
||||||
@ -109,7 +109,7 @@ class ZVMInspector(virt_inspector.Inspector):
|
|||||||
'nic_fr_tx': int(nic['nic_fr_tx']),
|
'nic_fr_tx': int(nic['nic_fr_tx']),
|
||||||
'nic_rx': int(nic['nic_rx']),
|
'nic_rx': int(nic['nic_rx']),
|
||||||
'nic_tx': int(nic['nic_tx'])}
|
'nic_tx': int(nic['nic_tx'])}
|
||||||
inst_stat = self.cache.get(inst_name)
|
inst_stat = self.cache.get('vnics', inst_name)
|
||||||
if inst_stat is None:
|
if inst_stat is None:
|
||||||
inst_stat = {
|
inst_stat = {
|
||||||
'nodename': inst_name,
|
'nodename': inst_name,
|
||||||
@ -118,7 +118,7 @@ class ZVMInspector(virt_inspector.Inspector):
|
|||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
inst_stat['nics'].append(nic_entry)
|
inst_stat['nics'].append(nic_entry)
|
||||||
self.cache.set(inst_stat)
|
self.cache.set('vnics', inst_stat)
|
||||||
|
|
||||||
def _update_cache(self, meter, instances={}):
|
def _update_cache(self, meter, instances={}):
|
||||||
if instances == {}:
|
if instances == {}:
|
||||||
@ -127,10 +127,9 @@ class ZVMInspector(virt_inspector.Inspector):
|
|||||||
CONF.zvm.cache_update_interval)
|
CONF.zvm.cache_update_interval)
|
||||||
instances = self.instances = zvmutils.list_instances(
|
instances = self.instances = zvmutils.list_instances(
|
||||||
self.zhcp_info)
|
self.zhcp_info)
|
||||||
|
if meter == 'cpumem':
|
||||||
if meter in ('cpus', 'memory.usage'):
|
|
||||||
self._update_inst_cpu_mem_stat(instances)
|
self._update_inst_cpu_mem_stat(instances)
|
||||||
elif meter in ('vnics',):
|
if meter == 'vnics':
|
||||||
self._update_inst_nic_stat(instances)
|
self._update_inst_nic_stat(instances)
|
||||||
|
|
||||||
def _check_expiration_and_update_cache(self, meter):
|
def _check_expiration_and_update_cache(self, meter):
|
||||||
@ -142,13 +141,13 @@ class ZVMInspector(virt_inspector.Inspector):
|
|||||||
self._check_expiration_and_update_cache(meter)
|
self._check_expiration_and_update_cache(meter)
|
||||||
|
|
||||||
inst_name = zvmutils.get_inst_name(instance)
|
inst_name = zvmutils.get_inst_name(instance)
|
||||||
inst_stat = self.cache.get(inst_name)
|
inst_stat = self.cache.get(meter, inst_name)
|
||||||
|
|
||||||
if inst_stat is None:
|
if inst_stat is None:
|
||||||
userid = (self.instances.get(inst_name) or
|
userid = (self.instances.get(inst_name) or
|
||||||
zvmutils.get_userid(inst_name))
|
zvmutils.get_userid(inst_name))
|
||||||
self._update_cache(meter, {inst_name: userid})
|
self._update_cache(meter, {inst_name: userid})
|
||||||
inst_stat = self.cache.get(inst_name)
|
inst_stat = self.cache.get(meter, inst_name)
|
||||||
|
|
||||||
if inst_stat is None:
|
if inst_stat is None:
|
||||||
raise virt_inspector.InstanceNotFoundException()
|
raise virt_inspector.InstanceNotFoundException()
|
||||||
@ -156,12 +155,12 @@ class ZVMInspector(virt_inspector.Inspector):
|
|||||||
return inst_stat
|
return inst_stat
|
||||||
|
|
||||||
def inspect_cpus(self, instance):
|
def inspect_cpus(self, instance):
|
||||||
inst_stat = self._get_inst_stat('cpus', instance)
|
inst_stat = self._get_inst_stat('cpumem', instance)
|
||||||
return virt_inspector.CPUStats(number=inst_stat['guest_cpus'],
|
return virt_inspector.CPUStats(number=inst_stat['guest_cpus'],
|
||||||
time=inst_stat['used_cpu_time'])
|
time=inst_stat['used_cpu_time'])
|
||||||
|
|
||||||
def inspect_memory_usage(self, instance, duration=None):
|
def inspect_memory_usage(self, instance, duration=None):
|
||||||
inst_stat = self._get_inst_stat('memory.usage', instance)
|
inst_stat = self._get_inst_stat('cpumem', instance)
|
||||||
return virt_inspector.MemoryUsageStats(usage=inst_stat['used_memory'])
|
return virt_inspector.MemoryUsageStats(usage=inst_stat['used_memory'])
|
||||||
|
|
||||||
def inspect_vnics(self, instance):
|
def inspect_vnics(self, instance):
|
||||||
|
@ -34,22 +34,35 @@ class ZVMException(inspector.InspectorException):
|
|||||||
|
|
||||||
|
|
||||||
class CacheData(object):
|
class CacheData(object):
|
||||||
|
"""Virtual machine stat cache."""
|
||||||
|
_CTYPES = ('cpumem', 'vnics')
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.cache = {}
|
self._reset()
|
||||||
|
|
||||||
def set(self, inst_stat):
|
def _reset(self):
|
||||||
self.cache[inst_stat['nodename']] = inst_stat
|
self.cache = dict((tp, {}) for tp in self._CTYPES)
|
||||||
|
|
||||||
def get(self, inst_name):
|
def set(self, ctype, inst_stat):
|
||||||
return self.cache.get(inst_name, None)
|
"""Set or update cache content.
|
||||||
|
|
||||||
def delete(self, inst_name):
|
@ctype: cache type.
|
||||||
if inst_name in self.cache:
|
@inst_stat: cache data.
|
||||||
del self.cache[inst_name]
|
"""
|
||||||
|
self.cache[ctype][inst_stat['nodename']] = inst_stat
|
||||||
|
|
||||||
def clear(self):
|
def get(self, ctype, inst_name):
|
||||||
self.cache = {}
|
return self.cache[ctype].get(inst_name, None)
|
||||||
|
|
||||||
|
def delete(self, ctype, inst_name):
|
||||||
|
if inst_name in self.cache[ctype]:
|
||||||
|
del self.cache[ctype][inst_name]
|
||||||
|
|
||||||
|
def clear(self, ctype='all'):
|
||||||
|
if ctype == 'all':
|
||||||
|
self._reset()
|
||||||
|
else:
|
||||||
|
self.cache[ctype] = {}
|
||||||
|
|
||||||
|
|
||||||
class XCATUrl(object):
|
class XCATUrl(object):
|
||||||
|
@ -61,8 +61,9 @@ class TestZVMInspector(base.BaseTestCase):
|
|||||||
'used_cpu_time': 1710205201000,
|
'used_cpu_time': 1710205201000,
|
||||||
'used_memory': 4091,
|
'used_memory': 4091,
|
||||||
'userid': 'INST1'}
|
'userid': 'INST1'}
|
||||||
self.assertEqual(exp1, self.inspector.cache.get('inst1'))
|
self.assertEqual(exp1, self.inspector.cache.get('cpumem', 'inst1'))
|
||||||
self.assertEqual(4, self.inspector.cache.get('inst2')['guest_cpus'])
|
self.assertEqual(4,
|
||||||
|
self.inspector.cache.get('cpumem', 'inst2')['guest_cpus'])
|
||||||
|
|
||||||
@mock.patch.object(zvmutils, 'image_performance_query')
|
@mock.patch.object(zvmutils, 'image_performance_query')
|
||||||
def test_update_inst_cpu_mem_stat_invalid_data(self, ipq):
|
def test_update_inst_cpu_mem_stat_invalid_data(self, ipq):
|
||||||
@ -77,7 +78,7 @@ class TestZVMInspector(base.BaseTestCase):
|
|||||||
def test_update_cache_all(self, list_inst, upd):
|
def test_update_cache_all(self, list_inst, upd):
|
||||||
inst_list = {'inst1': 'INST1', 'inst2': 'INST2'}
|
inst_list = {'inst1': 'INST1', 'inst2': 'INST2'}
|
||||||
list_inst.return_value = inst_list
|
list_inst.return_value = inst_list
|
||||||
self.inspector._update_cache("cpus", {})
|
self.inspector._update_cache("cpumem", {})
|
||||||
list_inst.assert_called_with(self.inspector.zhcp_info)
|
list_inst.assert_called_with(self.inspector.zhcp_info)
|
||||||
upd.assert_called_with(inst_list)
|
upd.assert_called_with(inst_list)
|
||||||
|
|
||||||
@ -85,7 +86,7 @@ class TestZVMInspector(base.BaseTestCase):
|
|||||||
"_update_inst_cpu_mem_stat")
|
"_update_inst_cpu_mem_stat")
|
||||||
def test_update_cache_one_inst(self, upd):
|
def test_update_cache_one_inst(self, upd):
|
||||||
inst = {'inst1': 'INST1'}
|
inst = {'inst1': 'INST1'}
|
||||||
self.inspector._update_cache('memory.usage', inst)
|
self.inspector._update_cache('cpumem', inst)
|
||||||
upd.assert_called_with(inst)
|
upd.assert_called_with(inst)
|
||||||
|
|
||||||
@mock.patch("ceilometer_zvm.compute.virt.zvm.inspector.ZVMInspector."
|
@mock.patch("ceilometer_zvm.compute.virt.zvm.inspector.ZVMInspector."
|
||||||
@ -106,11 +107,12 @@ class TestZVMInspector(base.BaseTestCase):
|
|||||||
"_check_expiration_and_update_cache")
|
"_check_expiration_and_update_cache")
|
||||||
def test_get_inst_stat(self, check_update, get_name):
|
def test_get_inst_stat(self, check_update, get_name):
|
||||||
get_name.return_value = 'inst1'
|
get_name.return_value = 'inst1'
|
||||||
self.inspector.cache.set({'guest_cpus': 2, 'nodename': 'inst1'})
|
self.inspector.cache.set('cpumem',
|
||||||
|
{'guest_cpus': 2, 'nodename': 'inst1'})
|
||||||
|
|
||||||
inst_stat = self.inspector._get_inst_stat('cpus', {'inst1': 'INST1'})
|
inst_stat = self.inspector._get_inst_stat('cpumem', {'inst1': 'INST1'})
|
||||||
self.assertEqual(2, inst_stat['guest_cpus'])
|
self.assertEqual(2, inst_stat['guest_cpus'])
|
||||||
check_update.assert_called_once_with('cpus')
|
check_update.assert_called_once_with('cpumem')
|
||||||
|
|
||||||
@mock.patch("ceilometer_zvm.compute.virt.zvm.inspector.ZVMInspector."
|
@mock.patch("ceilometer_zvm.compute.virt.zvm.inspector.ZVMInspector."
|
||||||
"_update_cache")
|
"_update_cache")
|
||||||
@ -124,10 +126,10 @@ class TestZVMInspector(base.BaseTestCase):
|
|||||||
get_uid.return_value = 'INST1'
|
get_uid.return_value = 'INST1'
|
||||||
|
|
||||||
self.assertRaises(virt_inspertor.InstanceNotFoundException,
|
self.assertRaises(virt_inspertor.InstanceNotFoundException,
|
||||||
self.inspector._get_inst_stat, 'cpus',
|
self.inspector._get_inst_stat, 'cpumem',
|
||||||
{'inst1': 'INST1'})
|
{'inst1': 'INST1'})
|
||||||
check_update.assert_called_once_with('cpus')
|
check_update.assert_called_once_with('cpumem')
|
||||||
update.assert_called_once_with('cpus', {'inst1': 'INST1'})
|
update.assert_called_once_with('cpumem', {'inst1': 'INST1'})
|
||||||
|
|
||||||
@mock.patch("ceilometer_zvm.compute.virt.zvm.inspector.ZVMInspector."
|
@mock.patch("ceilometer_zvm.compute.virt.zvm.inspector.ZVMInspector."
|
||||||
"_update_cache")
|
"_update_cache")
|
||||||
@ -152,10 +154,11 @@ class TestZVMInspector(base.BaseTestCase):
|
|||||||
"_check_expiration_and_update_cache")
|
"_check_expiration_and_update_cache")
|
||||||
def test_get_inst_stat_nics(self, check_update, get_name):
|
def test_get_inst_stat_nics(self, check_update, get_name):
|
||||||
get_name.return_value = 'inst1'
|
get_name.return_value = 'inst1'
|
||||||
self.inspector.cache.set({'nodename': 'inst1',
|
self.inspector.cache.set(
|
||||||
|
'vnics',
|
||||||
|
{'nodename': 'inst1',
|
||||||
'userid': 'INST1',
|
'userid': 'INST1',
|
||||||
'nics': [
|
'nics': [{'vswitch_name': 'vsw1',
|
||||||
{'vswitch_name': 'vsw1',
|
|
||||||
'nic_vdev': '0600',
|
'nic_vdev': '0600',
|
||||||
'nic_fr_rx': 99999,
|
'nic_fr_rx': 99999,
|
||||||
'nic_fr_tx': 99999,
|
'nic_fr_tx': 99999,
|
||||||
@ -166,7 +169,8 @@ class TestZVMInspector(base.BaseTestCase):
|
|||||||
'nic_fr_rx': 88888,
|
'nic_fr_rx': 88888,
|
||||||
'nic_fr_tx': 88888,
|
'nic_fr_tx': 88888,
|
||||||
'nic_rx': 8888888,
|
'nic_rx': 8888888,
|
||||||
'nic_tx': 8888888}]})
|
'nic_tx': 8888888}]}
|
||||||
|
)
|
||||||
inst_stat = self.inspector._get_inst_stat('vnics', {'inst1': 'INST1'})
|
inst_stat = self.inspector._get_inst_stat('vnics', {'inst1': 'INST1'})
|
||||||
self.assertEqual(2, len(inst_stat['nics']))
|
self.assertEqual(2, len(inst_stat['nics']))
|
||||||
check_update.assert_called_once_with('vnics')
|
check_update.assert_called_once_with('vnics')
|
||||||
@ -178,7 +182,7 @@ class TestZVMInspector(base.BaseTestCase):
|
|||||||
cpu_stat = self.inspector.inspect_cpus(None)
|
cpu_stat = self.inspector.inspect_cpus(None)
|
||||||
self.assertEqual(2, cpu_stat.number)
|
self.assertEqual(2, cpu_stat.number)
|
||||||
self.assertEqual(99999999, cpu_stat.time)
|
self.assertEqual(99999999, cpu_stat.time)
|
||||||
get_stat.assert_called_once_with('cpus', None)
|
get_stat.assert_called_once_with('cpumem', None)
|
||||||
|
|
||||||
@mock.patch("ceilometer_zvm.compute.virt.zvm.inspector.ZVMInspector."
|
@mock.patch("ceilometer_zvm.compute.virt.zvm.inspector.ZVMInspector."
|
||||||
"_get_inst_stat")
|
"_get_inst_stat")
|
||||||
@ -186,7 +190,7 @@ class TestZVMInspector(base.BaseTestCase):
|
|||||||
get_stat.return_value = {'used_memory': 1998}
|
get_stat.return_value = {'used_memory': 1998}
|
||||||
mem_usage = self.inspector.inspect_memory_usage(None)
|
mem_usage = self.inspector.inspect_memory_usage(None)
|
||||||
self.assertEqual(1998, mem_usage.usage)
|
self.assertEqual(1998, mem_usage.usage)
|
||||||
get_stat.assert_called_once_with('memory.usage', None)
|
get_stat.assert_called_once_with('cpumem', None)
|
||||||
|
|
||||||
@mock.patch.object(zvmutils, 'virutal_network_vswitch_query_iuo_stats')
|
@mock.patch.object(zvmutils, 'virutal_network_vswitch_query_iuo_stats')
|
||||||
def test_update_inst_nic_stat(self, vswq):
|
def test_update_inst_nic_stat(self, vswq):
|
||||||
@ -255,7 +259,7 @@ class TestZVMInspector(base.BaseTestCase):
|
|||||||
'nic_tx': 3316601}
|
'nic_tx': 3316601}
|
||||||
]
|
]
|
||||||
self.assertEqual(exp_inst1_nics_data,
|
self.assertEqual(exp_inst1_nics_data,
|
||||||
self.inspector.cache.get('inst1')['nics'])
|
self.inspector.cache.get('vnics', 'inst1')['nics'])
|
||||||
vswq.assert_called_once_with('zhcp')
|
vswq.assert_called_once_with('zhcp')
|
||||||
|
|
||||||
@mock.patch("ceilometer_zvm.compute.virt.zvm.inspector.ZVMInspector."
|
@mock.patch("ceilometer_zvm.compute.virt.zvm.inspector.ZVMInspector."
|
||||||
|
@ -307,20 +307,23 @@ class TestCacheData(base.BaseTestCase):
|
|||||||
super(TestCacheData, self).tearDown()
|
super(TestCacheData, self).tearDown()
|
||||||
|
|
||||||
def test_set(self):
|
def test_set(self):
|
||||||
self.cache_data.set({'nodename': 'node'})
|
self.cache_data.set('cpumem', {'nodename': 'node'})
|
||||||
self.assertEqual({'nodename': 'node'}, self.cache_data.cache['node'])
|
self.assertEqual({'nodename': 'node'},
|
||||||
|
self.cache_data.cache['cpumem']['node'])
|
||||||
|
|
||||||
def test_get(self):
|
def test_get(self):
|
||||||
self.cache_data.set({'nodename': 'node'})
|
self.cache_data.set('vnics', {'nodename': 'node'})
|
||||||
self.assertEqual({'nodename': 'node'}, self.cache_data.get('node'))
|
self.assertEqual({'nodename': 'node'},
|
||||||
|
self.cache_data.get('vnics', 'node'))
|
||||||
|
|
||||||
def test_delete(self):
|
def test_delete(self):
|
||||||
self.cache_data.set({'nodename': 'node'})
|
self.cache_data.set('cpumem', {'nodename': 'node'})
|
||||||
self.cache_data.delete('node')
|
self.cache_data.delete('cpumem', 'node')
|
||||||
self.assertEqual(None, self.cache_data.get('node'))
|
self.assertEqual(None, self.cache_data.get('cpumem', 'node'))
|
||||||
|
|
||||||
def test_clear(self):
|
def test_clear(self):
|
||||||
self.cache_data.set({'nodename': 'node1'})
|
self.cache_data.set('cpumem', {'nodename': 'node1'})
|
||||||
self.cache_data.set({'nodename': 'node2'})
|
self.cache_data.set('vnics', {'nodename': 'node2'})
|
||||||
self.cache_data.clear()
|
self.cache_data.clear()
|
||||||
self.assertEqual({}, self.cache_data.cache)
|
self.assertEqual({'cpumem': {}, 'vnics': {}},
|
||||||
|
self.cache_data.cache)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user