Search in sources :

Example 1 with ForkJoinWorkerThread

use of java.util.concurrent.ForkJoinWorkerThread 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 ForkJoinWorkerThread

use of java.util.concurrent.ForkJoinWorkerThread 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 ForkJoinWorkerThread

use of java.util.concurrent.ForkJoinWorkerThread in project quasar by puniverse.

the class ExtendedForkJoinWorkerFactory method newThread.

@Override
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
    // ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
    ForkJoinWorkerThread thread = createThread(pool);
    final String workerNumber = thread.getName().substring(thread.getName().lastIndexOf('-') + 1);
    final String newThreadName = "ForkJoinPool-" + name + "-worker-" + workerNumber;
    thread.setName(newThreadName);
    //thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
    return thread;
}
Also used : ForkJoinWorkerThread(java.util.concurrent.ForkJoinWorkerThread)

Example 4 with ForkJoinWorkerThread

use of java.util.concurrent.ForkJoinWorkerThread in project intellij-community by JetBrains.

the class IdeaForkJoinWorkerThreadFactory method newThread.

@Override
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
    final int n = setNextBit();
    ForkJoinWorkerThread thread = new ForkJoinWorkerThread(pool) {

        @Override
        protected void onTermination(Throwable exception) {
            clearBit(n);
            super.onTermination(exception);
        }
    };
    thread.setName("JobScheduler FJ pool " + n + "/" + PARALLELISM);
    thread.setPriority(Thread.NORM_PRIORITY - 1);
    return thread;
}
Also used : ForkJoinWorkerThread(java.util.concurrent.ForkJoinWorkerThread)

Aggregations

ForkJoinWorkerThread (java.util.concurrent.ForkJoinWorkerThread)4 ForkJoinPool (java.util.concurrent.ForkJoinPool)2 ForkJoinWorkerThreadFactory (java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 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 ForkJoinPool.defaultForkJoinWorkerThreadFactory (java.util.concurrent.ForkJoinPool.defaultForkJoinWorkerThreadFactory)1