Skip to content

Commit

Permalink
bazel_registry.json
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Apr 20, 2024
1 parent e6106dd commit 1d5da5c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void afterCommand() throws AbruptExitException {
.setFileHashes(
// We ensure that the same file is not downloaded multiple times and can thus
// collect without worrying about duplicates.
// FIXME
downloadEvents.stream()
.sorted(comparing(RegistryFileDownloadEvent::getUri))
.collect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.devtools.build.lib.bazel.repository.downloader.Checksum;
import com.google.devtools.build.lib.bazel.repository.downloader.DownloadManager;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
import com.google.devtools.build.lib.events.StoredEventHandler;
import com.google.devtools.build.lib.profiler.Profiler;
import com.google.devtools.build.lib.profiler.ProfilerTask;
import com.google.devtools.build.lib.profiler.SilentCloseable;
Expand Down Expand Up @@ -65,6 +66,7 @@ public enum KnownFileHashesMode {
private final ImmutableMap<String, Optional<Checksum>> knownFileHashes;
private final KnownFileHashesMode knownFileHashesMode;
private volatile Optional<BazelRegistryJson> bazelRegistryJson;
private volatile StoredEventHandler bazelRegistryJsonEvents;

private static final String SOURCE_JSON_FILENAME = "source.json";

Expand Down Expand Up @@ -120,13 +122,20 @@ private Optional<byte[]> grabFile(
checksum = knownChecksum;
}
}
Optional<byte[]> maybeContent;
try (SilentCloseable c =
Profiler.instance().profile(ProfilerTask.BZLMOD, () -> "download file: " + url)) {
return Optional.of(
downloadManager.downloadAndReadOneUrl(new URL(url), eventHandler, clientEnv, checksum));
maybeContent =
Optional.of(
downloadManager.downloadAndReadOneUrl(
new URL(url), eventHandler, clientEnv, checksum));
} catch (FileNotFoundException e) {
return Optional.empty();
maybeContent = Optional.empty();
}
if (knownFileHashesMode == KnownFileHashesMode.USE_AND_UPDATE && useChecksum) {
eventHandler.post(new RegistryFileDownloadEvent(url, maybeContent));
}
return maybeContent;
}

@Override
Expand All @@ -136,17 +145,9 @@ public Optional<ModuleFile> getModuleFile(ModuleKey key, ExtendedEventHandler ev
constructUrl(
uri.toString(), "modules", key.getName(), key.getVersion().toString(), "MODULE.bazel");
Optional<byte[]> maybeContent = grabFile(url, eventHandler, /* useChecksum= */ true);
recordContentIfRequested(eventHandler, url, maybeContent);
return maybeContent.map(content -> ModuleFile.create(content, url));
}

private void recordContentIfRequested(
ExtendedEventHandler eventHandler, String url, Optional<byte[]> maybeContent) {
if (knownFileHashesMode == KnownFileHashesMode.USE_AND_UPDATE) {
eventHandler.post(new RegistryFileDownloadEvent(url, maybeContent));
}
}

/** Represents fields available in {@code bazel_registry.json} for the registry. */
private static class BazelRegistryJson {
String[] mirrors;
Expand Down Expand Up @@ -198,9 +199,10 @@ private Optional<String> grabJsonFile(
* Grabs a JSON file from the given URL, and returns it as a parsed object with fields in {@code
* T}. Returns {@link Optional#empty} if the file doesn't exist.
*/
private <T> Optional<T> grabJson(String url, Class<T> klass, ExtendedEventHandler eventHandler)
private <T> Optional<T> grabJson(
String url, Class<T> klass, ExtendedEventHandler eventHandler, boolean useChecksum)
throws IOException, InterruptedException {
Optional<String> jsonString = grabJsonFile(url, eventHandler, /* useChecksum= */ false);
Optional<String> jsonString = grabJsonFile(url, eventHandler, useChecksum);
if (jsonString.isEmpty() || jsonString.get().isBlank()) {
return Optional.empty();
}
Expand Down Expand Up @@ -232,7 +234,6 @@ public RepoSpec getRepoSpec(ModuleKey key, ExtendedEventHandler eventHandler)
throw new FileNotFoundException(
String.format("Module %s's %s not found in registry %s", key, SOURCE_JSON_FILENAME, uri));
}
recordContentIfRequested(eventHandler, jsonUrl, jsonString.map(s -> s.getBytes(UTF_8)));
SourceJson sourceJson = parseJson(jsonString.get(), jsonUrl, SourceJson.class);
switch (sourceJson.type) {
case "archive":
Expand Down Expand Up @@ -265,14 +266,18 @@ private Optional<BazelRegistryJson> getBazelRegistryJson(ExtendedEventHandler ev
if (bazelRegistryJson == null) {
synchronized (this) {
if (bazelRegistryJson == null) {
var storedEventHandler = new StoredEventHandler();
bazelRegistryJson =
grabJson(
constructUrl(uri.toString(), "bazel_registry.json"),
BazelRegistryJson.class,
eventHandler);
storedEventHandler,
/* useChecksum= */ true);
bazelRegistryJsonEvents = storedEventHandler;
}
}
}
bazelRegistryJsonEvents.replayOn(eventHandler);
return bazelRegistryJson;
}

Expand Down Expand Up @@ -381,7 +386,8 @@ public Optional<ImmutableMap<Version, String>> getYankedVersions(
grabJson(
constructUrl(uri.toString(), "modules", moduleName, "metadata.json"),
MetadataJson.class,
eventHandler);
eventHandler,
/* useChecksum= */ false);
if (metadataJson.isEmpty()) {
return Optional.empty();
}
Expand Down
1 change: 1 addition & 0 deletions src/test/tools/bzlmod/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1d5da5c

Please sign in to comment.