-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Do not Submit] [Test PR] Fix testbench and disable flaky tests #13371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
2db4acb
351729c
f7b6ff7
6000ead
650d973
f5a9e50
e17d215
e754481
c02746a
6254d81
ca67ea0
98f2576
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * 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.cloud.storage; | ||
|
|
||
| import com.google.api.gax.grpc.GrpcStatusCode; | ||
| import com.google.cloud.storage.Crc32cValue.Crc32cLengthKnown; | ||
| import com.google.protobuf.ByteString; | ||
| import com.google.storage.v2.Object; | ||
| import io.grpc.Status.Code; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.Locale; | ||
| import java.util.OptionalLong; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * A wrapper around hasher that accumulates checksums and validates them at the end of the read if | ||
| * it was a full object read. | ||
| */ | ||
| final class CumulativeHasher implements Hasher { | ||
| private final Hasher delegate; | ||
| private final long startOffset; | ||
| private final OptionalLong limit; | ||
| private Crc32cLengthKnown cumulativeHash; | ||
|
|
||
| CumulativeHasher(Hasher delegate, long startOffset, OptionalLong limit) { | ||
| this.delegate = delegate; | ||
| this.startOffset = startOffset; | ||
| this.limit = limit; | ||
| this.cumulativeHash = Crc32cValue.zero(); | ||
| } | ||
|
|
||
| @Override | ||
| public Crc32cLengthKnown hash(ByteBuffer b) { | ||
| return delegate.hash(b); | ||
| } | ||
|
|
||
| @Override | ||
| public Crc32cLengthKnown hash(ByteString byteString) { | ||
| return delegate.hash(byteString); | ||
| } | ||
|
|
||
| @Override | ||
| public void validate(Crc32cValue<?> expected, Supplier<ByteBuffer> b) | ||
| throws ChecksumMismatchException { | ||
| ByteBuffer byteBuffer = b.get(); | ||
| Crc32cLengthKnown actual = delegate.hash(byteBuffer); | ||
| if (actual != null) { | ||
| if (expected != null && !actual.eqValue(expected)) { | ||
| throw new ChecksumMismatchException(expected, actual); | ||
| } | ||
| accumulate(actual); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void validate(Crc32cValue<?> expected, ByteString byteString) | ||
| throws ChecksumMismatchException { | ||
| Crc32cLengthKnown actual = delegate.hash(byteString); | ||
| if (actual != null) { | ||
| if (expected != null && !actual.eqValue(expected)) { | ||
| throw new ChecksumMismatchException(expected, actual); | ||
| } | ||
| accumulate(actual); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void validateUnchecked(Crc32cValue<?> expected, ByteString byteString) | ||
| throws UncheckedChecksumMismatchException { | ||
| Crc32cLengthKnown actual = delegate.hash(byteString); | ||
| if (actual != null) { | ||
| if (expected != null && !actual.eqValue(expected)) { | ||
| throw new UncheckedChecksumMismatchException(expected, actual); | ||
| } | ||
| accumulate(actual); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public <C extends Crc32cValue<?>> C nullSafeConcat(C r1, Crc32cLengthKnown r2) { | ||
| return delegate.nullSafeConcat(r1, r2); | ||
| } | ||
|
|
||
| @Override | ||
| public Crc32cLengthKnown initialValue() { | ||
| return delegate.initialValue(); | ||
| } | ||
|
|
||
| // Checks if it was a full object read. | ||
| boolean qualifiesForVerification(Object metadata) { | ||
| return startOffset == 0 | ||
| && metadata != null | ||
| && metadata.hasChecksums() | ||
| && metadata.getChecksums().hasCrc32C() | ||
| && (!limit.isPresent() || limit.getAsLong() >= metadata.getSize()); | ||
| } | ||
|
|
||
| void validateCumulativeChecksum(Object metadata) | ||
| throws UncheckedCumulativeChecksumMismatchException { | ||
| if (qualifiesForVerification(metadata)) { | ||
| Crc32cValue<?> expected = Crc32cValue.of(metadata.getChecksums().getCrc32C()); | ||
| Crc32cLengthKnown actual = getCumulativeHash(); | ||
| if (!actual.eqValue(expected)) { | ||
| throw new UncheckedCumulativeChecksumMismatchException(expected, actual); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void accumulate(Crc32cLengthKnown actual) { | ||
| cumulativeHash = cumulativeHash.concat(actual); | ||
| } | ||
|
|
||
| Crc32cLengthKnown getCumulativeHash() { | ||
| return cumulativeHash; | ||
| } | ||
| } | ||
|
|
||
| final class UncheckedCumulativeChecksumMismatchException | ||
| extends com.google.api.gax.rpc.DataLossException { | ||
| private static final GrpcStatusCode STATUS_CODE = GrpcStatusCode.of(Code.DATA_LOSS); | ||
| private final Crc32cValue<?> expected; | ||
| private final Crc32cLengthKnown actual; | ||
|
|
||
| UncheckedCumulativeChecksumMismatchException(Crc32cValue<?> expected, Crc32cLengthKnown actual) { | ||
| super( | ||
| String.format( | ||
| Locale.US, | ||
| "Mismatch cumulative checksum value. Expected %s actual %s", | ||
| expected.debugString(), | ||
| actual.debugString()), | ||
| /* cause= */ null, | ||
| STATUS_CODE, | ||
| /* retryable= */ false); | ||
| this.expected = expected; | ||
| this.actual = actual; | ||
| } | ||
|
|
||
| Crc32cValue<?> getExpected() { | ||
| return expected; | ||
| } | ||
|
|
||
| Crc32cLengthKnown getActual() { | ||
| return actual; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,7 @@ | |
| import java.io.InputStreamReader; | ||
| import java.net.SocketException; | ||
| import java.net.URI; | ||
| import java.util.concurrent.Callable; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
@@ -186,6 +187,7 @@ public List<RetryTestResource> listRetryTests() throws IOException { | |
| return b.build(); | ||
| } | ||
|
|
||
|
|
||
| private boolean startGRPCServer(int gRPCPort) throws IOException { | ||
| GenericUrl url = new GenericUrl(baseUri + "/start_grpc?port=9090"); | ||
| HttpRequest req = requestFactory.buildGetRequest(url); | ||
|
|
@@ -219,6 +221,9 @@ public void start() { | |
| LOGGER.info("Redirecting server stdout to: {}", outFile.getAbsolutePath()); | ||
| LOGGER.info("Redirecting server stderr to: {}", errFile.getAbsolutePath()); | ||
| String dockerImage = String.format(Locale.US, "%s:%s", dockerImageName, dockerImageTag); | ||
| try { | ||
| new ProcessBuilder("docker", "rm", "-f", containerName).start().waitFor(5, TimeUnit.SECONDS); | ||
| } catch (Exception ignore) {} | ||
|
Comment on lines
+224
to
+226
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When starting a process with try {
Process process = new ProcessBuilder("docker", "rm", "-f", containerName).start();
if (!process.waitFor(5, TimeUnit.SECONDS)) {
process.destroyForcibly();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception ignore) {}References
|
||
| // First try and pull the docker image, this validates docker is available and running | ||
| // on the host, as well as gives time for the image to be downloaded independently of | ||
| // trying to start the container. (Below, when we first start the container we then attempt | ||
|
|
@@ -263,8 +268,8 @@ public void start() { | |
| "gunicorn", | ||
| "--bind=0.0.0.0:9000", | ||
| "--worker-class=sync", | ||
| "--threads=10", | ||
| "--access-logfile=-", | ||
| "--threads=15", | ||
| "--access-logfile=-", | ||
| "--keep-alive=0", | ||
| "testbench:run()"); | ||
| process = | ||
|
|
@@ -282,7 +287,7 @@ public void start() { | |
| runWithRetries( | ||
| TestBench.this::listRetryTests, | ||
| RetrySettings.newBuilder() | ||
| .setTotalTimeoutDuration(Duration.ofSeconds(30)) | ||
| .setTotalTimeoutDuration(Duration.ofSeconds(90)) | ||
| .setInitialRetryDelayDuration(Duration.ofMillis(500)) | ||
| .setRetryDelayMultiplier(1.5) | ||
| .setMaxRetryDelayDuration(Duration.ofSeconds(5)) | ||
|
|
@@ -330,8 +335,14 @@ public void stop() { | |
| int processExitValue = process.exitValue(); | ||
| if (processExitValue != 0) { | ||
| attemptForceStopContainer = true; | ||
| LOGGER.warn("Container exit value = {}", processExitValue); | ||
| try { | ||
| dumpServerLogs(outPath, errPath); | ||
| } catch (Exception ignore) { | ||
| } | ||
| } else { | ||
| LOGGER.warn("Container exit value = {}", processExitValue); | ||
| } | ||
| LOGGER.warn("Container exit value = {}", processExitValue); | ||
| } catch (IllegalThreadStateException e) { | ||
| attemptForceStopContainer = true; | ||
| } | ||
|
|
@@ -487,6 +498,41 @@ static final class Builder { | |
|
|
||
| private static final String DEFAULT_CONTAINER_NAME = "default"; | ||
|
|
||
| private static int getForkNumber() { | ||
| String forkStr = System.getProperty("surefire.forkNumber"); | ||
| if (forkStr != null) { | ||
| try { | ||
| return Integer.parseInt(forkStr.trim()); | ||
| } catch (NumberFormatException ignore) { | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| private static String getDefaultBaseUri() { | ||
| int fork = getForkNumber(); | ||
| if (fork > 1) { | ||
| return "http://localhost:" + (9000 + (fork - 1) * 10); | ||
| } | ||
| return DEFAULT_BASE_URI; | ||
| } | ||
|
|
||
| private static String getDefaultGrpcBaseUri() { | ||
| int fork = getForkNumber(); | ||
| if (fork > 1) { | ||
| return "http://localhost:" + (9005 + (fork - 1) * 10); | ||
| } | ||
| return DEFAULT_GRPC_BASE_URI; | ||
| } | ||
|
|
||
| private static String getDefaultContainerName() { | ||
| int fork = getForkNumber(); | ||
| if (fork > 1) { | ||
| return DEFAULT_CONTAINER_NAME + "_" + fork; | ||
| } | ||
| return DEFAULT_CONTAINER_NAME; | ||
| } | ||
|
|
||
| private boolean ignorePullError; | ||
| private String baseUri; | ||
| private String gRPCBaseUri; | ||
|
|
@@ -496,12 +542,12 @@ static final class Builder { | |
|
|
||
| private Builder() { | ||
| this( | ||
| false, | ||
| DEFAULT_BASE_URI, | ||
| DEFAULT_GRPC_BASE_URI, | ||
| true, | ||
| getDefaultBaseUri(), | ||
| getDefaultGrpcBaseUri(), | ||
| DEFAULT_IMAGE_NAME, | ||
| DEFAULT_IMAGE_TAG, | ||
| DEFAULT_CONTAINER_NAME); | ||
| getDefaultContainerName()); | ||
| } | ||
|
|
||
| private Builder( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If any chunk read bypasses the
validatemethods (by callinghashdirectly) or ifdelegate.hashreturnsnull(e.g., if hashing is disabled or skipped for a chunk), the cumulative hash will be incomplete or remain atzero(). IfqualifiesForVerificationstill returnstrue,validateCumulativeChecksumwill compare this incomplete hash with the expected full-object checksum and throw a false-positiveUncheckedCumulativeChecksumMismatchException(which is aDataLossException).To prevent this, we should introduce a
private boolean canVerify = true;flag:canVerify = falseifhash(ByteBuffer)orhash(ByteString)is called directly.canVerify = falseifdelegate.hashreturnsnullin any of thevalidateorvalidateUncheckedmethods.canVerifyinqualifiesForVerification.