Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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.skyframe; |
| 15 | |
| 16 | import com.google.common.base.Function; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 17 | import com.google.common.base.Preconditions; |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 18 | import com.google.common.base.Predicates; |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.common.collect.Iterables; |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import com.google.common.collect.Sets; |
| 24 | import com.google.devtools.build.lib.actions.Action; |
| 25 | import com.google.devtools.build.lib.actions.ActionCacheChecker.Token; |
| 26 | import com.google.devtools.build.lib.actions.ActionExecutionContext; |
| 27 | import com.google.devtools.build.lib.actions.ActionExecutionException; |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.actions.ActionLookupData; |
| 29 | import com.google.devtools.build.lib.actions.ActionLookupValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | import com.google.devtools.build.lib.actions.AlreadyReportedActionExecutionException; |
| 31 | import com.google.devtools.build.lib.actions.Artifact; |
| 32 | import com.google.devtools.build.lib.actions.MissingInputFileException; |
| 33 | import com.google.devtools.build.lib.actions.NotifyOnActionCacheHit; |
Marian Lobur | 4e0f856 | 2015-02-11 08:49:36 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.actions.PackageRootResolver; |
Klaus Aehlig | d1f4a16 | 2016-10-25 14:51:55 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.causes.Cause; |
| 36 | import com.google.devtools.build.lib.causes.LabelCause; |
philwo | 3bcb9f6 | 2017-09-06 12:52:21 +0200 | [diff] [blame] | 37 | import com.google.devtools.build.lib.clock.BlazeClock; |
Ulf Adams | a28b540 | 2017-02-24 09:28:44 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.cmdline.LabelSyntaxException; |
Kristina Chodorow | 73fa203 | 2015-08-28 17:57:46 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
| 41 | import com.google.devtools.build.lib.events.Event; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | import com.google.devtools.build.lib.util.Pair; |
| 43 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
Marian Lobur | 4e0f856 | 2015-02-11 08:49:36 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.vfs.PathFragment; |
janakr | 7f15b68 | 2018-04-11 14:55:28 -0700 | [diff] [blame] | 45 | import com.google.devtools.build.lib.vfs.Root; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | import com.google.devtools.build.skyframe.SkyFunction; |
| 47 | import com.google.devtools.build.skyframe.SkyFunctionException; |
| 48 | import com.google.devtools.build.skyframe.SkyKey; |
| 49 | import com.google.devtools.build.skyframe.SkyValue; |
| 50 | import com.google.devtools.build.skyframe.ValueOrException2; |
Han-Wen Nienhuys | 2c41b59 | 2015-02-18 16:59:12 +0000 | [diff] [blame] | 51 | import java.io.IOException; |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 52 | import java.util.ArrayList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | import java.util.Collection; |
| 54 | import java.util.Collections; |
| 55 | import java.util.HashMap; |
| 56 | import java.util.HashSet; |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 57 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 58 | import java.util.Map; |
| 59 | import java.util.Set; |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 60 | import java.util.concurrent.ConcurrentMap; |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 61 | import java.util.concurrent.atomic.AtomicReference; |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 62 | import javax.annotation.Nullable; |
| 63 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 64 | /** |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 65 | * A {@link SkyFunction} that creates {@link ActionExecutionValue}s. There are four points where |
| 66 | * this function can abort due to missing values in the graph: |
| 67 | * <ol> |
| 68 | * <li>For actions that discover inputs, if missing metadata needed to resolve an artifact from a |
| 69 | * string input in the action cache.</li> |
| 70 | * <li>If missing metadata for artifacts in inputs (including the artifacts above).</li> |
| 71 | * <li>For actions that discover inputs, if missing metadata for inputs discovered prior to |
| 72 | * execution.</li> |
| 73 | * <li>For actions that discover inputs, but do so during execution, if missing metadata for |
| 74 | * inputs discovered during execution.</li> |
| 75 | * </ol> |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | */ |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 77 | public class ActionExecutionFunction implements SkyFunction, CompletionReceiver { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 78 | private final SkyframeActionExecutor skyframeActionExecutor; |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 79 | private final AtomicReference<TimestampGranularityMonitor> tsgm; |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 80 | private ConcurrentMap<Action, ContinuationState> stateMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 81 | |
| 82 | public ActionExecutionFunction(SkyframeActionExecutor skyframeActionExecutor, |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 83 | AtomicReference<TimestampGranularityMonitor> tsgm) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 84 | this.skyframeActionExecutor = skyframeActionExecutor; |
| 85 | this.tsgm = tsgm; |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 86 | stateMap = Maps.newConcurrentMap(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public SkyValue compute(SkyKey skyKey, Environment env) throws ActionExecutionFunctionException, |
| 91 | InterruptedException { |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 92 | ActionLookupData actionLookupData = (ActionLookupData) skyKey.argument(); |
| 93 | ActionLookupValue actionLookupValue = |
janakr | baf52ae | 2018-02-14 09:03:18 -0800 | [diff] [blame] | 94 | (ActionLookupValue) env.getValue(actionLookupData.getActionLookupKey()); |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 95 | int actionIndex = actionLookupData.getActionIndex(); |
| 96 | Action action = actionLookupValue.getAction(actionIndex); |
| 97 | skyframeActionExecutor.noteActionEvaluationStarted(actionLookupData, action); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 98 | // TODO(bazel-team): Non-volatile NotifyOnActionCacheHit actions perform worse in Skyframe than |
| 99 | // legacy when they are not at the top of the action graph. In legacy, they are stored |
| 100 | // separately, so notifying non-dirty actions is cheap. In Skyframe, they depend on the |
| 101 | // BUILD_ID, forcing invalidation of upward transitive closure on each build. |
Laszlo Csomor | d4d9993 | 2015-07-10 08:36:11 +0000 | [diff] [blame] | 102 | if ((action.isVolatile() && !(action instanceof SkyframeAwareAction)) |
| 103 | || action instanceof NotifyOnActionCacheHit) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 104 | // Volatile build actions may need to execute even if none of their known inputs have changed. |
| 105 | // Depending on the buildID ensure that these actions have a chance to execute. |
| 106 | PrecomputedValue.BUILD_ID.get(env); |
| 107 | } |
Klaus Aehlig | cd708c3 | 2016-09-15 13:48:47 +0000 | [diff] [blame] | 108 | |
| 109 | // Look up the parts of the environment that influence the action. |
| 110 | Map<SkyKey, SkyValue> clientEnvLookup = |
Benjamin Peterson | a750468 | 2018-03-15 02:12:11 -0700 | [diff] [blame] | 111 | env.getValues( |
| 112 | Iterables.transform( |
| 113 | action.getClientEnvironmentVariables(), ClientEnvironmentFunction::key)); |
Klaus Aehlig | cd708c3 | 2016-09-15 13:48:47 +0000 | [diff] [blame] | 114 | if (env.valuesMissing()) { |
| 115 | return null; |
| 116 | } |
| 117 | Map<String, String> clientEnv = new HashMap<>(); |
jcater | cecb3a8 | 2018-05-01 14:37:48 -0700 | [diff] [blame] | 118 | for (Map.Entry<SkyKey, SkyValue> entry : clientEnvLookup.entrySet()) { |
Klaus Aehlig | cd708c3 | 2016-09-15 13:48:47 +0000 | [diff] [blame] | 119 | ClientEnvironmentValue envValue = (ClientEnvironmentValue) entry.getValue(); |
| 120 | if (envValue.getValue() != null) { |
| 121 | clientEnv.put((String) entry.getKey().argument(), envValue.getValue()); |
| 122 | } |
| 123 | } |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 124 | |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 125 | // For restarts of this ActionExecutionFunction we use a ContinuationState variable, below, to |
| 126 | // avoid redoing work. However, if two actions are shared and the first one executes, when the |
| 127 | // second one goes to execute, we should detect that and short-circuit, even without taking |
| 128 | // ContinuationState into account. |
| 129 | boolean sharedActionAlreadyRan = skyframeActionExecutor.probeActionExecution(action); |
| 130 | ContinuationState state; |
| 131 | if (action.discoversInputs()) { |
| 132 | state = getState(action); |
| 133 | } else { |
| 134 | // Because this is a new state, all conditionals below about whether state has already done |
| 135 | // something will return false, and so we will execute all necessary steps. |
| 136 | state = new ContinuationState(); |
| 137 | } |
| 138 | if (!state.hasCollectedInputs()) { |
| 139 | state.allInputs = collectInputs(action, env); |
| 140 | if (state.allInputs == null) { |
| 141 | // Missing deps. |
| 142 | return null; |
| 143 | } |
| 144 | } else if (state.allInputs.keysRequested != null) { |
| 145 | // Preserve the invariant that we ask for the same deps each build. |
| 146 | env.getValues(state.allInputs.keysRequested); |
| 147 | Preconditions.checkState(!env.valuesMissing(), "%s %s", action, state); |
| 148 | } |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 149 | Pair<Map<Artifact, FileArtifactValue>, Map<Artifact, Collection<Artifact>>> checkedInputs = |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 150 | null; |
| 151 | try { |
| 152 | // Declare deps on known inputs to action. We do this unconditionally to maintain our |
| 153 | // invariant of asking for the same deps each build. |
| 154 | Map<SkyKey, ValueOrException2<MissingInputFileException, ActionExecutionException>> inputDeps |
| 155 | = env.getValuesOrThrow(toKeys(state.allInputs.getAllInputs(), |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 156 | action.discoversInputs() ? action.getMandatoryInputs() : null), |
| 157 | MissingInputFileException.class, ActionExecutionException.class); |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 158 | |
| 159 | if (!sharedActionAlreadyRan && !state.hasArtifactData()) { |
| 160 | // Do we actually need to find our metadata? |
| 161 | checkedInputs = checkInputs(env, action, inputDeps); |
| 162 | } |
| 163 | } catch (ActionExecutionException e) { |
| 164 | // Remove action from state map in case it's there (won't be unless it discovers inputs). |
| 165 | stateMap.remove(action); |
| 166 | throw new ActionExecutionFunctionException(e); |
| 167 | } |
Laszlo Csomor | d4d9993 | 2015-07-10 08:36:11 +0000 | [diff] [blame] | 168 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 169 | if (env.valuesMissing()) { |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 170 | // There was missing artifact metadata in the graph. Wait for it to be present. |
Laszlo Csomor | d4d9993 | 2015-07-10 08:36:11 +0000 | [diff] [blame] | 171 | // We must check this and return here before attempting to establish any Skyframe dependencies |
| 172 | // of the action; see establishSkyframeDependencies why. |
| 173 | return null; |
| 174 | } |
| 175 | |
| 176 | try { |
| 177 | establishSkyframeDependencies(env, action); |
| 178 | } catch (ActionExecutionException e) { |
| 179 | throw new ActionExecutionFunctionException(e); |
| 180 | } |
| 181 | if (env.valuesMissing()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 182 | return null; |
| 183 | } |
| 184 | |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 185 | if (checkedInputs != null) { |
| 186 | Preconditions.checkState(!state.hasArtifactData(), "%s %s", state, action); |
| 187 | state.inputArtifactData = checkedInputs.first; |
Michael Thvedt | 434e68e | 2016-02-09 00:57:46 +0000 | [diff] [blame] | 188 | state.expandedArtifacts = checkedInputs.second; |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 191 | ActionExecutionValue result; |
| 192 | try { |
hlopko | 8b33784 | 2018-04-24 04:07:33 -0700 | [diff] [blame] | 193 | result = |
| 194 | checkCacheAndExecuteIfNeeded( |
| 195 | action, state, env, clientEnv, actionLookupData, sharedActionAlreadyRan); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 196 | } catch (ActionExecutionException e) { |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 197 | // Remove action from state map in case it's there (won't be unless it discovers inputs). |
| 198 | stateMap.remove(action); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 199 | // In this case we do not report the error to the action reporter because we have already |
| 200 | // done it in SkyframeExecutor.reportErrorIfNotAbortingMode() method. That method |
| 201 | // prints the error in the top-level reporter and also dumps the recorded StdErr for the |
| 202 | // action. Label can be null in the case of, e.g., the SystemActionOwner (for build-info.txt). |
| 203 | throw new ActionExecutionFunctionException(new AlreadyReportedActionExecutionException(e)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 204 | } |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 205 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 206 | if (env.valuesMissing()) { |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 207 | Preconditions.checkState(stateMap.containsKey(action), action); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 208 | return null; |
| 209 | } |
| 210 | |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 211 | // Remove action from state map in case it's there (won't be unless it discovers inputs). |
| 212 | stateMap.remove(action); |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 213 | actionLookupValue.actionEvaluated(actionIndex, action); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 214 | return result; |
| 215 | } |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 216 | |
| 217 | /** |
| 218 | * An action's inputs needed for execution. May not just be the result of Action#getInputs(). If |
| 219 | * the action cache's view of this action contains additional inputs, it will request metadata for |
| 220 | * them, so we consider those inputs as dependencies of this action as well. Returns null if some |
| 221 | * dependencies were missing and this ActionExecutionFunction needs to restart. |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 222 | * |
Michajlo Matijkiw | 331633c | 2015-06-09 22:09:03 +0000 | [diff] [blame] | 223 | * @throws ActionExecutionFunctionException |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 224 | */ |
| 225 | @Nullable |
Michajlo Matijkiw | 331633c | 2015-06-09 22:09:03 +0000 | [diff] [blame] | 226 | private AllInputs collectInputs(Action action, Environment env) |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 227 | throws ActionExecutionFunctionException, InterruptedException { |
Michajlo Matijkiw | dee781c | 2015-05-22 23:25:34 +0000 | [diff] [blame] | 228 | Iterable<Artifact> allKnownInputs = Iterables.concat( |
| 229 | action.getInputs(), action.getRunfilesSupplier().getArtifacts()); |
Lukacs Berki | 5ea2b14 | 2017-02-28 10:46:53 +0000 | [diff] [blame] | 230 | if (action.inputsDiscovered()) { |
Michajlo Matijkiw | dee781c | 2015-05-22 23:25:34 +0000 | [diff] [blame] | 231 | return new AllInputs(allKnownInputs); |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 232 | } |
Michajlo Matijkiw | dee781c | 2015-05-22 23:25:34 +0000 | [diff] [blame] | 233 | |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 234 | Preconditions.checkState(action.discoversInputs(), action); |
| 235 | PackageRootResolverWithEnvironment resolver = new PackageRootResolverWithEnvironment(env); |
ulfjack | 8989e19 | 2017-04-20 13:45:25 +0200 | [diff] [blame] | 236 | Iterable<Artifact> actionCacheInputs = |
| 237 | skyframeActionExecutor.getActionCachedInputs(action, resolver); |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 238 | if (actionCacheInputs == null) { |
| 239 | Preconditions.checkState(env.valuesMissing(), action); |
| 240 | return null; |
| 241 | } |
Michajlo Matijkiw | dee781c | 2015-05-22 23:25:34 +0000 | [diff] [blame] | 242 | return new AllInputs(allKnownInputs, actionCacheInputs, resolver.keysRequested); |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | private static class AllInputs { |
| 246 | final Iterable<Artifact> defaultInputs; |
| 247 | @Nullable |
| 248 | final Iterable<Artifact> actionCacheInputs; |
| 249 | @Nullable |
| 250 | final List<SkyKey> keysRequested; |
| 251 | |
| 252 | AllInputs(Iterable<Artifact> defaultInputs) { |
| 253 | this.defaultInputs = Preconditions.checkNotNull(defaultInputs); |
| 254 | this.actionCacheInputs = null; |
| 255 | this.keysRequested = null; |
| 256 | } |
| 257 | |
| 258 | AllInputs(Iterable<Artifact> defaultInputs, Iterable<Artifact> actionCacheInputs, |
| 259 | List<SkyKey> keysRequested) { |
| 260 | this.defaultInputs = Preconditions.checkNotNull(defaultInputs); |
| 261 | this.actionCacheInputs = Preconditions.checkNotNull(actionCacheInputs); |
| 262 | this.keysRequested = keysRequested; |
| 263 | } |
| 264 | |
| 265 | Iterable<Artifact> getAllInputs() { |
| 266 | return actionCacheInputs == null |
| 267 | ? defaultInputs |
| 268 | : Iterables.concat(defaultInputs, actionCacheInputs); |
| 269 | } |
| 270 | } |
| 271 | |
Marian Lobur | 4e0f856 | 2015-02-11 08:49:36 +0000 | [diff] [blame] | 272 | /** |
| 273 | * Skyframe implementation of {@link PackageRootResolver}. Should be used only from SkyFunctions, |
| 274 | * because it uses SkyFunction.Environment for evaluation of ContainingPackageLookupValue. |
| 275 | */ |
| 276 | private static class PackageRootResolverWithEnvironment implements PackageRootResolver { |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 277 | final List<SkyKey> keysRequested = new ArrayList<>(); |
Marian Lobur | 4e0f856 | 2015-02-11 08:49:36 +0000 | [diff] [blame] | 278 | private final Environment env; |
| 279 | |
| 280 | public PackageRootResolverWithEnvironment(Environment env) { |
| 281 | this.env = env; |
| 282 | } |
| 283 | |
| 284 | @Override |
janakr | 7f15b68 | 2018-04-11 14:55:28 -0700 | [diff] [blame] | 285 | public Map<PathFragment, Root> findPackageRootsForFiles(Iterable<PathFragment> execPaths) |
| 286 | throws InterruptedException { |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 287 | Preconditions.checkState(keysRequested.isEmpty(), |
| 288 | "resolver should only be called once: %s %s", keysRequested, execPaths); |
Marian Lobur | 4e0f856 | 2015-02-11 08:49:36 +0000 | [diff] [blame] | 289 | // Create SkyKeys list based on execPaths. |
Michajlo Matijkiw | 331633c | 2015-06-09 22:09:03 +0000 | [diff] [blame] | 290 | Map<PathFragment, SkyKey> depKeys = new HashMap<>(); |
Marian Lobur | 4e0f856 | 2015-02-11 08:49:36 +0000 | [diff] [blame] | 291 | for (PathFragment path : execPaths) { |
Janak Ramakrishnan | 659f80e | 2015-11-12 23:16:17 +0000 | [diff] [blame] | 292 | PathFragment parent = Preconditions.checkNotNull( |
| 293 | path.getParentDirectory(), "Must pass in files, not root directory"); |
| 294 | Preconditions.checkArgument(!parent.isAbsolute(), path); |
Ulf Adams | a28b540 | 2017-02-24 09:28:44 +0000 | [diff] [blame] | 295 | try { |
| 296 | SkyKey depKey = |
| 297 | ContainingPackageLookupValue.key(PackageIdentifier.discoverFromExecPath(path, true)); |
| 298 | depKeys.put(path, depKey); |
| 299 | keysRequested.add(depKey); |
| 300 | } catch (LabelSyntaxException e) { |
ulfjack | 8989e19 | 2017-04-20 13:45:25 +0200 | [diff] [blame] | 301 | // This code is only used to do action cache checks. If one of the file names we got from |
| 302 | // the action cache is corrupted, or if the action cache is from a different Bazel |
| 303 | // binary, then the path may not be valid for this Bazel binary, and trigger this |
| 304 | // exception. In that case, it's acceptable for us to ignore the exception - we'll get an |
| 305 | // action cache miss and re-execute the action, which is what we should do. |
| 306 | continue; |
Ulf Adams | a28b540 | 2017-02-24 09:28:44 +0000 | [diff] [blame] | 307 | } |
Marian Lobur | 4e0f856 | 2015-02-11 08:49:36 +0000 | [diff] [blame] | 308 | } |
Michajlo Matijkiw | 331633c | 2015-06-09 22:09:03 +0000 | [diff] [blame] | 309 | |
ulfjack | 8989e19 | 2017-04-20 13:45:25 +0200 | [diff] [blame] | 310 | Map<SkyKey, SkyValue> values = env.getValues(depKeys.values()); |
| 311 | if (env.valuesMissing()) { |
| 312 | return null; |
| 313 | } |
Michajlo Matijkiw | 331633c | 2015-06-09 22:09:03 +0000 | [diff] [blame] | 314 | |
janakr | 7f15b68 | 2018-04-11 14:55:28 -0700 | [diff] [blame] | 315 | Map<PathFragment, Root> result = new HashMap<>(); |
Marian Lobur | 4e0f856 | 2015-02-11 08:49:36 +0000 | [diff] [blame] | 316 | for (PathFragment path : execPaths) { |
ulfjack | 8989e19 | 2017-04-20 13:45:25 +0200 | [diff] [blame] | 317 | if (!depKeys.containsKey(path)) { |
Janak Ramakrishnan | a88e65b | 2015-09-17 15:07:26 +0000 | [diff] [blame] | 318 | continue; |
| 319 | } |
ulfjack | 8989e19 | 2017-04-20 13:45:25 +0200 | [diff] [blame] | 320 | ContainingPackageLookupValue value = |
| 321 | (ContainingPackageLookupValue) values.get(depKeys.get(path)); |
Marian Lobur | 4e0f856 | 2015-02-11 08:49:36 +0000 | [diff] [blame] | 322 | if (value.hasContainingPackage()) { |
| 323 | // We have found corresponding root for current execPath. |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 324 | result.put( |
| 325 | path, |
janakr | 7f15b68 | 2018-04-11 14:55:28 -0700 | [diff] [blame] | 326 | SkyframeExecutor.maybeTransformRootForRepository( |
Ulf Adams | a28b540 | 2017-02-24 09:28:44 +0000 | [diff] [blame] | 327 | value.getContainingPackageRoot(), |
| 328 | value.getContainingPackageName().getRepository())); |
Marian Lobur | 4e0f856 | 2015-02-11 08:49:36 +0000 | [diff] [blame] | 329 | } else { |
| 330 | // We haven't found corresponding root for current execPath. |
| 331 | result.put(path, null); |
| 332 | } |
| 333 | } |
ulfjack | 8989e19 | 2017-04-20 13:45:25 +0200 | [diff] [blame] | 334 | return result; |
Marian Lobur | 4e0f856 | 2015-02-11 08:49:36 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 337 | |
| 338 | private ActionExecutionValue checkCacheAndExecuteIfNeeded( |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 339 | Action action, |
| 340 | ContinuationState state, |
| 341 | Environment env, |
| 342 | Map<String, String> clientEnv, |
hlopko | 8b33784 | 2018-04-24 04:07:33 -0700 | [diff] [blame] | 343 | ActionLookupData actionLookupData, |
| 344 | boolean sharedActionAlreadyRan) |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 345 | throws ActionExecutionException, InterruptedException { |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 346 | // If this is a shared action and the other action is the one that executed, we must use that |
| 347 | // other action's value, provided here, since it is populated with metadata for the outputs. |
hlopko | 8b33784 | 2018-04-24 04:07:33 -0700 | [diff] [blame] | 348 | if (sharedActionAlreadyRan) { |
| 349 | return skyframeActionExecutor.executeAction( |
| 350 | env.getListener(), |
| 351 | action, |
| 352 | /* metadataHandler= */ null, |
| 353 | /* actionStartTime= */ -1, |
| 354 | /* actionExecutionContext= */ null, |
| 355 | actionLookupData, |
| 356 | /* inputDiscoveryRan= */ false); |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 357 | } |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 358 | // This may be recreated if we discover inputs. |
Janak Ramakrishnan | a5c1f96 | 2015-04-03 23:06:31 +0000 | [diff] [blame] | 359 | ActionMetadataHandler metadataHandler = new ActionMetadataHandler(state.inputArtifactData, |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 360 | action.getOutputs(), tsgm.get()); |
Lukacs Berki | 536d9cb | 2017-01-03 14:25:25 +0000 | [diff] [blame] | 361 | long actionStartTime = BlazeClock.nanoTime(); |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 362 | // We only need to check the action cache if we haven't done it on a previous run. |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 363 | if (!state.hasCheckedActionCache()) { |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 364 | state.token = |
| 365 | skyframeActionExecutor.checkActionCache( |
ulfjack | 225b57b | 2018-01-16 12:44:15 -0800 | [diff] [blame] | 366 | env.getListener(), |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 367 | action, |
| 368 | metadataHandler, |
| 369 | actionStartTime, |
| 370 | state.allInputs.actionCacheInputs, |
| 371 | clientEnv); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 372 | } |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 373 | |
| 374 | if (state.token == null) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 375 | // We got a hit from the action cache -- no need to execute. |
| 376 | return new ActionExecutionValue( |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 377 | metadataHandler.getOutputArtifactData(), |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 378 | metadataHandler.getOutputTreeArtifactData(), |
Janak Ramakrishnan | a5c1f96 | 2015-04-03 23:06:31 +0000 | [diff] [blame] | 379 | metadataHandler.getAdditionalOutputData()); |
Han-Wen Nienhuys | 2c41b59 | 2015-02-18 16:59:12 +0000 | [diff] [blame] | 380 | } |
| 381 | |
ulfjack | 1a328e3 | 2017-04-06 10:25:16 +0000 | [diff] [blame] | 382 | // Delete the metadataHandler's cache of the action's outputs, since they are being deleted. |
| 383 | metadataHandler.discardOutputMetadata(); |
| 384 | |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 385 | // This may be recreated if we discover inputs. |
janakr | d8a5753 | 2017-10-10 08:31:32 +0200 | [diff] [blame] | 386 | PerActionFileCache perActionFileCache = |
| 387 | new PerActionFileCache( |
| 388 | state.inputArtifactData, /*missingArtifactsAllowed=*/ action.discoversInputs()); |
ulfjack | 1a328e3 | 2017-04-06 10:25:16 +0000 | [diff] [blame] | 389 | if (action.discoversInputs()) { |
| 390 | if (state.discoveredInputs == null) { |
| 391 | try { |
| 392 | state.discoveredInputs = skyframeActionExecutor.discoverInputs(action, |
| 393 | perActionFileCache, metadataHandler, env); |
| 394 | Preconditions.checkState(state.discoveredInputs != null, |
| 395 | "discoverInputs() returned null on action %s", action); |
| 396 | } catch (MissingDepException e) { |
| 397 | Preconditions.checkState(env.valuesMissing(), action); |
| 398 | return null; |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 399 | } |
ulfjack | 1a328e3 | 2017-04-06 10:25:16 +0000 | [diff] [blame] | 400 | } |
| 401 | addDiscoveredInputs( |
| 402 | state.inputArtifactData, state.expandedArtifacts, state.discoveredInputs, env); |
| 403 | if (env.valuesMissing()) { |
| 404 | return null; |
| 405 | } |
janakr | d8a5753 | 2017-10-10 08:31:32 +0200 | [diff] [blame] | 406 | perActionFileCache = |
| 407 | new PerActionFileCache(state.inputArtifactData, /*missingArtifactsAllowed=*/ false); |
ulfjack | 1a328e3 | 2017-04-06 10:25:16 +0000 | [diff] [blame] | 408 | |
| 409 | // Stage 1 finished, let's do stage 2. The stage 1 of input discovery will have added some |
| 410 | // files with addDiscoveredInputs() and then have waited for those files to be available |
| 411 | // by returning null if env.valuesMissing() returned true. So stage 2 can now access those |
| 412 | // inputs to discover even more inputs and then potentially also wait for those to be |
| 413 | // available. |
| 414 | if (state.discoveredInputsStage2 == null) { |
| 415 | state.discoveredInputsStage2 = action.discoverInputsStage2(env); |
| 416 | } |
| 417 | if (state.discoveredInputsStage2 != null) { |
Googler | eba6a44 | 2017-02-06 20:08:48 +0000 | [diff] [blame] | 418 | addDiscoveredInputs( |
ulfjack | 1a328e3 | 2017-04-06 10:25:16 +0000 | [diff] [blame] | 419 | state.inputArtifactData, state.expandedArtifacts, state.discoveredInputsStage2, env); |
Lukacs Berki | 41ab6db | 2017-01-31 10:48:31 +0000 | [diff] [blame] | 420 | if (env.valuesMissing()) { |
| 421 | return null; |
| 422 | } |
janakr | d8a5753 | 2017-10-10 08:31:32 +0200 | [diff] [blame] | 423 | perActionFileCache = |
| 424 | new PerActionFileCache(state.inputArtifactData, /*missingArtifactsAllowed=*/ false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 425 | } |
ulfjack | 1a328e3 | 2017-04-06 10:25:16 +0000 | [diff] [blame] | 426 | metadataHandler = |
| 427 | new ActionMetadataHandler(state.inputArtifactData, action.getOutputs(), tsgm.get()); |
| 428 | // Set the MetadataHandler to accept output information. |
| 429 | metadataHandler.discardOutputMetadata(); |
| 430 | } |
Lukacs Berki | 41ab6db | 2017-01-31 10:48:31 +0000 | [diff] [blame] | 431 | |
ulfjack | 1a328e3 | 2017-04-06 10:25:16 +0000 | [diff] [blame] | 432 | try (ActionExecutionContext actionExecutionContext = |
| 433 | skyframeActionExecutor.getContext( |
| 434 | perActionFileCache, |
| 435 | metadataHandler, |
| 436 | Collections.unmodifiableMap(state.expandedArtifacts))) { |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 437 | if (!state.hasExecutedAction()) { |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 438 | state.value = |
| 439 | skyframeActionExecutor.executeAction( |
hlopko | 8b33784 | 2018-04-24 04:07:33 -0700 | [diff] [blame] | 440 | env.getListener(), |
| 441 | action, |
| 442 | metadataHandler, |
| 443 | actionStartTime, |
| 444 | actionExecutionContext, |
| 445 | actionLookupData, |
| 446 | /* inputDiscoveryRan= */ true); |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 447 | } |
ulfjack | 1a328e3 | 2017-04-06 10:25:16 +0000 | [diff] [blame] | 448 | } catch (IOException e) { |
| 449 | throw new ActionExecutionException( |
| 450 | "Failed to close action output", e, action, /*catastrophe=*/ false); |
Janak Ramakrishnan | 7a1db6e | 2015-06-30 19:56:11 +0000 | [diff] [blame] | 451 | } |
ulfjack | 1a328e3 | 2017-04-06 10:25:16 +0000 | [diff] [blame] | 452 | |
Janak Ramakrishnan | e905ec3 | 2015-06-30 20:16:32 +0000 | [diff] [blame] | 453 | if (action.discoversInputs()) { |
Janak Ramakrishnan | e5060fd | 2016-08-19 17:26:24 +0000 | [diff] [blame] | 454 | Iterable<Artifact> newInputs = |
| 455 | filterKnownInputs(action.getInputs(), state.inputArtifactData.keySet()); |
Michajlo Matijkiw | 9d4af3b | 2016-07-28 20:01:49 +0000 | [diff] [blame] | 456 | Map<SkyKey, SkyValue> metadataFoundDuringActionExecution = |
Janak Ramakrishnan | e5060fd | 2016-08-19 17:26:24 +0000 | [diff] [blame] | 457 | env.getValues(toKeys(newInputs, action.getMandatoryInputs())); |
Lukacs Berki | 41ab6db | 2017-01-31 10:48:31 +0000 | [diff] [blame] | 458 | state.discoveredInputs = newInputs; |
| 459 | if (env.valuesMissing()) { |
| 460 | return null; |
| 461 | } |
| 462 | if (!Iterables.isEmpty(newInputs)) { |
| 463 | // We are in the interesting case of an action that discovered its inputs during |
| 464 | // execution, and found some new ones, but the new ones were already present in the graph. |
| 465 | // We must therefore cache the metadata for those new ones. |
| 466 | Map<Artifact, FileArtifactValue> inputArtifactData = new HashMap<>(); |
| 467 | inputArtifactData.putAll(state.inputArtifactData); |
| 468 | for (Map.Entry<SkyKey, SkyValue> entry : metadataFoundDuringActionExecution.entrySet()) { |
| 469 | inputArtifactData.put( |
| 470 | ArtifactSkyKey.artifact(entry.getKey()), (FileArtifactValue) entry.getValue()); |
Janak Ramakrishnan | 271c10b | 2015-10-05 15:14:48 +0000 | [diff] [blame] | 471 | } |
Lukacs Berki | 41ab6db | 2017-01-31 10:48:31 +0000 | [diff] [blame] | 472 | state.inputArtifactData = inputArtifactData; |
ulfjack | 1a328e3 | 2017-04-06 10:25:16 +0000 | [diff] [blame] | 473 | // TODO(ulfjack): This causes information loss about omitted and injected outputs. Also see |
| 474 | // the documentation on MetadataHandler.artifactOmitted. This works by accident because |
| 475 | // markOmitted is only called for remote execution, and this code only gets executed for |
| 476 | // local execution. |
Lukacs Berki | 41ab6db | 2017-01-31 10:48:31 +0000 | [diff] [blame] | 477 | metadataHandler = |
| 478 | new ActionMetadataHandler(state.inputArtifactData, action.getOutputs(), tsgm.get()); |
Janak Ramakrishnan | 7a1db6e | 2015-06-30 19:56:11 +0000 | [diff] [blame] | 479 | } |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 480 | } |
Janak Ramakrishnan | dc89673 | 2015-12-08 18:44:45 +0000 | [diff] [blame] | 481 | Preconditions.checkState(!env.valuesMissing(), action); |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 482 | skyframeActionExecutor.afterExecution( |
| 483 | action, metadataHandler, state.token, clientEnv, actionLookupData); |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 484 | return state.value; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Janak Ramakrishnan | e5060fd | 2016-08-19 17:26:24 +0000 | [diff] [blame] | 487 | private static final Function<Artifact, SkyKey> TO_NONMANDATORY_SKYKEY = |
| 488 | new Function<Artifact, SkyKey>() { |
| 489 | @Nullable |
| 490 | @Override |
| 491 | public SkyKey apply(@Nullable Artifact artifact) { |
cushon | e1d2e39 | 2017-09-29 13:54:04 -0400 | [diff] [blame] | 492 | return ArtifactSkyKey.key(artifact, /*isMandatory=*/ false); |
Janak Ramakrishnan | e5060fd | 2016-08-19 17:26:24 +0000 | [diff] [blame] | 493 | } |
| 494 | }; |
| 495 | |
| 496 | private static Iterable<SkyKey> newlyDiscoveredInputsToSkyKeys( |
| 497 | Iterable<Artifact> discoveredInputs, Set<Artifact> knownInputs) { |
| 498 | return Iterables.transform( |
| 499 | filterKnownInputs(discoveredInputs, knownInputs), TO_NONMANDATORY_SKYKEY); |
| 500 | } |
| 501 | |
Janak Ramakrishnan | a03258d | 2016-07-27 23:16:58 +0000 | [diff] [blame] | 502 | private static void addDiscoveredInputs( |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 503 | Map<Artifact, FileArtifactValue> inputData, |
Googler | eba6a44 | 2017-02-06 20:08:48 +0000 | [diff] [blame] | 504 | Map<Artifact, Collection<Artifact>> expandedArtifacts, |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 505 | Iterable<Artifact> discoveredInputs, |
| 506 | Environment env) |
| 507 | throws InterruptedException { |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 508 | // We do not do a getValuesOrThrow() call for the following reasons: |
| 509 | // 1. No exceptions can be thrown for non-mandatory inputs; |
| 510 | // 2. Any derived inputs must be in the transitive closure of this action's inputs. Therefore, |
| 511 | // if there was an error building one of them, then that exception would have percolated up to |
| 512 | // this action already, through one of its declared inputs, and we would not have reached input |
| 513 | // discovery. |
| 514 | // Therefore there is no need to catch and rethrow exceptions as there is with #checkInputs. |
Janak Ramakrishnan | e5060fd | 2016-08-19 17:26:24 +0000 | [diff] [blame] | 515 | Map<SkyKey, SkyValue> nonMandatoryDiscovered = |
| 516 | env.getValues(newlyDiscoveredInputsToSkyKeys(discoveredInputs, inputData.keySet())); |
Janak Ramakrishnan | a03258d | 2016-07-27 23:16:58 +0000 | [diff] [blame] | 517 | if (!env.valuesMissing()) { |
jcater | cecb3a8 | 2018-05-01 14:37:48 -0700 | [diff] [blame] | 518 | for (Map.Entry<SkyKey, SkyValue> entry : nonMandatoryDiscovered.entrySet()) { |
Googler | eba6a44 | 2017-02-06 20:08:48 +0000 | [diff] [blame] | 519 | Artifact input = ArtifactSkyKey.artifact(entry.getKey()); |
| 520 | if (entry.getValue() instanceof TreeArtifactValue) { |
| 521 | TreeArtifactValue treeValue = (TreeArtifactValue) entry.getValue(); |
| 522 | expandedArtifacts.put(input, ImmutableSet.<Artifact>copyOf(treeValue.getChildren())); |
| 523 | inputData.putAll(treeValue.getChildValues()); |
| 524 | |
| 525 | inputData.put(input, treeValue.getSelfData()); |
| 526 | } else { |
| 527 | inputData.put(input, (FileArtifactValue) entry.getValue()); |
| 528 | } |
Michajlo Matijkiw | 9d4af3b | 2016-07-28 20:01:49 +0000 | [diff] [blame] | 529 | } |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 530 | } |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 533 | private static void establishSkyframeDependencies(Environment env, Action action) |
| 534 | throws ActionExecutionException, InterruptedException { |
Laszlo Csomor | d4d9993 | 2015-07-10 08:36:11 +0000 | [diff] [blame] | 535 | // Before we may safely establish Skyframe dependencies, we must build all action inputs by |
| 536 | // requesting their ArtifactValues. |
| 537 | // This is very important to do, because the establishSkyframeDependencies method may request |
| 538 | // FileValues for input files of this action (directly requesting them, or requesting some other |
| 539 | // SkyValue whose builder requests FileValues), which may not yet exist if their generating |
| 540 | // actions have not yet run. |
| 541 | // See SkyframeAwareActionTest.testRaceConditionBetweenInputAcquisitionAndSkyframeDeps |
| 542 | Preconditions.checkState(!env.valuesMissing(), action); |
| 543 | |
| 544 | if (action instanceof SkyframeAwareAction) { |
| 545 | // Skyframe-aware actions should be executed unconditionally, i.e. bypass action cache |
| 546 | // checking. See documentation of SkyframeAwareAction. |
| 547 | Preconditions.checkState(action.executeUnconditionally(), action); |
| 548 | |
| 549 | try { |
| 550 | ((SkyframeAwareAction) action).establishSkyframeDependencies(env); |
| 551 | } catch (SkyframeAwareAction.ExceptionBase e) { |
| 552 | throw new ActionExecutionException(e, action, false); |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 557 | private static Iterable<SkyKey> toKeys(Iterable<Artifact> inputs, |
| 558 | Iterable<Artifact> mandatoryInputs) { |
| 559 | if (mandatoryInputs == null) { |
| 560 | // This is a non inputs-discovering action, so no need to distinguish mandatory from regular |
| 561 | // inputs. |
Janak Ramakrishnan | ad77f97 | 2016-07-29 20:58:42 +0000 | [diff] [blame] | 562 | return Iterables.transform( |
| 563 | inputs, |
| 564 | new Function<Artifact, SkyKey>() { |
| 565 | @Override |
| 566 | public SkyKey apply(Artifact artifact) { |
| 567 | return ArtifactSkyKey.key(artifact, true); |
| 568 | } |
| 569 | }); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 570 | } else { |
| 571 | Collection<SkyKey> discoveredArtifacts = new HashSet<>(); |
| 572 | Set<Artifact> mandatory = Sets.newHashSet(mandatoryInputs); |
| 573 | for (Artifact artifact : inputs) { |
Janak Ramakrishnan | ad77f97 | 2016-07-29 20:58:42 +0000 | [diff] [blame] | 574 | discoveredArtifacts.add(ArtifactSkyKey.key(artifact, mandatory.contains(artifact))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 575 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 576 | return discoveredArtifacts; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Declare dependency on all known inputs of action. Throws exception if any are known to be |
| 582 | * missing. Some inputs may not yet be in the graph, in which case the builder should abort. |
| 583 | */ |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 584 | private Pair<Map<Artifact, FileArtifactValue>, Map<Artifact, Collection<Artifact>>> |
Michael Thvedt | 434e68e | 2016-02-09 00:57:46 +0000 | [diff] [blame] | 585 | checkInputs(Environment env, Action action, |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 586 | Map<SkyKey, ValueOrException2<MissingInputFileException, ActionExecutionException>> inputDeps) |
| 587 | throws ActionExecutionException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 588 | int missingCount = 0; |
| 589 | int actionFailures = 0; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 590 | // Only populate input data if we have the input values, otherwise they'll just go unused. |
| 591 | // We still want to loop through the inputs to collect missing deps errors. During the |
| 592 | // evaluator "error bubbling", we may get one last chance at reporting errors even though |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 593 | // some deps are still missing. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 594 | boolean populateInputData = !env.valuesMissing(); |
Klaus Aehlig | d1f4a16 | 2016-10-25 14:51:55 +0000 | [diff] [blame] | 595 | NestedSetBuilder<Cause> rootCauses = NestedSetBuilder.stableOrder(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 596 | Map<Artifact, FileArtifactValue> inputArtifactData = |
| 597 | new HashMap<>(populateInputData ? inputDeps.size() : 0); |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 598 | Map<Artifact, Collection<Artifact>> expandedArtifacts = |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 599 | new HashMap<>(populateInputData ? 128 : 0); |
| 600 | |
| 601 | ActionExecutionException firstActionExecutionException = null; |
| 602 | for (Map.Entry<SkyKey, ValueOrException2<MissingInputFileException, |
| 603 | ActionExecutionException>> depsEntry : inputDeps.entrySet()) { |
Janak Ramakrishnan | ad77f97 | 2016-07-29 20:58:42 +0000 | [diff] [blame] | 604 | Artifact input = ArtifactSkyKey.artifact(depsEntry.getKey()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 605 | try { |
Janak Ramakrishnan | ad77f97 | 2016-07-29 20:58:42 +0000 | [diff] [blame] | 606 | SkyValue value = depsEntry.getValue().get(); |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 607 | if (populateInputData) { |
| 608 | if (value instanceof AggregatingArtifactValue) { |
| 609 | AggregatingArtifactValue aggregatingValue = (AggregatingArtifactValue) value; |
| 610 | for (Pair<Artifact, FileArtifactValue> entry : aggregatingValue.getInputs()) { |
| 611 | inputArtifactData.put(entry.first, entry.second); |
| 612 | } |
| 613 | // We have to cache the "digest" of the aggregating value itself, |
| 614 | // because the action cache checker may want it. |
| 615 | inputArtifactData.put(input, aggregatingValue.getSelfData()); |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 616 | ImmutableList.Builder<Artifact> expansionBuilder = ImmutableList.builder(); |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 617 | for (Pair<Artifact, FileArtifactValue> pair : aggregatingValue.getInputs()) { |
| 618 | expansionBuilder.add(pair.first); |
| 619 | } |
| 620 | expandedArtifacts.put(input, expansionBuilder.build()); |
| 621 | } else if (value instanceof TreeArtifactValue) { |
Rumou Duan | baeed33 | 2016-10-18 15:30:25 +0000 | [diff] [blame] | 622 | TreeArtifactValue treeValue = (TreeArtifactValue) value; |
Janak Ramakrishnan | 529fe5c | 2016-10-19 10:06:38 +0000 | [diff] [blame] | 623 | expandedArtifacts.put(input, ImmutableSet.<Artifact>copyOf(treeValue.getChildren())); |
Rumou Duan | baeed33 | 2016-10-18 15:30:25 +0000 | [diff] [blame] | 624 | inputArtifactData.putAll(treeValue.getChildValues()); |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 625 | |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 626 | // Again, we cache the "digest" of the value for cache checking. |
Rumou Duan | baeed33 | 2016-10-18 15:30:25 +0000 | [diff] [blame] | 627 | inputArtifactData.put(input, treeValue.getSelfData()); |
Janak Ramakrishnan | ad77f97 | 2016-07-29 20:58:42 +0000 | [diff] [blame] | 628 | } else { |
| 629 | Preconditions.checkState(value instanceof FileArtifactValue, depsEntry); |
Googler | ece7572 | 2016-02-11 17:55:41 +0000 | [diff] [blame] | 630 | inputArtifactData.put(input, (FileArtifactValue) value); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 631 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 632 | } |
| 633 | } catch (MissingInputFileException e) { |
| 634 | missingCount++; |
| 635 | if (input.getOwner() != null) { |
Klaus Aehlig | d1f4a16 | 2016-10-25 14:51:55 +0000 | [diff] [blame] | 636 | rootCauses.add(new LabelCause(input.getOwner())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 637 | } |
| 638 | } catch (ActionExecutionException e) { |
| 639 | actionFailures++; |
janakr | 8122806 | 2017-11-06 23:13:28 +0100 | [diff] [blame] | 640 | // Prefer a catastrophic exception as the one we propagate. |
| 641 | if (firstActionExecutionException == null |
| 642 | || !firstActionExecutionException.isCatastrophe() && e.isCatastrophe()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 643 | firstActionExecutionException = e; |
| 644 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 645 | rootCauses.addTransitive(e.getRootCauses()); |
| 646 | } |
| 647 | } |
| 648 | // We need to rethrow first exception because it can contain useful error message |
| 649 | if (firstActionExecutionException != null) { |
| 650 | if (missingCount == 0 && actionFailures == 1) { |
| 651 | // In the case a single action failed, just propagate the exception upward. This avoids |
| 652 | // having to copy the root causes to the upwards transitive closure. |
| 653 | throw firstActionExecutionException; |
| 654 | } |
janakr | 8122806 | 2017-11-06 23:13:28 +0100 | [diff] [blame] | 655 | throw new ActionExecutionException( |
| 656 | firstActionExecutionException.getMessage(), |
| 657 | firstActionExecutionException.getCause(), |
| 658 | action, |
| 659 | rootCauses.build(), |
| 660 | firstActionExecutionException.isCatastrophe(), |
Googler | a715802 | 2016-01-22 21:04:55 +0000 | [diff] [blame] | 661 | firstActionExecutionException.getExitCode()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | if (missingCount > 0) { |
Klaus Aehlig | d1f4a16 | 2016-10-25 14:51:55 +0000 | [diff] [blame] | 665 | for (Cause missingInput : rootCauses.build()) { |
| 666 | env.getListener() |
| 667 | .handle( |
| 668 | Event.error( |
| 669 | action.getOwner().getLocation(), |
| 670 | String.format( |
| 671 | "%s: missing input file '%s'", |
| 672 | action.getOwner().getLabel(), missingInput.getLabel()))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 673 | } |
| 674 | throw new ActionExecutionException(missingCount + " input file(s) do not exist", action, |
| 675 | rootCauses.build(), /*catastrophe=*/false); |
| 676 | } |
Googler | eba6a44 | 2017-02-06 20:08:48 +0000 | [diff] [blame] | 677 | return Pair.of(inputArtifactData, expandedArtifacts); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 678 | } |
| 679 | |
Janak Ramakrishnan | e5060fd | 2016-08-19 17:26:24 +0000 | [diff] [blame] | 680 | private static Iterable<Artifact> filterKnownInputs( |
| 681 | Iterable<Artifact> newInputs, Set<Artifact> knownInputs) { |
| 682 | return Iterables.filter(newInputs, Predicates.not(Predicates.in(knownInputs))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /** |
| 686 | * All info/warning messages associated with actions should be always displayed. |
| 687 | */ |
| 688 | @Override |
| 689 | public String extractTag(SkyKey skyKey) { |
| 690 | return null; |
| 691 | } |
| 692 | |
| 693 | /** |
Janak Ramakrishnan | 29c5ab4 | 2015-05-14 19:38:12 +0000 | [diff] [blame] | 694 | * Exception to be thrown if an action is missing Skyframe dependencies that it finds are missing |
| 695 | * during execution/input discovery. |
| 696 | */ |
| 697 | public static class MissingDepException extends RuntimeException {} |
| 698 | |
| 699 | /** |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 700 | * Should be called once execution is over, and the intra-build cache of in-progress computations |
| 701 | * should be discarded. If the cache is non-empty (due to an interrupted/failed build), failure to |
| 702 | * call complete() can both cause a memory leak and incorrect results on the subsequent build. |
| 703 | */ |
| 704 | @Override |
| 705 | public void complete() { |
| 706 | // Discard all remaining state (there should be none after a successful execution). |
| 707 | stateMap = Maps.newConcurrentMap(); |
| 708 | } |
| 709 | |
| 710 | private ContinuationState getState(Action action) { |
| 711 | ContinuationState state = stateMap.get(action); |
| 712 | if (state == null) { |
| 713 | state = new ContinuationState(); |
| 714 | Preconditions.checkState(stateMap.put(action, state) == null, action); |
| 715 | } |
| 716 | return state; |
| 717 | } |
| 718 | |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 719 | /** |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 720 | * State to save work across restarts of ActionExecutionFunction due to missing values in the |
| 721 | * graph for actions that discover inputs. There are three places where we save work, all for |
| 722 | * actions that discover inputs: |
| 723 | * <ol> |
| 724 | * <li>If not all known input metadata (coming from Action#getInputs) is available yet, then the |
| 725 | * calculated set of inputs (including the inputs resolved from the action cache) is saved.</li> |
| 726 | * <li>If not all discovered inputs' metadata is available yet, then the known input metadata |
| 727 | * together with the set of discovered inputs is saved, as well as the Token used to identify |
| 728 | * this action to the action cache.</li> |
| 729 | * <li>If, after execution, new inputs are discovered whose metadata is not yet available, then |
| 730 | * the same data as in the previous case is saved, along with the actual result of execution. |
| 731 | * </li> |
| 732 | * </ol> |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 733 | */ |
| 734 | private static class ContinuationState { |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 735 | AllInputs allInputs; |
Janak Ramakrishnan | a03258d | 2016-07-27 23:16:58 +0000 | [diff] [blame] | 736 | /** Mutable map containing metadata for known artifacts. */ |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 737 | Map<Artifact, FileArtifactValue> inputArtifactData = null; |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 738 | Map<Artifact, Collection<Artifact>> expandedArtifacts = null; |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 739 | Token token = null; |
Michajlo Matijkiw | 9d4af3b | 2016-07-28 20:01:49 +0000 | [diff] [blame] | 740 | Iterable<Artifact> discoveredInputs = null; |
Googler | 33d6351 | 2016-12-02 00:27:22 +0000 | [diff] [blame] | 741 | Iterable<Artifact> discoveredInputsStage2 = null; |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 742 | ActionExecutionValue value = null; |
| 743 | |
| 744 | boolean hasCollectedInputs() { |
| 745 | return allInputs != null; |
| 746 | } |
| 747 | |
| 748 | boolean hasArtifactData() { |
| 749 | boolean result = inputArtifactData != null; |
Michael Thvedt | 434e68e | 2016-02-09 00:57:46 +0000 | [diff] [blame] | 750 | Preconditions.checkState(result == (expandedArtifacts != null), this); |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 751 | return result; |
| 752 | } |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 753 | |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 754 | boolean hasCheckedActionCache() { |
| 755 | // If token is null because there was an action cache hit, this method is never called again |
| 756 | // because we return immediately. |
| 757 | return token != null; |
| 758 | } |
| 759 | |
| 760 | boolean hasExecutedAction() { |
| 761 | return value != null; |
| 762 | } |
| 763 | |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 764 | @Override |
| 765 | public String toString() { |
Janak Ramakrishnan | 90f3d34 | 2015-03-27 19:45:18 +0000 | [diff] [blame] | 766 | return token + ", " + value + ", " + allInputs + ", " + inputArtifactData + ", " |
| 767 | + discoveredInputs; |
Janak Ramakrishnan | 41fad44 | 2015-03-20 17:24:45 +0000 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
| 771 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 772 | * Used to declare all the exception types that can be wrapped in the exception thrown by |
| 773 | * {@link ActionExecutionFunction#compute}. |
| 774 | */ |
| 775 | private static final class ActionExecutionFunctionException extends SkyFunctionException { |
| 776 | |
| 777 | private final ActionExecutionException actionException; |
| 778 | |
| 779 | public ActionExecutionFunctionException(ActionExecutionException e) { |
| 780 | // We conservatively assume that the error is transient. We don't have enough information to |
| 781 | // distinguish non-transient errors (e.g. compilation error from a deterministic compiler) |
| 782 | // from transient ones (e.g. IO error). |
| 783 | // TODO(bazel-team): Have ActionExecutionExceptions declare their transience. |
| 784 | super(e, Transience.TRANSIENT); |
| 785 | this.actionException = e; |
| 786 | } |
| 787 | |
| 788 | @Override |
| 789 | public boolean isCatastrophic() { |
| 790 | return actionException.isCatastrophe(); |
| 791 | } |
| 792 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 793 | } |