From 0d3f62705aedb08590f7b73b405b317957aae52b Mon Sep 17 00:00:00 2001 From: Googler Date: Wed, 24 Jan 2024 11:47:33 -0800 Subject: [PATCH] Use reflection to support `--experimental_worker_for_repo_fetching=virtual` 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 https://github.com/bazelbuild/bazel/issues/10515 PiperOrigin-RevId: 601188027 Change-Id: I65c1712da0347bef40fc4af94bad4c211687a8b7 --- .../lib/bazel/BazelRepositoryModule.java | 20 +++++++++++++++---- .../shell/bazel/starlark_repository_test.sh | 6 ++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java b/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java index 0a2661fde95dc7..51162dd1491951 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java @@ -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) { diff --git a/src/test/shell/bazel/starlark_repository_test.sh b/src/test/shell/bazel/starlark_repository_test.sh index d5008c47cd5dd4..8493fe92f86ca0 100755 --- a/src/test/shell/bazel/starlark_repository_test.sh +++ b/src/test/shell/bazel/starlark_repository_test.sh @@ -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() {