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
This commit is contained in:
Hemanth Makkapati 2015-03-31 04:36:48 -04:00
parent e7d94536fb
commit 95d3561de8
2 changed files with 21 additions and 12 deletions

View File

@ -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):

View File

@ -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