13
13
// limitations under the License.
14
14
package com .google .devtools .build .lib .remote .common ;
15
15
16
- import build .bazel .remote .execution .v2 .ExecuteResponse ;
17
16
import build .bazel .remote .execution .v2 .RequestMetadata ;
18
17
import com .google .devtools .build .lib .actions .ActionExecutionMetadata ;
19
18
import com .google .devtools .build .lib .actions .Spawn ;
20
19
import javax .annotation .Nullable ;
21
20
22
21
/** A context that provide remote execution related information for executing an action remotely. */
23
22
public 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
+ }
33
61
}
34
62
35
63
@ Nullable private final Spawn spawn ;
36
64
private final RequestMetadata requestMetadata ;
37
65
private final NetworkTime networkTime ;
38
-
39
- @ Nullable private ExecuteResponse executeResponse ;
40
- private Step step ;
66
+ private final CachePolicy writeCachePolicy ;
67
+ private final CachePolicy readCachePolicy ;
41
68
42
69
private RemoteActionExecutionContext (
43
70
@ Nullable Spawn spawn , RequestMetadata requestMetadata , NetworkTime networkTime ) {
44
71
this .spawn = spawn ;
45
72
this .requestMetadata = requestMetadata ;
46
73
this .networkTime = networkTime ;
47
- this .step = Step .INIT ;
74
+ this .writeCachePolicy = CachePolicy .ANY_CACHE ;
75
+ this .readCachePolicy = CachePolicy .ANY_CACHE ;
48
76
}
49
77
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 );
53
94
}
54
95
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 ) ;
58
99
}
59
100
60
101
/** Returns the {@link Spawn} of the action being executed or {@code null}. */
@@ -86,13 +127,12 @@ public ActionExecutionMetadata getSpawnOwner() {
86
127
return spawn .getResourceOwner ();
87
128
}
88
129
89
- public void setExecuteResponse ( @ Nullable ExecuteResponse executeResponse ) {
90
- this . executeResponse = executeResponse ;
130
+ public CachePolicy getWriteCachePolicy ( ) {
131
+ return writeCachePolicy ;
91
132
}
92
133
93
- @ Nullable
94
- public ExecuteResponse getExecuteResponse () {
95
- return executeResponse ;
134
+ public CachePolicy getReadCachePolicy () {
135
+ return readCachePolicy ;
96
136
}
97
137
98
138
/** Creates a {@link RemoteActionExecutionContext} with given {@link RequestMetadata}. */
@@ -108,4 +148,13 @@ public static RemoteActionExecutionContext create(
108
148
@ Nullable Spawn spawn , RequestMetadata metadata ) {
109
149
return new RemoteActionExecutionContext (spawn , metadata , new NetworkTime ());
110
150
}
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
+ }
111
160
}
0 commit comments