• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tradefed.cache;
18 
19 import com.android.tradefed.invoker.tracing.CloseableTraceScope;
20 
21 import build.bazel.remote.execution.v2.Action;
22 import build.bazel.remote.execution.v2.Command;
23 import build.bazel.remote.execution.v2.Command.EnvironmentVariable;
24 import build.bazel.remote.execution.v2.Digest;
25 import build.bazel.remote.execution.v2.Platform;
26 import build.bazel.remote.execution.v2.Platform.Property;
27 
28 import com.google.auto.value.AutoValue;
29 import com.google.protobuf.Duration;
30 
31 import java.io.File;
32 import java.io.IOException;
33 import java.util.Map;
34 import java.util.stream.Collectors;
35 
36 /**
37  * A value class representing an action which can be executed.
38  *
39  * <p>Terminology note: "action" is used here in the remote execution protocol sense.
40  */
41 @AutoValue
42 public abstract class ExecutableAction {
43 
44     // A silo key used to invalid stale cache.
45     private static final String SILO_CACHE_KEY = "0.0";
46 
47     /** Builds an {@link ExecutableAction}. */
create( File input, Iterable<String> args, Map<String, String> envVariables, long timeout)48     public static ExecutableAction create(
49             File input, Iterable<String> args, Map<String, String> envVariables, long timeout)
50             throws IOException {
51         try (CloseableTraceScope ignored = new CloseableTraceScope("create_executable_action")) {
52             Command command =
53                     Command.newBuilder()
54                             .addAllArguments(args)
55                             .setPlatform(
56                                     Platform.newBuilder()
57                                             .addProperties(
58                                                     Property.newBuilder()
59                                                             .setName("cache-silo-key")
60                                                             .setValue(SILO_CACHE_KEY)
61                                                             .build())
62                                             .build())
63                             .addAllEnvironmentVariables(
64                                     envVariables.entrySet().stream()
65                                             .map(
66                                                     entry ->
67                                                             EnvironmentVariable.newBuilder()
68                                                                     .setName(entry.getKey())
69                                                                     .setValue(entry.getValue())
70                                                                     .build())
71                                             .collect(Collectors.toList()))
72                             .build();
73 
74             MerkleTree inputMerkleTree = MerkleTree.buildFromDir(input);
75             Action.Builder actionBuilder =
76                     Action.newBuilder()
77                             .setInputRootDigest(inputMerkleTree.rootDigest())
78                             .setCommandDigest(DigestCalculator.compute(command));
79             if (timeout > 0L) {
80                 actionBuilder.setTimeout(Duration.newBuilder().setSeconds(timeout).build());
81             }
82 
83             Action action = actionBuilder.build();
84             return new AutoValue_ExecutableAction(
85                     action,
86                     DigestCalculator.compute(action),
87                     command,
88                     DigestCalculator.compute(command),
89                     inputMerkleTree);
90         }
91     }
92 
action()93     public abstract Action action();
94 
actionDigest()95     public abstract Digest actionDigest();
96 
command()97     public abstract Command command();
98 
commandDigest()99     public abstract Digest commandDigest();
100 
input()101     public abstract MerkleTree input();
102 }
103