# glance.image_cache.drivers.common

# Source code for glance.image_cache.drivers.common

    # Copyright 2023 OpenStack Foundation
    # All Rights Reserved.
    #
    #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    #    not use this file except in compliance with the License. You may obtain
    #    a copy of the License at
    #
    #         http://www.apache.org/licenses/LICENSE-2.0
    #
    #    Unless required by applicable law or agreed to in writing, software
    #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    #    License for the specific language governing permissions and limitations
    #    under the License.

    """
    Common code which will be used in SQLite and centralzed_db driver until SQLite
    driver is removed from glance.
    """
    from contextlib import contextmanager
    import sqlite3
    import time

    from oslo_log import log as logging

    from glance.i18n import _LE

    LOG = logging.getLogger(__name__)

    DEFAULT_SQL_CALL_TIMEOUT = 2

    def dict_factory(cur, row):
        return {col[0]: row[idx] for idx, col in enumerate(cur.description)}

    class SqliteConnection(sqlite3.Connection):

        """
        SQLite connection that retries on database-is-locked errors with a
        bounded total wait, similar to Swift's SQLite helper.
        """

        def __init__(self, *args, **kwargs):
            self.timeout_seconds = kwargs.get('timeout', DEFAULT_SQL_CALL_TIMEOUT)
            kwargs['timeout'] = 0
            sqlite3.Connection.__init__(self, *args, **kwargs)

        def _timeout(self, call):
            deadline = time.monotonic() + self.timeout_seconds
            while True:
                if time.monotonic() >= deadline:
                    raise TimeoutError(
                        'SQLite operation exceeded %.2f seconds' %
                        self.timeout_seconds)
                try:
                    return call()
                except sqlite3.OperationalError as e:
                    if 'locked' not in str(e):
                        raise
                time.sleep(0.05)

        def execute(self, *args, **kwargs):
            return self._timeout(lambda: sqlite3.Connection.execute(
                self, *args, **kwargs))

        def commit(self):
            return self._timeout(lambda: sqlite3.Connection.commit(self))

    @contextmanager
    def get_db(db_path):
        """
        Returns a context manager that produces a database connection that
        self-closes and calls rollback if an error occurs while using the
        database connection
        """
        conn = sqlite3.connect(db_path, check_same_thread=False,
                               factory=SqliteConnection)
        conn.row_factory = sqlite3.Row
        conn.text_factory = str
        conn.execute('PRAGMA synchronous = NORMAL')
        conn.execute('PRAGMA count_changes = OFF')
        conn.execute('PRAGMA temp_store = MEMORY')
        try:
            yield conn
        except sqlite3.DatabaseError as e:
            msg = _LE("Error executing SQLite call. Got error: %s") % e
            LOG.error(msg)
            conn.rollback()
        finally:
            conn.close()

this page last updated: 2026-08-19 03:42:39

Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.

found an error? report a bug