diff --git a/swift/common/utils.py b/swift/common/utils.py index 048e64d65d..a6b981b7d8 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -5370,22 +5370,24 @@ def make_db_file_path(db_path, epoch): Given a path to a db file, return a modified path whose filename part has the given epoch. - A db filename takes the form [_].db; this method replaces the - part of the given ``db_path`` with the given ``epoch`` value. + A db filename takes the form ``[_].db``; this method replaces + the ```` part of the given ``db_path`` with the given ``epoch`` + value, or drops the epoch part if the given ``epoch`` is ``None``. :param db_path: Path to a db file that does not necessarily exist. - :param epoch: A string that will be used as the epoch in the new path's - filename; the value will be normalized to the normal string - representation of a :class:`~swift.common.utils.Timestamp`. + :param epoch: A string (or ``None``) that will be used as the epoch + in the new path's filename; non-``None`` values will be + normalized to the normal string representation of a + :class:`~swift.common.utils.Timestamp`. :return: A modified path to a db file. :raises ValueError: if the ``epoch`` is not valid for constructing a :class:`~swift.common.utils.Timestamp`. """ - if epoch is None: - raise ValueError('epoch must not be None') - epoch = Timestamp(epoch).normal hash_, _, ext = parse_db_filename(db_path) db_dir = os.path.dirname(db_path) + if epoch is None: + return os.path.join(db_dir, hash_ + ext) + epoch = Timestamp(epoch).normal return os.path.join(db_dir, '%s_%s%s' % (hash_, epoch, ext)) diff --git a/swift/container/backend.py b/swift/container/backend.py index c109114848..34539abc15 100644 --- a/swift/container/backend.py +++ b/swift/container/backend.py @@ -329,9 +329,7 @@ class ContainerBroker(DatabaseBroker): if db_file == ':memory:': base_db_file = db_file else: - db_dir = os.path.dirname(db_file) - hash_, other, ext = parse_db_filename(db_file) - base_db_file = os.path.join(db_dir, hash_ + ext) + base_db_file = make_db_file_path(db_file, None) super(ContainerBroker, self).__init__( base_db_file, timeout, logger, account, container, pending_timeout, stale_reads_ok, skip_commits=skip_commits) @@ -361,9 +359,8 @@ class ContainerBroker(DatabaseBroker): """ hsh = hash_path(account, container) db_dir = storage_directory(DATADIR, part, hsh) - db_path = os.path.join(device_path, db_dir, hsh + '.db') - if epoch: - db_path = make_db_file_path(db_path, epoch) + db_path = make_db_file_path( + os.path.join(device_path, db_dir, hsh + '.db'), epoch) broker = ContainerBroker(db_path, account=account, container=container, logger=logger) if not os.path.exists(broker.db_file): diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index 7abad33ec2..07b5fb5d4f 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -3955,6 +3955,11 @@ cluster_dfw1 = http://dfw1.host/v1/ actual = utils.make_db_file_path(actual, epoch) self.assertEqual('/path/to/hash_%s.db' % epoch.internal, actual) + # None strips epoch + self.assertEqual('hash.db', utils.make_db_file_path('hash.db', None)) + self.assertEqual('/path/to/hash.db', utils.make_db_file_path( + '/path/to/hash_withepoch.db', None)) + # epochs shouldn't have offsets epoch = utils.Timestamp.now(offset=10) actual = utils.make_db_file_path(actual, epoch) @@ -3963,9 +3968,6 @@ cluster_dfw1 = http://dfw1.host/v1/ self.assertRaises(ValueError, utils.make_db_file_path, '/path/to/hash.db', 'bad epoch') - self.assertRaises(ValueError, utils.make_db_file_path, - '/path/to/hash.db', None) - def test_modify_priority(self): pid = os.getpid() logger = debug_logger()