Search in sources :

Example 41 with DefaultThreadFactory

use of io.netty.util.concurrent.DefaultThreadFactory in project incubator-pulsar by apache.

the class ZookeeperCacheTest method testInvalidateCacheOnFailure.

/**
 * <pre>
 * Verifies that if {@link ZooKeeperCache} fails to fetch data into the cache then
 * (1) it invalidates failed future so, next time it helps to get fresh data from zk
 * (2) handles zk.getData() unexpected exception if zkSession is lost
 * </pre>
 *
 * @throws Exception
 */
@Test(timeOut = 10000)
public void testInvalidateCacheOnFailure() throws Exception {
    ExecutorService zkExecutor = Executors.newSingleThreadExecutor(new DefaultThreadFactory("mockZk"));
    // add readOpDelayMs so, main thread will not serve zkCacahe-returned future and let zkExecutor-thread handle
    // callback-result process
    MockZooKeeper zkClient = MockZooKeeper.newInstance(zkExecutor, 100);
    ZooKeeperCache zkCacheService = new LocalZooKeeperCache(zkClient, executor, scheduledExecutor);
    final AtomicInteger count = new AtomicInteger(0);
    ZooKeeperDataCache<String> zkCache = new ZooKeeperDataCache<String>(zkCacheService) {

        @Override
        public String deserialize(String key, byte[] content) throws Exception {
            if (count.getAndIncrement() == 0) {
                throw new NullPointerException("data is null");
            } else {
                return new String(content);
            }
        }
    };
    String value = "test";
    String key1 = "/zkDesrializationExceptionTest";
    String key2 = "/zkSessionExceptionTest";
    zkClient.create(key1, value.getBytes(), null, null);
    zkClient.create(key2, value.getBytes(), null, null);
    // (1) deserialization will fail so, result should be exception
    try {
        zkCache.getAsync(key1).get();
        fail("it should have failed with NPE");
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof NullPointerException);
    }
    // (2) sleep to let cache to be invalidated async
    Thread.sleep(1000);
    // (3) now, cache should be invalidate failed-future and should refetch the data
    assertEquals(zkCache.getAsync(key1).get().get(), value);
    // (4) make zk-session invalid
    ZooKeeper zkSession = zkCacheService.zkSession.get();
    zkCacheService.zkSession.set(null);
    try {
        zkCache.getAsync(key2).get();
        fail("it should have failed with NPE");
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof NullPointerException);
    }
    // global-Zk session is connected now
    zkCacheService.zkSession.set(zkSession);
    // (5) sleep to let cache to be invalidated async
    Thread.sleep(1000);
    // (6) now, cache should be invalidate failed-future and should refetch the data
    assertEquals(zkCache.getAsync(key1).get().get(), value);
    zkExecutor.shutdown();
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) MockZooKeeper(org.apache.zookeeper.MockZooKeeper) ZooKeeper(org.apache.zookeeper.ZooKeeper) MockZooKeeper(org.apache.zookeeper.MockZooKeeper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Test(org.testng.annotations.Test)

Example 42 with DefaultThreadFactory

use of io.netty.util.concurrent.DefaultThreadFactory in project incubator-pulsar by apache.

the class ConnectionPoolTest method testSingleIpAddress.

@Test
public void testSingleIpAddress() throws Exception {
    ClientConfigurationData conf = new ClientConfigurationData();
    EventLoopGroup eventLoop = EventLoopUtil.newEventLoopGroup(1, new DefaultThreadFactory("test"));
    ConnectionPool pool = Mockito.spy(new ConnectionPool(conf, eventLoop));
    conf.setServiceUrl(serviceUrl);
    PulsarClientImpl client = new PulsarClientImpl(conf, eventLoop, pool);
    List<InetAddress> result = Lists.newArrayList();
    result.add(InetAddress.getByName("127.0.0.1"));
    Mockito.when(pool.resolveName("non-existing-dns-name")).thenReturn(CompletableFuture.completedFuture(result));
    client.createProducer("persistent://sample/standalone/ns/my-topic");
    client.close();
}
Also used : ClientConfigurationData(org.apache.pulsar.client.impl.conf.ClientConfigurationData) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) EventLoopGroup(io.netty.channel.EventLoopGroup) InetAddress(java.net.InetAddress) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 43 with DefaultThreadFactory

use of io.netty.util.concurrent.DefaultThreadFactory in project netty by netty.

the class MsgEchoPeerBase method run.

public void run() throws Exception {
    // Configure the peer.
    final ThreadFactory connectFactory = new DefaultThreadFactory("rendezvous");
    final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.MESSAGE_PROVIDER);
    try {
        final Bootstrap boot = new Bootstrap();
        boot.group(connectGroup).channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS).handler(new ChannelInitializer<UdtChannel>() {

            @Override
            public void initChannel(final UdtChannel ch) throws Exception {
                ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new MsgEchoPeerHandler(messageSize));
            }
        });
        // Start the peer.
        final ChannelFuture f = boot.connect(peer, self).sync();
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        connectGroup.shutdownGracefully();
    }
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ChannelFuture(io.netty.channel.ChannelFuture) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) LoggingHandler(io.netty.handler.logging.LoggingHandler) UdtChannel(io.netty.channel.udt.UdtChannel) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 44 with DefaultThreadFactory

use of io.netty.util.concurrent.DefaultThreadFactory in project netty by netty.

the class ByteEchoClient method main.

public static void main(String[] args) throws Exception {
    // Configure the client.
    final ThreadFactory connectFactory = new DefaultThreadFactory("connect");
    final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.BYTE_PROVIDER);
    try {
        final Bootstrap boot = new Bootstrap();
        boot.group(connectGroup).channelFactory(NioUdtProvider.BYTE_CONNECTOR).handler(new ChannelInitializer<UdtChannel>() {

            @Override
            public void initChannel(final UdtChannel ch) throws Exception {
                ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new ByteEchoClientHandler());
            }
        });
        // Start the client.
        final ChannelFuture f = boot.connect(HOST, PORT).sync();
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        connectGroup.shutdownGracefully();
    }
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ChannelFuture(io.netty.channel.ChannelFuture) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) LoggingHandler(io.netty.handler.logging.LoggingHandler) UdtChannel(io.netty.channel.udt.UdtChannel) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 45 with DefaultThreadFactory

use of io.netty.util.concurrent.DefaultThreadFactory in project netty by netty.

the class ByteEchoServer method main.

public static void main(String[] args) throws Exception {
    final ThreadFactory acceptFactory = new DefaultThreadFactory("accept");
    final ThreadFactory connectFactory = new DefaultThreadFactory("connect");
    final NioEventLoopGroup acceptGroup = new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.BYTE_PROVIDER);
    final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.BYTE_PROVIDER);
    // Configure the server.
    try {
        final ServerBootstrap boot = new ServerBootstrap();
        boot.group(acceptGroup, connectGroup).channelFactory(NioUdtProvider.BYTE_ACCEPTOR).option(ChannelOption.SO_BACKLOG, 10).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<UdtChannel>() {

            @Override
            public void initChannel(final UdtChannel ch) throws Exception {
                ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new ByteEchoServerHandler());
            }
        });
        // Start the server.
        final ChannelFuture future = boot.bind(PORT).sync();
        // Wait until the server socket is closed.
        future.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        acceptGroup.shutdownGracefully();
        connectGroup.shutdownGracefully();
    }
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ChannelFuture(io.netty.channel.ChannelFuture) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) LoggingHandler(io.netty.handler.logging.LoggingHandler) UdtChannel(io.netty.channel.udt.UdtChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Aggregations

DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)49 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)24 EventLoopGroup (io.netty.channel.EventLoopGroup)15 ThreadFactory (java.util.concurrent.ThreadFactory)12 ChannelFuture (io.netty.channel.ChannelFuture)9 ExecutorService (java.util.concurrent.ExecutorService)8 Test (org.junit.jupiter.api.Test)8 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)7 IOException (java.io.IOException)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)6 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)6 UdtChannel (io.netty.channel.udt.UdtChannel)6 LoggingHandler (io.netty.handler.logging.LoggingHandler)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 RateLimiter (com.google.common.util.concurrent.RateLimiter)5 Channel (io.netty.channel.Channel)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Timeout (org.junit.jupiter.api.Timeout)5 Test (org.testng.annotations.Test)5 ParameterException (com.beust.jcommander.ParameterException)4