Search in sources :

Example 1 with ForkJoinWorkerThreadFactory

use of java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory in project grpc-java by grpc.

the class AsyncServer method newServer.

static Server newServer(ServerConfiguration config) throws IOException {
    SslContext sslContext = null;
    if (config.tls) {
        System.out.println("Using fake CA for TLS certificate.\n" + "Run the Java client with --tls --testca");
        File cert = TestUtils.loadCert("server1.pem");
        File key = TestUtils.loadCert("server1.key");
        SslContextBuilder sslContextBuilder = GrpcSslContexts.forServer(cert, key);
        if (config.transport == ServerConfiguration.Transport.NETTY_NIO) {
            sslContextBuilder = GrpcSslContexts.configure(sslContextBuilder, SslProvider.JDK);
        } else {
            // Native transport with OpenSSL
            sslContextBuilder = GrpcSslContexts.configure(sslContextBuilder, SslProvider.OPENSSL);
        }
        if (config.useDefaultCiphers) {
            sslContextBuilder.ciphers(null);
        }
        sslContext = sslContextBuilder.build();
    }
    final EventLoopGroup boss;
    final EventLoopGroup worker;
    final Class<? extends ServerChannel> channelType;
    switch(config.transport) {
        case NETTY_NIO:
            {
                boss = new NioEventLoopGroup();
                worker = new NioEventLoopGroup();
                channelType = NioServerSocketChannel.class;
                break;
            }
        case NETTY_EPOLL:
            {
                try {
                    // These classes are only available on linux.
                    Class<?> groupClass = Class.forName("io.netty.channel.epoll.EpollEventLoopGroup");
                    @SuppressWarnings("unchecked") Class<? extends ServerChannel> channelClass = (Class<? extends ServerChannel>) Class.forName("io.netty.channel.epoll.EpollServerSocketChannel");
                    boss = (EventLoopGroup) groupClass.getConstructor().newInstance();
                    worker = (EventLoopGroup) groupClass.getConstructor().newInstance();
                    channelType = channelClass;
                    break;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        case NETTY_UNIX_DOMAIN_SOCKET:
            {
                try {
                    // These classes are only available on linux.
                    Class<?> groupClass = Class.forName("io.netty.channel.epoll.EpollEventLoopGroup");
                    @SuppressWarnings("unchecked") Class<? extends ServerChannel> channelClass = (Class<? extends ServerChannel>) Class.forName("io.netty.channel.epoll.EpollServerDomainSocketChannel");
                    boss = (EventLoopGroup) groupClass.getConstructor().newInstance();
                    worker = (EventLoopGroup) groupClass.getConstructor().newInstance();
                    channelType = channelClass;
                    break;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        default:
            {
                // Should never get here.
                throw new IllegalArgumentException("Unsupported transport: " + config.transport);
            }
    }
    NettyServerBuilder builder = NettyServerBuilder.forAddress(config.address).bossEventLoopGroup(boss).workerEventLoopGroup(worker).channelType(channelType).addService(new BenchmarkServiceImpl()).sslContext(sslContext).flowControlWindow(config.flowControlWindow);
    if (config.directExecutor) {
        builder.directExecutor();
    } else {
        // TODO(carl-mastrangelo): This should not be necessary.  I don't know where this should be
        // put.  Move it somewhere else, or remove it if no longer necessary.
        // See: https://github.com/grpc/grpc-java/issues/2119
        builder.executor(new ForkJoinPool(Runtime.getRuntime().availableProcessors(), new ForkJoinWorkerThreadFactory() {

            final AtomicInteger num = new AtomicInteger();

            @Override
            public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
                ForkJoinWorkerThread thread = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
                thread.setDaemon(true);
                thread.setName("grpc-server-app-" + "-" + num.getAndIncrement());
                return thread;
            }
        }, UncaughtExceptionHandlers.systemExit(), true));
    }
    return builder.build();
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NettyServerBuilder(io.grpc.netty.NettyServerBuilder) ServerChannel(io.netty.channel.ServerChannel) IOException(java.io.IOException) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ForkJoinWorkerThreadFactory(java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) ForkJoinWorkerThread(java.util.concurrent.ForkJoinWorkerThread) File(java.io.File) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Example 2 with ForkJoinWorkerThreadFactory

use of java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory in project grpc-java by grpc.

the class Utils method getExecutor.

private static synchronized ExecutorService getExecutor() {
    if (clientExecutor == null) {
        clientExecutor = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), new ForkJoinWorkerThreadFactory() {

            final AtomicInteger num = new AtomicInteger();

            @Override
            public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
                ForkJoinWorkerThread thread = defaultForkJoinWorkerThreadFactory.newThread(pool);
                thread.setDaemon(true);
                thread.setName("grpc-client-app-" + "-" + num.getAndIncrement());
                return thread;
            }
        }, UncaughtExceptionHandlers.systemExit(), true);
    }
    return clientExecutor;
}
Also used : ForkJoinWorkerThreadFactory(java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory) ForkJoinPool.defaultForkJoinWorkerThreadFactory(java.util.concurrent.ForkJoinPool.defaultForkJoinWorkerThreadFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ForkJoinWorkerThread(java.util.concurrent.ForkJoinWorkerThread) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Example 3 with ForkJoinWorkerThreadFactory

use of java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory in project torodb by torodb.

the class DefaultConcurrentToolsFactory method createExecutorService.

@Override
@SuppressFBWarnings(value = { "NP_NONNULL_PARAM_VIOLATION" }, justification = "ForkJoinPool constructor admits a null " + "UncaughtExceptionHandler")
public ExecutorService createExecutorService(String prefix, boolean blockerTasks, int maxThreads) {
    ExecutorService executorService;
    if (blockerTasks) {
        ThreadFactory threadFactory = blockerThreadFactoryFunction.apply(prefix);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(maxThreads, maxThreads, 10L, TimeUnit.MINUTES, new LinkedBlockingQueue<>(), threadFactory);
        threadPoolExecutor.allowCoreThreadTimeOut(true);
        executorService = threadPoolExecutor;
    } else {
        ForkJoinWorkerThreadFactory threadFactory = forkJoinThreadFactoryFunction.apply(prefix);
        executorService = new ForkJoinPool(maxThreads, threadFactory, null, true);
    }
    shutdownHelper.terminateOnShutdown(prefix, executorService);
    return executorService;
}
Also used : ForkJoinWorkerThreadFactory(java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) ForkJoinWorkerThreadFactory(java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ForkJoinPool(java.util.concurrent.ForkJoinPool) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

ForkJoinPool (java.util.concurrent.ForkJoinPool)3 ForkJoinWorkerThreadFactory (java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory)3 ForkJoinWorkerThread (java.util.concurrent.ForkJoinWorkerThread)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 NettyServerBuilder (io.grpc.netty.NettyServerBuilder)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 ServerChannel (io.netty.channel.ServerChannel)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 SslContext (io.netty.handler.ssl.SslContext)1 SslContextBuilder (io.netty.handler.ssl.SslContextBuilder)1 File (java.io.File)1 IOException (java.io.IOException)1 ExecutorService (java.util.concurrent.ExecutorService)1 ForkJoinPool.defaultForkJoinWorkerThreadFactory (java.util.concurrent.ForkJoinPool.defaultForkJoinWorkerThreadFactory)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1