diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/BazelJavaBuilder.java b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/BazelJavaBuilder.java index f1b12e62d19e7f..6939bfc3539e54 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/BazelJavaBuilder.java +++ b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/BazelJavaBuilder.java @@ -25,6 +25,7 @@ import com.google.devtools.build.buildjar.javac.plugins.errorprone.ErrorPronePlugin; import com.google.devtools.build.lib.worker.ProtoWorkerMessageProcessor; import com.google.devtools.build.lib.worker.WorkRequestHandler; +import com.google.devtools.build.lib.worker.WorkRequestHandler.WorkRequestHandlerBuilder; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; @@ -44,11 +45,12 @@ public static void main(String[] args) { BazelJavaBuilder builder = new BazelJavaBuilder(); if (args.length == 1 && args[0].equals("--persistent_worker")) { WorkRequestHandler workerHandler = - new WorkRequestHandler( - builder::parseAndBuild, - System.err, - new ProtoWorkerMessageProcessor(System.in, System.out), - Duration.ofSeconds(10)); + new WorkRequestHandlerBuilder( + builder::parseAndBuild, + System.err, + new ProtoWorkerMessageProcessor(System.in, System.out)) + .setCpuUsageBeforeGc(Duration.ofSeconds(10)) + .build(); try { workerHandler.processRequests(); } catch (IOException e) { diff --git a/src/main/java/com/google/devtools/build/lib/worker/WorkRequestHandler.java b/src/main/java/com/google/devtools/build/lib/worker/WorkRequestHandler.java index 7da37742d48a15..9f6e3c62516c06 100644 --- a/src/main/java/com/google/devtools/build/lib/worker/WorkRequestHandler.java +++ b/src/main/java/com/google/devtools/build/lib/worker/WorkRequestHandler.java @@ -91,8 +91,10 @@ public WorkRequestHandler( * @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server and * writing {@code WorkResponses} to the server. * @param cpuUsageBeforeGc The minimum amount of CPU time between explicit garbage collection - * calls. + * calls. Pass Duration.ZERO to not do explicit garbage collection. + * @deprecated Use WorkRequestHandlerBuilder instead. */ + @Deprecated() public WorkRequestHandler( BiFunction, PrintWriter, Integer> callback, PrintStream stderr, @@ -104,6 +106,48 @@ public WorkRequestHandler( this.gcScheduler = new CpuTimeBasedGcScheduler(cpuUsageBeforeGc); } + /** Builder class for WorkRequestHandler. Required parameters are passed to the constructor. */ + public static class WorkRequestHandlerBuilder { + private final BiFunction, PrintWriter, Integer> callback; + private final PrintStream stderr; + private final WorkerMessageProcessor messageProcessor; + private Duration cpuUsageBeforeGc = Duration.ZERO; + + /** + * Creates a {@code WorkRequestHandlerBuilder}. + * + * @param callback Callback method for executing a single WorkRequest in a thread. The first + * argument to {@code callback} is the set of command-line arguments, the second is where + * all error messages and other user-oriented messages should be written to. The callback + * must return an exit code indicating success (zero) or failure (nonzero). + * @param stderr Stream that log messages should be written to, typically the process' stderr. + * @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server + * and writing {@code WorkResponses} to the server. + */ + public WorkRequestHandlerBuilder( + BiFunction, PrintWriter, Integer> callback, + PrintStream stderr, + WorkerMessageProcessor messageProcessor) { + this.callback = callback; + this.stderr = stderr; + this.messageProcessor = messageProcessor; + } + + /** + * Sets the minimum amount of CPU time between explicit garbage collection calls. Pass + * Duration.ZERO to not do explicit garbage collection (the default). + */ + public WorkRequestHandlerBuilder setCpuUsageBeforeGc(Duration cpuUsageBeforeGc) { + this.cpuUsageBeforeGc = cpuUsageBeforeGc; + return this; + } + + /** Returns a WorkRequestHandler instance with the values in this Builder. */ + public WorkRequestHandler build() { + return new WorkRequestHandler(callback, stderr, messageProcessor, cpuUsageBeforeGc); + } + } + /** * Runs an infinite loop of reading {@link WorkRequest} from {@code in}, running the callback, * then writing the corresponding {@link WorkResponse} to {@code out}. If there is an error