Search in sources :

Example 81 with NioEventLoopGroup

use of io.netty.channel.nio.NioEventLoopGroup in project intellij-community by JetBrains.

the class BuildMain method main.

public static void main(String[] args) throws Throwable {
    try {
        final long processStart = System.currentTimeMillis();
        final String startMessage = "Build process started. Classpath: " + System.getProperty("java.class.path");
        System.out.println(startMessage);
        LOG.info(startMessage);
        final String host = args[HOST_ARG];
        final int port = Integer.parseInt(args[PORT_ARG]);
        final UUID sessionId = UUID.fromString(args[SESSION_ID_ARG]);
        @SuppressWarnings("ConstantConditions") final File systemDir = new File(FileUtil.toCanonicalPath(args[SYSTEM_DIR_ARG]));
        Utils.setSystemRoot(systemDir);
        final long connectStart = System.currentTimeMillis();
        // IDEA-123132, let's try again
        for (int attempt = 0; attempt < 3; attempt++) {
            try {
                ourEventLoopGroup = new NioEventLoopGroup(1, SharedThreadPool.getInstance());
                break;
            } catch (IllegalStateException e) {
                if (attempt == 2) {
                    printErrorAndExit(host, port, e);
                    return;
                } else {
                    LOG.warn("Cannot create event loop, attempt #" + attempt, e);
                    try {
                        //noinspection BusyWait
                        Thread.sleep(10 * (attempt + 1));
                    } catch (InterruptedException ignored) {
                    }
                }
            }
        }
        final Bootstrap bootstrap = new Bootstrap().group(ourEventLoopGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer() {

            @Override
            protected void initChannel(Channel channel) throws Exception {
                channel.pipeline().addLast(new ProtobufVarint32FrameDecoder(), new ProtobufDecoder(CmdlineRemoteProto.Message.getDefaultInstance()), new ProtobufVarint32LengthFieldPrepender(), new ProtobufEncoder(), new MyMessageHandler(sessionId));
            }
        }).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true);
        final ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)).awaitUninterruptibly();
        final boolean success = future.isSuccess();
        if (success) {
            LOG.info("Connection to IDE established in " + (System.currentTimeMillis() - connectStart) + " ms");
            final String projectPathToPreload = System.getProperty(PRELOAD_PROJECT_PATH, null);
            final String globalsPathToPreload = System.getProperty(PRELOAD_CONFIG_PATH, null);
            if (projectPathToPreload != null && globalsPathToPreload != null) {
                final PreloadedData data = new PreloadedData();
                ourPreloadedData = data;
                try {
                    // this will pre-load all FS optimizations
                    FileSystemUtil.getAttributes(projectPathToPreload);
                    final BuildRunner runner = new BuildRunner(new JpsModelLoaderImpl(projectPathToPreload, globalsPathToPreload, null));
                    data.setRunner(runner);
                    final File dataStorageRoot = Utils.getDataStorageRoot(projectPathToPreload);
                    final BuildFSState fsState = new BuildFSState(false);
                    final ProjectDescriptor pd = runner.load(new MessageHandler() {

                        @Override
                        public void processMessage(BuildMessage msg) {
                            data.addMessage(msg);
                        }
                    }, dataStorageRoot, fsState);
                    data.setProjectDescriptor(pd);
                    try {
                        final File fsStateFile = new File(dataStorageRoot, BuildSession.FS_STATE_FILE);
                        final DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(fsStateFile)));
                        try {
                            final int version = in.readInt();
                            if (version == BuildFSState.VERSION) {
                                final long savedOrdinal = in.readLong();
                                // must skip "has-work-to-do" flag
                                final boolean hasWorkToDo = in.readBoolean();
                                fsState.load(in, pd.getModel(), pd.getBuildRootIndex());
                                data.setFsEventOrdinal(savedOrdinal);
                                data.setHasHasWorkToDo(hasWorkToDo);
                            }
                        } finally {
                            in.close();
                        }
                    } catch (FileNotFoundException ignored) {
                    } catch (IOException e) {
                        LOG.info("Error pre-loading FS state", e);
                        fsState.clearAll();
                    }
                    // preloading target configurations
                    final BuildTargetsState targetsState = pd.getTargetsState();
                    for (BuildTarget<?> target : pd.getBuildTargetIndex().getAllTargets()) {
                        targetsState.getTargetConfiguration(target);
                    }
                    BuilderRegistry.getInstance();
                    LOG.info("Pre-loaded process ready in " + (System.currentTimeMillis() - processStart) + " ms");
                } catch (Throwable e) {
                    LOG.info("Failed to pre-load project " + projectPathToPreload, e);
                // just failed to preload the project, the situation will be handled later, when real build starts
                }
            } else if (projectPathToPreload != null || globalsPathToPreload != null) {
                LOG.info("Skipping project pre-loading step: both paths to project configuration files and path to global settings must be specified");
            }
            future.channel().writeAndFlush(CmdlineProtoUtil.toMessage(sessionId, CmdlineProtoUtil.createParamRequest()));
        } else {
            printErrorAndExit(host, port, future.cause());
        }
    } catch (Throwable e) {
        LOG.error(e);
        throw e;
    }
}
Also used : MessageHandler(org.jetbrains.jps.incremental.MessageHandler) InetSocketAddress(java.net.InetSocketAddress) ProtobufDecoder(io.netty.handler.codec.protobuf.ProtobufDecoder) ProtobufVarint32LengthFieldPrepender(io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender) BuildTargetsState(org.jetbrains.jps.incremental.storage.BuildTargetsState) BuildFSState(org.jetbrains.jps.incremental.fs.BuildFSState) Bootstrap(io.netty.bootstrap.Bootstrap) UUID(java.util.UUID) ProtobufVarint32FrameDecoder(io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ProtobufEncoder(io.netty.handler.codec.protobuf.ProtobufEncoder) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) BuildMessage(org.jetbrains.jps.incremental.messages.BuildMessage)

Example 82 with NioEventLoopGroup

use of io.netty.channel.nio.NioEventLoopGroup in project undertow by undertow-io.

the class WebSocketTestClient method connect.

/**
     * Connect the WebSocket client
     *
     * @throws Exception
     */
public WebSocketTestClient connect() throws Exception {
    String protocol = uri.getScheme();
    if (!"ws".equals(protocol)) {
        throw new IllegalArgumentException("Unsupported protocol: " + protocol);
    }
    final WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, version, null, false, new DefaultHttpHeaders());
    EventLoopGroup group = new NioEventLoopGroup();
    final CountDownLatch handshakeLatch = new CountDownLatch(1);
    bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer() {

        @Override
        protected void initChannel(Channel channel) throws Exception {
            ChannelPipeline p = channel.pipeline();
            p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), new WSClientHandler(handshaker, handshakeLatch));
        }
    });
    // Connect
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(uri.getHost(), uri.getPort()));
    future.syncUninterruptibly();
    ch = future.channel();
    handshaker.handshake(ch).syncUninterruptibly();
    handshakeLatch.await();
    return this;
}
Also used : WebSocketClientHandshaker(io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker) ChannelFuture(io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ExecutionException(java.util.concurrent.ExecutionException) ChannelPipeline(io.netty.channel.ChannelPipeline) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 83 with NioEventLoopGroup

use of io.netty.channel.nio.NioEventLoopGroup in project aerospike-client-java by aerospike.

the class Main method runBenchmarks.

public void runBenchmarks() throws Exception {
    if (this.asyncEnabled) {
        EventPolicy eventPolicy = new EventPolicy();
        if (args.readPolicy.socketTimeout > 0 && args.readPolicy.socketTimeout < eventPolicy.minTimeout) {
            eventPolicy.minTimeout = args.readPolicy.socketTimeout;
        }
        if (args.writePolicy.socketTimeout > 0 && args.writePolicy.socketTimeout < eventPolicy.minTimeout) {
            eventPolicy.minTimeout = args.writePolicy.socketTimeout;
        }
        switch(this.eventLoopType) {
            default:
            case DIRECT_NIO:
                {
                    eventLoops = new NioEventLoops(eventPolicy, this.eventLoopSize);
                    break;
                }
            case NETTY_NIO:
                {
                    EventLoopGroup group = new NioEventLoopGroup(this.eventLoopSize);
                    eventLoops = new NettyEventLoops(eventPolicy, group);
                    break;
                }
            case NETTY_EPOLL:
                {
                    EventLoopGroup group = new EpollEventLoopGroup(this.eventLoopSize);
                    eventLoops = new NettyEventLoops(eventPolicy, group);
                    break;
                }
        }
        try {
            clientPolicy.eventLoops = eventLoops;
            if (clientPolicy.maxConnsPerNode < this.asyncMaxCommands) {
                clientPolicy.maxConnsPerNode = this.asyncMaxCommands;
            }
            AerospikeClient client = new AerospikeClient(clientPolicy, hosts);
            try {
                if (initialize) {
                    doAsyncInserts(client);
                } else {
                    doAsyncRWTest(client);
                }
            } finally {
                client.close();
            }
        } finally {
            eventLoops.close();
        }
    } else {
        AerospikeClient client = new AerospikeClient(clientPolicy, hosts);
        try {
            if (initialize) {
                doInserts(client);
            } else {
                doRWTest(client);
            }
        } finally {
            client.close();
        }
    }
}
Also used : AerospikeClient(com.aerospike.client.AerospikeClient) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NettyEventLoops(com.aerospike.client.async.NettyEventLoops) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EventPolicy(com.aerospike.client.async.EventPolicy) NioEventLoops(com.aerospike.client.async.NioEventLoops) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 84 with NioEventLoopGroup

use of io.netty.channel.nio.NioEventLoopGroup in project carbondata by apache.

the class DictionaryClient method startClient.

/**
   * start dictionary client
   *
   * @param address
   * @param port
   */
public void startClient(String address, int port) {
    LOGGER.audit("Starting client on " + address + " " + port);
    long start = System.currentTimeMillis();
    // Create an Event with 1 thread.
    workerGroup = new NioEventLoopGroup(1);
    Bootstrap clientBootstrap = new Bootstrap();
    clientBootstrap.group(workerGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            // Based on length provided at header, it collects all packets
            pipeline.addLast("LengthDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 2, 0, 2));
            pipeline.addLast("DictionaryClientHandler", dictionaryClientHandler);
        }
    });
    clientBootstrap.connect(new InetSocketAddress(address, port));
    LOGGER.info("Dictionary client Started, Total time spent : " + (System.currentTimeMillis() - start));
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) Bootstrap(io.netty.bootstrap.Bootstrap) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 85 with NioEventLoopGroup

use of io.netty.channel.nio.NioEventLoopGroup in project intellij-community by JetBrains.

the class ExternalJavacManager method start.

public void start(int listenPort) {
    final ServerBootstrap bootstrap = new ServerBootstrap().group(new NioEventLoopGroup(1, SharedThreadPool.getInstance())).channel(NioServerSocketChannel.class);
    bootstrap.childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true);
    final ChannelHandler compilationRequestsHandler = new CompilationRequestsHandler();
    bootstrap.childHandler(new ChannelInitializer() {

        @Override
        protected void initChannel(Channel channel) throws Exception {
            channel.pipeline().addLast(myChannelRegistrar, new ProtobufVarint32FrameDecoder(), new ProtobufDecoder(JavacRemoteProto.Message.getDefaultInstance()), new ProtobufVarint32LengthFieldPrepender(), new ProtobufEncoder(), compilationRequestsHandler);
        }
    });
    try {
        final InetAddress loopback = InetAddress.getByName(null);
        myChannelRegistrar.add(bootstrap.bind(loopback, listenPort).syncUninterruptibly().channel());
        myListenPort = listenPort;
    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
    }
}
Also used : ProtobufEncoder(io.netty.handler.codec.protobuf.ProtobufEncoder) UnknownHostException(java.net.UnknownHostException) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ProtobufDecoder(io.netty.handler.codec.protobuf.ProtobufDecoder) ProtobufVarint32LengthFieldPrepender(io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) UnknownHostException(java.net.UnknownHostException) ProtobufVarint32FrameDecoder(io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder) InetAddress(java.net.InetAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)201 EventLoopGroup (io.netty.channel.EventLoopGroup)100 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)80 Bootstrap (io.netty.bootstrap.Bootstrap)72 ChannelFuture (io.netty.channel.ChannelFuture)58 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)56 Channel (io.netty.channel.Channel)55 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)54 SocketChannel (io.netty.channel.socket.SocketChannel)48 SslContext (io.netty.handler.ssl.SslContext)41 LoggingHandler (io.netty.handler.logging.LoggingHandler)38 InetSocketAddress (java.net.InetSocketAddress)36 ChannelPipeline (io.netty.channel.ChannelPipeline)35 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)26 Test (org.testng.annotations.Test)23 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)20 ByteBuf (io.netty.buffer.ByteBuf)18 NettyClientMetrics (com.linkedin.pinot.transport.metrics.NettyClientMetrics)16 ChannelInitializer (io.netty.channel.ChannelInitializer)16 HashedWheelTimer (io.netty.util.HashedWheelTimer)16