Skip to content

Commit 3835d9b

Browse files
Googlercopybara-github
Googler
authored andcommitted
Update the WorkRequestHandler to use callbacks of type: BiFunction<WorkRequest, PrintWriter, Integer>:
- Mark constructors that use BiFunction<List<String>, PrintWriter, Integer> callback as deprecated. - Use a wrapper class for the BiFunction<WorkRequest, PrintWriter, Integer>. Suggesting this to avoid having two constructors that takes a BiFunction, as it creates a confusion between the deprecated and new constructor when given a lambda expressions. RELNOTES: None. PiperOrigin-RevId: 384643302
1 parent 958316f commit 3835d9b

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

src/main/java/com/google/devtools/build/lib/worker/WorkRequestHandler.java

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ synchronized void addOutput(String s) {
106106
final ConcurrentMap<Integer, RequestInfo> activeRequests = new ConcurrentHashMap<>();
107107

108108
/** The function to be called after each {@link WorkRequest} is read. */
109-
private final BiFunction<List<String>, PrintWriter, Integer> callback;
109+
private final WorkRequestCallback callback;
110110

111111
/** This worker's stderr. */
112112
private final PrintStream stderr;
@@ -129,6 +129,7 @@ synchronized void addOutput(String s) {
129129
* @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server and
130130
* writing {@code WorkResponses} to the server.
131131
*/
132+
@Deprecated
132133
public WorkRequestHandler(
133134
BiFunction<List<String>, PrintWriter, Integer> callback,
134135
PrintStream stderr,
@@ -163,23 +164,69 @@ public WorkRequestHandler(
163164
/**
164165
* Creates a {@code WorkRequestHandler} that will call {@code callback} for each WorkRequest
165166
* received. Only used for the Builder.
167+
*
168+
* @deprecated Use WorkRequestHandlerBuilder instead.
166169
*/
170+
@Deprecated
167171
private WorkRequestHandler(
168172
BiFunction<List<String>, PrintWriter, Integer> callback,
169173
PrintStream stderr,
170174
WorkerMessageProcessor messageProcessor,
171175
Duration cpuUsageBeforeGc,
172176
BiConsumer<Integer, Thread> cancelCallback) {
177+
this(
178+
new WorkRequestCallback((request, pw) -> callback.apply(request.getArgumentsList(), pw)),
179+
stderr,
180+
messageProcessor,
181+
cpuUsageBeforeGc,
182+
cancelCallback);
183+
}
184+
185+
/**
186+
* Creates a {@code WorkRequestHandler} that will call {@code callback} for each WorkRequest
187+
* received. Only used for the Builder.
188+
*
189+
* @param callback WorkRequestCallback object with Callback method for executing a single
190+
* WorkRequest in a thread. The first argument to {@code callback} is the WorkRequest, the
191+
* second is where all error messages and other user-oriented messages should be written to.
192+
* The callback must return an exit code indicating success (zero) or failure (nonzero).
193+
*/
194+
private WorkRequestHandler(
195+
WorkRequestCallback callback,
196+
PrintStream stderr,
197+
WorkerMessageProcessor messageProcessor,
198+
Duration cpuUsageBeforeGc,
199+
BiConsumer<Integer, Thread> cancelCallback) {
173200
this.callback = callback;
174201
this.stderr = stderr;
175202
this.messageProcessor = messageProcessor;
176203
this.gcScheduler = new CpuTimeBasedGcScheduler(cpuUsageBeforeGc);
177204
this.cancelCallback = cancelCallback;
178205
}
179206

207+
/** A wrapper class for the callback BiFunction */
208+
public static class WorkRequestCallback {
209+
210+
/**
211+
* Callback method for executing a single WorkRequest in a thread. The first argument to {@code
212+
* callback} is the WorkRequest, the second is where all error messages and other user-oriented
213+
* messages should be written to. The callback must return an exit code indicating success
214+
* (zero) or failure (nonzero).
215+
*/
216+
private final BiFunction<WorkRequest, PrintWriter, Integer> callback;
217+
218+
public WorkRequestCallback(BiFunction<WorkRequest, PrintWriter, Integer> callback) {
219+
this.callback = callback;
220+
}
221+
222+
public Integer apply(WorkRequest workRequest, PrintWriter printWriter) {
223+
return callback.apply(workRequest, printWriter);
224+
}
225+
}
226+
180227
/** Builder class for WorkRequestHandler. Required parameters are passed to the constructor. */
181228
public static class WorkRequestHandlerBuilder {
182-
private final BiFunction<List<String>, PrintWriter, Integer> callback;
229+
private final WorkRequestCallback callback;
183230
private final PrintStream stderr;
184231
private final WorkerMessageProcessor messageProcessor;
185232
private Duration cpuUsageBeforeGc = Duration.ZERO;
@@ -195,11 +242,32 @@ public static class WorkRequestHandlerBuilder {
195242
* @param stderr Stream that log messages should be written to, typically the process' stderr.
196243
* @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server
197244
* and writing {@code WorkResponses} to the server.
245+
* @deprecated use WorkRequestHandlerBuilder with WorkRequestCallback instead
198246
*/
247+
@Deprecated
199248
public WorkRequestHandlerBuilder(
200249
BiFunction<List<String>, PrintWriter, Integer> callback,
201250
PrintStream stderr,
202251
WorkerMessageProcessor messageProcessor) {
252+
this(
253+
new WorkRequestCallback((request, pw) -> callback.apply(request.getArgumentsList(), pw)),
254+
stderr,
255+
messageProcessor);
256+
}
257+
258+
/**
259+
* Creates a {@code WorkRequestHandlerBuilder}.
260+
*
261+
* @param callback WorkRequestCallback object with Callback method for executing a single
262+
* WorkRequest in a thread. The first argument to {@code callback} is the WorkRequest, the
263+
* second is where all error messages and other user-oriented messages should be written to.
264+
* The callback must return an exit code indicating success (zero) or failure (nonzero).
265+
* @param stderr Stream that log messages should be written to, typically the process' stderr.
266+
* @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server
267+
* and writing {@code WorkResponses} to the server.
268+
*/
269+
public WorkRequestHandlerBuilder(
270+
WorkRequestCallback callback, PrintStream stderr, WorkerMessageProcessor messageProcessor) {
203271
this.callback = callback;
204272
this.stderr = stderr;
205273
this.messageProcessor = messageProcessor;
@@ -287,7 +355,7 @@ void respondToRequest(WorkRequest request, RequestInfo requestInfo) throws IOExc
287355
PrintWriter pw = new PrintWriter(sw)) {
288356
int exitCode;
289357
try {
290-
exitCode = callback.apply(request.getArgumentsList(), pw);
358+
exitCode = callback.apply(request, pw);
291359
} catch (RuntimeException e) {
292360
e.printStackTrace(pw);
293361
exitCode = 1;

src/test/java/com/google/devtools/build/lib/worker/WorkRequestHandlerTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static com.google.common.truth.Truth.assertThat;
1818

1919
import com.google.devtools.build.lib.worker.WorkRequestHandler.RequestInfo;
20+
import com.google.devtools.build.lib.worker.WorkRequestHandler.WorkRequestCallback;
2021
import com.google.devtools.build.lib.worker.WorkRequestHandler.WorkRequestHandlerBuilder;
2122
import com.google.devtools.build.lib.worker.WorkRequestHandler.WorkerMessageProcessor;
2223
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest;
@@ -407,4 +408,27 @@ public void close() throws IOException {
407408
delegate.close();
408409
}
409410
}
411+
412+
@Test
413+
public void testWorkRequestHandler_withWorkRequestCallback() throws IOException {
414+
ByteArrayOutputStream out = new ByteArrayOutputStream();
415+
WorkRequestCallback callback =
416+
new WorkRequestCallback((request, err) -> request.getArgumentsCount());
417+
WorkRequestHandler handler =
418+
new WorkRequestHandlerBuilder(
419+
callback,
420+
new PrintStream(new ByteArrayOutputStream()),
421+
new ProtoWorkerMessageProcessor(new ByteArrayInputStream(new byte[0]), out))
422+
.build();
423+
424+
List<String> args = Arrays.asList("--sources", "B.java");
425+
WorkRequest request = WorkRequest.newBuilder().addAllArguments(args).build();
426+
handler.respondToRequest(request, new RequestInfo(null));
427+
428+
WorkResponse response =
429+
WorkResponse.parseDelimitedFrom(new ByteArrayInputStream(out.toByteArray()));
430+
assertThat(response.getRequestId()).isEqualTo(0);
431+
assertThat(response.getExitCode()).isEqualTo(2);
432+
assertThat(response.getOutput()).isEmpty();
433+
}
410434
}

0 commit comments

Comments
 (0)