forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send and receive work requests via proxy and multiplexer
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
Showing
11 changed files
with
474 additions
and
21 deletions.
There are no files selected for viewing
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
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
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
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
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
211 changes: 211 additions & 0 deletions
211
src/main/java/com/google/devtools/build/lib/worker/WorkerMultiplexer.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,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. | ||
} | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/com/google/devtools/build/lib/worker/WorkerMultiplexerManager.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,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(); | ||
} | ||
} |
Oops, something went wrong.