diff --git a/glance/image_cache/drivers/sqlite.py b/glance/image_cache/drivers/sqlite.py index 89a8764558..d276c4ef62 100644 --- a/glance/image_cache/drivers/sqlite.py +++ b/glance/image_cache/drivers/sqlite.py @@ -26,6 +26,7 @@ import time from eventlet import sleep from eventlet import timeout +from oslo_concurrency import lockutils from oslo_config import cfg from oslo_log import log as logging from oslo_utils import excutils @@ -119,26 +120,32 @@ class Driver(base.Driver): def initialize_db(self): db = CONF.image_cache_sqlite_db self.db_path = os.path.join(self.base_dir, db) - try: - conn = sqlite3.connect(self.db_path, check_same_thread=False, - factory=SqliteConnection) - conn.executescript(""" - CREATE TABLE IF NOT EXISTS cached_images ( - image_id TEXT PRIMARY KEY, - last_accessed REAL DEFAULT 0.0, - last_modified REAL DEFAULT 0.0, - size INTEGER DEFAULT 0, - hits INTEGER DEFAULT 0, - checksum TEXT - ); - """) - conn.close() - except sqlite3.DatabaseError as e: - msg = _("Failed to initialize the image cache database. " - "Got error: %s") % e - LOG.error(msg) - raise exception.BadDriverConfiguration(driver_name='sqlite', - reason=msg) + lockutils.set_defaults(self.base_dir) + + @lockutils.synchronized('image_cache_db_init', external=True) + def create_db(): + try: + conn = sqlite3.connect(self.db_path, check_same_thread=False, + factory=SqliteConnection) + conn.executescript(""" + CREATE TABLE IF NOT EXISTS cached_images ( + image_id TEXT PRIMARY KEY, + last_accessed REAL DEFAULT 0.0, + last_modified REAL DEFAULT 0.0, + size INTEGER DEFAULT 0, + hits INTEGER DEFAULT 0, + checksum TEXT + ); + """) + conn.close() + except sqlite3.DatabaseError as e: + msg = _("Failed to initialize the image cache database. " + "Got error: %s") % e + LOG.error(msg) + raise exception.BadDriverConfiguration(driver_name='sqlite', + reason=msg) + + create_db() def get_cache_size(self): """ diff --git a/glance/tests/functional/test_bin_glance_cache_manage.py b/glance/tests/functional/test_bin_glance_cache_manage.py index 7ab63e84b8..d933f783ec 100644 --- a/glance/tests/functional/test_bin_glance_cache_manage.py +++ b/glance/tests/functional/test_bin_glance_cache_manage.py @@ -214,6 +214,7 @@ class TestBinGlanceCacheManage(functional.FunctionalTest): 'image_cache_dir': self.api_server.image_cache_dir, 'image_cache_driver': self.image_cache_driver, 'registry_port': self.registry_server.bind_port, + 'lock_path': self.test_dir, 'log_file': os.path.join(self.test_dir, 'cache.log'), 'metadata_encryption_key': "012345678901234567890123456789ab", 'filesystem_store_datadir': self.test_dir @@ -221,6 +222,7 @@ class TestBinGlanceCacheManage(functional.FunctionalTest): with open(cache_config_filepath, 'w') as cache_file: cache_file.write("""[DEFAULT] debug = True +lock_path = %(lock_path)s image_cache_dir = %(image_cache_dir)s image_cache_driver = %(image_cache_driver)s registry_host = 127.0.0.1 diff --git a/glance/tests/functional/test_cache_middleware.py b/glance/tests/functional/test_cache_middleware.py index 1bc86f8b41..9508d97897 100644 --- a/glance/tests/functional/test_cache_middleware.py +++ b/glance/tests/functional/test_cache_middleware.py @@ -932,12 +932,14 @@ class BaseCacheManageMiddlewareTest(object): 'image_cache_driver': self.image_cache_driver, 'registry_port': self.registry_server.bind_port, 'log_file': os.path.join(self.test_dir, 'cache.log'), + 'lock_path': self.test_dir, 'metadata_encryption_key': "012345678901234567890123456789ab", 'filesystem_store_datadir': self.test_dir } with open(cache_config_filepath, 'w') as cache_file: cache_file.write("""[DEFAULT] debug = True +lock_path = %(lock_path)s image_cache_dir = %(image_cache_dir)s image_cache_driver = %(image_cache_driver)s registry_host = 127.0.0.1 diff --git a/releasenotes/notes/lock_path_config_option-2771feaa649e4563.yaml b/releasenotes/notes/lock_path_config_option-2771feaa649e4563.yaml new file mode 100644 index 0000000000..f6171c00cc --- /dev/null +++ b/releasenotes/notes/lock_path_config_option-2771feaa649e4563.yaml @@ -0,0 +1,5 @@ +--- +upgrade: + - The lock_path config option from oslo.concurrency is now required for using + the sql image_cache driver. If one is not specified it will default to + the image_cache_dir and emit a warning.