Skip to content
Snippets Groups Projects
Commit 537d9408 authored by Valentin David's avatar Valentin David
Browse files

Move cas server from ref-based to object-based garbage collection.

parent bf31d6ed
No related branches found
No related tags found
No related merge requests found
......@@ -623,6 +623,41 @@ class CASCache():
# first ref of this list will be the file modified earliest.
return [ref for _, ref in sorted(zip(mtimes, refs))]
# list_objects():
#
# List cached objects in Least Recently Modified (LRM) order.
#
# Returns:
# (list) - A list of objects and timestamps in LRM order
#
def list_objects(self):
objs = []
mtimes = []
for root, _, files in os.walk(os.path.join(self.casdir, 'objects')):
for filename in files:
obj_path = os.path.join(root, filename)
try:
mtimes.append(os.path.getmtime(obj_path))
except FileNotFoundError:
pass
else:
objs.append(obj_path)
# NOTE: Sorted will sort from earliest to latest, thus the
# first element of this list will be the file modified earliest.
return sorted(zip(mtimes, objs))
def clean_up_refs_until(self, time):
ref_heads = os.path.join(self.casdir, 'refs', 'heads')
for root, _, files in os.walk(ref_heads):
for filename in files:
ref_path = os.path.join(root, filename)
# Obtain the mtime (the time a file was last modified)
if os.path.getmtime(ref_path) < time:
os.unlink(ref_path)
# remove():
#
# Removes the given symbolic ref from the repo.
......
......@@ -463,12 +463,13 @@ def _clean_up_cache(cas, object_size):
return 0
# obtain a list of LRP artifacts
LRP_artifacts = cas.list_refs()
LRP_objects = cas.list_objects()
removed_size = 0 # in bytes
last_mtime = 0
while object_size - removed_size > free_disk_space:
try:
to_remove = LRP_artifacts.pop(0) # The first element in the list is the LRP artifact
last_mtime, to_remove = LRP_objects.pop(0) # The first element in the list is the LRP objects
except IndexError:
# This exception is caught if there are no more artifacts in the list
# LRP_artifacts. This means the the artifact is too large for the filesystem
......@@ -477,7 +478,14 @@ def _clean_up_cache(cas, object_size):
"the filesystem which mounts the remote "
"cache".format(object_size))
removed_size += cas.remove(to_remove, defer_prune=False)
try:
size = os.stat(to_remove).st_size
os.unlink(to_remove)
removed_size += size
except FileNotFoundError:
pass
cas.clean_up_refs_until(last_mtime)
if removed_size > 0:
logging.info("Successfully removed {} bytes from the cache".format(removed_size))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment