Skip to content

Commit c4e22b9

Browse files
susinmotionlarsrc-google
authored andcommitted
Migrate ExampleWorker to use WorkRequestHandler.
This means that we're using a real worker implementation in our example and integration tests. It also includes a JSON WorkRequestHandler, but it isn't used except in the tests. RELNOTES: None. PiperOrigin-RevId: 345765354
1 parent 35c98d0 commit c4e22b9

File tree

6 files changed

+164
-178
lines changed

6 files changed

+164
-178
lines changed

src/main/java/com/google/devtools/build/lib/worker/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,15 @@ java_library(
5151
java_library(
5252
name = "work_request_handlers",
5353
srcs = [
54+
"JsonWorkerMessageProcessor.java",
5455
"ProtoWorkerMessageProcessor.java",
5556
"WorkRequestHandler.java",
5657
],
5758
deps = [
5859
"//src/main/protobuf:worker_protocol_java_proto",
60+
"//third_party:gson",
5961
"//third_party:guava",
62+
"//third_party/protobuf:protobuf_java",
63+
"//third_party/protobuf:protobuf_java_util",
6064
],
6165
)

src/test/java/com/google/devtools/build/lib/worker/JsonExampleWorkerProtocolImpl.java renamed to src/main/java/com/google/devtools/build/lib/worker/JsonWorkerMessageProcessor.java

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
// limitations under the License.
1414
package com.google.devtools.build.lib.worker;
1515

16-
import static java.nio.charset.StandardCharsets.UTF_8;
17-
16+
import com.google.common.collect.ImmutableList;
1817
import com.google.devtools.build.lib.worker.WorkerProtocol.Input;
1918
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest;
2019
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse;
@@ -23,43 +22,42 @@
2322
import com.google.protobuf.ByteString;
2423
import com.google.protobuf.util.JsonFormat;
2524
import com.google.protobuf.util.JsonFormat.Printer;
26-
import java.io.BufferedReader;
2725
import java.io.BufferedWriter;
2826
import java.io.EOFException;
2927
import java.io.IOException;
30-
import java.io.InputStream;
31-
import java.io.InputStreamReader;
32-
import java.io.OutputStream;
33-
import java.io.OutputStreamWriter;
34-
import java.util.ArrayList;
3528
import java.util.List;
3629

37-
/** Sample implementation of the Worker Protocol using JSON to communicate with Bazel. */
38-
public class JsonExampleWorkerProtocolImpl implements ExampleWorkerProtocol {
39-
private final Printer jsonPrinter =
40-
JsonFormat.printer().omittingInsignificantWhitespace().includingDefaultValueFields();
30+
/** Implementation of the Worker Protocol using JSON to communicate with Bazel. */
31+
public final class JsonWorkerMessageProcessor implements WorkRequestHandler.WorkerMessageProcessor {
32+
/** Reader for reading the WorkResponse. */
4133
private final JsonReader reader;
34+
/** Printer for printing the WorkRequest. */
35+
private final Printer jsonPrinter;
36+
/** Writer for writing the WorkRequest to the worker. */
4237
private final BufferedWriter jsonWriter;
4338

44-
public JsonExampleWorkerProtocolImpl(InputStream stdin, OutputStream stdout) {
45-
reader = new JsonReader(new BufferedReader(new InputStreamReader(stdin, UTF_8)));
39+
/** Constructs a {@code WorkRequestHandler} that reads and writes JSON. */
40+
public JsonWorkerMessageProcessor(JsonReader reader, BufferedWriter jsonWriter) {
41+
this.reader = reader;
4642
reader.setLenient(true);
47-
jsonWriter = new BufferedWriter(new OutputStreamWriter(stdout, UTF_8));
43+
this.jsonWriter = jsonWriter;
44+
jsonPrinter =
45+
JsonFormat.printer().omittingInsignificantWhitespace().includingDefaultValueFields();
4846
}
4947

50-
private static ArrayList<String> readArguments(JsonReader reader) throws IOException {
48+
private static ImmutableList<String> readArguments(JsonReader reader) throws IOException {
5149
reader.beginArray();
52-
ArrayList<String> arguments = new ArrayList<>();
50+
ImmutableList.Builder<String> argumentsBuilder = ImmutableList.builder();
5351
while (reader.hasNext()) {
54-
arguments.add(reader.nextString());
52+
argumentsBuilder.add(reader.nextString());
5553
}
5654
reader.endArray();
57-
return arguments;
55+
return argumentsBuilder.build();
5856
}
5957

60-
private static ArrayList<Input> readInputs(JsonReader reader) throws IOException {
58+
private static ImmutableList<Input> readInputs(JsonReader reader) throws IOException {
6159
reader.beginArray();
62-
ArrayList<Input> inputs = new ArrayList<>();
60+
ImmutableList.Builder<Input> inputsBuilder = ImmutableList.builder();
6361
while (reader.hasNext()) {
6462
String digest = null;
6563
String path = null;
@@ -81,19 +79,25 @@ private static ArrayList<Input> readInputs(JsonReader reader) throws IOException
8179
path = reader.nextString();
8280
break;
8381
default:
84-
throw new IOException(name + " is an incorrect field in input");
82+
continue;
8583
}
8684
}
8785
reader.endObject();
88-
inputs.add(
89-
Input.newBuilder().setDigest(ByteString.copyFromUtf8(digest)).setPath(path).build());
86+
Input.Builder inputBuilder = Input.newBuilder();
87+
if (digest != null) {
88+
inputBuilder.setDigest(ByteString.copyFromUtf8(digest));
89+
}
90+
if (path != null) {
91+
inputBuilder.setPath(path);
92+
}
93+
inputsBuilder.add(inputBuilder.build());
9094
}
9195
reader.endArray();
92-
return inputs;
96+
return inputsBuilder.build();
9397
}
9498

9599
@Override
96-
public WorkRequest readRequest() throws IOException {
100+
public WorkRequest readWorkRequest() throws IOException {
97101
List<String> arguments = null;
98102
List<Input> inputs = null;
99103
Integer requestId = null;
@@ -104,24 +108,24 @@ public WorkRequest readRequest() throws IOException {
104108
switch (name) {
105109
case "arguments":
106110
if (arguments != null) {
107-
throw new IOException("Work request cannot have more than one list of arguments");
111+
throw new IOException("WorkRequest cannot have more than one 'arguments' field");
108112
}
109113
arguments = readArguments(reader);
110114
break;
111115
case "inputs":
112116
if (inputs != null) {
113-
throw new IOException("Work request cannot have more than one list of inputs");
117+
throw new IOException("WorkRequest cannot have more than one 'inputs' field");
114118
}
115119
inputs = readInputs(reader);
116120
break;
117121
case "requestId":
118122
if (requestId != null) {
119-
throw new IOException("Work request cannot have more than one requestId");
123+
throw new IOException("WorkRequest cannot have more than one requestId");
120124
}
121125
requestId = reader.nextInt();
122126
break;
123127
default:
124-
throw new IOException(name + " is an incorrect field in work request");
128+
break;
125129
}
126130
}
127131
reader.endObject();
@@ -143,17 +147,13 @@ public WorkRequest readRequest() throws IOException {
143147
}
144148

145149
@Override
146-
public void writeResponse(WorkResponse response) throws IOException {
150+
public void writeWorkResponse(WorkResponse response) throws IOException {
147151
jsonPrinter.appendTo(response, jsonWriter);
148152
jsonWriter.flush();
149153
}
150154

151155
@Override
152-
public void close() {
153-
try {
154-
jsonWriter.close();
155-
} catch (IOException e) {
156-
System.err.printf("Could not close json writer. %s", e);
157-
}
156+
public void close() throws IOException {
157+
jsonWriter.close();
158158
}
159159
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public interface WorkerMessageProcessor {
5656
/** This worker's stderr. */
5757
private final PrintStream stderr;
5858

59-
private final WorkerMessageProcessor messageProcessor;
59+
final WorkerMessageProcessor messageProcessor;
6060

6161
private final CpuTimeBasedGcScheduler gcScheduler;
6262

src/test/java/com/google/devtools/build/lib/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ java_library(
605605
],
606606
deps = [
607607
"//src/main/java/com/google/devtools/build/lib/actions:execution_requirements",
608+
"//src/main/java/com/google/devtools/build/lib/worker:work_request_handlers",
608609
"//src/main/java/com/google/devtools/common/options",
609610
"//src/main/protobuf:worker_protocol_java_proto",
610611
"//third_party:gson",

0 commit comments

Comments
 (0)