diff options
author | Hemanth Makkapati <hemanth.makkapati@rackspace.com> | 2015-03-31 04:36:48 -0400 |
---|---|---|
committer | Hemanth Makkapati <hemanth.makkapati@rackspace.com> | 2015-04-01 23:26:12 -0400 |
commit | 95d3561de8afb01251b657b639d028bcf5f13002 (patch) | |
tree | 3a3be9e9118ebe02c697c0eab8f2ed2505606ac4 | |
parent | e7d94536fbadf101bc7cb4d796d267eb95eb9de5 (diff) | |
download | glance-95d3561de8afb01251b657b639d028bcf5f13002.tar.gz |
Glance cache to not prune newly cached images
Sqlite driver doesn't set the last accessed time when an image is
first added to cache. This makes the newly cached images susceptible
to pruning first instead of older images.
Change-Id: I46973a921c7cfb42811c58383e1b7a4004e70f27
Closes-bug: #1438564
-rw-r--r-- | glance/image_cache/drivers/sqlite.py | 4 | ||||
-rw-r--r-- | glance/tests/unit/test_image_cache.py | 29 |
2 files changed, 21 insertions, 12 deletions
diff --git a/glance/image_cache/drivers/sqlite.py b/glance/image_cache/drivers/sqlite.py index 21730c84c..74c31736f 100644 --- a/glance/image_cache/drivers/sqlite.py +++ b/glance/image_cache/drivers/sqlite.py @@ -321,8 +321,8 @@ class Driver(base.Driver): db.execute("""INSERT INTO cached_images (image_id, last_accessed, last_modified, hits, size) - VALUES (?, 0, ?, 0, ?)""", - (image_id, now, filesize)) + VALUES (?, ?, ?, 0, ?)""", + (image_id, now, now, filesize)) db.commit() def rollback(e): diff --git a/glance/tests/unit/test_image_cache.py b/glance/tests/unit/test_image_cache.py index 2730d244e..260958426 100644 --- a/glance/tests/unit/test_image_cache.py +++ b/glance/tests/unit/test_image_cache.py @@ -174,16 +174,15 @@ class ImageCacheTestCase(object): """ self.assertEqual(0, self.cache.get_cache_size()) - # Add a bunch of images to the cache. The max cache - # size for the cache is set to 5KB and each image is - # 1K. We add 10 images to the cache and then we'll - # prune it. We should see only 5 images left after - # pruning, and the images that are least recently accessed - # should be the ones pruned... + # Add a bunch of images to the cache. The max cache size for the cache + # is set to 5KB and each image is 1K. We use 11 images in this test. + # The first 10 are added to and retrieved from cache in the same order. + # Then, the 11th image is added to cache but not retrieved before we + # prune. We should see only 5 images left after pruning, and the + # images that are least recently accessed should be the ones pruned... for x in range(10): FIXTURE_FILE = six.StringIO(FIXTURE_DATA) - self.assertTrue(self.cache.cache_image_file(x, - FIXTURE_FILE)) + self.assertTrue(self.cache.cache_image_file(x, FIXTURE_FILE)) self.assertEqual(10 * units.Ki, self.cache.get_cache_size()) @@ -194,18 +193,28 @@ class ImageCacheTestCase(object): for chunk in cache_file: buff.write(chunk) + # Add a new image to cache. + # This is specifically to test the bug: 1438564 + FIXTURE_FILE = six.StringIO(FIXTURE_DATA) + self.assertTrue(self.cache.cache_image_file(99, FIXTURE_FILE)) + self.cache.prune() self.assertEqual(5 * units.Ki, self.cache.get_cache_size()) - for x in range(0, 5): + # Ensure images 0, 1, 2, 3, 4 & 5 are not cached anymore + for x in range(0, 6): self.assertFalse(self.cache.is_cached(x), "Image %s was cached!" % x) - for x in range(5, 10): + # Ensure images 6, 7, 8 and 9 are still cached + for x in range(6, 10): self.assertTrue(self.cache.is_cached(x), "Image %s was not cached!" % x) + # Ensure the newly added image, 99, is still cached + self.assertTrue(self.cache.is_cached(99), "Image 99 was not cached!") + @skip_if_disabled def test_prune_to_zero(self): """Test that an image_cache_max_size of 0 doesn't kill the pruner |