Merge "db: Attempt to clean up part dir post replication"

This commit is contained in:
Zuul 2022-03-01 00:03:51 +00:00 committed by Gerrit Code Review
commit 3ff3076ce6
2 changed files with 20 additions and 15 deletions

View File

@ -710,16 +710,22 @@ class Replicator(Daemon):
suf_dir = os.path.dirname(hash_dir)
with lock_parent_directory(object_file):
shutil.rmtree(hash_dir, True)
try:
os.rmdir(suf_dir)
except OSError as err:
if err.errno not in (errno.ENOENT, errno.ENOTEMPTY):
self.logger.exception(
_('ERROR while trying to clean up %s') % suf_dir)
return False
self.stats['remove'] += 1
device_name = self.extract_device(object_file)
self.logger.increment('removes.' + device_name)
for parent_dir in (suf_dir, os.path.dirname(suf_dir)):
try:
os.rmdir(parent_dir)
except OSError as err:
if err.errno == errno.ENOTEMPTY:
break
elif err.errno == errno.ENOENT:
continue
else:
self.logger.exception(
'ERROR while trying to clean up %s', parent_dir)
return False
return True
def extract_device(self, object_file):

View File

@ -1066,7 +1066,9 @@ class TestDBReplicator(unittest.TestCase):
temp_dir = mkdtemp()
try:
temp_suf_dir = os.path.join(temp_dir, '16e')
temp_part_dir = os.path.join(temp_dir, '140')
os.mkdir(temp_part_dir)
temp_suf_dir = os.path.join(temp_part_dir, '16e')
os.mkdir(temp_suf_dir)
temp_hash_dir = os.path.join(temp_suf_dir,
'166e33924a08ede4204871468c11e16e')
@ -1079,6 +1081,7 @@ class TestDBReplicator(unittest.TestCase):
# sanity-checks
self.assertTrue(os.path.exists(temp_dir))
self.assertTrue(os.path.exists(temp_part_dir))
self.assertTrue(os.path.exists(temp_suf_dir))
self.assertTrue(os.path.exists(temp_hash_dir))
self.assertTrue(os.path.exists(temp_file.name))
@ -1090,6 +1093,7 @@ class TestDBReplicator(unittest.TestCase):
replicator.delete_db(temp_file)
self.assertTrue(os.path.exists(temp_dir))
self.assertTrue(os.path.exists(temp_part_dir))
self.assertTrue(os.path.exists(temp_suf_dir))
self.assertFalse(os.path.exists(temp_hash_dir))
self.assertFalse(os.path.exists(temp_file.name))
@ -1103,6 +1107,7 @@ class TestDBReplicator(unittest.TestCase):
replicator.delete_db(temp_file2)
self.assertTrue(os.path.exists(temp_dir))
self.assertFalse(os.path.exists(temp_part_dir))
self.assertFalse(os.path.exists(temp_suf_dir))
self.assertFalse(os.path.exists(temp_hash_dir))
self.assertFalse(os.path.exists(temp_file.name))
@ -2186,13 +2191,7 @@ class TestReplicatorSync(unittest.TestCase):
# running replicator will remove the deleted db
daemon = self._run_once(node, daemon=daemon)
self.assertEqual(1, daemon.stats['remove'])
# we still have a part dir (but it's empty)
suff = os.listdir(os.path.join(part_root, part))
self.assertEqual(0, len(suff))
# run it again and there's nothing to do...
daemon = self._run_once(node, daemon=daemon)
self.assertEqual(0, daemon.stats['attempted'])
# but empty part dir is cleaned up!
# which also takes out the empty part dir
parts = os.listdir(part_root)
self.assertEqual(0, len(parts))