Let make_db_file_path accept epoch=None

...in which case it should strip the epoch if the original path had one.

Change-Id: I8739a474c56c0f2376a276d2691c84448cb9c647
This commit is contained in:
Tim Burke 2018-05-22 13:49:17 -07:00
parent 53f418f919
commit 4af57dbc65
3 changed files with 18 additions and 17 deletions

View File

@ -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 <hash>[_<epoch>].db; this method replaces the
<epoch> part of the given ``db_path`` with the given ``epoch`` value.
A db filename takes the form ``<hash>[_<epoch>].db``; this method replaces
the ``<epoch>`` 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))

View File

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

View File

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