Provide compatibliity for db.compute_node_statistics
As some computes are providing the host field and others are not, let's modify how we calculate the statistics by merging both versions. Note: This change won't impact how the API is getting stastistics as the result is not a ComputeNode or a list of ComputeNodes but instead a mix of stats. Partially-Implements blueprint detach-service-from-computenode Change-Id: I889efc41babb2c82018122b5f255eb0fca9b452b
This commit is contained in:
@@ -634,6 +634,12 @@ def compute_node_delete(context, compute_id):
|
|||||||
|
|
||||||
def compute_node_statistics(context):
|
def compute_node_statistics(context):
|
||||||
"""Compute statistics over all compute nodes."""
|
"""Compute statistics over all compute nodes."""
|
||||||
|
|
||||||
|
# TODO(sbauza): Remove the service_id filter in a later release
|
||||||
|
# once we are sure that all compute nodes report the host field
|
||||||
|
_filter = or_(models.Service.host == models.ComputeNode.host,
|
||||||
|
models.Service.id == models.ComputeNode.service_id)
|
||||||
|
|
||||||
result = model_query(context,
|
result = model_query(context,
|
||||||
models.ComputeNode, (
|
models.ComputeNode, (
|
||||||
func.count(models.ComputeNode.id),
|
func.count(models.ComputeNode.id),
|
||||||
@@ -650,9 +656,7 @@ def compute_node_statistics(context):
|
|||||||
func.sum(models.ComputeNode.disk_available_least),
|
func.sum(models.ComputeNode.disk_available_least),
|
||||||
), read_deleted="no").\
|
), read_deleted="no").\
|
||||||
filter(models.Service.disabled == false()).\
|
filter(models.Service.disabled == false()).\
|
||||||
filter(
|
filter(_filter).\
|
||||||
models.Service.id ==
|
|
||||||
models.ComputeNode.service_id).\
|
|
||||||
first()
|
first()
|
||||||
|
|
||||||
# Build a dict of the info--making no assumptions about result
|
# Build a dict of the info--making no assumptions about result
|
||||||
|
|||||||
@@ -6430,6 +6430,25 @@ class ComputeNodeTestCase(test.TestCase, ModelsObjectComparatorMixin):
|
|||||||
stats = db.compute_node_statistics(self.ctxt)
|
stats = db.compute_node_statistics(self.ctxt)
|
||||||
self.assertEqual(stats.pop('count'), 0)
|
self.assertEqual(stats.pop('count'), 0)
|
||||||
|
|
||||||
|
def test_compute_node_statistics_with_old_service_id(self):
|
||||||
|
# NOTE(sbauza): This test is only for checking backwards compatibility
|
||||||
|
# with old versions of compute_nodes not providing host column.
|
||||||
|
# This test could be removed once we are sure that all compute nodes
|
||||||
|
# are populating the host field thanks to the ResourceTracker
|
||||||
|
|
||||||
|
service2 = self.service_dict.copy()
|
||||||
|
service2['host'] = 'host2'
|
||||||
|
db_service2 = db.service_create(self.ctxt, service2)
|
||||||
|
compute_node_old_host = self.compute_node_dict.copy()
|
||||||
|
compute_node_old_host['stats'] = jsonutils.dumps(self.stats)
|
||||||
|
compute_node_old_host['hypervisor_hostname'] = 'node_2'
|
||||||
|
compute_node_old_host['service_id'] = db_service2['id']
|
||||||
|
compute_node_old_host.pop('host')
|
||||||
|
|
||||||
|
db.compute_node_create(self.ctxt, compute_node_old_host)
|
||||||
|
stats = db.compute_node_statistics(self.ctxt)
|
||||||
|
self.assertEqual(2, stats.pop('count'))
|
||||||
|
|
||||||
def test_compute_node_not_found(self):
|
def test_compute_node_not_found(self):
|
||||||
self.assertRaises(exception.ComputeHostNotFound, db.compute_node_get,
|
self.assertRaises(exception.ComputeHostNotFound, db.compute_node_get,
|
||||||
self.ctxt, 100500)
|
self.ctxt, 100500)
|
||||||
|
|||||||
Reference in New Issue
Block a user