|
| 1 | +// Copyright 2026 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 | + |
| 15 | +package com.google.devtools.build.lib.remote; |
| 16 | + |
| 17 | +import static com.google.devtools.build.lib.remote.util.Utils.getFromFuture; |
| 18 | +import build.bazel.remote.execution.v2.Digest; |
| 19 | +import build.bazel.remote.execution.v2.SplitBlobResponse; |
| 20 | +import com.google.common.collect.ImmutableSet; |
| 21 | +import com.google.common.collect.Lists; |
| 22 | +import com.google.common.util.concurrent.Futures; |
| 23 | +import com.google.common.util.concurrent.ListenableFuture; |
| 24 | +import com.google.devtools.build.lib.concurrent.TaskDeduplicator; |
| 25 | +import com.google.devtools.build.lib.remote.chunking.ChunkingConfig; |
| 26 | +import com.google.devtools.build.lib.remote.chunking.FastCDCChunker; |
| 27 | +import com.google.devtools.build.lib.remote.common.RemoteActionExecutionContext; |
| 28 | +import com.google.devtools.build.lib.remote.util.DigestUtil; |
| 29 | +import com.google.devtools.build.lib.vfs.Path; |
| 30 | +import java.io.ByteArrayInputStream; |
| 31 | +import java.io.IOException; |
| 32 | +import java.io.InputStream; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.concurrent.ExecutionException; |
| 38 | + |
| 39 | +/** |
| 40 | + * Uploads blobs in chunks using Content-Defined Chunking with FastCDC 2020. |
| 41 | + * |
| 42 | + * <p> |
| 43 | + * Upload flow for blobs above threshold: |
| 44 | + * |
| 45 | + * <ol> |
| 46 | + * <li>Chunk file with FastCDC |
| 47 | + * <li>Call findMissingDigests on chunk digests |
| 48 | + * <li>Upload only missing chunks |
| 49 | + * <li>Call SpliceBlob to register the blob as the concatenation of chunks |
| 50 | + * </ol> |
| 51 | + */ |
| 52 | +public class ChunkedBlobUploader { |
| 53 | + private final GrpcCacheClient grpcCacheClient; |
| 54 | + private final FastCDCChunker chunker; |
| 55 | + private final long chunkingThreshold; |
| 56 | + private final TaskDeduplicator<Digest, Void> chunkUploadDeduplicator = new TaskDeduplicator<>(); |
| 57 | + |
| 58 | + public ChunkedBlobUploader(GrpcCacheClient grpcCacheClient, DigestUtil digestUtil) { |
| 59 | + this(grpcCacheClient, ChunkingConfig.defaults(), digestUtil); |
| 60 | + } |
| 61 | + |
| 62 | + public ChunkedBlobUploader( |
| 63 | + GrpcCacheClient grpcCacheClient, ChunkingConfig config, DigestUtil digestUtil) { |
| 64 | + this.grpcCacheClient = grpcCacheClient; |
| 65 | + this.chunker = new FastCDCChunker(config, digestUtil); |
| 66 | + this.chunkingThreshold = config.chunkingThreshold(); |
| 67 | + } |
| 68 | + |
| 69 | + public long getChunkingThreshold() { |
| 70 | + return chunkingThreshold; |
| 71 | + } |
| 72 | + |
| 73 | + public void uploadChunked(RemoteActionExecutionContext context, Digest blobDigest, Path file) |
| 74 | + throws IOException, InterruptedException { |
| 75 | + if (isAlreadyChunkedOnServer(context, blobDigest)) { |
| 76 | + return; |
| 77 | + } |
| 78 | + doChunkedUpload(context, blobDigest, file); |
| 79 | + } |
| 80 | + |
| 81 | + private boolean isAlreadyChunkedOnServer( |
| 82 | + RemoteActionExecutionContext context, Digest blobDigest) throws InterruptedException { |
| 83 | + ListenableFuture<SplitBlobResponse> splitFuture = grpcCacheClient.splitBlob(context, blobDigest); |
| 84 | + if (splitFuture == null) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + try { |
| 88 | + SplitBlobResponse response = splitFuture.get(); |
| 89 | + return isTrulyChunked(response, blobDigest); |
| 90 | + } catch (ExecutionException e) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // TODO(https://github.com/bazelbuild/remote-apis/pull/358): should make this check unnecessary. |
| 96 | + private static boolean isTrulyChunked(SplitBlobResponse response, Digest blobDigest) { |
| 97 | + if (response == null || response.getChunkDigestsCount() == 0) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + if (response.getChunkDigestsCount() == 1 && response.getChunkDigests(0).equals(blobDigest)) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + private void doChunkedUpload(RemoteActionExecutionContext context, Digest blobDigest, Path file) |
| 107 | + throws IOException, InterruptedException { |
| 108 | + List<Digest> chunkDigests; |
| 109 | + try (InputStream input = file.getInputStream()) { |
| 110 | + chunkDigests = chunker.chunkToDigests(input); |
| 111 | + } |
| 112 | + if (chunkDigests.isEmpty()) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + ImmutableSet<Digest> missingDigests; |
| 117 | + try { |
| 118 | + missingDigests = grpcCacheClient.findMissingDigests(context, chunkDigests).get(); |
| 119 | + } catch (ExecutionException e) { |
| 120 | + throw new IOException("Failed to find missing digests", e.getCause()); |
| 121 | + } |
| 122 | + |
| 123 | + uploadMissingChunks(context, missingDigests, chunkDigests, file); |
| 124 | + |
| 125 | + try { |
| 126 | + grpcCacheClient.spliceBlob(context, blobDigest, chunkDigests).get(); |
| 127 | + } catch (ExecutionException e) { |
| 128 | + throw new IOException("Failed to splice blob", e.getCause()); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private void uploadMissingChunks( |
| 133 | + RemoteActionExecutionContext context, |
| 134 | + ImmutableSet<Digest> missingDigests, |
| 135 | + List<Digest> chunkDigests, |
| 136 | + Path file) |
| 137 | + throws IOException, InterruptedException { |
| 138 | + if (missingDigests.isEmpty()) { |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + // Rather than keeping the offsets of the chunks, |
| 143 | + // We can just use the size from the digests of the prev |
| 144 | + // chunks to compute just the offsets we need. |
| 145 | + Map<Digest, Long> digestToOffset = new HashMap<>(); |
| 146 | + long offset = 0; |
| 147 | + for (Digest digest : chunkDigests) { |
| 148 | + if (missingDigests.contains(digest)) { |
| 149 | + digestToOffset.put(digest, offset); |
| 150 | + } |
| 151 | + offset += digest.getSizeBytes(); |
| 152 | + } |
| 153 | + |
| 154 | + for (Digest chunkDigest : missingDigests) { |
| 155 | + long chunkOffset = digestToOffset.get(chunkDigest); |
| 156 | + getFromFuture(chunkUploadDeduplicator.executeIfNew(chunkDigest, |
| 157 | + () -> uploadChunk(context, chunkDigest, chunkOffset, file))); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private ListenableFuture<Void> uploadChunk(RemoteActionExecutionContext context, Digest digest, long offset, |
| 162 | + Path file) { |
| 163 | + try { |
| 164 | + byte[] data = readChunkData(file, offset, (int) digest.getSizeBytes()); |
| 165 | + return grpcCacheClient.uploadBlob(context, digest, () -> new ByteArrayInputStream(data)); |
| 166 | + } catch (IOException e) { |
| 167 | + return Futures.immediateFailedFuture(e); |
| 168 | + } |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | + private byte[] readChunkData(Path file, long offset, int length) throws IOException { |
| 173 | + try (InputStream input = file.getInputStream()) { |
| 174 | + input.skipNBytes(offset); |
| 175 | + return input.readNBytes(length); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments