Skip to content

Commit

Permalink
Send and receive work requests via proxy and multiplexer
Browse files Browse the repository at this point in the history
For each unique WorkerKey, Bazel can launch a multiplexer to talk to one multi-threaded worker process optionally. We use less JVM processes but maintain the approximately same performance, hence, save more memory. The worker process should be able to handle multiple requests to fully utilize this feature.

Fix: bazelbuild#2832
  • Loading branch information
borkaehw committed Aug 6, 2019
1 parent 1967b90 commit d42aee9
Show file tree
Hide file tree
Showing 11 changed files with 474 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ public String parseIfMatches(String tag) throws ValidationException {
/** If an action supports running in persistent worker mode. */
public static final String SUPPORTS_WORKERS = "supports-workers";

public static final String SUPPORTS_MULTIPLEX_WORKERS = "supports-multiplex-workers";

public static final ImmutableMap<String, String> WORKER_MODE_ENABLED =
ImmutableMap.of(SUPPORTS_WORKERS, "1");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public static boolean supportsWorkers(Spawn spawn) {
return "1".equals(spawn.getExecutionInfo().get(ExecutionRequirements.SUPPORTS_WORKERS));
}

/**
* Returns whether a Spawn claims to support being executed with the persistent multiplex worker strategy
* according to its execution info tags.
*/
public static boolean supportsMultiplexWorkers(Spawn spawn) {
return "1".equals(spawn.getExecutionInfo().get(ExecutionRequirements.SUPPORTS_MULTIPLEX_WORKERS));
}

/**
* Parse the timeout key in the spawn execution info, if it exists. Otherwise, return -1.
*/
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/com/google/devtools/build/lib/worker/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,22 @@
* class.
*/
class Worker {
private final WorkerKey workerKey;
private final int workerId;
private final Path workDir;
private final Path logFile;
/**
* An unique identifier of the work process.
*/
protected final WorkerKey workerKey;
/**
* An unique ID of the worker. It will be used in WorkRequest and WorkResponse as well.
*/
protected final int workerId;
/**
* The execution root of the worker.
*/
protected final Path workDir;
/**
* The path of the log file.
*/
protected final Path logFile;

private Subprocess process;
private Thread shutdownHook;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public Worker create(WorkerKey key) throws Exception {
if (sandboxed) {
Path workDir = getSandboxedWorkerPath(key, workerId);
worker = new SandboxedWorker(key, workerId, workDir, logFile);
} else if (key.proxied()) {
worker = new WorkerProxy(key, workerId, key.getExecRoot(), logFile);
} else {
worker = new Worker(key, workerId, key.getExecRoot(), logFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ final class WorkerKey {
private final HashCode workerFilesCombinedHash;
private final SortedMap<PathFragment, HashCode> workerFilesWithHashes;
private final boolean mustBeSandboxed;
/**
* A WorkerProxy will be instantiated if true, instantiate a regular Worker if false.
*/
private final boolean proxied;

WorkerKey(
List<String> args,
Expand All @@ -50,14 +54,24 @@ final class WorkerKey {
String mnemonic,
HashCode workerFilesCombinedHash,
SortedMap<PathFragment, HashCode> workerFilesWithHashes,
boolean mustBeSandboxed) {
boolean mustBeSandboxed,
boolean proxied) {
/** Build options. */
this.args = ImmutableList.copyOf(Preconditions.checkNotNull(args));
/** Environment variables. */
this.env = ImmutableMap.copyOf(Preconditions.checkNotNull(env));
/** Execution root of Bazel process. */
this.execRoot = Preconditions.checkNotNull(execRoot);
/** Mnemonic of the worker. */
this.mnemonic = Preconditions.checkNotNull(mnemonic);
/** One combined hash code for all files. */
this.workerFilesCombinedHash = Preconditions.checkNotNull(workerFilesCombinedHash);
/** Worker files with the corresponding hash code. */
this.workerFilesWithHashes = Preconditions.checkNotNull(workerFilesWithHashes);
/** Set it to true if this job should be run in sandbox. */
this.mustBeSandboxed = mustBeSandboxed;
/** Set it to true if this job should be run with WorkerProxy. */
this.proxied = proxied;
}

public ImmutableList<String> getArgs() {
Expand Down Expand Up @@ -88,6 +102,10 @@ public boolean mustBeSandboxed() {
return mustBeSandboxed;
}

public boolean proxied() {
return proxied;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
// Copyright 2018 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.shell.Subprocess;
import com.google.devtools.build.lib.shell.SubprocessBuilder;
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse;
import com.google.devtools.build.lib.vfs.Path;
import java.io.File;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;

/** An intermediate worker that sends request and receives response from the worker processes. */
public class WorkerMultiplexer extends Thread {
/**
* WorkerMultiplexer is running as a thread on its own. When worker process
* returns the WorkResponse, it is stored in this map and wait for
* WorkerProxy to retrieve the response.
*/
private Map<Integer, InputStream> workerProcessResponse;
/**
* A semaphore to protect workerProcessResponse object.
*/
private Semaphore semWorkerProcessResponse;
/**
* After sending the WorkRequest, WorkerProxy will wait on a semaphore to be
* released. WorkerMultiplexer is responsible to release the corresponding
* semaphore in order to signal WorkerProxy that the WorkerResponse has been
* received.
*/
private Map<Integer, Semaphore> responseChecker;
/**
* A semaphore to protect responseChecker object.
*/
private Semaphore semResponseChecker;
/**
* The worker process that this WorkerMultiplexer should be talking to.
*/
private Subprocess process;

private Thread shutdownHook;
private Integer workerHash;

WorkerMultiplexer(Integer workerHash) {
semWorkerProcessResponse = new Semaphore(1);
semResponseChecker = new Semaphore(1);
responseChecker = new HashMap<>();
workerProcessResponse = new HashMap<>();
this.workerHash = workerHash;

final WorkerMultiplexer self = this;
this.shutdownHook =
new Thread(
() -> {
try {
self.shutdownHook = null;
self.destroyMultiplexer();
} finally {
// We can't do anything here.
}
});
Runtime.getRuntime().addShutdownHook(shutdownHook);
}

/**
* Only start one worker process for each WorkerMultiplexer, if it hasn't.
*/
public synchronized void createProcess(WorkerKey workerKey, Path workDir, Path logFile) throws IOException {
if (this.process == null) {
List<String> args = workerKey.getArgs();
File executable = new File(args.get(0));
if (!executable.isAbsolute() && executable.getParent() != null) {
args = new ArrayList<>(args);
args.set(0, new File(workDir.getPathFile(), args.get(0)).getAbsolutePath());
}
SubprocessBuilder processBuilder = new SubprocessBuilder();
processBuilder.setArgv(args);
processBuilder.setWorkingDirectory(workDir.getPathFile());
processBuilder.setStderr(logFile.getPathFile());
processBuilder.setEnv(workerKey.getEnv());
this.process = processBuilder.start();
}
if (!this.isAlive()) {
this.start();
}
}

public synchronized void destroyMultiplexer() {
if (shutdownHook != null) {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}
if (this.process != null) {
destroyProcess(this.process);
}
}

private void destroyProcess(Subprocess process) {
boolean wasInterrupted = false;
try {
process.destroy();
while (true) {
try {
process.waitFor();
return;
} catch (InterruptedException ie) {
wasInterrupted = true;
}
}
} finally {
// Read this for detailed explanation: http://www.ibm.com/developerworks/library/j-jtp05236/
if (wasInterrupted) {
Thread.currentThread().interrupt(); // preserve interrupted status
}
}
}

public boolean isProcessAlive() {
return !this.process.finished();
}

/**
* Pass the WorkRequest to worker process.
*/
public synchronized void putRequest(byte[] request) throws IOException {
OutputStream stdin = process.getOutputStream();
stdin.write(request);
stdin.flush();
}

/**
* A WorkerProxy waits on a semaphore for the WorkResponse returned from worker process.
*/
public InputStream getResponse(Integer workerId) throws InterruptedException {
semResponseChecker.acquire();
Semaphore waitForResponse = responseChecker.get(workerId);
semResponseChecker.release();

// If there is a compilation error, the semaphore will throw InterruptedException.
waitForResponse.acquire();

semWorkerProcessResponse.acquire();
InputStream response = workerProcessResponse.get(workerId);
semWorkerProcessResponse.release();
return response;
}

/**
* Reset the map that indicates if the WorkResponses have been returned.
*/
public void resetResponseChecker(Integer workerId) throws InterruptedException {
semResponseChecker.acquire();
responseChecker.put(workerId, new Semaphore(0));
semResponseChecker.release();
}

/**
* When it gets a WorkResponse from worker process, put that WorkResponse in
* workerProcessResponse and signal responseChecker.
*/
private void waitResponse() throws InterruptedException, IOException {
InputStream stdout = process.getInputStream();
WorkResponse parsedResponse = WorkResponse.parseDelimitedFrom(stdout);

if (parsedResponse == null) return;

Integer workerId = parsedResponse.getRequestId();
ByteArrayOutputStream tempOs = new ByteArrayOutputStream();
parsedResponse.writeDelimitedTo(tempOs);

semWorkerProcessResponse.acquire();
workerProcessResponse.put(workerId, new ByteArrayInputStream(tempOs.toByteArray()));
semWorkerProcessResponse.release();

semResponseChecker.acquire();
responseChecker.get(workerId).release();
semResponseChecker.release();
}

/**
* A multiplexer thread that listens to the WorkResponses from worker process.
*/
public void run() {
while (!this.interrupted()) {
try {
waitResponse();
} catch (Exception e) {
// We can't do anything here.
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2018 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 java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Semaphore;

/**
* An intermediate worker that sends request and receives response from the
* worker processes.
*/
public class WorkerMultiplexerManager {
/**
* There should only be one WorkerMultiplexer corresponding to workers with
* the same mnemonic. If the WorkerMultiplexer has been constructed, other
* workers should point to the same one. The hash of WorkerKey is used as
* key.
*/
private static Map<Integer, WorkerMultiplexer> multiplexerInstance = new HashMap<>();
/**
* An accumulator of how many WorkerProxies are referencing a particular
* WorkerMultiplexer.
*/
private static Map<Integer, Integer> multiplexerRefCount = new HashMap<>();
/**
* A semaphore to protect multiplexerInstance and multiplexerRefCount objects.
*/
private static Semaphore semMultiplexer = new Semaphore(1);

/**
* Returns a WorkerMultiplexer instance to WorkerProxy. WorkerProxies with the
* same workerHash talk to the same WorkerMultiplexer. Also, record how many
* WorkerProxies are talking to this WorkerMultiplexer.
*/
public static WorkerMultiplexer getInstance(Integer workerHash) throws InterruptedException {
semMultiplexer.acquire();
if (!multiplexerInstance.containsKey(workerHash)) {
multiplexerInstance.put(workerHash, new WorkerMultiplexer(workerHash));
multiplexerRefCount.put(workerHash, 0);
}
multiplexerRefCount.put(workerHash, multiplexerRefCount.get(workerHash) + 1);
WorkerMultiplexer workerMultiplexer = multiplexerInstance.get(workerHash);
semMultiplexer.release();
return workerMultiplexer;
}

/**
* Remove the WorkerMultiplexer instance and reference count since it is no
* longer in use.
*/
public static void removeInstance(Integer workerHash) throws InterruptedException {
semMultiplexer.acquire();
multiplexerRefCount.put(workerHash, multiplexerRefCount.get(workerHash) - 1);
if (multiplexerRefCount.get(workerHash) == 0) {
multiplexerInstance.get(workerHash).interrupt();
multiplexerInstance.get(workerHash).destroyMultiplexer();
multiplexerInstance.remove(workerHash);
multiplexerRefCount.remove(workerHash);
}
semMultiplexer.release();
}
}
Loading

0 comments on commit d42aee9

Please sign in to comment.