-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation (but not plumbing) of the gRPC remote downloader
Extracted from #10622 Per discussion on that PR, there's still some unanswered questions about how exactly we plumb the new `Downloader` type into `RemoteModule`. And per #10742 (comment), it is unlikely that even heroic effort from me will get the full end-to-end functionality into v3.0. Given this, to simplify the review, I'm taking some of the bits the reviewer is happy with and moving them to a separate PR. After merger, `GrpcRemoteDownloader` and its tests will exist in the source tree, but will not yet be available as CLI options. R: @michajlo CC: @adunham-stripe @dslomov @EricBurnett @philwo @sstriker Closes #10914. PiperOrigin-RevId: 299908615
- Loading branch information
1 parent
63b01f7
commit 80a2d7c
Showing
16 changed files
with
817 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...main/java/com/google/devtools/build/lib/bazel/repository/downloader/HashOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2020 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.devtools.build.lib.bazel.repository.downloader; | ||
|
||
import com.google.common.hash.HashCode; | ||
import com.google.common.hash.Hasher; | ||
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import javax.annotation.Nullable; | ||
import javax.annotation.WillCloseWhenClosed; | ||
|
||
/** | ||
* Output stream that guarantees its contents matches a hash code. | ||
* | ||
* <p>The actual checksum is computed gradually as the output is written. If it doesn't match, then | ||
* an {@link IOException} will be thrown when {@link #close()} is called. This error will be thrown | ||
* multiple times if these methods are called again for some reason. | ||
* | ||
* <p>Note that as the checksum can only be computed once the stream is closed, data will be written | ||
* to the underlying stream regardless of whether it matches the expected checksum. | ||
* | ||
* <p>This class is not thread safe, but it is safe to message pass this object between threads. | ||
*/ | ||
@ThreadCompatible | ||
public final class HashOutputStream extends OutputStream { | ||
|
||
private final OutputStream delegate; | ||
private final Hasher hasher; | ||
private final HashCode code; | ||
@Nullable private volatile HashCode actual; | ||
|
||
public HashOutputStream(@WillCloseWhenClosed OutputStream delegate, Checksum checksum) { | ||
this.delegate = delegate; | ||
this.hasher = checksum.getKeyType().newHasher(); | ||
this.code = checksum.getHashCode(); | ||
} | ||
|
||
@Override | ||
public void write(int buffer) throws IOException { | ||
hasher.putByte((byte) buffer); | ||
delegate.write(buffer); | ||
} | ||
|
||
@Override | ||
public void write(byte[] buffer) throws IOException { | ||
hasher.putBytes(buffer); | ||
delegate.write(buffer); | ||
} | ||
|
||
@Override | ||
public void write(byte[] buffer, int offset, int length) throws IOException { | ||
hasher.putBytes(buffer, offset, length); | ||
delegate.write(buffer, offset, length); | ||
} | ||
|
||
@Override | ||
public void flush() throws IOException { | ||
delegate.flush(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
delegate.close(); | ||
check(); | ||
} | ||
|
||
private void check() throws IOException { | ||
if (actual == null) { | ||
actual = hasher.hash(); | ||
} | ||
if (!code.equals(actual)) { | ||
throw new UnrecoverableHttpException( | ||
String.format("Checksum was %s but wanted %s", actual, code)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/google/devtools/build/lib/remote/downloader/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
load("@rules_java//java:defs.bzl", "java_library") | ||
|
||
package( | ||
default_visibility = ["//src:__subpackages__"], | ||
) | ||
|
||
filegroup( | ||
name = "srcs", | ||
srcs = glob(["*"]), | ||
) | ||
|
||
java_library( | ||
name = "downloader", | ||
srcs = glob(["*.java"]), | ||
deps = [ | ||
"//src/main/java/com/google/devtools/build/lib:events", | ||
"//src/main/java/com/google/devtools/build/lib/bazel/repository/downloader", | ||
"//src/main/java/com/google/devtools/build/lib/remote", | ||
"//src/main/java/com/google/devtools/build/lib/remote:ReferenceCountedChannel", | ||
"//src/main/java/com/google/devtools/build/lib/remote:Retrier", | ||
"//src/main/java/com/google/devtools/build/lib/remote/common", | ||
"//src/main/java/com/google/devtools/build/lib/remote/options", | ||
"//src/main/java/com/google/devtools/build/lib/remote/util", | ||
"//src/main/java/com/google/devtools/build/lib/vfs", | ||
"//third_party:gson", | ||
"//third_party:guava", | ||
"//third_party/grpc:grpc-jar", | ||
"@remoteapis//:build_bazel_remote_asset_v1_remote_asset_java_grpc", | ||
"@remoteapis//:build_bazel_remote_asset_v1_remote_asset_java_proto", | ||
"@remoteapis//:build_bazel_remote_execution_v2_remote_execution_java_proto", | ||
], | ||
) |
Oops, something went wrong.