Skip to content

Commit

Permalink
Use reflection to support `--experimental_worker_for_repo_fetching=vi…
Browse files Browse the repository at this point in the history
…rtual`

Bazel already ships JDK 21, but the codebase can't use JDK 21 features (language OR runtime) because Google hasn't migrated to JDK 21 yet (soon TM). But we can cheat a bit using reflection!

Work towards #10515

PiperOrigin-RevId: 601188027
Change-Id: I65c1712da0347bef40fc4af94bad4c211687a8b7
  • Loading branch information
Wyverald committed Jan 26, 2024
1 parent c6c7693 commit 0d3f627
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,22 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
starlarkRepositoryFunction.setWorkerExecutorService(repoFetchingWorkerThreadPool);
break;
case VIRTUAL:
throw new AbruptExitException(
detailedExitCode(
"using a virtual worker thread for repo fetching is not yet supported",
Code.BAD_DOWNLOADER_CONFIG));
try {
// Since Google hasn't migrated to JDK 21 yet, we can't directly call
// Executors.newVirtualThreadPerTaskExecutor here. But a bit of reflection never hurt
// anyone... right? (OSS Bazel already ships with a bundled JDK 21)
starlarkRepositoryFunction.setWorkerExecutorService(
(ExecutorService)
Executors.class
.getDeclaredMethod("newVirtualThreadPerTaskExecutor")
.invoke(null));
} catch (ReflectiveOperationException e) {
throw new AbruptExitException(
detailedExitCode(
"couldn't create virtual worker thread executor for repo fetching",
Code.BAD_DOWNLOADER_CONFIG),
e);
}
}
downloadManager.setDisableDownload(repoOptions.disableDownload);
if (repoOptions.repositoryDownloaderRetries >= 0) {
Expand Down
6 changes: 6 additions & 0 deletions src/test/shell/bazel/starlark_repository_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2384,6 +2384,12 @@ EOF
bazel build @foo//:bar --experimental_worker_for_repo_fetching=platform >& $TEST_log \
|| fail "Expected build to succeed"
expect_log_n "hello world!" 1

# virtual worker thread, never restarts
bazel shutdown
bazel build @foo//:bar --experimental_worker_for_repo_fetching=virtual >& $TEST_log \
|| fail "Expected build to succeed"
expect_log_n "hello world!" 1
}

function test_duplicate_value_in_environ() {
Expand Down

0 comments on commit 0d3f627

Please sign in to comment.