From 2d9cb0c0360d8d10bdae387533e61a13da3f5f1f Mon Sep 17 00:00:00 2001
From: Tiago Gomes <tiago.gomes@codethink.co.uk>
Date: Tue, 11 Sep 2018 15:10:40 +0100
Subject: [PATCH] artifactcache: replace set_cache_size()

Replace set_cache_size() with subtract_cache_size(), for symmetry with
add_artifact_size().
---
 buildstream/_artifactcache/artifactcache.py | 47 ++++++++-------------
 buildstream/_scheduler/jobs/cleanupjob.py   |  2 +-
 2 files changed, 18 insertions(+), 31 deletions(-)

diff --git a/buildstream/_artifactcache/artifactcache.py b/buildstream/_artifactcache/artifactcache.py
index 047587b4ed..ea8a323714 100644
--- a/buildstream/_artifactcache/artifactcache.py
+++ b/buildstream/_artifactcache/artifactcache.py
@@ -234,7 +234,7 @@ class ArtifactCache():
     # Clean the artifact cache as much as possible.
     #
     # Returns:
-    #    (int): The size of the cache after having cleaned up
+    #     (int): Amount of bytes cleaned from the cache.
     #
     def clean(self):
         artifacts = self.list_artifacts()
@@ -252,7 +252,7 @@ class ArtifactCache():
             ])
 
         # Do a real computation of the cache size once, just in case
-        self.compute_cache_size()
+        old_cache_size = self.compute_cache_size()
 
         while self.get_cache_size() >= self._cache_lower_threshold:
             try:
@@ -280,12 +280,9 @@ class ArtifactCache():
 
                 # Remove the actual artifact, if it's not required.
                 size = self.remove(to_remove)
+                self._cache_size -= size
 
-                # Remove the size from the removed size
-                self.set_cache_size(self._cache_size - size)
-
-        # This should be O(1) if implemented correctly
-        return self.get_cache_size()
+        return old_cache_size - self._cache_size
 
     # compute_cache_size()
     #
@@ -302,17 +299,24 @@ class ArtifactCache():
 
     # add_artifact_size()
     #
-    # Adds the reported size of a newly cached artifact to the
-    # current cache size.
+    # Adds given artifact size to the cache size
     #
     # Args:
-    #     artifact_size (int): The size to add.
+    #     artifact_size (int): The artifact size to add.
     #
     def add_artifact_size(self, artifact_size):
-        cache_size = self.get_cache_size()
-        cache_size += artifact_size
+        self._cache_size = self.get_cache_size() + artifact_size
+        self._write_cache_size(self._cache_size)
 
-        self.set_cache_size(cache_size)
+    # subtract_artifact_size()
+    #
+    # Subtracts given artifact size from the cache size
+    #
+    # Args:
+    #     artifact_size (int): The artifact size to subtract.
+    #
+    def subtract_artifact_size(self, artifact_size):
+        self.add_artifact_size(artifact_size * -1)
 
     # get_cache_size()
     #
@@ -330,23 +334,6 @@ class ArtifactCache():
 
         return self._cache_size
 
-    # set_cache_size()
-    #
-    # Forcefully set the overall cache size.
-    #
-    # This is used to update the size in the main process after
-    # having calculated in a cleanup or a cache size calculation job.
-    #
-    # Args:
-    #     cache_size (int): The size to set.
-    #
-    def set_cache_size(self, cache_size):
-
-        assert cache_size is not None
-
-        self._cache_size = cache_size
-        self._write_cache_size(self._cache_size)
-
     # has_quota_exceeded()
     #
     # Checks if the current artifact cache size exceeds the quota.
diff --git a/buildstream/_scheduler/jobs/cleanupjob.py b/buildstream/_scheduler/jobs/cleanupjob.py
index 399435ad9b..1f1cb146fe 100644
--- a/buildstream/_scheduler/jobs/cleanupjob.py
+++ b/buildstream/_scheduler/jobs/cleanupjob.py
@@ -32,4 +32,4 @@ class CleanupJob(Job):
 
     def parent_complete(self, success, result):
         if success:
-            self._artifacts.set_cache_size(result)
+            self._artifacts.subtract_artifact_size(result)
-- 
GitLab