ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 1 | // Copyright 2017 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 | |
Jakob Buchgraber | 321138f | 2018-05-07 05:24:53 -0700 | [diff] [blame] | 16 | import static com.google.common.base.Strings.isNullOrEmpty; |
buchgr | d480c5f | 2019-04-03 00:53:34 -0700 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.remote.util.Utils.createSpawnResult; |
| 18 | import static com.google.devtools.build.lib.remote.util.Utils.getInMemoryOutputPath; |
Jakob Buchgraber | 11a98a5 | 2019-09-02 07:57:02 -0700 | [diff] [blame] | 19 | import static com.google.devtools.build.lib.remote.util.Utils.hasFilesToDownload; |
Jakob Buchgraber | 50c1004 | 2019-04-11 02:11:19 -0700 | [diff] [blame] | 20 | import static com.google.devtools.build.lib.remote.util.Utils.shouldDownloadAllSpawnOutputs; |
Jakob Buchgraber | 321138f | 2018-05-07 05:24:53 -0700 | [diff] [blame] | 21 | |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 22 | import build.bazel.remote.execution.v2.Action; |
| 23 | import build.bazel.remote.execution.v2.ActionResult; |
| 24 | import build.bazel.remote.execution.v2.Command; |
buchgr | 7f72544 | 2019-03-08 03:20:25 -0800 | [diff] [blame] | 25 | import build.bazel.remote.execution.v2.Digest; |
John Cater | 3afb7b0 | 2019-01-18 11:44:51 -0800 | [diff] [blame] | 26 | import build.bazel.remote.execution.v2.Platform; |
Jakob Buchgraber | 50c1004 | 2019-04-11 02:11:19 -0700 | [diff] [blame] | 27 | import com.google.common.base.Preconditions; |
George Gensure | 7aa7498 | 2020-04-01 00:54:15 -0700 | [diff] [blame] | 28 | import com.google.common.base.Stopwatch; |
George Gensure | 54b062c | 2020-09-02 05:06:40 -0700 | [diff] [blame] | 29 | import com.google.common.base.Throwables; |
Jakob Buchgraber | 50c1004 | 2019-04-11 02:11:19 -0700 | [diff] [blame] | 30 | import com.google.common.collect.ImmutableSet; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 31 | import com.google.devtools.build.lib.actions.ActionInput; |
| 32 | import com.google.devtools.build.lib.actions.ExecException; |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.lib.actions.FileArtifactValue; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 34 | import com.google.devtools.build.lib.actions.Spawn; |
George Gensure | 7aa7498 | 2020-04-01 00:54:15 -0700 | [diff] [blame] | 35 | import com.google.devtools.build.lib.actions.SpawnMetrics; |
ruperts | da40fbf | 2017-09-22 05:59:42 +0200 | [diff] [blame] | 36 | import com.google.devtools.build.lib.actions.SpawnResult; |
| 37 | import com.google.devtools.build.lib.actions.SpawnResult.Status; |
olaola | a22d0e9 | 2017-12-11 07:53:15 -0800 | [diff] [blame] | 38 | import com.google.devtools.build.lib.actions.Spawns; |
Mike Morearty | d8ac06a | 2018-04-12 01:59:34 -0700 | [diff] [blame] | 39 | import com.google.devtools.build.lib.actions.cache.VirtualActionInput; |
Googler | 77ef8d6 | 2019-07-23 11:55:53 -0700 | [diff] [blame] | 40 | import com.google.devtools.build.lib.analysis.platform.PlatformUtils; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 41 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 42 | import com.google.devtools.build.lib.events.Event; |
| 43 | import com.google.devtools.build.lib.events.Reporter; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 44 | import com.google.devtools.build.lib.exec.SpawnCache; |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 45 | import com.google.devtools.build.lib.exec.SpawnRunner.ProgressStatus; |
tomlu | 29e306d | 2018-04-19 05:41:44 -0700 | [diff] [blame] | 46 | import com.google.devtools.build.lib.exec.SpawnRunner.SpawnExecutionContext; |
ulfjack | 5f436f3 | 2018-11-06 05:17:23 -0800 | [diff] [blame] | 47 | import com.google.devtools.build.lib.profiler.Profiler; |
Jakob Buchgraber | 4b3c2eb | 2019-04-04 02:01:48 -0700 | [diff] [blame] | 48 | import com.google.devtools.build.lib.profiler.ProfilerTask; |
ulfjack | 5f436f3 | 2018-11-06 05:17:23 -0800 | [diff] [blame] | 49 | import com.google.devtools.build.lib.profiler.SilentCloseable; |
Jakob Buchgraber | 1846269 | 2019-11-06 04:34:16 -0800 | [diff] [blame] | 50 | import com.google.devtools.build.lib.remote.common.CacheNotFoundException; |
Jakob Buchgraber | 6056609 | 2019-11-11 06:28:22 -0800 | [diff] [blame] | 51 | import com.google.devtools.build.lib.remote.common.RemoteCacheClient.ActionKey; |
buchgr | 7f72544 | 2019-03-08 03:20:25 -0800 | [diff] [blame] | 52 | import com.google.devtools.build.lib.remote.merkletree.MerkleTree; |
Jakob Buchgraber | 75b7ed4 | 2019-03-27 10:27:13 -0700 | [diff] [blame] | 53 | import com.google.devtools.build.lib.remote.options.RemoteOptions; |
buchgr | d480c5f | 2019-04-03 00:53:34 -0700 | [diff] [blame] | 54 | import com.google.devtools.build.lib.remote.options.RemoteOutputsMode; |
Googler | 922d1e6 | 2018-03-05 14:49:00 -0800 | [diff] [blame] | 55 | import com.google.devtools.build.lib.remote.util.DigestUtil; |
George Gensure | 7aa7498 | 2020-04-01 00:54:15 -0700 | [diff] [blame] | 56 | import com.google.devtools.build.lib.remote.util.NetworkTime; |
Googler | 922d1e6 | 2018-03-05 14:49:00 -0800 | [diff] [blame] | 57 | import com.google.devtools.build.lib.remote.util.TracingMetadataUtils; |
Jakob Buchgraber | a79a4b6 | 2019-06-23 02:06:20 -0700 | [diff] [blame] | 58 | import com.google.devtools.build.lib.remote.util.Utils; |
buchgr | d480c5f | 2019-04-03 00:53:34 -0700 | [diff] [blame] | 59 | import com.google.devtools.build.lib.remote.util.Utils.InMemoryOutput; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 60 | import com.google.devtools.build.lib.vfs.Path; |
| 61 | import com.google.devtools.build.lib.vfs.PathFragment; |
olaola | 6f32d5a | 2017-09-20 17:12:19 +0200 | [diff] [blame] | 62 | import io.grpc.Context; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 63 | import java.io.IOException; |
| 64 | import java.util.Collection; |
Jakob Buchgraber | 321138f | 2018-05-07 05:24:53 -0700 | [diff] [blame] | 65 | import java.util.HashSet; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 66 | import java.util.NoSuchElementException; |
Jakob Buchgraber | 321138f | 2018-05-07 05:24:53 -0700 | [diff] [blame] | 67 | import java.util.Set; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 68 | import java.util.SortedMap; |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 69 | import javax.annotation.Nullable; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 70 | |
Jakob Buchgraber | 321138f | 2018-05-07 05:24:53 -0700 | [diff] [blame] | 71 | /** A remote {@link SpawnCache} implementation. */ |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 72 | @ThreadSafe // If the RemoteActionCache implementation is thread-safe. |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 73 | final class RemoteSpawnCache implements SpawnCache { |
Jakob Buchgraber | 50c1004 | 2019-04-11 02:11:19 -0700 | [diff] [blame] | 74 | |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 75 | private final Path execRoot; |
| 76 | private final RemoteOptions options; |
George Gensure | 54b062c | 2020-09-02 05:06:40 -0700 | [diff] [blame] | 77 | private final boolean verboseFailures; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 78 | |
Jakob Buchgraber | 6056609 | 2019-11-11 06:28:22 -0800 | [diff] [blame] | 79 | private final RemoteCache remoteCache; |
olaola | 6f32d5a | 2017-09-20 17:12:19 +0200 | [diff] [blame] | 80 | private final String buildRequestId; |
| 81 | private final String commandId; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 82 | |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 83 | @Nullable private final Reporter cmdlineReporter; |
| 84 | |
Jakob Buchgraber | 321138f | 2018-05-07 05:24:53 -0700 | [diff] [blame] | 85 | private final Set<String> reportedErrors = new HashSet<>(); |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 86 | |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 87 | private final DigestUtil digestUtil; |
| 88 | |
Jakob Buchgraber | 50c1004 | 2019-04-11 02:11:19 -0700 | [diff] [blame] | 89 | /** |
Jakob Buchgraber | 11a98a5 | 2019-09-02 07:57:02 -0700 | [diff] [blame] | 90 | * If {@link RemoteOutputsMode#TOPLEVEL} is specified it contains the artifacts that should be |
| 91 | * downloaded. |
Jakob Buchgraber | 50c1004 | 2019-04-11 02:11:19 -0700 | [diff] [blame] | 92 | */ |
Jakob Buchgraber | 11a98a5 | 2019-09-02 07:57:02 -0700 | [diff] [blame] | 93 | private final ImmutableSet<ActionInput> filesToDownload; |
Jakob Buchgraber | 50c1004 | 2019-04-11 02:11:19 -0700 | [diff] [blame] | 94 | |
olaola | 6f32d5a | 2017-09-20 17:12:19 +0200 | [diff] [blame] | 95 | RemoteSpawnCache( |
| 96 | Path execRoot, |
| 97 | RemoteOptions options, |
George Gensure | 54b062c | 2020-09-02 05:06:40 -0700 | [diff] [blame] | 98 | boolean verboseFailures, |
Jakob Buchgraber | 6056609 | 2019-11-11 06:28:22 -0800 | [diff] [blame] | 99 | RemoteCache remoteCache, |
olaola | 6f32d5a | 2017-09-20 17:12:19 +0200 | [diff] [blame] | 100 | String buildRequestId, |
| 101 | String commandId, |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 102 | @Nullable Reporter cmdlineReporter, |
Jakob Buchgraber | 50c1004 | 2019-04-11 02:11:19 -0700 | [diff] [blame] | 103 | DigestUtil digestUtil, |
Jakob Buchgraber | 11a98a5 | 2019-09-02 07:57:02 -0700 | [diff] [blame] | 104 | ImmutableSet<ActionInput> filesToDownload) { |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 105 | this.execRoot = execRoot; |
| 106 | this.options = options; |
George Gensure | 54b062c | 2020-09-02 05:06:40 -0700 | [diff] [blame] | 107 | this.verboseFailures = verboseFailures; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 108 | this.remoteCache = remoteCache; |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 109 | this.cmdlineReporter = cmdlineReporter; |
olaola | 6f32d5a | 2017-09-20 17:12:19 +0200 | [diff] [blame] | 110 | this.buildRequestId = buildRequestId; |
| 111 | this.commandId = commandId; |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 112 | this.digestUtil = digestUtil; |
Jakob Buchgraber | 11a98a5 | 2019-09-02 07:57:02 -0700 | [diff] [blame] | 113 | this.filesToDownload = Preconditions.checkNotNull(filesToDownload, "filesToDownload"); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
tomlu | 29e306d | 2018-04-19 05:41:44 -0700 | [diff] [blame] | 117 | public CacheHandle lookup(Spawn spawn, SpawnExecutionContext context) |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 118 | throws InterruptedException, IOException, ExecException { |
Sergio Rodriguez Orellana | 8860c3e | 2019-07-25 01:12:58 -0700 | [diff] [blame] | 119 | if (!Spawns.mayBeCached(spawn) |
Bor Kae Hwang | 9c0cab4 | 2019-12-19 06:39:10 -0800 | [diff] [blame] | 120 | || (!Spawns.mayBeCachedRemotely(spawn) && useRemoteCache(options))) { |
Sergio Rodriguez Orellana | 8860c3e | 2019-07-25 01:12:58 -0700 | [diff] [blame] | 121 | // returning SpawnCache.NO_RESULT_NO_STORE in case the caching is disabled or in case |
| 122 | // the remote caching is disabled and the only configured cache is remote. |
Benjamin Peterson | f157053 | 2019-01-24 07:14:18 -0800 | [diff] [blame] | 123 | return SpawnCache.NO_RESULT_NO_STORE; |
| 124 | } |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 125 | |
George Gensure | 7aa7498 | 2020-04-01 00:54:15 -0700 | [diff] [blame] | 126 | NetworkTime networkTime = new NetworkTime(); |
| 127 | Stopwatch totalTime = Stopwatch.createStarted(); |
| 128 | |
twerth | c4dfb91 | 2020-07-20 10:32:42 -0700 | [diff] [blame] | 129 | SortedMap<PathFragment, ActionInput> inputMap = context.getInputMapping(); |
buchgr | 7f72544 | 2019-03-08 03:20:25 -0800 | [diff] [blame] | 130 | MerkleTree merkleTree = |
| 131 | MerkleTree.build(inputMap, context.getMetadataProvider(), execRoot, digestUtil); |
George Gensure | 7aa7498 | 2020-04-01 00:54:15 -0700 | [diff] [blame] | 132 | SpawnMetrics.Builder spawnMetrics = |
Googler | 2b7e71a | 2020-06-04 01:32:01 -0700 | [diff] [blame] | 133 | SpawnMetrics.Builder.forRemoteExec() |
George Gensure | 7aa7498 | 2020-04-01 00:54:15 -0700 | [diff] [blame] | 134 | .setInputBytes(merkleTree.getInputBytes()) |
| 135 | .setInputFiles(merkleTree.getInputFiles()); |
buchgr | 7f72544 | 2019-03-08 03:20:25 -0800 | [diff] [blame] | 136 | Digest merkleTreeRoot = merkleTree.getRootDigest(); |
John Cater | 3afb7b0 | 2019-01-18 11:44:51 -0800 | [diff] [blame] | 137 | |
| 138 | // Get the remote platform properties. |
Googler | 0dc53a2 | 2019-08-25 22:09:56 -0700 | [diff] [blame] | 139 | Platform platform = PlatformUtils.getPlatformProto(spawn, options); |
John Cater | 3afb7b0 | 2019-01-18 11:44:51 -0800 | [diff] [blame] | 140 | |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 141 | Command command = |
| 142 | RemoteSpawnRunner.buildCommand( |
buchgr | 06a26c3 | 2019-11-20 02:01:12 -0800 | [diff] [blame] | 143 | spawn.getOutputFiles(), |
| 144 | spawn.getArguments(), |
| 145 | spawn.getEnvironment(), |
| 146 | platform, |
| 147 | /* workingDirectory= */ null); |
buchgr | d480c5f | 2019-04-03 00:53:34 -0700 | [diff] [blame] | 148 | RemoteOutputsMode remoteOutputsMode = options.remoteOutputsMode; |
Jakob Buchgraber | 54d7d61 | 2019-03-25 08:30:17 -0700 | [diff] [blame] | 149 | Action action = |
| 150 | RemoteSpawnRunner.buildAction( |
| 151 | digestUtil.compute(command), merkleTreeRoot, context.getTimeout(), true); |
| 152 | // Look up action cache, and reuse the action output if it is found. |
| 153 | ActionKey actionKey = digestUtil.computeActionKey(action); |
olaola | 6f32d5a | 2017-09-20 17:12:19 +0200 | [diff] [blame] | 154 | Context withMetadata = |
George Gensure | 7aa7498 | 2020-04-01 00:54:15 -0700 | [diff] [blame] | 155 | TracingMetadataUtils.contextWithMetadata(buildRequestId, commandId, actionKey) |
| 156 | .withValue(NetworkTime.CONTEXT_KEY, networkTime); |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 157 | |
Jakob Buchgraber | 4b3c2eb | 2019-04-04 02:01:48 -0700 | [diff] [blame] | 158 | Profiler prof = Profiler.instance(); |
Bor Kae Hwang | 9c0cab4 | 2019-12-19 06:39:10 -0800 | [diff] [blame] | 159 | if (options.remoteAcceptCached |
| 160 | || (options.incompatibleRemoteResultsIgnoreDisk && useDiskCache(options))) { |
| 161 | context.report(ProgressStatus.CHECKING_CACHE, "remote-cache"); |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 162 | // Metadata will be available in context.current() until we detach. |
| 163 | // This is done via a thread-local variable. |
| 164 | Context previous = withMetadata.attach(); |
| 165 | try { |
ulfjack | 5f436f3 | 2018-11-06 05:17:23 -0800 | [diff] [blame] | 166 | ActionResult result; |
Jakob Buchgraber | 4b3c2eb | 2019-04-04 02:01:48 -0700 | [diff] [blame] | 167 | try (SilentCloseable c = prof.profile(ProfilerTask.REMOTE_CACHE_CHECK, "check cache hit")) { |
Jakob Buchgraber | 21577f2 | 2020-03-23 03:11:29 -0700 | [diff] [blame] | 168 | result = remoteCache.downloadActionResult(actionKey, /* inlineOutErr= */ false); |
ulfjack | 5f436f3 | 2018-11-06 05:17:23 -0800 | [diff] [blame] | 169 | } |
Jakob Buchgraber | 50c1004 | 2019-04-11 02:11:19 -0700 | [diff] [blame] | 170 | // In case the remote cache returned a failed action (exit code != 0) we treat it as a |
| 171 | // cache miss |
ishikhman | 62f5458 | 2019-03-18 03:42:42 -0700 | [diff] [blame] | 172 | if (result != null && result.getExitCode() == 0) { |
buchgr | d480c5f | 2019-04-03 00:53:34 -0700 | [diff] [blame] | 173 | InMemoryOutput inMemoryOutput = null; |
Jakob Buchgraber | 50c1004 | 2019-04-11 02:11:19 -0700 | [diff] [blame] | 174 | boolean downloadOutputs = |
| 175 | shouldDownloadAllSpawnOutputs( |
| 176 | remoteOutputsMode, |
| 177 | /* exitCode = */ 0, |
Jakob Buchgraber | 11a98a5 | 2019-09-02 07:57:02 -0700 | [diff] [blame] | 178 | hasFilesToDownload(spawn.getOutputFiles(), filesToDownload)); |
George Gensure | 7aa7498 | 2020-04-01 00:54:15 -0700 | [diff] [blame] | 179 | Stopwatch fetchTime = Stopwatch.createStarted(); |
Jakob Buchgraber | 50c1004 | 2019-04-11 02:11:19 -0700 | [diff] [blame] | 180 | if (downloadOutputs) { |
| 181 | try (SilentCloseable c = |
| 182 | prof.profile(ProfilerTask.REMOTE_DOWNLOAD, "download outputs")) { |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 183 | remoteCache.download( |
| 184 | result, execRoot, context.getFileOutErr(), context::lockOutputFiles); |
Jakob Buchgraber | 50c1004 | 2019-04-11 02:11:19 -0700 | [diff] [blame] | 185 | } |
| 186 | } else { |
| 187 | PathFragment inMemoryOutputPath = getInMemoryOutputPath(spawn); |
| 188 | // inject output metadata |
| 189 | try (SilentCloseable c = |
| 190 | prof.profile(ProfilerTask.REMOTE_DOWNLOAD, "download outputs minimal")) { |
| 191 | inMemoryOutput = |
| 192 | remoteCache.downloadMinimal( |
George Gensure | 3ef8fb9 | 2020-05-06 09:49:48 -0700 | [diff] [blame] | 193 | actionKey.getDigest().getHash(), |
Jakob Buchgraber | 50c1004 | 2019-04-11 02:11:19 -0700 | [diff] [blame] | 194 | result, |
| 195 | spawn.getOutputFiles(), |
| 196 | inMemoryOutputPath, |
| 197 | context.getFileOutErr(), |
| 198 | execRoot, |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 199 | context.getMetadataInjector(), |
| 200 | context::lockOutputFiles); |
Jakob Buchgraber | 50c1004 | 2019-04-11 02:11:19 -0700 | [diff] [blame] | 201 | } |
ulfjack | 5f436f3 | 2018-11-06 05:17:23 -0800 | [diff] [blame] | 202 | } |
George Gensure | 7aa7498 | 2020-04-01 00:54:15 -0700 | [diff] [blame] | 203 | fetchTime.stop(); |
| 204 | totalTime.stop(); |
| 205 | spawnMetrics |
| 206 | .setFetchTime(fetchTime.elapsed()) |
| 207 | .setTotalTime(totalTime.elapsed()) |
| 208 | .setNetworkTime(networkTime.getDuration()); |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 209 | SpawnResult spawnResult = |
buchgr | d480c5f | 2019-04-03 00:53:34 -0700 | [diff] [blame] | 210 | createSpawnResult( |
George Gensure | 7aa7498 | 2020-04-01 00:54:15 -0700 | [diff] [blame] | 211 | result.getExitCode(), |
janakr | f8dc2eb | 2020-09-30 08:06:53 -0700 | [diff] [blame] | 212 | /*cacheHit=*/ true, |
George Gensure | 7aa7498 | 2020-04-01 00:54:15 -0700 | [diff] [blame] | 213 | "remote", |
| 214 | inMemoryOutput, |
janakr | f8dc2eb | 2020-09-30 08:06:53 -0700 | [diff] [blame] | 215 | spawnMetrics.build(), |
| 216 | spawn.getMnemonic()); |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 217 | return SpawnCache.success(spawnResult); |
| 218 | } |
Benjamin Peterson | 1532df0 | 2019-01-24 08:45:44 -0800 | [diff] [blame] | 219 | } catch (CacheNotFoundException e) { |
| 220 | // Intentionally left blank |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 221 | } catch (IOException e) { |
George Gensure | 54b062c | 2020-09-02 05:06:40 -0700 | [diff] [blame] | 222 | if (BulkTransferException.isOnlyCausedByCacheNotFoundException(e)) { |
| 223 | // Intentionally left blank |
| 224 | } else { |
| 225 | String errorMessage; |
| 226 | if (!verboseFailures) { |
| 227 | errorMessage = Utils.grpcAwareErrorMessage(e); |
| 228 | } else { |
| 229 | // On --verbose_failures print the whole stack trace |
| 230 | errorMessage = Throwables.getStackTraceAsString(e); |
| 231 | } |
| 232 | if (isNullOrEmpty(errorMessage)) { |
| 233 | errorMessage = e.getClass().getSimpleName(); |
| 234 | } |
| 235 | errorMessage = "Reading from Remote Cache:\n" + errorMessage; |
| 236 | report(Event.warn(errorMessage)); |
Benjamin Peterson | 1532df0 | 2019-01-24 08:45:44 -0800 | [diff] [blame] | 237 | } |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 238 | } finally { |
| 239 | withMetadata.detach(previous); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 240 | } |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 241 | } |
buchgr | d480c5f | 2019-04-03 00:53:34 -0700 | [diff] [blame] | 242 | |
| 243 | context.prefetchInputs(); |
| 244 | |
Bor Kae Hwang | 9c0cab4 | 2019-12-19 06:39:10 -0800 | [diff] [blame] | 245 | if (options.remoteUploadLocalResults |
| 246 | || (options.incompatibleRemoteResultsIgnoreDisk && useDiskCache(options))) { |
olaola | ba8b0b3 | 2017-10-20 09:48:56 +0200 | [diff] [blame] | 247 | return new CacheHandle() { |
| 248 | @Override |
| 249 | public boolean hasResult() { |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public SpawnResult getResult() { |
| 255 | throw new NoSuchElementException(); |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public boolean willStore() { |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | @Override |
Jakob Buchgraber | bac30fe | 2019-01-28 05:24:23 -0800 | [diff] [blame] | 264 | public void store(SpawnResult result) throws ExecException, InterruptedException { |
| 265 | boolean uploadResults = Status.SUCCESS.equals(result.status()) && result.exitCode() == 0; |
| 266 | if (!uploadResults) { |
| 267 | return; |
| 268 | } |
| 269 | |
ulfjack | 8896d2e | 2018-01-19 02:55:21 -0800 | [diff] [blame] | 270 | if (options.experimentalGuardAgainstConcurrentChanges) { |
Jakob Buchgraber | 4b3c2eb | 2019-04-04 02:01:48 -0700 | [diff] [blame] | 271 | try (SilentCloseable c = prof.profile("RemoteCache.checkForConcurrentModifications")) { |
ulfjack | 8896d2e | 2018-01-19 02:55:21 -0800 | [diff] [blame] | 272 | checkForConcurrentModifications(); |
| 273 | } catch (IOException e) { |
| 274 | report(Event.warn(e.getMessage())); |
| 275 | return; |
| 276 | } |
| 277 | } |
Jakob Buchgraber | bac30fe | 2019-01-28 05:24:23 -0800 | [diff] [blame] | 278 | |
olaola | ba8b0b3 | 2017-10-20 09:48:56 +0200 | [diff] [blame] | 279 | Context previous = withMetadata.attach(); |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 280 | Collection<Path> files = |
| 281 | RemoteSpawnRunner.resolveActionInputs(execRoot, spawn.getOutputFiles()); |
Jakob Buchgraber | 4b3c2eb | 2019-04-04 02:01:48 -0700 | [diff] [blame] | 282 | try (SilentCloseable c = prof.profile(ProfilerTask.UPLOAD_TIME, "upload outputs")) { |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 283 | remoteCache.upload( |
Jakob Buchgraber | bac30fe | 2019-01-28 05:24:23 -0800 | [diff] [blame] | 284 | actionKey, action, command, execRoot, files, context.getFileOutErr()); |
olaola | ba8b0b3 | 2017-10-20 09:48:56 +0200 | [diff] [blame] | 285 | } catch (IOException e) { |
George Gensure | 54b062c | 2020-09-02 05:06:40 -0700 | [diff] [blame] | 286 | String errorMessage; |
| 287 | if (!verboseFailures) { |
| 288 | errorMessage = Utils.grpcAwareErrorMessage(e); |
| 289 | } else { |
| 290 | // On --verbose_failures print the whole stack trace |
| 291 | errorMessage = Throwables.getStackTraceAsString(e); |
olaola | ba8b0b3 | 2017-10-20 09:48:56 +0200 | [diff] [blame] | 292 | } |
George Gensure | 54b062c | 2020-09-02 05:06:40 -0700 | [diff] [blame] | 293 | if (isNullOrEmpty(errorMessage)) { |
| 294 | errorMessage = e.getClass().getSimpleName(); |
| 295 | } |
| 296 | errorMessage = "Writing to Remote Cache:\n" + errorMessage; |
| 297 | report(Event.warn(errorMessage)); |
olaola | ba8b0b3 | 2017-10-20 09:48:56 +0200 | [diff] [blame] | 298 | } finally { |
| 299 | withMetadata.detach(previous); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void close() {} |
ulfjack | 8896d2e | 2018-01-19 02:55:21 -0800 | [diff] [blame] | 305 | |
| 306 | private void checkForConcurrentModifications() throws IOException { |
| 307 | for (ActionInput input : inputMap.values()) { |
Mike Morearty | d8ac06a | 2018-04-12 01:59:34 -0700 | [diff] [blame] | 308 | if (input instanceof VirtualActionInput) { |
| 309 | continue; |
| 310 | } |
shahan | 499503b | 2018-06-07 18:57:07 -0700 | [diff] [blame] | 311 | FileArtifactValue metadata = context.getMetadataProvider().getMetadata(input); |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 312 | Path path = execRoot.getRelative(input.getExecPath()); |
| 313 | if (metadata.wasModifiedSinceDigest(path)) { |
| 314 | throw new IOException(path + " was modified during execution"); |
ulfjack | 8896d2e | 2018-01-19 02:55:21 -0800 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | } |
olaola | ba8b0b3 | 2017-10-20 09:48:56 +0200 | [diff] [blame] | 318 | }; |
| 319 | } else { |
| 320 | return SpawnCache.NO_RESULT_NO_STORE; |
| 321 | } |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 322 | } |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 323 | |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 324 | private void report(Event evt) { |
Jakob Buchgraber | 321138f | 2018-05-07 05:24:53 -0700 | [diff] [blame] | 325 | if (cmdlineReporter == null) { |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | synchronized (this) { |
| 330 | if (reportedErrors.contains(evt.getMessage())) { |
| 331 | return; |
| 332 | } |
| 333 | reportedErrors.add(evt.getMessage()); |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 334 | cmdlineReporter.handle(evt); |
| 335 | } |
| 336 | } |
Sergio Rodriguez Orellana | 8860c3e | 2019-07-25 01:12:58 -0700 | [diff] [blame] | 337 | |
Bor Kae Hwang | 9c0cab4 | 2019-12-19 06:39:10 -0800 | [diff] [blame] | 338 | private static boolean useRemoteCache(RemoteOptions options) { |
Josh Smith | 33d0a1c | 2020-05-05 07:42:37 -0700 | [diff] [blame] | 339 | return !isNullOrEmpty(options.remoteCache) || !isNullOrEmpty(options.remoteExecutor); |
Sergio Rodriguez Orellana | 8860c3e | 2019-07-25 01:12:58 -0700 | [diff] [blame] | 340 | } |
Bor Kae Hwang | 9c0cab4 | 2019-12-19 06:39:10 -0800 | [diff] [blame] | 341 | |
| 342 | private static boolean useDiskCache(RemoteOptions options) { |
| 343 | return options.diskCache != null && !options.diskCache.isEmpty(); |
| 344 | } |
larsrc | 02838a1 | 2020-11-10 03:46:53 -0800 | [diff] [blame^] | 345 | |
| 346 | @Override |
| 347 | public boolean usefulInDynamicExecution() { |
| 348 | return false; |
| 349 | } |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 350 | } |