Skip to content

Commit

Permalink
[worker] Move WorkerProtocolImpl into its own package
Browse files Browse the repository at this point in the history
Splitting it out of the main worker package makes re-using the code
for other implementations for dispatching requests to workers (e.g.,
for remote persistent workers, bazelbuild#10091) easier.
  • Loading branch information
Yannic committed May 1, 2022
1 parent 17cfa01 commit 1363b14
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/google/devtools/build/lib/worker/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ java_library(
],
),
deps = [
# ":worker_spawn_runner",
"//third_party:guava",
"//third_party/protobuf:protobuf_java",
"//third_party/protobuf:protobuf_java_util",
Expand All @@ -35,6 +34,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/util:resource_converter",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/com/google/devtools/build/lib/worker/runner",
"//src/main/java/com/google/devtools/common/options",
"//src/main/protobuf:failure_details_java_proto",
"//src/main/protobuf:worker_protocol_java_proto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest;
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse;
import com.google.devtools.build.lib.worker.runner.JsonWorkerProtocol;
import com.google.devtools.build.lib.worker.runner.ProtoWorkerProtocol;
import com.google.devtools.build.lib.worker.runner.WorkerProtocolImpl;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest;
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse;
import com.google.devtools.build.lib.worker.runner.JsonWorkerProtocol;
import com.google.devtools.build.lib.worker.runner.ProtoWorkerProtocol;
import com.google.devtools.build.lib.worker.runner.WorkerProtocolImpl;
import java.io.File;
import java.io.IOException;
import java.io.InterruptedIOException;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/google/devtools/build/lib/worker/runner/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@rules_java//java:defs.bzl", "java_library")

package(default_visibility = ["//src:__subpackages__"])

filegroup(
name = "srcs",
srcs = glob(["**"]),
visibility = ["//src:__subpackages__"],
)

java_library(
name = "runner",
srcs = glob(["*.java"]),
deps = [
"//third_party/protobuf:protobuf_java_util",
"//src/main/protobuf:worker_protocol_java_proto",
"//third_party:gson",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// 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;
package com.google.devtools.build.lib.worker.runner;

import static java.nio.charset.StandardCharsets.UTF_8;

Expand All @@ -31,15 +31,15 @@
import java.io.OutputStreamWriter;

/** An implementation of a Bazel worker using JSON to communicate with the worker process. */
final class JsonWorkerProtocol implements WorkerProtocolImpl {
public 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(OutputStream workersStdin, InputStream workersStdout) {
public JsonWorkerProtocol(OutputStream workersStdin, InputStream workersStdout) {
jsonPrinter = JsonFormat.printer().omittingInsignificantWhitespace();
jsonWriter = new BufferedWriter(new OutputStreamWriter(workersStdin, UTF_8));
reader = new JsonReader(new BufferedReader(new InputStreamReader(workersStdout, UTF_8)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// 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;
package com.google.devtools.build.lib.worker.runner;

import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest;
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse;
Expand All @@ -20,7 +20,7 @@
import java.io.OutputStream;

/** An implementation of a Bazel worker using Proto to communicate with the worker process. */
final class ProtoWorkerProtocol implements WorkerProtocolImpl {
public final class ProtoWorkerProtocol implements WorkerProtocolImpl {

/** The worker process's stdin, which we send requests to. */
private final OutputStream workersStdin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
// 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;
package com.google.devtools.build.lib.worker.runner;

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 {
public interface WorkerProtocolImpl extends Closeable {
/** Writes the provided work request to the worker. */
void putRequest(WorkRequest request) throws IOException;

Expand Down

0 comments on commit 1363b14

Please sign in to comment.