Skip to content

Commit

Permalink
Merge pull request LesnyRumcajs#94 from brunoborges/configurable-java…
Browse files Browse the repository at this point in the history
…-executor

Make the Java GRPC Server Executor Configurable
  • Loading branch information
LesnyRumcajs authored Aug 14, 2020
2 parents e777e94 + 1f6fc4d commit c8e253f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
4 changes: 2 additions & 2 deletions java_grpc_bench/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ repositories {
mavenLocal()
}

sourceCompatibility = 1.7
targetCompatibility = 1.7
sourceCompatibility = 14
targetCompatibility = 14

// IMPORTANT: You probably want the non-SNAPSHOT version of gRPC. Make sure you
// are looking at a tagged version of the example and not "master"!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public class HelloWorldServer {
private void start() throws IOException {
/* The port on which the server should run */
int port = 50051;
server = ServerBuilder.forPort(port)
.addService(new GreeterImpl())
.build()
.start();
ServerBuilder<?> sb = ServerBuilder.forPort(port);
sb = configureExecutor(sb);
server = sb.addService(new GreeterImpl()).build().start();
logger.info("Server started, listening on " + port);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
// Use stderr here since the logger may have been reset by its JVM shutdown
// hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
try {
HelloWorldServer.this.stop();
Expand All @@ -54,14 +54,57 @@ public void run() {
});
}

/**
* Allow customization of the Executor with two environment variables:
*
* <p>
* <ul>
* <li>JVM_EXECUTOR_TYPE: direct, workStealing, single, fixed, cached</li>
* <li>JVM_EXECUTOR_THREADS: integer value.</li>
* </ul>
* </p>
*
* The number of Executor Threads will default to the number of
* availableProcessors(). Only the workStealing and fixed executors will use
* this value.
*/
private ServerBuilder<?> configureExecutor(ServerBuilder<?> sb) {
String threads = System.getenv("JVM_EXECUTOR_THREADS");
int i_threads = Runtime.getRuntime().availableProcessors();
if (threads != null && !threads.isEmpty()) {
i_threads = Integer.parseInt(threads);
}

String value = System.getenv("JVM_EXECUTOR_TYPE");
if ("direct".equals(value)) {
sb = sb.directExecutor();
} else if ("workStealing".equals(value)) {
sb = sb.executor(java.util.concurrent.Executors.newWorkStealingPool(i_threads));
} else if ("single".equals(value)) {
sb = sb.executor(java.util.concurrent.Executors.newSingleThreadExecutor());
} else if ("fixed".equals(value)) {
sb = sb.executor(java.util.concurrent.Executors.newFixedThreadPool(i_threads));
} else if ("cached".equals(value)) {
sb = sb.executor(java.util.concurrent.Executors.newCachedThreadPool());
} else {
/*
* Use a Direct Executor by default since the GRPC service in this code is
* guaranteed non-blocking
*/
sb = sb.directExecutor();
}
return sb;
}

private void stop() throws InterruptedException {
if (server != null) {
server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
}
}

/**
* Await termination on the main thread since the grpc library uses daemon threads.
* Await termination on the main thread since the grpc library uses daemon
* threads.
*/
private void blockUntilShutdown() throws InterruptedException {
if (server != null) {
Expand Down

0 comments on commit c8e253f

Please sign in to comment.