1313// limitations under the License.
1414package com .google .devtools .build .lib .remote .common ;
1515
16- import build .bazel .remote .execution .v2 .ExecuteResponse ;
1716import build .bazel .remote .execution .v2 .RequestMetadata ;
1817import com .google .devtools .build .lib .actions .ActionExecutionMetadata ;
1918import com .google .devtools .build .lib .actions .Spawn ;
2019import javax .annotation .Nullable ;
2120
2221/** A context that provide remote execution related information for executing an action remotely. */
2322public class RemoteActionExecutionContext {
24- /** The current step of the context. */
25- public enum Step {
26- INIT ,
27- CHECK_ACTION_CACHE ,
28- UPLOAD_INPUTS ,
29- EXECUTE_REMOTELY ,
30- UPLOAD_OUTPUTS ,
31- DOWNLOAD_OUTPUTS ,
32- UPLOAD_BES_FILES ,
23+ /** Determines whether to read/write remote cache, disk cache or both. */
24+ public enum CachePolicy {
25+ NO_CACHE ,
26+ REMOTE_CACHE_ONLY ,
27+ DISK_CACHE_ONLY ,
28+ ANY_CACHE ;
29+
30+ public static CachePolicy create (boolean allowRemoteCache , boolean allowDiskCache ) {
31+ if (allowRemoteCache && allowDiskCache ) {
32+ return ANY_CACHE ;
33+ } else if (allowRemoteCache ) {
34+ return REMOTE_CACHE_ONLY ;
35+ } else if (allowDiskCache ) {
36+ return DISK_CACHE_ONLY ;
37+ } else {
38+ return NO_CACHE ;
39+ }
40+ }
41+
42+ public boolean allowAnyCache () {
43+ return this != NO_CACHE ;
44+ }
45+
46+ public boolean allowRemoteCache () {
47+ return this == REMOTE_CACHE_ONLY || this == ANY_CACHE ;
48+ }
49+
50+ public boolean allowDiskCache () {
51+ return this == DISK_CACHE_ONLY || this == ANY_CACHE ;
52+ }
53+
54+ public CachePolicy addRemoteCache () {
55+ if (this == DISK_CACHE_ONLY || this == ANY_CACHE ) {
56+ return ANY_CACHE ;
57+ }
58+
59+ return REMOTE_CACHE_ONLY ;
60+ }
3361 }
3462
3563 @ Nullable private final Spawn spawn ;
3664 private final RequestMetadata requestMetadata ;
3765 private final NetworkTime networkTime ;
38-
39- @ Nullable private ExecuteResponse executeResponse ;
40- private Step step ;
66+ private final CachePolicy writeCachePolicy ;
67+ private final CachePolicy readCachePolicy ;
4168
4269 private RemoteActionExecutionContext (
4370 @ Nullable Spawn spawn , RequestMetadata requestMetadata , NetworkTime networkTime ) {
4471 this .spawn = spawn ;
4572 this .requestMetadata = requestMetadata ;
4673 this .networkTime = networkTime ;
47- this .step = Step .INIT ;
74+ this .writeCachePolicy = CachePolicy .ANY_CACHE ;
75+ this .readCachePolicy = CachePolicy .ANY_CACHE ;
4876 }
4977
50- /** Returns current {@link Step} of the context. */
51- public Step getStep () {
52- return step ;
78+ private RemoteActionExecutionContext (
79+ @ Nullable Spawn spawn ,
80+ RequestMetadata requestMetadata ,
81+ NetworkTime networkTime ,
82+ CachePolicy writeCachePolicy ,
83+ CachePolicy readCachePolicy ) {
84+ this .spawn = spawn ;
85+ this .requestMetadata = requestMetadata ;
86+ this .networkTime = networkTime ;
87+ this .writeCachePolicy = writeCachePolicy ;
88+ this .readCachePolicy = readCachePolicy ;
89+ }
90+
91+ public RemoteActionExecutionContext withWriteCachePolicy (CachePolicy writeCachePolicy ) {
92+ return new RemoteActionExecutionContext (
93+ spawn , requestMetadata , networkTime , writeCachePolicy , readCachePolicy );
5394 }
5495
55- /** Sets current {@link Step} of the context. */
56- public void setStep ( Step step ) {
57- this . step = step ;
96+ public RemoteActionExecutionContext withReadCachePolicy ( CachePolicy readCachePolicy ) {
97+ return new RemoteActionExecutionContext (
98+ spawn , requestMetadata , networkTime , writeCachePolicy , readCachePolicy ) ;
5899 }
59100
60101 /** Returns the {@link Spawn} of the action being executed or {@code null}. */
@@ -86,13 +127,12 @@ public ActionExecutionMetadata getSpawnOwner() {
86127 return spawn .getResourceOwner ();
87128 }
88129
89- public void setExecuteResponse ( @ Nullable ExecuteResponse executeResponse ) {
90- this . executeResponse = executeResponse ;
130+ public CachePolicy getWriteCachePolicy ( ) {
131+ return writeCachePolicy ;
91132 }
92133
93- @ Nullable
94- public ExecuteResponse getExecuteResponse () {
95- return executeResponse ;
134+ public CachePolicy getReadCachePolicy () {
135+ return readCachePolicy ;
96136 }
97137
98138 /** Creates a {@link RemoteActionExecutionContext} with given {@link RequestMetadata}. */
@@ -108,4 +148,13 @@ public static RemoteActionExecutionContext create(
108148 @ Nullable Spawn spawn , RequestMetadata metadata ) {
109149 return new RemoteActionExecutionContext (spawn , metadata , new NetworkTime ());
110150 }
151+
152+ public static RemoteActionExecutionContext create (
153+ @ Nullable Spawn spawn ,
154+ RequestMetadata requestMetadata ,
155+ CachePolicy writeCachePolicy ,
156+ CachePolicy readCachePolicy ) {
157+ return new RemoteActionExecutionContext (
158+ spawn , requestMetadata , new NetworkTime (), writeCachePolicy , readCachePolicy );
159+ }
111160}
0 commit comments