You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by no...@apache.org on 2020/12/29 12:55:45 UTC

[buildstream] 01/12: element.py: Cache the result of checking whether an artifact is cached weakly

This is an automated email from the ASF dual-hosted git repository.

not-in-ldap pushed a commit to branch richardmaw/wip/log-show
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit 0fef7bcaece0b1e461cb2d0f0d9f47e9a8942cfe
Author: Richard Maw <ri...@codethink.co.uk>
AuthorDate: Mon Jun 4 16:26:26 2018 +0100

    element.py: Cache the result of checking whether an artifact is cached weakly
    
    Normally we'd only need it in the case of scheduling a weakly cached build,
    but to allow caching of failed builds we need to be able to distinguish
    between cached successes and cached failures
    for both strong and weak cache keys.
    
    To allow other cache lookup codepaths to look up via the weak key
    requires changes through the call stack to consult which key to use,
    and cache invalidation of the saved state when it changes.
---
 buildstream/element.py | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/buildstream/element.py b/buildstream/element.py
index e9cbdb6..9c61ed0 100644
--- a/buildstream/element.py
+++ b/buildstream/element.py
@@ -215,6 +215,7 @@ class Element(Plugin):
         self.__consistency = Consistency.INCONSISTENT  # Cached overall consistency state
         self.__cached = None                    # Whether we have a cached artifact
         self.__strong_cached = None             # Whether we have a cached artifact
+        self.__weak_cached = None               # Whether we have a cached artifact
         self.__assemble_scheduled = False       # Element is scheduled to be assembled
         self.__assemble_done = False            # Element is assembled
         self.__tracking_scheduled = False       # Sources are scheduled to be tracked
@@ -950,7 +951,7 @@ class Element(Plugin):
     #            the artifact cache
     #
     def _cached(self):
-        return self.__cached
+        return self.__is_cached(keystrength=None)
 
     # _buildable():
     #
@@ -1038,6 +1039,7 @@ class Element(Plugin):
             self.__weak_cache_key = None
             self.__strict_cache_key = None
             self.__strong_cached = None
+            self.__weak_cached = None
             return
 
         if self.__weak_cache_key is None:
@@ -1060,6 +1062,9 @@ class Element(Plugin):
                 # Weak cache key could not be calculated yet
                 return
 
+            if not self.__weak_cached:
+                self.__weak_cached = self.__artifacts.contains(self, self.__weak_cache_key)
+
         if not context.get_strict():
             # Full cache query in non-strict mode requires both the weak and
             # strict cache keys. However, we need to determine as early as
@@ -1067,9 +1072,9 @@ class Element(Plugin):
             # for workspaced elements. For this cache check the weak cache keys
             # are sufficient. However, don't update the `cached` attributes
             # until the full cache query below.
-            cached = self.__artifacts.contains(self, self.__weak_cache_key)
             if (not self.__assemble_scheduled and not self.__assemble_done and
-                    not cached and not self._pull_pending() and self._is_required()):
+                    not self.__is_cached(keystrength=_KeyStrength.WEAK) and
+                    not self._pull_pending() and self._is_required()):
                 self._schedule_assemble()
                 return
 
@@ -1089,6 +1094,9 @@ class Element(Plugin):
             self.__cached = self.__artifacts.contains(self, key_for_cache_lookup)
         if not self.__strong_cached:
             self.__strong_cached = self.__artifacts.contains(self, self.__strict_cache_key)
+        if key_for_cache_lookup == self.__weak_cache_key:
+            if not self.__weak_cached:
+                self.__weak_cached = self.__artifacts.contains(self, self.__weak_cache_key)
 
         if (not self.__assemble_scheduled and not self.__assemble_done and
                 not self.__cached and not self._pull_pending() and self._is_required()):
@@ -1982,12 +1990,19 @@ class Element(Plugin):
             if workspace:
                 workspace.prepared = True
 
+    def __is_cached(self, keystrength):
+        if keystrength is None:
+            return self.__cached
+
+        return self.__strong_cached if keystrength == _KeyStrength.STRONG else self.__weak_cached
+
     # __assert_cached()
     #
     # Raises an error if the artifact is not cached.
     #
-    def __assert_cached(self):
-        assert self._cached(), "{}: Missing artifact {}".format(self, self._get_brief_display_key())
+    def __assert_cached(self, keystrength=_KeyStrength.STRONG):
+        assert self.__is_cached(keystrength=keystrength), "{}: Missing artifact {}".format(
+            self, self._get_brief_display_key())
 
     # __get_tainted():
     #