-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create BootClassPathCachingFileManager a subclass of JavacFileManager…
… which handles caching for the boot classpaths. The class also decides if an instance can be kept for the current compilation or needs to be updated (e.g., if a bootclasspath digest has changed) Note: BootClassPathCachingFileManager is used only with singleplex-workers that have list of boot classpaths along with their digest information. RELNOTES: None. PiperOrigin-RevId: 387592935
- Loading branch information
1 parent
8ffc333
commit a0e5e45
Showing
4 changed files
with
315 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...ildjar/java/com/google/devtools/build/buildjar/javac/BootClassPathCachingFileManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2021 The Bazel Authors. All rights reserved. | ||
// | ||
// 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.devtools.build.buildjar.javac; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.protobuf.ByteString; | ||
import com.sun.tools.javac.file.JavacFileManager; | ||
import com.sun.tools.javac.util.Context; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** A subclass of the JavacFileManager that handles only boot classpaths */ | ||
@VisibleForTesting | ||
class BootClassPathCachingFileManager extends JavacFileManager { | ||
|
||
private final Map<String, ByteString> bootJarsAndDigest = new HashMap<>(); | ||
|
||
/** Create a JavacFileManager using a given context and BlazeJavacArguments */ | ||
public BootClassPathCachingFileManager(Context context, BlazeJavacArguments arguments) { | ||
super(context, false, UTF_8); | ||
|
||
for (Path bootClassPath : arguments.bootClassPath()) { | ||
bootJarsAndDigest.put( | ||
bootClassPath.toString(), arguments.inputsAndDigest().get(bootClassPath.toString())); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if this instance or a new instance is needed for the {@code BlazeJavacArguments}. An | ||
* update is needed if the bootclasspath in {@code BlazeJavacArguments} have changed its digest. | ||
*/ | ||
@VisibleForTesting | ||
boolean needsUpdate(BlazeJavacArguments arguments) { | ||
for (Path bootClassPath : arguments.bootClassPath()) { | ||
ByteString currDigest = arguments.inputsAndDigest().get(bootClassPath.toString()); | ||
|
||
if (currDigest == null) { | ||
return true; | ||
} | ||
|
||
ByteString oldDigest = bootJarsAndDigest.putIfAbsent(bootClassPath.toString(), currDigest); | ||
|
||
if (oldDigest != null && !oldDigest.equals(currDigest)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Checks if the bootClassPaths in {@code BlazeJavacArguments} can benefit from the {@code | ||
* BootCachingFileManager}. Arguments are not valid if missing boot classpath or at least one | ||
* digest | ||
*/ | ||
@VisibleForTesting | ||
static boolean areArgumentsValid(BlazeJavacArguments arguments) { | ||
if (arguments.bootClassPath().isEmpty()) { | ||
return false; | ||
} | ||
|
||
for (Path bootClassPath : arguments.bootClassPath()) { | ||
ByteString currDigest = arguments.inputsAndDigest().get(bootClassPath.toString()); | ||
if (currDigest == null) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
168 changes: 168 additions & 0 deletions
168
...ar/java/com/google/devtools/build/buildjar/javac/BootClassPathCachingFileManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// Copyright 2021 The Bazel Authors. All rights reserved. | ||
// | ||
// 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.devtools.build.buildjar.javac; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.protobuf.ByteString; | ||
import com.sun.tools.javac.util.Context; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** {@link BootClassPathCachingFileManager}Test */ | ||
@RunWith(JUnit4.class) | ||
public class BootClassPathCachingFileManagerTest { | ||
private ImmutableList<Path> bootClassPathsCandidates; | ||
private BootClassPathCachingFileManager bootFileManager; | ||
|
||
@Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
||
@Before | ||
public void createBootFileManager() throws IOException { | ||
bootClassPathsCandidates = | ||
ImmutableList.of( | ||
temporaryFolder.newFolder().toPath().resolve("BootClassPath0.jar"), | ||
temporaryFolder.newFolder().toPath().resolve("BootClassPath1.jar"), | ||
temporaryFolder.newFolder().toPath().resolve("BootClassPath2.jar")); | ||
|
||
ImmutableList<Path> bootClassPaths = | ||
ImmutableList.of(bootClassPathsCandidates.get(0), bootClassPathsCandidates.get(1)); | ||
|
||
ImmutableMap<String, ByteString> inputsAndDigest = | ||
ImmutableMap.<String, ByteString>builder() | ||
.put( | ||
bootClassPaths.get(0).toString(), | ||
ByteString.copyFromUtf8(bootClassPaths.get(0).toString())) | ||
.put( | ||
bootClassPaths.get(1).toString(), | ||
ByteString.copyFromUtf8(bootClassPaths.get(1).toString())) | ||
.build(); | ||
|
||
BlazeJavacArguments arguments = | ||
BlazeJavacArguments.builder() | ||
.bootClassPath(bootClassPaths) | ||
.inputsAndDigest(inputsAndDigest) | ||
.classOutput(temporaryFolder.newFolder().toPath().resolve("output_0.jar")) | ||
.build(); | ||
|
||
bootFileManager = new BootClassPathCachingFileManager(new Context(), arguments); | ||
} | ||
|
||
@Test | ||
public void testNeedsUpdate() throws IOException { | ||
ImmutableList<Path> bootClassPaths = | ||
ImmutableList.of( | ||
bootClassPathsCandidates.get(0), | ||
bootClassPathsCandidates.get(1), | ||
bootClassPathsCandidates.get(2)); | ||
|
||
ImmutableMap<String, ByteString> inputsAndDigest = | ||
ImmutableMap.<String, ByteString>builder() | ||
.put( | ||
bootClassPaths.get(0).toString(), | ||
ByteString.copyFromUtf8(bootClassPaths.get(0).toString())) | ||
.put( | ||
bootClassPaths.get(1).toString(), | ||
ByteString.copyFromUtf8(bootClassPaths.get(1).toString())) | ||
.put( | ||
bootClassPaths.get(2).toString(), | ||
ByteString.copyFromUtf8(bootClassPaths.get(2).toString())) | ||
.build(); | ||
|
||
BlazeJavacArguments arguments = | ||
BlazeJavacArguments.builder() | ||
.bootClassPath(bootClassPaths) | ||
.inputsAndDigest(inputsAndDigest) | ||
.classOutput(temporaryFolder.newFolder().toPath().resolve("output_1.jar")) | ||
.build(); | ||
|
||
assertThat(bootFileManager.needsUpdate(arguments)).isFalse(); | ||
} | ||
|
||
@Test | ||
public void testNeedsUpdate_withDifferentDigest() throws IOException { | ||
ImmutableList<Path> bootClassPaths = | ||
ImmutableList.of(bootClassPathsCandidates.get(0), bootClassPathsCandidates.get(1)); | ||
|
||
ImmutableMap<String, ByteString> inputsAndDigest = | ||
ImmutableMap.<String, ByteString>builder() | ||
.put(bootClassPaths.get(0).toString(), ByteString.copyFromUtf8("different digest")) | ||
.put( | ||
bootClassPaths.get(1).toString(), | ||
ByteString.copyFromUtf8(bootClassPaths.get(1).toString())) | ||
.build(); | ||
|
||
BlazeJavacArguments arguments = | ||
BlazeJavacArguments.builder() | ||
.bootClassPath(bootClassPaths) | ||
.inputsAndDigest(inputsAndDigest) | ||
.classOutput(temporaryFolder.newFolder().toPath().resolve("output_1.jar")) | ||
.build(); | ||
|
||
assertThat(bootFileManager.needsUpdate(arguments)).isTrue(); | ||
} | ||
|
||
@Test | ||
public void testAreArgumentsValid_withOneMissingDigest() throws IOException { | ||
ImmutableList<Path> bootClassPaths = | ||
ImmutableList.of(bootClassPathsCandidates.get(0), bootClassPathsCandidates.get(1)); | ||
|
||
ImmutableMap<String, ByteString> inputsAndDigest = | ||
ImmutableMap.of( | ||
bootClassPaths.get(1).toString(), | ||
ByteString.copyFromUtf8(bootClassPaths.get(1).toString())); | ||
|
||
BlazeJavacArguments arguments = | ||
BlazeJavacArguments.builder() | ||
.bootClassPath(bootClassPaths) | ||
.inputsAndDigest(inputsAndDigest) | ||
.classOutput(temporaryFolder.newFolder().toPath().resolve("output_1.jar")) | ||
.build(); | ||
|
||
assertThat(BootClassPathCachingFileManager.areArgumentsValid(arguments)).isFalse(); | ||
} | ||
|
||
@Test | ||
public void testAreArgumentsValid_withEmptyInputsAndDigest() throws IOException { | ||
ImmutableList<Path> bootClassPaths = | ||
ImmutableList.of(bootClassPathsCandidates.get(0), bootClassPathsCandidates.get(1)); | ||
|
||
BlazeJavacArguments arguments = | ||
BlazeJavacArguments.builder() | ||
.bootClassPath(bootClassPaths) | ||
.classOutput(temporaryFolder.newFolder().toPath().resolve("output_1.jar")) | ||
.build(); | ||
|
||
assertThat(BootClassPathCachingFileManager.areArgumentsValid(arguments)).isFalse(); | ||
} | ||
|
||
@Test | ||
public void testAreArgumentsValid_withEmptyBootClassPaths() throws IOException { | ||
assertThat( | ||
BootClassPathCachingFileManager.areArgumentsValid( | ||
BlazeJavacArguments.builder() | ||
.classOutput(temporaryFolder.newFolder().toPath().resolve("output_1.jar")) | ||
.build())) | ||
.isFalse(); | ||
} | ||
} |