buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.remote; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
jcater | b922677 | 2019-04-29 12:04:52 -0700 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows; |
tomlu | 9efbc25 | 2018-08-10 11:51:20 -0700 | [diff] [blame] | 18 | import static org.mockito.Mockito.mock; |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 19 | |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 20 | import build.bazel.remote.execution.v2.Digest; |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 21 | import com.google.bytestream.ByteStreamProto.WriteRequest; |
| 22 | import com.google.bytestream.ByteStreamProto.WriteResponse; |
George Gensure | 3c9089b | 2019-05-02 07:07:55 -0700 | [diff] [blame] | 23 | import com.google.common.hash.HashCode; |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 24 | import com.google.common.io.BaseEncoding; |
| 25 | import com.google.common.util.concurrent.ListeningScheduledExecutorService; |
| 26 | import com.google.common.util.concurrent.MoreExecutors; |
| 27 | import com.google.devtools.build.lib.buildeventstream.BuildEvent.LocalFile; |
| 28 | import com.google.devtools.build.lib.buildeventstream.BuildEvent.LocalFile.LocalFileType; |
| 29 | import com.google.devtools.build.lib.buildeventstream.PathConverter; |
| 30 | import com.google.devtools.build.lib.clock.JavaClock; |
| 31 | import com.google.devtools.build.lib.remote.ByteStreamUploaderTest.FixedBackoff; |
| 32 | import com.google.devtools.build.lib.remote.ByteStreamUploaderTest.MaybeFailOnceUploadService; |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.lib.remote.util.DigestUtil; |
Jakob Buchgraber | bc06db9 | 2019-03-07 00:21:52 -0800 | [diff] [blame] | 34 | import com.google.devtools.build.lib.remote.util.TestUtils; |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 35 | import com.google.devtools.build.lib.remote.util.TracingMetadataUtils; |
| 36 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
| 37 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 38 | import com.google.devtools.build.lib.vfs.Path; |
| 39 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 40 | import io.grpc.Context; |
| 41 | import io.grpc.ManagedChannel; |
| 42 | import io.grpc.Server; |
| 43 | import io.grpc.Status; |
| 44 | import io.grpc.inprocess.InProcessChannelBuilder; |
| 45 | import io.grpc.inprocess.InProcessServerBuilder; |
| 46 | import io.grpc.stub.StreamObserver; |
| 47 | import io.grpc.util.MutableHandlerRegistry; |
| 48 | import java.io.OutputStream; |
| 49 | import java.util.HashMap; |
| 50 | import java.util.Map; |
| 51 | import java.util.Random; |
| 52 | import java.util.concurrent.ExecutionException; |
| 53 | import java.util.concurrent.Executors; |
Benjamin Peterson | 97b6732 | 2019-04-23 06:03:59 -0700 | [diff] [blame] | 54 | import java.util.concurrent.TimeUnit; |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 55 | import org.junit.After; |
| 56 | import org.junit.AfterClass; |
| 57 | import org.junit.Before; |
| 58 | import org.junit.BeforeClass; |
| 59 | import org.junit.Test; |
| 60 | import org.junit.runner.RunWith; |
| 61 | import org.junit.runners.JUnit4; |
| 62 | import org.mockito.MockitoAnnotations; |
| 63 | |
| 64 | /** Test for {@link ByteStreamBuildEventArtifactUploader}. */ |
| 65 | @RunWith(JUnit4.class) |
| 66 | public class ByteStreamBuildEventArtifactUploaderTest { |
| 67 | |
| 68 | private static final DigestUtil DIGEST_UTIL = new DigestUtil(DigestHashFunction.SHA256); |
| 69 | |
| 70 | private final MutableHandlerRegistry serviceRegistry = new MutableHandlerRegistry(); |
| 71 | private static ListeningScheduledExecutorService retryService; |
| 72 | |
| 73 | private Server server; |
| 74 | private ManagedChannel channel; |
| 75 | private Context withEmptyMetadata; |
| 76 | private Context prevContext; |
| 77 | private final FileSystem fs = new InMemoryFileSystem(new JavaClock(), DigestHashFunction.SHA256); |
| 78 | |
| 79 | @BeforeClass |
| 80 | public static void beforeEverything() { |
| 81 | retryService = MoreExecutors.listeningDecorator(Executors.newScheduledThreadPool(1)); |
| 82 | } |
| 83 | |
| 84 | @Before |
| 85 | public final void setUp() throws Exception { |
| 86 | MockitoAnnotations.initMocks(this); |
| 87 | |
| 88 | String serverName = "Server for " + this.getClass(); |
| 89 | server = |
| 90 | InProcessServerBuilder.forName(serverName) |
| 91 | .fallbackHandlerRegistry(serviceRegistry) |
| 92 | .build() |
| 93 | .start(); |
| 94 | channel = InProcessChannelBuilder.forName(serverName).build(); |
| 95 | withEmptyMetadata = |
| 96 | TracingMetadataUtils.contextWithMetadata( |
| 97 | "none", "none", DIGEST_UTIL.asActionKey(Digest.getDefaultInstance())); |
| 98 | // Needs to be repeated in every test that uses the timeout setting, since the tests run |
| 99 | // on different threads than the setUp. |
| 100 | prevContext = withEmptyMetadata.attach(); |
| 101 | } |
| 102 | |
| 103 | @After |
| 104 | public void tearDown() throws Exception { |
| 105 | // Needs to be repeated in every test that uses the timeout setting, since the tests run |
| 106 | // on different threads than the tearDown. |
| 107 | withEmptyMetadata.detach(prevContext); |
| 108 | |
Benjamin Peterson | 97b6732 | 2019-04-23 06:03:59 -0700 | [diff] [blame] | 109 | channel.shutdownNow(); |
| 110 | channel.awaitTermination(5, TimeUnit.SECONDS); |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 111 | server.shutdownNow(); |
| 112 | server.awaitTermination(); |
| 113 | } |
| 114 | |
| 115 | @AfterClass |
| 116 | public static void afterEverything() { |
| 117 | retryService.shutdownNow(); |
| 118 | } |
| 119 | |
| 120 | @Before |
| 121 | public void setup() { |
| 122 | MockitoAnnotations.initMocks(this); |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void uploadsShouldWork() throws Exception { |
| 127 | int numUploads = 2; |
George Gensure | 3c9089b | 2019-05-02 07:07:55 -0700 | [diff] [blame] | 128 | Map<HashCode, byte[]> blobsByHash = new HashMap<>(); |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 129 | Map<Path, LocalFile> filesToUpload = new HashMap<>(); |
| 130 | Random rand = new Random(); |
| 131 | for (int i = 0; i < numUploads; i++) { |
| 132 | Path file = fs.getPath("/file" + i); |
| 133 | OutputStream out = file.getOutputStream(); |
| 134 | int blobSize = rand.nextInt(100) + 1; |
| 135 | byte[] blob = new byte[blobSize]; |
| 136 | rand.nextBytes(blob); |
| 137 | out.write(blob); |
| 138 | out.close(); |
George Gensure | 3c9089b | 2019-05-02 07:07:55 -0700 | [diff] [blame] | 139 | blobsByHash.put(HashCode.fromString(DIGEST_UTIL.compute(file).getHash()), blob); |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 140 | filesToUpload.put(file, new LocalFile(file, LocalFileType.OUTPUT)); |
| 141 | } |
| 142 | serviceRegistry.addService(new MaybeFailOnceUploadService(blobsByHash)); |
| 143 | |
| 144 | RemoteRetrier retrier = |
Jakob Buchgraber | bc06db9 | 2019-03-07 00:21:52 -0800 | [diff] [blame] | 145 | TestUtils.newRemoteRetrier(() -> new FixedBackoff(1, 0), (e) -> true, retryService); |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 146 | ReferenceCountedChannel refCntChannel = new ReferenceCountedChannel(channel); |
| 147 | ByteStreamUploader uploader = |
| 148 | new ByteStreamUploader("instance", refCntChannel, null, 3, retrier); |
| 149 | ByteStreamBuildEventArtifactUploader artifactUploader = |
| 150 | new ByteStreamBuildEventArtifactUploader( |
pcloudy | 798b9a9 | 2018-12-04 02:31:49 -0800 | [diff] [blame] | 151 | uploader, "localhost", withEmptyMetadata, "instance", /* maxUploadThreads= */ 100); |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 152 | |
| 153 | PathConverter pathConverter = artifactUploader.upload(filesToUpload).get(); |
| 154 | for (Path file : filesToUpload.keySet()) { |
| 155 | String hash = BaseEncoding.base16().lowerCase().encode(file.getDigest()); |
| 156 | long size = file.getFileSize(); |
| 157 | String conversion = pathConverter.apply(file); |
| 158 | assertThat(conversion) |
| 159 | .isEqualTo("bytestream://localhost/instance/blobs/" + hash + "/" + size); |
| 160 | } |
| 161 | |
| 162 | artifactUploader.shutdown(); |
| 163 | |
| 164 | assertThat(uploader.refCnt()).isEqualTo(0); |
| 165 | assertThat(refCntChannel.isShutdown()).isTrue(); |
| 166 | } |
| 167 | |
| 168 | @Test |
tomlu | 9efbc25 | 2018-08-10 11:51:20 -0700 | [diff] [blame] | 169 | public void testUploadDirectoryDoesNotCrash() throws Exception { |
| 170 | Path dir = fs.getPath("/dir"); |
| 171 | dir.createDirectoryAndParents(); |
| 172 | Map<Path, LocalFile> filesToUpload = new HashMap<>(); |
| 173 | filesToUpload.put(dir, new LocalFile(dir, LocalFileType.OUTPUT)); |
| 174 | ByteStreamUploader uploader = mock(ByteStreamUploader.class); |
| 175 | ByteStreamBuildEventArtifactUploader artifactUploader = |
| 176 | new ByteStreamBuildEventArtifactUploader( |
pcloudy | 798b9a9 | 2018-12-04 02:31:49 -0800 | [diff] [blame] | 177 | uploader, "localhost", withEmptyMetadata, "instance", /* maxUploadThreads= */ 100); |
tomlu | 9efbc25 | 2018-08-10 11:51:20 -0700 | [diff] [blame] | 178 | PathConverter pathConverter = artifactUploader.upload(filesToUpload).get(); |
| 179 | assertThat(pathConverter.apply(dir)).isNull(); |
| 180 | artifactUploader.shutdown(); |
| 181 | } |
| 182 | |
| 183 | @Test |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 184 | public void someUploadsFail() throws Exception { |
| 185 | // Test that if one of multiple file uploads fails, the upload future fails and that the |
| 186 | // error is propagated correctly. |
| 187 | |
| 188 | int numUploads = 10; |
George Gensure | 3c9089b | 2019-05-02 07:07:55 -0700 | [diff] [blame] | 189 | Map<HashCode, byte[]> blobsByHash = new HashMap<>(); |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 190 | Map<Path, LocalFile> filesToUpload = new HashMap<>(); |
| 191 | Random rand = new Random(); |
| 192 | for (int i = 0; i < numUploads; i++) { |
| 193 | Path file = fs.getPath("/file" + i); |
| 194 | OutputStream out = file.getOutputStream(); |
| 195 | int blobSize = rand.nextInt(100) + 1; |
| 196 | byte[] blob = new byte[blobSize]; |
| 197 | rand.nextBytes(blob); |
| 198 | out.write(blob); |
| 199 | out.flush(); |
| 200 | out.close(); |
George Gensure | 3c9089b | 2019-05-02 07:07:55 -0700 | [diff] [blame] | 201 | blobsByHash.put(HashCode.fromString(DIGEST_UTIL.compute(file).getHash()), blob); |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 202 | filesToUpload.put(file, new LocalFile(file, LocalFileType.OUTPUT)); |
| 203 | } |
George Gensure | 3c9089b | 2019-05-02 07:07:55 -0700 | [diff] [blame] | 204 | String hashOfBlobThatShouldFail = blobsByHash.keySet().iterator().next().toString(); |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 205 | serviceRegistry.addService(new MaybeFailOnceUploadService(blobsByHash) { |
| 206 | @Override |
| 207 | public StreamObserver<WriteRequest> write(StreamObserver<WriteResponse> response) { |
| 208 | StreamObserver<WriteRequest> delegate = super.write(response); |
| 209 | return new StreamObserver<WriteRequest>() { |
| 210 | @Override |
| 211 | public void onNext(WriteRequest value) { |
| 212 | if (value.getResourceName().contains(hashOfBlobThatShouldFail)) { |
| 213 | response.onError(Status.CANCELLED.asException()); |
| 214 | } else { |
| 215 | delegate.onNext(value); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void onError(Throwable t) { |
| 221 | delegate.onError(t); |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void onCompleted() { |
| 226 | delegate.onCompleted(); |
| 227 | } |
| 228 | }; |
| 229 | } |
| 230 | }); |
| 231 | |
| 232 | RemoteRetrier retrier = |
Jakob Buchgraber | bc06db9 | 2019-03-07 00:21:52 -0800 | [diff] [blame] | 233 | TestUtils.newRemoteRetrier(() -> new FixedBackoff(1, 0), (e) -> true, retryService); |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 234 | ReferenceCountedChannel refCntChannel = new ReferenceCountedChannel(channel); |
| 235 | ByteStreamUploader uploader = |
| 236 | new ByteStreamUploader("instance", refCntChannel, null, 3, retrier); |
| 237 | ByteStreamBuildEventArtifactUploader artifactUploader = |
| 238 | new ByteStreamBuildEventArtifactUploader( |
pcloudy | 798b9a9 | 2018-12-04 02:31:49 -0800 | [diff] [blame] | 239 | uploader, "localhost", withEmptyMetadata, "instance", /* maxUploadThreads= */ 100); |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 240 | |
jcater | b922677 | 2019-04-29 12:04:52 -0700 | [diff] [blame] | 241 | ExecutionException e = |
| 242 | assertThrows(ExecutionException.class, () -> artifactUploader.upload(filesToUpload).get()); |
| 243 | assertThat(Status.fromThrowable(e).getCode()).isEqualTo(Status.CANCELLED.getCode()); |
buchgr | b50fe86 | 2018-07-12 04:01:45 -0700 | [diff] [blame] | 244 | |
| 245 | artifactUploader.shutdown(); |
| 246 | |
| 247 | assertThat(uploader.refCnt()).isEqualTo(0); |
| 248 | assertThat(refCntChannel.isShutdown()).isTrue(); |
| 249 | } |
| 250 | } |