... |
... |
@@ -17,9 +17,17 @@ |
17
|
17
|
|
18
|
18
|
"""
|
19
|
19
|
ActionCache
|
20
|
|
-==================
|
|
20
|
+===========
|
21
|
21
|
|
22
|
22
|
Implements a simple in-memory action cache.
|
|
23
|
+
|
|
24
|
+The action cache maps Action to their corresponding ActionResult. An
|
|
25
|
+ActionResult may be found in cache, for any given Action, if that action has
|
|
26
|
+already been executed.
|
|
27
|
+
|
|
28
|
+Note:
|
|
29
|
+ Action and ActionResult are referenced by their Digest and mapping is stored
|
|
30
|
+ in-memory.
|
23
|
31
|
"""
|
24
|
32
|
|
25
|
33
|
import collections
|
... |
... |
@@ -28,15 +36,29 @@ from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_p |
28
|
36
|
|
29
|
37
|
|
30
|
38
|
class ActionCache:
|
|
39
|
+ """In-memory Action to ActionResult associative array.
|
|
40
|
+ """
|
31
|
41
|
|
32
|
42
|
def __init__(self, storage, max_cached_actions):
|
|
43
|
+ """Initialises a new ActionCache instance.
|
|
44
|
+
|
|
45
|
+ Args:
|
|
46
|
+ storage (StorageABC): storage backend instance to be used.
|
|
47
|
+ max_cached_actions (int): maximun number of entries to cache.
|
|
48
|
+ """
|
33
|
49
|
self._storage = storage
|
34
|
50
|
self._max_cached_actions = max_cached_actions
|
35
|
51
|
self._digest_map = collections.OrderedDict()
|
36
|
52
|
|
37
|
53
|
def get_action_result(self, action_digest):
|
38
|
|
- """Return the cached ActionResult for the given Action digest, or None
|
39
|
|
- if there isn't one.
|
|
54
|
+ """Retrieves the cached ActionResult for the given Action digest.
|
|
55
|
+
|
|
56
|
+ Args:
|
|
57
|
+ action_digest (Digest): digest of the Action to query.
|
|
58
|
+
|
|
59
|
+ Returns:
|
|
60
|
+ The cached ActionResult matching the given Action digest or None if
|
|
61
|
+ the nothing hass been cached yet for that Action.
|
40
|
62
|
"""
|
41
|
63
|
key = (action_digest.hash, action_digest.size_bytes)
|
42
|
64
|
if key in self._digest_map:
|
... |
... |
@@ -50,8 +72,15 @@ class ActionCache: |
50
|
72
|
return None
|
51
|
73
|
|
52
|
74
|
def put_action_result(self, action_digest, action_result):
|
53
|
|
- """Add the given ActionResult to the cache for the given Action
|
54
|
|
- digest.
|
|
75
|
+ """Stores an ActionResult in cache for the given Action digest.
|
|
76
|
+
|
|
77
|
+ If the cache size limit has been reached, the oldest cache entries will
|
|
78
|
+ be dropped before insertion so that the cache size never exceeds the
|
|
79
|
+ maximum numbers of entries allowed.
|
|
80
|
+
|
|
81
|
+ Args:
|
|
82
|
+ action_digest (Digest): digest of the Action to select.
|
|
83
|
+ action_result (ActionResult): result object to store.
|
55
|
84
|
"""
|
56
|
85
|
if self._max_cached_actions == 0:
|
57
|
86
|
return
|
... |
... |
@@ -64,8 +93,14 @@ class ActionCache: |
64
|
93
|
self._digest_map[key] = action_result_digest
|
65
|
94
|
|
66
|
95
|
def _blobs_still_exist(self, action_result):
|
67
|
|
- """Return True if all the CAS blobs referenced by the given
|
68
|
|
- ActionResult are present in CAS.
|
|
96
|
+ """Checks CAS for ActionResult output blobs existance.
|
|
97
|
+
|
|
98
|
+ Args:
|
|
99
|
+ action_result (ActionResult): ActionResult to search referenced
|
|
100
|
+ output blobs for.
|
|
101
|
+
|
|
102
|
+ Returns:
|
|
103
|
+ True if all referenced blobs are present in CAS, False otherwise.
|
69
|
104
|
"""
|
70
|
105
|
blobs_needed = []
|
71
|
106
|
|