chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 1 | // Copyright 2019 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 | |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 16 | import static com.google.common.base.Preconditions.checkState; |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 17 | import static com.google.common.base.Throwables.throwIfInstanceOf; |
| 18 | import static com.google.common.truth.Truth.assertThat; |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertWithMessage; |
Googler | ffc560d | 2022-06-23 04:30:57 -0700 | [diff] [blame] | 20 | import static com.google.devtools.build.lib.actions.util.ActionsTestUtil.createTreeArtifactWithGeneratingAction; |
Googler | 895f307 | 2023-03-15 07:13:15 -0700 | [diff] [blame] | 21 | import static com.google.devtools.build.lib.remote.util.Utils.getFromFuture; |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 22 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 23 | import static org.junit.Assert.assertThrows; |
| 24 | import static org.mockito.ArgumentMatchers.any; |
Googler | ebd6e58 | 2022-10-24 05:30:19 -0700 | [diff] [blame] | 25 | import static org.mockito.ArgumentMatchers.eq; |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 26 | import static org.mockito.Mockito.doAnswer; |
Googler | ebd6e58 | 2022-10-24 05:30:19 -0700 | [diff] [blame] | 27 | import static org.mockito.Mockito.never; |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 28 | import static org.mockito.Mockito.spy; |
Tiago Quelhas | e0cdace | 2023-03-08 06:50:56 -0800 | [diff] [blame] | 29 | import static org.mockito.Mockito.times; |
Googler | ebd6e58 | 2022-10-24 05:30:19 -0700 | [diff] [blame] | 30 | import static org.mockito.Mockito.verify; |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 31 | |
| 32 | import com.google.common.collect.ImmutableList; |
| 33 | import com.google.common.collect.ImmutableMap; |
| 34 | import com.google.common.hash.HashCode; |
| 35 | import com.google.common.util.concurrent.Futures; |
| 36 | import com.google.common.util.concurrent.ListenableFuture; |
| 37 | import com.google.common.util.concurrent.SettableFuture; |
| 38 | import com.google.devtools.build.lib.actions.ActionInput; |
Googler | 77c4ce3 | 2023-03-10 15:46:29 -0800 | [diff] [blame] | 39 | import com.google.devtools.build.lib.actions.ActionInputPrefetcher.Priority; |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 40 | import com.google.devtools.build.lib.actions.Artifact; |
Googler | ffc560d | 2022-06-23 04:30:57 -0700 | [diff] [blame] | 41 | import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact; |
| 42 | import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact; |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 43 | import com.google.devtools.build.lib.actions.ArtifactRoot; |
| 44 | import com.google.devtools.build.lib.actions.ArtifactRoot.RootType; |
Chi Wang | 99cff33 | 2023-02-14 05:01:31 -0800 | [diff] [blame] | 45 | import com.google.devtools.build.lib.actions.ExecException; |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 46 | import com.google.devtools.build.lib.actions.FileArtifactValue; |
| 47 | import com.google.devtools.build.lib.actions.FileArtifactValue.RemoteFileArtifactValue; |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 48 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil; |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 49 | import com.google.devtools.build.lib.remote.util.TempPathGenerator; |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 50 | import com.google.devtools.build.lib.skyframe.TreeArtifactValue; |
Tiago Quelhas | e0cdace | 2023-03-08 06:50:56 -0800 | [diff] [blame] | 51 | import com.google.devtools.build.lib.testing.vfs.SpiedFileSystem; |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 52 | import com.google.devtools.build.lib.util.Pair; |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 53 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 54 | import com.google.devtools.build.lib.vfs.Dirent; |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 55 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 56 | import com.google.devtools.build.lib.vfs.Path; |
Googler | ffc560d | 2022-06-23 04:30:57 -0700 | [diff] [blame] | 57 | import com.google.devtools.build.lib.vfs.PathFragment; |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 58 | import com.google.devtools.build.lib.vfs.Symlinks; |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 59 | import java.io.IOException; |
| 60 | import java.util.HashMap; |
| 61 | import java.util.Map; |
| 62 | import java.util.concurrent.ExecutionException; |
| 63 | import java.util.concurrent.Semaphore; |
| 64 | import java.util.concurrent.atomic.AtomicBoolean; |
| 65 | import java.util.function.Supplier; |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 66 | import javax.annotation.Nullable; |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 67 | import org.junit.Before; |
| 68 | import org.junit.Test; |
| 69 | |
| 70 | /** Base test class for {@link AbstractActionInputPrefetcher} implementations. */ |
| 71 | public abstract class ActionInputPrefetcherTestBase { |
| 72 | protected static final DigestHashFunction HASH_FUNCTION = DigestHashFunction.SHA256; |
| 73 | |
Tiago Quelhas | e0cdace | 2023-03-08 06:50:56 -0800 | [diff] [blame] | 74 | protected SpiedFileSystem fs; |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 75 | protected Path execRoot; |
| 76 | protected ArtifactRoot artifactRoot; |
| 77 | protected TempPathGenerator tempPathGenerator; |
| 78 | |
| 79 | @Before |
| 80 | public void setUp() throws IOException { |
Tiago Quelhas | e0cdace | 2023-03-08 06:50:56 -0800 | [diff] [blame] | 81 | fs = SpiedFileSystem.createInMemorySpy(); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 82 | execRoot = fs.getPath("/exec"); |
| 83 | execRoot.createDirectoryAndParents(); |
| 84 | artifactRoot = ArtifactRoot.asDerivedRoot(execRoot, RootType.Output, "root"); |
| 85 | artifactRoot.getRoot().asPath().createDirectoryAndParents(); |
| 86 | Path tempDir = fs.getPath("/tmp"); |
| 87 | tempDir.createDirectoryAndParents(); |
| 88 | tempPathGenerator = new TempPathGenerator(tempDir); |
| 89 | } |
| 90 | |
| 91 | protected Artifact createRemoteArtifact( |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 92 | String pathFragment, |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 93 | String contents, |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 94 | @Nullable PathFragment materializationExecPath, |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 95 | Map<ActionInput, FileArtifactValue> metadata, |
Chi Wang | 963640a | 2023-02-20 03:20:39 -0800 | [diff] [blame] | 96 | @Nullable Map<HashCode, byte[]> cas) { |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 97 | Path p = artifactRoot.getRoot().getRelative(pathFragment); |
| 98 | Artifact a = ActionsTestUtil.createArtifact(artifactRoot, p); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 99 | byte[] contentsBytes = contents.getBytes(UTF_8); |
| 100 | HashCode hashCode = HASH_FUNCTION.getHashFunction().hashBytes(contentsBytes); |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 101 | RemoteFileArtifactValue f = |
Googler | de02e3f | 2022-09-29 02:47:24 -0700 | [diff] [blame] | 102 | RemoteFileArtifactValue.create( |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 103 | hashCode.asBytes(), |
| 104 | contentsBytes.length, |
| 105 | /* locationIndex= */ 1, |
Chi Wang | 1ebb04b | 2023-03-03 07:13:57 -0800 | [diff] [blame] | 106 | /* expireAtEpochMilli= */ -1, |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 107 | materializationExecPath); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 108 | metadata.put(a, f); |
Chi Wang | 963640a | 2023-02-20 03:20:39 -0800 | [diff] [blame] | 109 | if (cas != null) { |
| 110 | cas.put(hashCode, contentsBytes); |
| 111 | } |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 112 | return a; |
| 113 | } |
| 114 | |
Googler | ffc560d | 2022-06-23 04:30:57 -0700 | [diff] [blame] | 115 | protected Artifact createRemoteArtifact( |
| 116 | String pathFragment, |
| 117 | String contents, |
| 118 | Map<ActionInput, FileArtifactValue> metadata, |
Chi Wang | 963640a | 2023-02-20 03:20:39 -0800 | [diff] [blame] | 119 | @Nullable Map<HashCode, byte[]> cas) { |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 120 | return createRemoteArtifact( |
| 121 | pathFragment, contents, /* materializationExecPath= */ null, metadata, cas); |
| 122 | } |
| 123 | |
| 124 | protected Pair<SpecialArtifact, ImmutableList<TreeFileArtifact>> createRemoteTreeArtifact( |
| 125 | String pathFragment, |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 126 | Map<String, String> localContentMap, |
| 127 | Map<String, String> remoteContentMap, |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 128 | @Nullable PathFragment materializationExecPath, |
| 129 | Map<ActionInput, FileArtifactValue> metadata, |
| 130 | Map<HashCode, byte[]> cas) |
| 131 | throws IOException { |
| 132 | SpecialArtifact parent = createTreeArtifactWithGeneratingAction(artifactRoot, pathFragment); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 133 | |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 134 | parent.getPath().createDirectoryAndParents(); |
| 135 | parent.getPath().chmod(0555); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 136 | |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 137 | TreeArtifactValue.Builder treeBuilder = TreeArtifactValue.newBuilder(parent); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 138 | for (Map.Entry<String, String> entry : localContentMap.entrySet()) { |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 139 | TreeFileArtifact child = |
| 140 | TreeFileArtifact.createTreeOutput(parent, PathFragment.create(entry.getKey())); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 141 | byte[] contents = entry.getValue().getBytes(UTF_8); |
| 142 | HashCode hashCode = HASH_FUNCTION.getHashFunction().hashBytes(contents); |
| 143 | FileArtifactValue childValue = |
| 144 | FileArtifactValue.createForNormalFile( |
| 145 | hashCode.asBytes(), /* proxy= */ null, contents.length); |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 146 | treeBuilder.putChild(child, childValue); |
| 147 | metadata.put(child, childValue); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 148 | cas.put(hashCode, contents); |
| 149 | } |
| 150 | for (Map.Entry<String, String> entry : remoteContentMap.entrySet()) { |
| 151 | TreeFileArtifact child = |
| 152 | TreeFileArtifact.createTreeOutput(parent, PathFragment.create(entry.getKey())); |
| 153 | byte[] contents = entry.getValue().getBytes(UTF_8); |
| 154 | HashCode hashCode = HASH_FUNCTION.getHashFunction().hashBytes(contents); |
| 155 | RemoteFileArtifactValue childValue = |
| 156 | RemoteFileArtifactValue.create( |
Chi Wang | 1ebb04b | 2023-03-03 07:13:57 -0800 | [diff] [blame] | 157 | hashCode.asBytes(), |
| 158 | contents.length, |
| 159 | /* locationIndex= */ 1, |
| 160 | /* expireAtEpochMilli= */ -1); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 161 | treeBuilder.putChild(child, childValue); |
| 162 | metadata.put(child, childValue); |
| 163 | cas.put(hashCode, contents); |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 164 | } |
| 165 | if (materializationExecPath != null) { |
| 166 | treeBuilder.setMaterializationExecPath(materializationExecPath); |
| 167 | } |
| 168 | TreeArtifactValue treeValue = treeBuilder.build(); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 169 | |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 170 | metadata.put(parent, treeValue.getMetadata()); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 171 | |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 172 | return Pair.of(parent, treeValue.getChildren().asList()); |
| 173 | } |
| 174 | |
| 175 | protected Pair<SpecialArtifact, ImmutableList<TreeFileArtifact>> createRemoteTreeArtifact( |
| 176 | String pathFragment, |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 177 | Map<String, String> localContentMap, |
| 178 | Map<String, String> remoteContentMap, |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 179 | Map<ActionInput, FileArtifactValue> metadata, |
| 180 | Map<HashCode, byte[]> cas) |
| 181 | throws IOException { |
| 182 | return createRemoteTreeArtifact( |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 183 | pathFragment, |
| 184 | localContentMap, |
| 185 | remoteContentMap, |
| 186 | /* materializationExecPath= */ null, |
| 187 | metadata, |
| 188 | cas); |
Googler | ffc560d | 2022-06-23 04:30:57 -0700 | [diff] [blame] | 189 | } |
| 190 | |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 191 | protected abstract AbstractActionInputPrefetcher createPrefetcher(Map<HashCode, byte[]> cas); |
| 192 | |
| 193 | @Test |
Chi Wang | 99cff33 | 2023-02-14 05:01:31 -0800 | [diff] [blame] | 194 | public void prefetchFiles_fileExists_doNotDownload() |
| 195 | throws IOException, ExecException, InterruptedException { |
Googler | ebd6e58 | 2022-10-24 05:30:19 -0700 | [diff] [blame] | 196 | Map<ActionInput, FileArtifactValue> metadata = new HashMap<>(); |
| 197 | Map<HashCode, byte[]> cas = new HashMap<>(); |
| 198 | Artifact a = createRemoteArtifact("file", "hello world", metadata, cas); |
| 199 | FileSystemUtils.writeContent(a.getPath(), "hello world".getBytes(UTF_8)); |
Googler | ebd6e58 | 2022-10-24 05:30:19 -0700 | [diff] [blame] | 200 | AbstractActionInputPrefetcher prefetcher = spy(createPrefetcher(cas)); |
| 201 | |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 202 | wait(prefetcher.prefetchFiles(metadata.keySet(), metadata::get, Priority.MEDIUM)); |
Googler | ebd6e58 | 2022-10-24 05:30:19 -0700 | [diff] [blame] | 203 | |
Chi Wang | 0f524c6 | 2023-02-28 08:03:19 -0800 | [diff] [blame] | 204 | verify(prefetcher, never()).doDownloadFile(any(), any(), any(), any(), any()); |
Googler | ebd6e58 | 2022-10-24 05:30:19 -0700 | [diff] [blame] | 205 | assertThat(prefetcher.downloadedFiles()).containsExactly(a.getPath()); |
| 206 | assertThat(prefetcher.downloadsInProgress()).isEmpty(); |
| 207 | } |
| 208 | |
| 209 | @Test |
| 210 | public void prefetchFiles_fileExistsButContentMismatches_download() |
Chi Wang | 99cff33 | 2023-02-14 05:01:31 -0800 | [diff] [blame] | 211 | throws IOException, ExecException, InterruptedException { |
Googler | ebd6e58 | 2022-10-24 05:30:19 -0700 | [diff] [blame] | 212 | Map<ActionInput, FileArtifactValue> metadata = new HashMap<>(); |
| 213 | Map<HashCode, byte[]> cas = new HashMap<>(); |
| 214 | Artifact a = createRemoteArtifact("file", "hello world remote", metadata, cas); |
| 215 | FileSystemUtils.writeContent(a.getPath(), "hello world local".getBytes(UTF_8)); |
Googler | ebd6e58 | 2022-10-24 05:30:19 -0700 | [diff] [blame] | 216 | AbstractActionInputPrefetcher prefetcher = spy(createPrefetcher(cas)); |
| 217 | |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 218 | wait(prefetcher.prefetchFiles(metadata.keySet(), metadata::get, Priority.MEDIUM)); |
Googler | ebd6e58 | 2022-10-24 05:30:19 -0700 | [diff] [blame] | 219 | |
Chi Wang | 0f524c6 | 2023-02-28 08:03:19 -0800 | [diff] [blame] | 220 | verify(prefetcher).doDownloadFile(any(), any(), eq(a.getExecPath()), any(), any()); |
Googler | ebd6e58 | 2022-10-24 05:30:19 -0700 | [diff] [blame] | 221 | assertThat(prefetcher.downloadedFiles()).containsExactly(a.getPath()); |
| 222 | assertThat(prefetcher.downloadsInProgress()).isEmpty(); |
| 223 | assertThat(FileSystemUtils.readContent(a.getPath(), UTF_8)).isEqualTo("hello world remote"); |
| 224 | } |
| 225 | |
| 226 | @Test |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 227 | public void prefetchFiles_downloadRemoteFiles() throws Exception { |
| 228 | Map<ActionInput, FileArtifactValue> metadata = new HashMap<>(); |
| 229 | Map<HashCode, byte[]> cas = new HashMap<>(); |
| 230 | Artifact a1 = createRemoteArtifact("file1", "hello world", metadata, cas); |
| 231 | Artifact a2 = createRemoteArtifact("file2", "fizz buzz", metadata, cas); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 232 | AbstractActionInputPrefetcher prefetcher = createPrefetcher(cas); |
| 233 | |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 234 | wait(prefetcher.prefetchFiles(metadata.keySet(), metadata::get, Priority.MEDIUM)); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 235 | |
| 236 | assertThat(FileSystemUtils.readContent(a1.getPath(), UTF_8)).isEqualTo("hello world"); |
Googler | ffc560d | 2022-06-23 04:30:57 -0700 | [diff] [blame] | 237 | assertReadableNonWritableAndExecutable(a1.getPath()); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 238 | assertThat(FileSystemUtils.readContent(a2.getPath(), UTF_8)).isEqualTo("fizz buzz"); |
Googler | ffc560d | 2022-06-23 04:30:57 -0700 | [diff] [blame] | 239 | assertReadableNonWritableAndExecutable(a2.getPath()); |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 240 | assertThat(prefetcher.downloadedFiles()).containsExactly(a1.getPath(), a2.getPath()); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 241 | assertThat(prefetcher.downloadsInProgress()).isEmpty(); |
| 242 | } |
| 243 | |
| 244 | @Test |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 245 | public void prefetchFiles_downloadRemoteFiles_withMaterializationExecPath() throws Exception { |
Googler | ffc560d | 2022-06-23 04:30:57 -0700 | [diff] [blame] | 246 | Map<ActionInput, FileArtifactValue> metadata = new HashMap<>(); |
| 247 | Map<HashCode, byte[]> cas = new HashMap<>(); |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 248 | PathFragment targetExecPath = artifactRoot.getExecPath().getChild("target"); |
| 249 | Artifact a = createRemoteArtifact("file", "hello world", targetExecPath, metadata, cas); |
Googler | ffc560d | 2022-06-23 04:30:57 -0700 | [diff] [blame] | 250 | AbstractActionInputPrefetcher prefetcher = createPrefetcher(cas); |
| 251 | |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 252 | wait(prefetcher.prefetchFiles(metadata.keySet(), metadata::get, Priority.MEDIUM)); |
Googler | ffc560d | 2022-06-23 04:30:57 -0700 | [diff] [blame] | 253 | |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 254 | assertThat(a.getPath().isSymbolicLink()).isTrue(); |
| 255 | assertThat(a.getPath().readSymbolicLink()) |
| 256 | .isEqualTo(execRoot.getRelative(targetExecPath).asFragment()); |
| 257 | assertThat(FileSystemUtils.readContent(a.getPath(), UTF_8)).isEqualTo("hello world"); |
| 258 | assertThat(prefetcher.downloadedFiles()) |
| 259 | .containsExactly(a.getPath(), execRoot.getRelative(targetExecPath)); |
| 260 | assertThat(prefetcher.downloadsInProgress()).isEmpty(); |
| 261 | } |
| 262 | |
| 263 | @Test |
| 264 | public void prefetchFiles_downloadRemoteTrees() throws Exception { |
| 265 | Map<ActionInput, FileArtifactValue> metadata = new HashMap<>(); |
| 266 | Map<HashCode, byte[]> cas = new HashMap<>(); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 267 | Pair<SpecialArtifact, ImmutableList<TreeFileArtifact>> treeAndChildren = |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 268 | createRemoteTreeArtifact( |
| 269 | "dir", |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 270 | /* localContentMap= */ ImmutableMap.of(), |
| 271 | /* remoteContentMap= */ ImmutableMap.of( |
| 272 | "file1", "content1", "nested_dir/file2", "content2"), |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 273 | metadata, |
| 274 | cas); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 275 | SpecialArtifact tree = treeAndChildren.getFirst(); |
| 276 | ImmutableList<TreeFileArtifact> children = treeAndChildren.getSecond(); |
| 277 | Artifact firstChild = children.get(0); |
| 278 | Artifact secondChild = children.get(1); |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 279 | |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 280 | AbstractActionInputPrefetcher prefetcher = createPrefetcher(cas); |
| 281 | |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 282 | wait(prefetcher.prefetchFiles(children, metadata::get, Priority.MEDIUM)); |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 283 | |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 284 | assertThat(FileSystemUtils.readContent(firstChild.getPath(), UTF_8)).isEqualTo("content1"); |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 285 | assertThat(FileSystemUtils.readContent(secondChild.getPath(), UTF_8)).isEqualTo("content2"); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 286 | |
| 287 | assertTreeReadableNonWritableAndExecutable(tree.getPath()); |
| 288 | |
| 289 | assertThat(prefetcher.downloadedFiles()) |
| 290 | .containsExactly(firstChild.getPath(), secondChild.getPath()); |
Googler | ffc560d | 2022-06-23 04:30:57 -0700 | [diff] [blame] | 291 | assertThat(prefetcher.downloadsInProgress()).isEmpty(); |
Googler | ffc560d | 2022-06-23 04:30:57 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | @Test |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 295 | public void prefetchFiles_downloadRemoteTrees_partial() throws Exception { |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 296 | Map<ActionInput, FileArtifactValue> metadata = new HashMap<>(); |
| 297 | Map<HashCode, byte[]> cas = new HashMap<>(); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 298 | Pair<SpecialArtifact, ImmutableList<TreeFileArtifact>> treeAndChildren = |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 299 | createRemoteTreeArtifact( |
| 300 | "dir", |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 301 | /* localContentMap= */ ImmutableMap.of("file1", "content1"), |
| 302 | /* remoteContentMap= */ ImmutableMap.of("file2", "content2"), |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 303 | metadata, |
| 304 | cas); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 305 | SpecialArtifact tree = treeAndChildren.getFirst(); |
| 306 | ImmutableList<TreeFileArtifact> children = treeAndChildren.getSecond(); |
| 307 | Artifact firstChild = children.get(0); |
| 308 | Artifact secondChild = children.get(1); |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 309 | |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 310 | AbstractActionInputPrefetcher prefetcher = createPrefetcher(cas); |
| 311 | |
Googler | 77c4ce3 | 2023-03-10 15:46:29 -0800 | [diff] [blame] | 312 | wait( |
| 313 | prefetcher.prefetchFiles( |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 314 | ImmutableList.of(firstChild, secondChild), metadata::get, Priority.MEDIUM)); |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 315 | |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 316 | assertThat(firstChild.getPath().exists()).isFalse(); |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 317 | assertThat(FileSystemUtils.readContent(secondChild.getPath(), UTF_8)).isEqualTo("content2"); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 318 | assertTreeReadableNonWritableAndExecutable(tree.getPath()); |
| 319 | assertThat(prefetcher.downloadedFiles()).containsExactly(secondChild.getPath()); |
| 320 | } |
| 321 | |
| 322 | @Test |
| 323 | public void prefetchFiles_downloadRemoteTrees_withMaterializationExecPath() throws Exception { |
| 324 | Map<ActionInput, FileArtifactValue> metadata = new HashMap<>(); |
| 325 | Map<HashCode, byte[]> cas = new HashMap<>(); |
| 326 | PathFragment targetExecPath = artifactRoot.getExecPath().getChild("target"); |
| 327 | Pair<SpecialArtifact, ImmutableList<TreeFileArtifact>> treeAndChildren = |
| 328 | createRemoteTreeArtifact( |
| 329 | "dir", |
| 330 | /* localContentMap= */ ImmutableMap.of(), |
| 331 | /* remoteContentMap= */ ImmutableMap.of( |
| 332 | "file1", "content1", "nested_dir/file2", "content2"), |
| 333 | targetExecPath, |
| 334 | metadata, |
| 335 | cas); |
| 336 | SpecialArtifact tree = treeAndChildren.getFirst(); |
| 337 | ImmutableList<TreeFileArtifact> children = treeAndChildren.getSecond(); |
| 338 | Artifact firstChild = children.get(0); |
| 339 | Artifact secondChild = children.get(1); |
| 340 | |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 341 | AbstractActionInputPrefetcher prefetcher = createPrefetcher(cas); |
| 342 | |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 343 | wait(prefetcher.prefetchFiles(children, metadata::get, Priority.MEDIUM)); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 344 | |
| 345 | assertThat(tree.getPath().isSymbolicLink()).isTrue(); |
| 346 | assertThat(tree.getPath().readSymbolicLink()) |
| 347 | .isEqualTo(execRoot.getRelative(targetExecPath).asFragment()); |
| 348 | assertThat(FileSystemUtils.readContent(firstChild.getPath(), UTF_8)).isEqualTo("content1"); |
| 349 | assertThat(FileSystemUtils.readContent(secondChild.getPath(), UTF_8)).isEqualTo("content2"); |
| 350 | |
| 351 | assertTreeReadableNonWritableAndExecutable(execRoot.getRelative(targetExecPath)); |
| 352 | |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 353 | assertThat(prefetcher.downloadedFiles()) |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 354 | .containsExactly( |
| 355 | tree.getPath(), |
| 356 | execRoot.getRelative(targetExecPath.getRelative(firstChild.getParentRelativePath())), |
| 357 | execRoot.getRelative(targetExecPath.getRelative(secondChild.getParentRelativePath()))); |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 358 | assertThat(prefetcher.downloadsInProgress()).isEmpty(); |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | @Test |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 362 | public void prefetchFiles_missingFiles_fails() throws Exception { |
| 363 | Map<ActionInput, FileArtifactValue> metadata = new HashMap<>(); |
| 364 | Artifact a = createRemoteArtifact("file1", "hello world", metadata, /* cas= */ new HashMap<>()); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 365 | AbstractActionInputPrefetcher prefetcher = createPrefetcher(new HashMap<>()); |
| 366 | |
| 367 | assertThrows( |
Chi Wang | 99cff33 | 2023-02-14 05:01:31 -0800 | [diff] [blame] | 368 | Exception.class, |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 369 | () -> wait(prefetcher.prefetchFiles(ImmutableList.of(a), metadata::get, Priority.MEDIUM))); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 370 | |
| 371 | assertThat(prefetcher.downloadedFiles()).isEmpty(); |
| 372 | assertThat(prefetcher.downloadsInProgress()).isEmpty(); |
| 373 | } |
| 374 | |
| 375 | @Test |
| 376 | public void prefetchFiles_ignoreNonRemoteFiles() throws Exception { |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 377 | // Test that non-remote files are not downloaded. |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 378 | |
| 379 | Path p = execRoot.getRelative(artifactRoot.getExecPath()).getRelative("file1"); |
| 380 | FileSystemUtils.writeContent(p, UTF_8, "hello world"); |
| 381 | Artifact a = ActionsTestUtil.createArtifact(artifactRoot, p); |
| 382 | FileArtifactValue f = FileArtifactValue.createForTesting(a); |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 383 | ImmutableMap<ActionInput, FileArtifactValue> metadata = ImmutableMap.of(a, f); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 384 | AbstractActionInputPrefetcher prefetcher = createPrefetcher(new HashMap<>()); |
| 385 | |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 386 | wait(prefetcher.prefetchFiles(ImmutableList.of(a), metadata::get, Priority.MEDIUM)); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 387 | |
| 388 | assertThat(prefetcher.downloadedFiles()).isEmpty(); |
| 389 | assertThat(prefetcher.downloadsInProgress()).isEmpty(); |
| 390 | } |
| 391 | |
| 392 | @Test |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 393 | public void prefetchFiles_ignoreNonRemoteFiles_tree() throws Exception { |
| 394 | // Test that non-remote tree files are not downloaded, but other files in the tree are. |
| 395 | |
| 396 | Map<ActionInput, FileArtifactValue> metadata = new HashMap<>(); |
| 397 | Map<HashCode, byte[]> cas = new HashMap<>(); |
| 398 | Pair<SpecialArtifact, ImmutableList<TreeFileArtifact>> treeAndChildren = |
| 399 | createRemoteTreeArtifact( |
| 400 | "dir", |
| 401 | ImmutableMap.of("file1", "content1"), |
| 402 | ImmutableMap.of("file2", "content2"), |
| 403 | metadata, |
| 404 | cas); |
| 405 | SpecialArtifact tree = treeAndChildren.getFirst(); |
| 406 | ImmutableList<TreeFileArtifact> children = treeAndChildren.getSecond(); |
| 407 | Artifact firstChild = children.get(0); |
| 408 | Artifact secondChild = children.get(1); |
| 409 | |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 410 | AbstractActionInputPrefetcher prefetcher = createPrefetcher(cas); |
| 411 | |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 412 | wait(prefetcher.prefetchFiles(children, metadata::get, Priority.MEDIUM)); |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 413 | |
| 414 | assertThat(firstChild.getPath().exists()).isFalse(); |
| 415 | assertThat(FileSystemUtils.readContent(secondChild.getPath(), UTF_8)).isEqualTo("content2"); |
| 416 | assertTreeReadableNonWritableAndExecutable(tree.getPath()); |
| 417 | assertThat(prefetcher.downloadedFiles()).containsExactly(secondChild.getPath()); |
| 418 | } |
| 419 | |
| 420 | @Test |
Tiago Quelhas | e0cdace | 2023-03-08 06:50:56 -0800 | [diff] [blame] | 421 | public void prefetchFiles_treeFiles_minimizeFilesystemOperations() throws Exception { |
| 422 | Map<ActionInput, FileArtifactValue> metadata = new HashMap<>(); |
| 423 | Map<HashCode, byte[]> cas = new HashMap<>(); |
| 424 | Pair<SpecialArtifact, ImmutableList<TreeFileArtifact>> treeAndChildren = |
| 425 | createRemoteTreeArtifact( |
| 426 | "dir", |
| 427 | /* localContentMap= */ ImmutableMap.of("subdir/file1", "content1"), |
| 428 | /* remoteContentMap= */ ImmutableMap.of("subdir/file2", "content2"), |
| 429 | metadata, |
| 430 | cas); |
| 431 | SpecialArtifact tree = treeAndChildren.getFirst(); |
| 432 | ImmutableList<TreeFileArtifact> children = treeAndChildren.getSecond(); |
| 433 | Artifact firstChild = children.get(0); |
| 434 | Artifact secondChild = children.get(1); |
| 435 | |
Tiago Quelhas | e0cdace | 2023-03-08 06:50:56 -0800 | [diff] [blame] | 436 | AbstractActionInputPrefetcher prefetcher = createPrefetcher(cas); |
| 437 | |
Googler | 77c4ce3 | 2023-03-10 15:46:29 -0800 | [diff] [blame] | 438 | wait( |
| 439 | prefetcher.prefetchFiles( |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 440 | ImmutableList.of(firstChild, secondChild), metadata::get, Priority.MEDIUM)); |
Tiago Quelhas | e0cdace | 2023-03-08 06:50:56 -0800 | [diff] [blame] | 441 | |
| 442 | verify(fs, times(1)).createWritableDirectory(tree.getPath().asFragment()); |
| 443 | verify(fs, times(1)).createWritableDirectory(tree.getPath().getChild("subdir").asFragment()); |
| 444 | } |
| 445 | |
| 446 | @Test |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 447 | public void prefetchFiles_multipleThreads_downloadIsCancelled() throws Exception { |
| 448 | // Test shared downloads are cancelled if all threads/callers are interrupted |
| 449 | |
| 450 | // arrange |
| 451 | Map<ActionInput, FileArtifactValue> metadata = new HashMap<>(); |
| 452 | Map<HashCode, byte[]> cas = new HashMap<>(); |
| 453 | Artifact artifact = createRemoteArtifact("file1", "hello world", metadata, cas); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 454 | |
| 455 | AbstractActionInputPrefetcher prefetcher = spy(createPrefetcher(cas)); |
| 456 | SettableFuture<Void> downloadThatNeverFinishes = SettableFuture.create(); |
| 457 | mockDownload(prefetcher, cas, () -> downloadThatNeverFinishes); |
| 458 | |
| 459 | Thread cancelledThread1 = |
| 460 | new Thread( |
| 461 | () -> { |
| 462 | try { |
Googler | 77c4ce3 | 2023-03-10 15:46:29 -0800 | [diff] [blame] | 463 | wait( |
| 464 | prefetcher.prefetchFiles( |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 465 | ImmutableList.of(artifact), metadata::get, Priority.MEDIUM)); |
Chi Wang | 99cff33 | 2023-02-14 05:01:31 -0800 | [diff] [blame] | 466 | } catch (IOException | ExecException | InterruptedException ignored) { |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 467 | // do nothing |
| 468 | } |
| 469 | }); |
| 470 | |
| 471 | Thread cancelledThread2 = |
| 472 | new Thread( |
| 473 | () -> { |
| 474 | try { |
Googler | 77c4ce3 | 2023-03-10 15:46:29 -0800 | [diff] [blame] | 475 | wait( |
| 476 | prefetcher.prefetchFiles( |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 477 | ImmutableList.of(artifact), metadata::get, Priority.MEDIUM)); |
Chi Wang | 99cff33 | 2023-02-14 05:01:31 -0800 | [diff] [blame] | 478 | } catch (IOException | ExecException | InterruptedException ignored) { |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 479 | // do nothing |
| 480 | } |
| 481 | }); |
| 482 | |
| 483 | // act |
| 484 | cancelledThread1.start(); |
| 485 | cancelledThread2.start(); |
| 486 | cancelledThread1.interrupt(); |
| 487 | cancelledThread2.interrupt(); |
| 488 | cancelledThread1.join(); |
| 489 | cancelledThread2.join(); |
| 490 | |
| 491 | // assert |
| 492 | assertThat(downloadThatNeverFinishes.isCancelled()).isTrue(); |
| 493 | assertThat(artifact.getPath().exists()).isFalse(); |
| 494 | assertThat(tempPathGenerator.getTempDir().getDirectoryEntries()).isEmpty(); |
| 495 | } |
| 496 | |
| 497 | @Test |
| 498 | public void prefetchFiles_multipleThreads_downloadIsNotCancelledByOtherThreads() |
| 499 | throws Exception { |
| 500 | // Test multiple threads can share downloads, but do not cancel each other when interrupted |
| 501 | |
| 502 | // arrange |
| 503 | Map<ActionInput, FileArtifactValue> metadata = new HashMap<>(); |
| 504 | Map<HashCode, byte[]> cas = new HashMap<>(); |
| 505 | Artifact artifact = createRemoteArtifact("file1", "hello world", metadata, cas); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 506 | SettableFuture<Void> download = SettableFuture.create(); |
| 507 | AbstractActionInputPrefetcher prefetcher = spy(createPrefetcher(cas)); |
| 508 | mockDownload(prefetcher, cas, () -> download); |
| 509 | Thread cancelledThread = |
| 510 | new Thread( |
| 511 | () -> { |
| 512 | try { |
Googler | 77c4ce3 | 2023-03-10 15:46:29 -0800 | [diff] [blame] | 513 | wait( |
| 514 | prefetcher.prefetchFiles( |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 515 | ImmutableList.of(artifact), metadata::get, Priority.MEDIUM)); |
Chi Wang | 99cff33 | 2023-02-14 05:01:31 -0800 | [diff] [blame] | 516 | } catch (IOException | ExecException | InterruptedException ignored) { |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 517 | // do nothing |
| 518 | } |
| 519 | }); |
| 520 | |
| 521 | AtomicBoolean successful = new AtomicBoolean(false); |
| 522 | Thread successfulThread = |
| 523 | new Thread( |
| 524 | () -> { |
| 525 | try { |
Googler | 77c4ce3 | 2023-03-10 15:46:29 -0800 | [diff] [blame] | 526 | wait( |
| 527 | prefetcher.prefetchFiles( |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 528 | ImmutableList.of(artifact), metadata::get, Priority.MEDIUM)); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 529 | successful.set(true); |
Chi Wang | 99cff33 | 2023-02-14 05:01:31 -0800 | [diff] [blame] | 530 | } catch (IOException | ExecException | InterruptedException ignored) { |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 531 | // do nothing |
| 532 | } |
| 533 | }); |
| 534 | cancelledThread.start(); |
| 535 | successfulThread.start(); |
| 536 | while (true) { |
| 537 | if (prefetcher |
| 538 | .getDownloadCache() |
| 539 | .getSubscriberCount(execRoot.getRelative(artifact.getExecPath())) |
| 540 | == 2) { |
| 541 | break; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | // act |
| 546 | cancelledThread.interrupt(); |
| 547 | cancelledThread.join(); |
| 548 | // simulate the download finishing |
| 549 | assertThat(download.isCancelled()).isFalse(); |
| 550 | download.set(null); |
| 551 | successfulThread.join(); |
| 552 | |
| 553 | // assert |
| 554 | assertThat(successful.get()).isTrue(); |
| 555 | assertThat(FileSystemUtils.readContent(artifact.getPath(), UTF_8)).isEqualTo("hello world"); |
| 556 | } |
| 557 | |
| 558 | @Test |
Googler | 895f307 | 2023-03-15 07:13:15 -0700 | [diff] [blame] | 559 | public void prefetchFiles_onInterrupt_deletePartialDownloadedFile() throws Exception { |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 560 | Semaphore startSemaphore = new Semaphore(0); |
| 561 | Semaphore endSemaphore = new Semaphore(0); |
| 562 | Map<ActionInput, FileArtifactValue> metadata = new HashMap<>(); |
| 563 | Map<HashCode, byte[]> cas = new HashMap<>(); |
| 564 | Artifact a1 = createRemoteArtifact("file1", "hello world", metadata, cas); |
| 565 | AbstractActionInputPrefetcher prefetcher = spy(createPrefetcher(cas)); |
| 566 | mockDownload( |
| 567 | prefetcher, |
| 568 | cas, |
| 569 | () -> { |
| 570 | startSemaphore.release(); |
| 571 | return SettableFuture.create(); // A future that never complete so we can interrupt later |
| 572 | }); |
| 573 | |
| 574 | AtomicBoolean interrupted = new AtomicBoolean(false); |
| 575 | Thread t = |
| 576 | new Thread( |
| 577 | () -> { |
| 578 | try { |
Googler | 895f307 | 2023-03-15 07:13:15 -0700 | [diff] [blame] | 579 | getFromFuture( |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 580 | prefetcher.prefetchFiles(ImmutableList.of(a1), metadata::get, Priority.MEDIUM)); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 581 | } catch (IOException ignored) { |
| 582 | // Intentionally left empty |
| 583 | } catch (InterruptedException e) { |
| 584 | interrupted.set(true); |
| 585 | } |
| 586 | endSemaphore.release(); |
| 587 | }); |
| 588 | t.start(); |
| 589 | startSemaphore.acquire(); |
| 590 | t.interrupt(); |
| 591 | endSemaphore.acquire(); |
| 592 | |
| 593 | assertThat(interrupted.get()).isTrue(); |
| 594 | assertThat(a1.getPath().exists()).isFalse(); |
| 595 | assertThat(tempPathGenerator.getTempDir().getDirectoryEntries()).isEmpty(); |
| 596 | } |
| 597 | |
Googler | ac695d7 | 2023-02-24 05:50:52 -0800 | [diff] [blame] | 598 | @Test |
| 599 | public void missingInputs_addedToList() { |
| 600 | Map<ActionInput, FileArtifactValue> metadata = new HashMap<>(); |
| 601 | Map<HashCode, byte[]> cas = new HashMap<>(); |
| 602 | Artifact a = createRemoteArtifact("file", "hello world", metadata, /* cas= */ null); |
Googler | ac695d7 | 2023-02-24 05:50:52 -0800 | [diff] [blame] | 603 | AbstractActionInputPrefetcher prefetcher = createPrefetcher(cas); |
| 604 | |
| 605 | assertThrows( |
Googler | 77c4ce3 | 2023-03-10 15:46:29 -0800 | [diff] [blame] | 606 | Exception.class, |
Googler | d8e13c8 | 2023-04-11 00:07:32 -0700 | [diff] [blame^] | 607 | () -> wait(prefetcher.prefetchFiles(metadata.keySet(), metadata::get, Priority.MEDIUM))); |
Googler | ac695d7 | 2023-02-24 05:50:52 -0800 | [diff] [blame] | 608 | |
| 609 | assertThat(prefetcher.getMissingActionInputs()).contains(a); |
| 610 | } |
| 611 | |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 612 | protected static void wait(ListenableFuture<Void> future) |
Chi Wang | 99cff33 | 2023-02-14 05:01:31 -0800 | [diff] [blame] | 613 | throws IOException, ExecException, InterruptedException { |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 614 | try { |
| 615 | future.get(); |
| 616 | } catch (ExecutionException e) { |
| 617 | Throwable cause = e.getCause(); |
| 618 | if (cause != null) { |
| 619 | throwIfInstanceOf(cause, IOException.class); |
Chi Wang | 99cff33 | 2023-02-14 05:01:31 -0800 | [diff] [blame] | 620 | throwIfInstanceOf(cause, ExecException.class); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 621 | throwIfInstanceOf(cause, InterruptedException.class); |
| 622 | throwIfInstanceOf(cause, RuntimeException.class); |
| 623 | } |
| 624 | throw new IOException(e); |
| 625 | } catch (InterruptedException e) { |
Googler | ac695d7 | 2023-02-24 05:50:52 -0800 | [diff] [blame] | 626 | future.cancel(/* mayInterruptIfRunning= */ true); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 627 | throw e; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | protected static void mockDownload( |
| 632 | AbstractActionInputPrefetcher prefetcher, |
| 633 | Map<HashCode, byte[]> cas, |
| 634 | Supplier<ListenableFuture<Void>> resultSupplier) |
| 635 | throws IOException { |
| 636 | doAnswer( |
| 637 | invocation -> { |
Chi Wang | 0f524c6 | 2023-02-28 08:03:19 -0800 | [diff] [blame] | 638 | Path path = invocation.getArgument(1); |
| 639 | FileArtifactValue metadata = invocation.getArgument(3); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 640 | byte[] content = cas.get(HashCode.fromBytes(metadata.getDigest())); |
| 641 | if (content == null) { |
| 642 | return Futures.immediateFailedFuture(new IOException("Not found")); |
| 643 | } |
| 644 | FileSystemUtils.writeContent(path, content); |
| 645 | return resultSupplier.get(); |
| 646 | }) |
| 647 | .when(prefetcher) |
Chi Wang | 0f524c6 | 2023-02-28 08:03:19 -0800 | [diff] [blame] | 648 | .doDownloadFile(any(), any(), any(), any(), any()); |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 649 | } |
Googler | ffc560d | 2022-06-23 04:30:57 -0700 | [diff] [blame] | 650 | |
| 651 | private void assertReadableNonWritableAndExecutable(Path path) throws IOException { |
Googler | 52deefe | 2023-03-01 05:08:50 -0800 | [diff] [blame] | 652 | assertWithMessage(path + " should be readable").that(path.isReadable()).isTrue(); |
| 653 | assertWithMessage(path + " should not be writable").that(path.isWritable()).isFalse(); |
| 654 | assertWithMessage(path + " should be executable").that(path.isExecutable()).isTrue(); |
| 655 | } |
| 656 | |
| 657 | private void assertTreeReadableNonWritableAndExecutable(Path path) throws IOException { |
| 658 | checkState(path.isDirectory()); |
| 659 | assertReadableNonWritableAndExecutable(path); |
| 660 | for (Dirent dirent : path.readdir(Symlinks.NOFOLLOW)) { |
| 661 | if (dirent.getType().equals(Dirent.Type.DIRECTORY)) { |
| 662 | assertTreeReadableNonWritableAndExecutable(path.getChild(dirent.getName())); |
| 663 | } |
| 664 | } |
Googler | ffc560d | 2022-06-23 04:30:57 -0700 | [diff] [blame] | 665 | } |
chiwang | 8cea765 | 2022-05-23 02:24:47 -0700 | [diff] [blame] | 666 | } |