Search in sources :

Example 1 with AddressResolver

use of io.netty.resolver.AddressResolver in project redisson by redisson.

the class RedisClient method resolveAddr.

public CompletableFuture<InetSocketAddress> resolveAddr() {
    if (resolvedAddrFuture.get() != null) {
        return resolvedAddrFuture.get();
    }
    CompletableFuture<InetSocketAddress> promise = new CompletableFuture<>();
    if (!resolvedAddrFuture.compareAndSet(null, promise)) {
        return resolvedAddrFuture.get();
    }
    byte[] addr = NetUtil.createByteArrayFromIpAddressString(uri.getHost());
    if (addr != null) {
        try {
            resolvedAddr = new InetSocketAddress(InetAddress.getByAddress(uri.getHost(), addr), uri.getPort());
        } catch (UnknownHostException e) {
        // skip
        }
        promise.complete(resolvedAddr);
        return promise;
    }
    AddressResolver<InetSocketAddress> resolver = (AddressResolver<InetSocketAddress>) bootstrap.config().resolver().getResolver(bootstrap.config().group().next());
    Future<InetSocketAddress> resolveFuture = resolver.resolve(InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort()));
    resolveFuture.addListener((FutureListener<InetSocketAddress>) future -> {
        if (!future.isSuccess()) {
            promise.completeExceptionally(future.cause());
            return;
        }
        InetSocketAddress resolved = future.getNow();
        byte[] addr1 = resolved.getAddress().getAddress();
        resolvedAddr = new InetSocketAddress(InetAddress.getByAddress(uri.getHost(), addr1), resolved.getPort());
        promise.complete(resolvedAddr);
    });
    return promise;
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) RedisURI(org.redisson.misc.RedisURI) AtomicReference(java.util.concurrent.atomic.AtomicReference) EpollDatagramChannel(io.netty.channel.epoll.EpollDatagramChannel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) RFuture(org.redisson.api.RFuture) InetAddress(java.net.InetAddress) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) io.netty.channel(io.netty.channel) RedisChannelInitializer(org.redisson.client.handler.RedisChannelInitializer) Type(org.redisson.client.handler.RedisChannelInitializer.Type) AddressResolver(io.netty.resolver.AddressResolver) ChannelGroup(io.netty.channel.group.ChannelGroup) FutureListener(io.netty.util.concurrent.FutureListener) java.util.concurrent(java.util.concurrent) NetUtil(io.netty.util.NetUtil) DnsServerAddressStreamProviders(io.netty.resolver.dns.DnsServerAddressStreamProviders) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) HashedWheelTimer(io.netty.util.HashedWheelTimer) Timer(io.netty.util.Timer) ChannelGroupFuture(io.netty.channel.group.ChannelGroupFuture) Future(io.netty.util.concurrent.Future) DnsAddressResolverGroup(io.netty.resolver.dns.DnsAddressResolverGroup) AddressResolver(io.netty.resolver.AddressResolver) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress)

Example 2 with AddressResolver

use of io.netty.resolver.AddressResolver in project redisson by redisson.

the class MasterSlaveConnectionManager method resolveIP.

protected CompletableFuture<RedisURI> resolveIP(String scheme, RedisURI address) {
    if (address.isIP()) {
        RedisURI addr = applyNatMap(address);
        return CompletableFuture.completedFuture(addr);
    }
    CompletableFuture<RedisURI> result = new CompletableFuture<>();
    AddressResolver<InetSocketAddress> resolver = resolverGroup.getResolver(getGroup().next());
    InetSocketAddress addr = InetSocketAddress.createUnresolved(address.getHost(), address.getPort());
    Future<InetSocketAddress> future = resolver.resolve(addr);
    future.addListener((FutureListener<InetSocketAddress>) f -> {
        if (!f.isSuccess()) {
            log.error("Unable to resolve " + address, f.cause());
            result.completeExceptionally(f.cause());
            return;
        }
        InetSocketAddress s = f.getNow();
        RedisURI uri = toURI(scheme, s.getAddress().getHostAddress(), "" + address.getPort());
        result.complete(uri);
    });
    return result;
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) java.util(java.util) NodeType(org.redisson.api.NodeType) Codec(org.redisson.client.codec.Codec) ClusterSlotRange(org.redisson.cluster.ClusterSlotRange) KQueueDatagramChannel(io.netty.channel.kqueue.KQueueDatagramChannel) LoggerFactory(org.slf4j.LoggerFactory) RedisURI(org.redisson.misc.RedisURI) InfinitySemaphoreLatch(org.redisson.misc.InfinitySemaphoreLatch) EpollDatagramChannel(io.netty.channel.epoll.EpollDatagramChannel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) InetAddress(java.net.InetAddress) io.netty.util.concurrent(io.netty.util.concurrent) PlatformDependent(io.netty.util.internal.PlatformDependent) KQueueSocketChannel(io.netty.channel.kqueue.KQueueSocketChannel) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) TimerTask(io.netty.util.TimerTask) SocketChannel(io.netty.channel.socket.SocketChannel) Version(org.redisson.Version) KQueueEventLoopGroup(io.netty.channel.kqueue.KQueueEventLoopGroup) AddressResolver(io.netty.resolver.AddressResolver) PublishSubscribeService(org.redisson.pubsub.PublishSubscribeService) EventLoopGroup(io.netty.channel.EventLoopGroup) org.redisson.config(org.redisson.config) Logger(org.slf4j.Logger) ElementsSubscribeService(org.redisson.ElementsSubscribeService) java.util.concurrent(java.util.concurrent) org.redisson.client(org.redisson.client) io.netty.util(io.netty.util) DnsServerAddressStreamProviders(io.netty.resolver.dns.DnsServerAddressStreamProviders) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) AddressResolverGroup(io.netty.resolver.AddressResolverGroup) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) Timer(io.netty.util.Timer) DefaultAddressResolverGroup(io.netty.resolver.DefaultAddressResolverGroup) RedisCommand(org.redisson.client.protocol.RedisCommand) Future(io.netty.util.concurrent.Future) RedisURI(org.redisson.misc.RedisURI) InetSocketAddress(java.net.InetSocketAddress)

Example 3 with AddressResolver

use of io.netty.resolver.AddressResolver in project netty by netty.

the class BootstrapTest method testGetResolverFailed.

@Test
public void testGetResolverFailed() throws Exception {
    class TestException extends RuntimeException {
    }
    final Bootstrap bootstrapA = new Bootstrap();
    bootstrapA.group(groupA);
    bootstrapA.channel(LocalChannel.class);
    bootstrapA.resolver(new AddressResolverGroup<SocketAddress>() {

        @Override
        protected AddressResolver<SocketAddress> newResolver(EventExecutor executor) {
            throw new TestException();
        }
    });
    bootstrapA.handler(dummyHandler);
    final ServerBootstrap bootstrapB = new ServerBootstrap();
    bootstrapB.group(groupB);
    bootstrapB.channel(LocalServerChannel.class);
    bootstrapB.childHandler(dummyHandler);
    SocketAddress localAddress = bootstrapB.bind(LocalAddress.ANY).sync().channel().localAddress();
    // Connect to the server using the asynchronous resolver.
    ChannelFuture connectFuture = bootstrapA.connect(localAddress);
    // Should fail with the IllegalStateException.
    assertThat(connectFuture.await(10000), is(true));
    assertThat(connectFuture.cause(), instanceOf(IllegalStateException.class));
    assertThat(connectFuture.cause().getCause(), instanceOf(TestException.class));
    assertThat(connectFuture.channel().isOpen(), is(false));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AddressResolver(io.netty.resolver.AddressResolver) AbstractAddressResolver(io.netty.resolver.AbstractAddressResolver) EventExecutor(io.netty.util.concurrent.EventExecutor) SocketAddress(java.net.SocketAddress) Test(org.junit.jupiter.api.Test)

Aggregations

AddressResolver (io.netty.resolver.AddressResolver)3 EpollDatagramChannel (io.netty.channel.epoll.EpollDatagramChannel)2 EpollSocketChannel (io.netty.channel.epoll.EpollSocketChannel)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)2 DnsServerAddressStreamProviders (io.netty.resolver.dns.DnsServerAddressStreamProviders)2 Timer (io.netty.util.Timer)2 Future (io.netty.util.concurrent.Future)2 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 UnknownHostException (java.net.UnknownHostException)2 java.util.concurrent (java.util.concurrent)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 io.netty.channel (io.netty.channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)1 ChannelGroup (io.netty.channel.group.ChannelGroup)1 ChannelGroupFuture (io.netty.channel.group.ChannelGroupFuture)1 DefaultChannelGroup (io.netty.channel.group.DefaultChannelGroup)1