summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Elliott <bdelliott@gmail.com>2013-05-17 17:46:30 +0000
committerBrian Elliott <bdelliott@gmail.com>2013-05-17 19:53:25 +0000
commite788bc24809693b19969c7f7fb44f414bb3b890a (patch)
treeb57a1669a1211b41c2edbba83300871572766aa3
parent0e760b3e94c53e8f794609a60343b392e7ff595c (diff)
downloadnova-e788bc24809693b19969c7f7fb44f414bb3b890a.tar.gz
nova-e788bc24809693b19969c7f7fb44f414bb3b890a.tar.xz
nova-e788bc24809693b19969c7f7fb44f414bb3b890a.zip
Don't update DB records for unchanged stats.
Change-Id: I6eb0adba12676cf057c92fb0b54431cfe6a76210
-rw-r--r--nova/db/sqlalchemy/api.py7
-rw-r--r--nova/tests/test_db_api.py32
2 files changed, 36 insertions, 3 deletions
diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py
index 95cbb6efa..9caf096cd 100644
--- a/nova/db/sqlalchemy/api.py
+++ b/nova/db/sqlalchemy/api.py
@@ -519,9 +519,10 @@ def _update_stats(context, new_stats, compute_id, session, prune_stats=False):
for k, v in new_stats.iteritems():
old_stat = statmap.pop(k, None)
if old_stat:
- # update existing value:
- old_stat.update({'value': v})
- stats.append(old_stat)
+ if old_stat['value'] != unicode(v):
+ # update existing value:
+ old_stat.update({'value': v})
+ stats.append(old_stat)
else:
# add new stat:
stat = models.ComputeNodeStat()
diff --git a/nova/tests/test_db_api.py b/nova/tests/test_db_api.py
index 8dbfa7010..7a642e451 100644
--- a/nova/tests/test_db_api.py
+++ b/nova/tests/test_db_api.py
@@ -1750,6 +1750,38 @@ class CapacityTestCase(test.TestCase):
item['id'], {})
self.assertNotEqual(item['updated_at'], item_updated['updated_at'])
+ def test_compute_node_stat_unchanged(self):
+ # don't update unchanged stat values:
+ item = self._create_helper('host1')
+
+ compute_node_id = item['id']
+ stats = self._stats_as_dict(item['stats'])
+ self.assertEqual(4, len(stats.keys()))
+
+ orig_update_stats = sqlalchemy_api._update_stats
+
+ def update(context, new_stats, compute_id, session, prune_stats=False):
+ # wrap the session object to see which stats get updated
+ orig_add = session.add
+ added = []
+
+ def add(instance):
+ added.append(instance)
+ orig_add(instance)
+
+ self.stubs.Set(session, 'add', add)
+ orig_update_stats(context, new_stats, compute_id, session,
+ prune_stats=False)
+
+ # no stats should have been added to the session:
+ self.assertEqual(0, len(added))
+
+ self.stubs.Set(sqlalchemy_api, '_update_stats', update)
+
+ # save with same (unchanged) stats again:
+ values = {'stats': stats}
+ db.compute_node_update(self.ctxt, compute_node_id, values)
+
def test_compute_node_stat_prune(self):
item = self._create_helper('host1')
for stat in item['stats']: