Search in sources :

Example 1 with NettyChannelAcceptor

use of herddb.network.netty.NettyChannelAcceptor in project herddb by diennea.

the class LocalChannelTest method test.

@Test
public void test() throws Exception {
    try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", 1111, true)) {
        acceptor.setEnableRealNetwork(false);
        acceptor.setAcceptor(new ServerSideConnectionAcceptor() {

            @Override
            public ServerSideConnection createConnection(Channel channel) {
                channel.setMessagesReceiver(new ChannelEventListener() {

                    @Override
                    public void messageReceived(Message message, Channel channel) {
                        channel.sendReplyMessage(message, Message.ACK("ciao"));
                    }

                    @Override
                    public void channelClosed(Channel channel) {
                    }
                });
                return new ServerSideConnection() {

                    @Override
                    public long getConnectionId() {
                        return new Random().nextLong();
                    }
                };
            }
        });
        acceptor.start();
        ExecutorService executor = Executors.newCachedThreadPool();
        try (Channel client = NettyConnector.connect("localhost", 1111, true, 0, 0, new ChannelEventListener() {

            @Override
            public void messageReceived(Message message, Channel channel) {
                System.out.println("client messageReceived " + message);
            }

            @Override
            public void channelClosed(Channel channel) {
                System.out.println("client channelClosed");
            }
        }, executor, new NioEventLoopGroup(10, executor), new DefaultEventLoopGroup())) {
            for (int i = 0; i < 100; i++) {
                Message result = client.sendMessageWithReply(Message.ACK("clientId"), 10000);
                // System.out.println("result:" + result);
                assertEquals(Message.TYPE_ACK, result.type);
            }
        } finally {
            executor.shutdown();
        }
    }
}
Also used : Random(java.util.Random) NettyChannelAcceptor(herddb.network.netty.NettyChannelAcceptor) ExecutorService(java.util.concurrent.ExecutorService) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 2 with NettyChannelAcceptor

use of herddb.network.netty.NettyChannelAcceptor in project herddb by diennea.

the class ChannelBenchTest method test.

@Test
public void test() throws Exception {
    try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", 1111, true)) {
        acceptor.setAcceptor(new ServerSideConnectionAcceptor() {

            @Override
            public ServerSideConnection createConnection(Channel channel) {
                channel.setMessagesReceiver(new ChannelEventListener() {

                    @Override
                    public void messageReceived(Message message, Channel channel) {
                        channel.sendReplyMessage(message, Message.ACK("ciao"));
                    }

                    @Override
                    public void channelClosed(Channel channel) {
                    }
                });
                return new ServerSideConnection() {

                    @Override
                    public long getConnectionId() {
                        return new Random().nextLong();
                    }
                };
            }
        });
        acceptor.start();
        ExecutorService executor = Executors.newCachedThreadPool();
        try (Channel client = NettyConnector.connect("localhost", 1111, true, 0, 0, new ChannelEventListener() {

            @Override
            public void messageReceived(Message message, Channel channel) {
                System.out.println("client messageReceived " + message);
            }

            @Override
            public void channelClosed(Channel channel) {
                System.out.println("client channelClosed");
            }
        }, executor, new NioEventLoopGroup(10, executor), new DefaultEventLoopGroup())) {
            for (int i = 0; i < 100; i++) {
                Message result = client.sendMessageWithReply(Message.ACK("clientId"), 10000);
                // System.out.println("result:" + result);
                assertEquals(Message.TYPE_ACK, result.type);
            }
        } finally {
            executor.shutdown();
        }
    }
}
Also used : Random(java.util.Random) NettyChannelAcceptor(herddb.network.netty.NettyChannelAcceptor) ExecutorService(java.util.concurrent.ExecutorService) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 3 with NettyChannelAcceptor

use of herddb.network.netty.NettyChannelAcceptor in project herddb by diennea.

the class NetworkChannelTest method test.

@Test
public void test() throws Exception {
    try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", 1111, true)) {
        acceptor.setEnableJVMNetwork(false);
        acceptor.setAcceptor(new ServerSideConnectionAcceptor() {

            @Override
            public ServerSideConnection createConnection(Channel channel) {
                channel.setMessagesReceiver(new ChannelEventListener() {

                    @Override
                    public void messageReceived(Message message, Channel channel) {
                        channel.sendReplyMessage(message, Message.ACK("ciao"));
                    }

                    @Override
                    public void channelClosed(Channel channel) {
                    }
                });
                return new ServerSideConnection() {

                    @Override
                    public long getConnectionId() {
                        return new Random().nextLong();
                    }
                };
            }
        });
        acceptor.start();
        ExecutorService executor = Executors.newCachedThreadPool();
        try (Channel client = NettyConnector.connect("localhost", 1111, true, 0, 0, new ChannelEventListener() {

            @Override
            public void messageReceived(Message message, Channel channel) {
                System.out.println("client messageReceived " + message);
            }

            @Override
            public void channelClosed(Channel channel) {
                System.out.println("client channelClosed");
            }
        }, executor, new NioEventLoopGroup(10, executor), new DefaultEventLoopGroup())) {
            for (int i = 0; i < 100; i++) {
                Message result = client.sendMessageWithReply(Message.ACK("clientId"), 10000);
                assertEquals(Message.TYPE_ACK, result.type);
            // System.out.println("result:" + result);
            }
        } finally {
            executor.shutdown();
        }
    }
    if (NetworkUtils.isEnableEpoolNative()) {
        try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", 1111, true)) {
            acceptor.setEnableJVMNetwork(false);
            acceptor.setAcceptor(new ServerSideConnectionAcceptor() {

                @Override
                public ServerSideConnection createConnection(Channel channel) {
                    channel.setMessagesReceiver(new ChannelEventListener() {

                        @Override
                        public void messageReceived(Message message, Channel channel) {
                            channel.sendReplyMessage(message, Message.ACK("ciao"));
                        }

                        @Override
                        public void channelClosed(Channel channel) {
                        }
                    });
                    return new ServerSideConnection() {

                        @Override
                        public long getConnectionId() {
                            return new Random().nextLong();
                        }
                    };
                }
            });
            acceptor.start();
            ExecutorService executor = Executors.newCachedThreadPool();
            try (Channel client = NettyConnector.connect("localhost", 1111, true, 0, 0, new ChannelEventListener() {

                @Override
                public void messageReceived(Message message, Channel channel) {
                    System.out.println("client messageReceived " + message);
                }

                @Override
                public void channelClosed(Channel channel) {
                    System.out.println("client channelClosed");
                }
            }, executor, new EpollEventLoopGroup(10, executor), new DefaultEventLoopGroup())) {
                for (int i = 0; i < 100; i++) {
                    Message result = client.sendMessageWithReply(Message.ACK("clientId"), 10000);
                    assertEquals(Message.TYPE_ACK, result.type);
                // System.out.println("result:" + result);
                }
            } finally {
                executor.shutdown();
            }
        }
    }
}
Also used : DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Random(java.util.Random) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) NettyChannelAcceptor(herddb.network.netty.NettyChannelAcceptor) ExecutorService(java.util.concurrent.ExecutorService) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 4 with NettyChannelAcceptor

use of herddb.network.netty.NettyChannelAcceptor in project herddb by diennea.

the class Server method buildChannelAcceptor.

private NettyChannelAcceptor buildChannelAcceptor() {
    String realHost = serverHostData.getAdditionalData().get(ServerConfiguration.PROPERTY_HOST);
    int realPort = Integer.parseInt(serverHostData.getAdditionalData().get(ServerConfiguration.PROPERTY_PORT));
    NettyChannelAcceptor acceptor = new NettyChannelAcceptor(realHost, realPort, serverHostData.isSsl(), statsLogger.scope("network"));
    // with 'local' mode we are disabling network by default
    // but in case you want to run benchmarks with 'local' mode using
    // the client on a separate process/machine you should be able
    // to enable network
    boolean isLocal = ServerConfiguration.PROPERTY_MODE_LOCAL.equals(mode);
    boolean nextworkEnabled = configuration.getBoolean(ServerConfiguration.PROPERTY_NETWORK_ENABLED, !isLocal && ServerConfiguration.PROPERTY_NETWORK_ENABLED_DEFAULT);
    if (!nextworkEnabled) {
        acceptor.setEnableRealNetwork(false);
        LOGGER.log(Level.FINE, "Local in-JVM acceptor on {0}:{1} ssl:{2}", new Object[] { realHost, realPort, serverHostData.isSsl() });
    } else {
        LOGGER.log(Level.INFO, "Binding network acceptor to {0}:{1} ssl:{2}", new Object[] { realHost, realPort, serverHostData.isSsl() });
    }
    int callbackThreads = configuration.getInt(ServerConfiguration.PROPERTY_NETWORK_CALLBACK_THREADS, ServerConfiguration.PROPERTY_NETWORK_CALLBACK_THREADS_DEFAULT);
    int workerThreads = configuration.getInt(ServerConfiguration.PROPERTY_NETWORK_WORKER_THREADS, ServerConfiguration.PROPERTY_NETWORK_WORKER_THREADS_DEFAULT);
    acceptor.setCallbackThreads(callbackThreads);
    acceptor.setWorkerThreads(workerThreads);
    return acceptor;
}
Also used : NettyChannelAcceptor(herddb.network.netty.NettyChannelAcceptor)

Aggregations

NettyChannelAcceptor (herddb.network.netty.NettyChannelAcceptor)4 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)3 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 Random (java.util.Random)3 ExecutorService (java.util.concurrent.ExecutorService)3 Test (org.junit.Test)3 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)1