-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Worker code to more cleanly toggle between JSON and Proto wo…
…rker protocol. PiperOrigin-RevId: 331024018
- Loading branch information
1 parent
7c3d8c8
commit aa5592e
Showing
4 changed files
with
210 additions
and
118 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
src/main/java/com/google/devtools/build/lib/worker/JsonWorkerProtocol.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,107 @@ | ||
// Copyright 2020 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.lib.worker; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest; | ||
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.protobuf.util.JsonFormat; | ||
import com.google.protobuf.util.JsonFormat.Printer; | ||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.io.OutputStreamWriter; | ||
|
||
/** An implementation of a Bazel worker using JSON to communicate with the worker process. */ | ||
final class JsonWorkerProtocol implements WorkerProtocolImpl { | ||
/** Reader for reading the WorkResponse. */ | ||
private final JsonReader reader; | ||
/** Printer for printing the WorkRequest */ | ||
private final Printer jsonPrinter; | ||
/** Writer for writing the WorkRequest to the worker */ | ||
private final BufferedWriter jsonWriter; | ||
|
||
JsonWorkerProtocol(InputStream stdin, OutputStream stdout) { | ||
jsonPrinter = JsonFormat.printer().omittingInsignificantWhitespace(); | ||
jsonWriter = new BufferedWriter(new OutputStreamWriter(stdout, UTF_8)); | ||
reader = new JsonReader(new BufferedReader(new InputStreamReader(stdin, UTF_8))); | ||
reader.setLenient(true); | ||
} | ||
|
||
@Override | ||
public void putRequest(WorkRequest request) throws IOException { | ||
jsonPrinter.appendTo(request, jsonWriter); | ||
jsonWriter.flush(); | ||
} | ||
|
||
@Override | ||
public WorkResponse getResponse() throws IOException { | ||
Integer exitCode = null; | ||
String output = null; | ||
Integer requestId = null; | ||
|
||
reader.beginObject(); | ||
while (reader.hasNext()) { | ||
String name = reader.nextName(); | ||
switch (name) { | ||
case "exitCode": | ||
if (exitCode != null) { | ||
throw new IOException("Work response cannot have more than one exit code"); | ||
} | ||
exitCode = reader.nextInt(); | ||
break; | ||
case "output": | ||
if (output != null) { | ||
throw new IOException("Work response cannot have more than one output"); | ||
} | ||
output = reader.nextString(); | ||
break; | ||
case "requestId": | ||
if (requestId != null) { | ||
throw new IOException("Work response cannot have more than one requestId"); | ||
} | ||
requestId = reader.nextInt(); | ||
break; | ||
default: | ||
throw new IOException(name + " is an incorrect field in work response"); | ||
} | ||
} | ||
reader.endObject(); | ||
|
||
WorkResponse.Builder responseBuilder = WorkResponse.newBuilder(); | ||
|
||
if (exitCode != null) { | ||
responseBuilder.setExitCode(exitCode); | ||
} | ||
if (output != null) { | ||
responseBuilder.setOutput(output); | ||
} | ||
if (requestId != null) { | ||
responseBuilder.setRequestId(requestId); | ||
} | ||
|
||
return responseBuilder.build(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
reader.close(); | ||
jsonWriter.close(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/google/devtools/build/lib/worker/ProtoWorkerProtocol.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,49 @@ | ||
// Copyright 2020 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.lib.worker; | ||
|
||
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest; | ||
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
/** An implementation of a Bazel worker using Proto to communicate with the worker process. */ | ||
final class ProtoWorkerProtocol implements WorkerProtocolImpl { | ||
|
||
/** The worker process's stdin */ | ||
private final InputStream stdin; | ||
|
||
/** The worker process's stdout. */ | ||
private final OutputStream stdout; | ||
|
||
public ProtoWorkerProtocol(InputStream stdin, OutputStream stdout) { | ||
this.stdin = stdin; | ||
this.stdout = stdout; | ||
} | ||
|
||
@Override | ||
public void putRequest(WorkRequest request) throws IOException { | ||
request.writeDelimitedTo(stdout); | ||
stdout.flush(); | ||
} | ||
|
||
@Override | ||
public WorkResponse getResponse() throws IOException { | ||
return WorkResponse.parseDelimitedFrom(stdin); | ||
} | ||
|
||
@Override | ||
public void close() {} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
src/main/java/com/google/devtools/build/lib/worker/WorkerProtocolImpl.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,28 @@ | ||
// Copyright 2020 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.lib.worker; | ||
|
||
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest; | ||
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse; | ||
import java.io.Closeable; | ||
import java.io.IOException; | ||
|
||
/** Represents the communication between Bazel and a persistent worker. */ | ||
interface WorkerProtocolImpl extends Closeable { | ||
/** Writes the provided work request to the worker. */ | ||
void putRequest(WorkRequest request) throws IOException; | ||
|
||
/** Reads a response from the worker. */ | ||
WorkResponse getResponse() throws IOException; | ||
} |