|
| 1 | +// Copyright 2022 The Bazel Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package com.google.devtools.build.lib.remote; |
| 15 | + |
| 16 | +import build.bazel.remote.execution.v2.Action; |
| 17 | +import build.bazel.remote.execution.v2.Command; |
| 18 | +import build.bazel.remote.execution.v2.Digest; |
| 19 | +import com.google.devtools.build.lib.actions.ActionInput; |
| 20 | +import com.google.devtools.build.lib.actions.ForbiddenActionInputException; |
| 21 | +import com.google.devtools.build.lib.actions.Spawn; |
| 22 | +import com.google.devtools.build.lib.exec.SpawnRunner.SpawnExecutionContext; |
| 23 | +import com.google.devtools.build.lib.remote.common.NetworkTime; |
| 24 | +import com.google.devtools.build.lib.remote.common.RemoteActionExecutionContext; |
| 25 | +import com.google.devtools.build.lib.remote.common.RemoteCacheClient.ActionKey; |
| 26 | +import com.google.devtools.build.lib.remote.common.RemotePathResolver; |
| 27 | +import com.google.devtools.build.lib.remote.merkletree.MerkleTree; |
| 28 | +import com.google.devtools.build.lib.vfs.PathFragment; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.SortedMap; |
| 31 | + |
| 32 | +/** A value class representing an action which can be executed remotely. */ |
| 33 | +public class RemoteAction { |
| 34 | + |
| 35 | + private final Spawn spawn; |
| 36 | + private final SpawnExecutionContext spawnExecutionContext; |
| 37 | + private final RemoteActionExecutionContext remoteActionExecutionContext; |
| 38 | + private final RemotePathResolver remotePathResolver; |
| 39 | + private final MerkleTree merkleTree; |
| 40 | + private final Digest commandHash; |
| 41 | + private final Command command; |
| 42 | + private final Action action; |
| 43 | + private final ActionKey actionKey; |
| 44 | + |
| 45 | + RemoteAction( |
| 46 | + Spawn spawn, |
| 47 | + SpawnExecutionContext spawnExecutionContext, |
| 48 | + RemoteActionExecutionContext remoteActionExecutionContext, |
| 49 | + RemotePathResolver remotePathResolver, |
| 50 | + MerkleTree merkleTree, |
| 51 | + Digest commandHash, |
| 52 | + Command command, |
| 53 | + Action action, |
| 54 | + ActionKey actionKey) { |
| 55 | + this.spawn = spawn; |
| 56 | + this.spawnExecutionContext = spawnExecutionContext; |
| 57 | + this.remoteActionExecutionContext = remoteActionExecutionContext; |
| 58 | + this.remotePathResolver = remotePathResolver; |
| 59 | + this.merkleTree = merkleTree; |
| 60 | + this.commandHash = commandHash; |
| 61 | + this.command = command; |
| 62 | + this.action = action; |
| 63 | + this.actionKey = actionKey; |
| 64 | + } |
| 65 | + |
| 66 | + public RemoteActionExecutionContext getRemoteActionExecutionContext() { |
| 67 | + return remoteActionExecutionContext; |
| 68 | + } |
| 69 | + |
| 70 | + public SpawnExecutionContext getSpawnExecutionContext() { |
| 71 | + return spawnExecutionContext; |
| 72 | + } |
| 73 | + |
| 74 | + /** Returns the {@link Spawn} that owns this action. */ |
| 75 | + public Spawn getSpawn() { |
| 76 | + return spawn; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns the sum of file sizes plus protobuf sizes used to represent the inputs of this action. |
| 81 | + */ |
| 82 | + public long getInputBytes() { |
| 83 | + return merkleTree.getInputBytes(); |
| 84 | + } |
| 85 | + |
| 86 | + /** Returns the number of input files of this action. */ |
| 87 | + public long getInputFiles() { |
| 88 | + return merkleTree.getInputFiles(); |
| 89 | + } |
| 90 | + |
| 91 | + /** Returns the id this is action. */ |
| 92 | + public String getActionId() { |
| 93 | + return actionKey.getDigest().getHash(); |
| 94 | + } |
| 95 | + |
| 96 | + /** Returns the {@link ActionKey} of this action. */ |
| 97 | + public ActionKey getActionKey() { |
| 98 | + return actionKey; |
| 99 | + } |
| 100 | + |
| 101 | + /** Returns underlying {@link Action} of this remote action. */ |
| 102 | + public Action getAction() { |
| 103 | + return action; |
| 104 | + } |
| 105 | + |
| 106 | + public Digest getCommandHash() { |
| 107 | + return commandHash; |
| 108 | + } |
| 109 | + |
| 110 | + public Command getCommand() { |
| 111 | + return command; |
| 112 | + } |
| 113 | + |
| 114 | + public MerkleTree getMerkleTree() { |
| 115 | + return merkleTree; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Returns a {@link SortedMap} which maps from input paths for remote action to {@link |
| 120 | + * ActionInput}. |
| 121 | + */ |
| 122 | + public SortedMap<PathFragment, ActionInput> getInputMap() |
| 123 | + throws IOException, ForbiddenActionInputException { |
| 124 | + return remotePathResolver.getInputMapping(spawnExecutionContext); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Returns the {@link NetworkTime} instance used to measure the network time during the action |
| 129 | + * execution. |
| 130 | + */ |
| 131 | + public NetworkTime getNetworkTime() { |
| 132 | + return remoteActionExecutionContext.getNetworkTime(); |
| 133 | + } |
| 134 | +} |
0 commit comments