Skip to content

Commit

Permalink
Replace usage of java.time.Clock with com.google.devtools.build.lib.c…
Browse files Browse the repository at this point in the history
…lock.Clock
  • Loading branch information
mislavmandaricaxilis committed Oct 14, 2024
1 parent ed82b98 commit be0c92a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ java_library(
srcs = glob(["*.java"]),
deps = [
"//src/main/java/com/google/devtools/build/lib/bazel/repository/downloader",
"//src/main/java/com/google/devtools/build/lib/clock",
"//src/main/java/com/google/devtools/build/lib/events",
"//src/main/java/com/google/devtools/build/lib/remote:ReferenceCountedChannel",
"//src/main/java/com/google/devtools/build/lib/remote:Retrier",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.devtools.build.lib.bazel.repository.downloader.Checksum;
import com.google.devtools.build.lib.bazel.repository.downloader.Downloader;
import com.google.devtools.build.lib.bazel.repository.downloader.HashOutputStream;
import com.google.devtools.build.lib.clock.BlazeClock;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
import com.google.devtools.build.lib.remote.ReferenceCountedChannel;
Expand All @@ -46,7 +47,6 @@
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.Clock;
import java.time.Duration;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -93,8 +93,6 @@ public class GrpcRemoteDownloader implements AutoCloseable, Downloader {
// value when both are present.
private static final String QUALIFIER_HTTP_HEADER_URL_PREFIX = "http_header_url:";

private Clock clock = Clock.systemUTC();

public GrpcRemoteDownloader(
String buildRequestId,
String commandId,
Expand Down Expand Up @@ -153,8 +151,7 @@ public void download(
canonicalId,
digestFunction,
headers,
credentials,
clock);
credentials);
try {
FetchBlobResponse response =
retrier.execute(
Expand Down Expand Up @@ -205,8 +202,7 @@ static FetchBlobRequest newFetchBlobRequest(
String canonicalId,
DigestFunction.Value digestFunction,
Map<String, List<String>> headers,
Credentials credentials,
Clock clock)
Credentials credentials)
throws IOException {
FetchBlobRequest.Builder requestBuilder =
FetchBlobRequest.newBuilder()
Expand Down Expand Up @@ -245,8 +241,8 @@ static FetchBlobRequest newFetchBlobRequest(
} else {
// If no checksum is provided, never accept cached content.
// Timestamp is offset by an hour to account for clock skew.
Clock c = Clock.offset(clock, Duration.ofHours(1));
requestBuilder.setOldestContentAccepted(Timestamps.fromMillis(c.millis()));
long milis = BlazeClock.instance().currentTimeMillis() + Duration.ofHours(1).toMillis();
requestBuilder.setOldestContentAccepted(Timestamps.fromMillis(milis));
}

if (!Strings.isNullOrEmpty(canonicalId)) {
Expand Down Expand Up @@ -290,9 +286,4 @@ private OutputStream newOutputStream(Path destination, Optional<Checksum> checks
public ReferenceCountedChannel getChannel() {
return channel;
}

@VisibleForTesting
public void setClock(Clock clock) {
this.clock = clock;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/authandtls",
"//src/main/java/com/google/devtools/build/lib/bazel/repository/cache",
"//src/main/java/com/google/devtools/build/lib/bazel/repository/downloader",
"//src/main/java/com/google/devtools/build/lib/clock",
"//src/main/java/com/google/devtools/build/lib/events",
"//src/main/java/com/google/devtools/build/lib/remote",
"//src/main/java/com/google/devtools/build/lib/remote/common",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.google.devtools.build.lib.bazel.repository.downloader.Checksum;
import com.google.devtools.build.lib.bazel.repository.downloader.Downloader;
import com.google.devtools.build.lib.bazel.repository.downloader.UnrecoverableHttpException;
import com.google.devtools.build.lib.clock.BlazeClock;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
import com.google.devtools.build.lib.remote.ChannelConnectionWithServerCapabilitiesFactory;
import com.google.devtools.build.lib.remote.ReferenceCountedChannel;
Expand All @@ -54,14 +55,14 @@
import com.google.devtools.build.lib.remote.util.InMemoryCacheClient;
import com.google.devtools.build.lib.remote.util.TestUtils;
import com.google.devtools.build.lib.remote.util.TracingMetadataUtils;
import com.google.devtools.build.lib.testutil.ManualClock;
import com.google.devtools.build.lib.testutil.Scratch;
import com.google.devtools.build.lib.vfs.DigestHashFunction;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.SyscallCache;
import com.google.devtools.common.options.Options;
import com.google.protobuf.ByteString;
import com.google.protobuf.Timestamp;
import com.google.protobuf.util.Timestamps;
import io.grpc.CallCredentials;
import io.grpc.ManagedChannel;
Expand All @@ -75,9 +76,7 @@
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -95,9 +94,7 @@
@RunWith(JUnit4.class)
public class GrpcRemoteDownloaderTest {

private static final String now = "2024-10-10T00:00:00Z";
private static final String oneHourFromNow = "2024-10-10T01:00:00Z";
private static final Clock clock = Clock.fixed(Instant.parse(now), ZoneId.of("UTC"));
private static final ManualClock clock = new ManualClock();

private static final DigestUtil DIGEST_UTIL =
new DigestUtil(SyscallCache.NO_CACHE, DigestHashFunction.SHA256);
Expand Down Expand Up @@ -126,6 +123,8 @@ public final void setUp() throws Exception {
context = RemoteActionExecutionContext.create(metadata);

retryService = MoreExecutors.listeningDecorator(Executors.newScheduledThreadPool(1));

BlazeClock.setClock(clock);
}

@After
Expand Down Expand Up @@ -178,7 +177,6 @@ public int maxConcurrency() {
remoteOptions,
/* verboseFailures= */ false,
fallbackDownloader);
downloader.setClock(clock);
return downloader;
}

Expand Down Expand Up @@ -213,7 +211,6 @@ private static byte[] downloadBlob(
public void testDownload() throws Exception {
final byte[] content = "example content".getBytes(UTF_8);
final Digest contentDigest = DIGEST_UTIL.compute(content);
final Timestamp timestamp = Timestamps.parse(oneHourFromNow);

serviceRegistry.addService(
new FetchImplBase() {
Expand All @@ -224,7 +221,7 @@ public void fetchBlob(
.isEqualTo(
FetchBlobRequest.newBuilder()
.setDigestFunction(DIGEST_UTIL.getDigestFunction())
.setOldestContentAccepted(timestamp)
.setOldestContentAccepted(Timestamps.fromMillis(clock.advance(Duration.ofHours(1))))
.addUris("http://example.com/content.txt")
.build());
responseObserver.onNext(
Expand Down Expand Up @@ -379,8 +376,7 @@ public void testFetchBlobRequest() throws Exception {
ImmutableMap.of(
"Authorization", ImmutableList.of("Basic Zm9vOmJhcg=="),
"X-Custom-Token", ImmutableList.of("foo", "bar")),
StaticCredentials.EMPTY,
clock);
StaticCredentials.EMPTY);

assertThat(request)
.isEqualTo(
Expand Down Expand Up @@ -429,8 +425,7 @@ public void testFetchBlobRequest_withCredentialsPropagation() throws Exception {
"canonical ID",
DIGEST_UTIL.getDigestFunction(),
ImmutableMap.of(),
credentials,
clock);
credentials);

assertThat(request)
.isEqualTo(
Expand Down Expand Up @@ -473,8 +468,7 @@ public void testFetchBlobRequest_withoutCredentialsPropagation() throws Exceptio
"canonical ID",
DIGEST_UTIL.getDigestFunction(),
ImmutableMap.of(),
credentials,
clock);
credentials);

assertThat(request)
.isEqualTo(
Expand Down Expand Up @@ -503,15 +497,14 @@ public void testFetchBlobRequest_withoutChecksum() throws Exception {
"canonical ID",
DIGEST_UTIL.getDigestFunction(),
ImmutableMap.of(),
StaticCredentials.EMPTY,
clock);
StaticCredentials.EMPTY);

assertThat(request)
.isEqualTo(
FetchBlobRequest.newBuilder()
.setInstanceName("instance name")
.setDigestFunction(DIGEST_UTIL.getDigestFunction())
.setOldestContentAccepted(Timestamps.parse(oneHourFromNow))
.setOldestContentAccepted(Timestamps.fromMillis(clock.advance(Duration.ofHours(1))))
.addUris("http://example.com/")
.addQualifiers(
Qualifier.newBuilder().setName("bazel.canonical_id").setValue("canonical ID"))
Expand Down

0 comments on commit be0c92a

Please sign in to comment.