Search in sources :

Example 1 with Future

use of io.netty.util.concurrent.Future in project hbase by apache.

the class FanOutOneBlockAsyncDFSOutputHelper method connectToDataNodes.

private static List<Future<Channel>> connectToDataNodes(Configuration conf, DFSClient client, String clientName, LocatedBlock locatedBlock, long maxBytesRcvd, long latestGS, BlockConstructionStage stage, DataChecksum summer, EventLoop eventLoop) {
    Enum<?>[] storageTypes = locatedBlock.getStorageTypes();
    DatanodeInfo[] datanodeInfos = locatedBlock.getLocations();
    boolean connectToDnViaHostname = conf.getBoolean(DFS_CLIENT_USE_DN_HOSTNAME, DFS_CLIENT_USE_DN_HOSTNAME_DEFAULT);
    int timeoutMs = conf.getInt(DFS_CLIENT_SOCKET_TIMEOUT_KEY, READ_TIMEOUT);
    ExtendedBlock blockCopy = new ExtendedBlock(locatedBlock.getBlock());
    blockCopy.setNumBytes(locatedBlock.getBlockSize());
    ClientOperationHeaderProto header = ClientOperationHeaderProto.newBuilder().setBaseHeader(BaseHeaderProto.newBuilder().setBlock(PB_HELPER.convert(blockCopy)).setToken(PB_HELPER.convert(locatedBlock.getBlockToken()))).setClientName(clientName).build();
    ChecksumProto checksumProto = DataTransferProtoUtil.toProto(summer);
    OpWriteBlockProto.Builder writeBlockProtoBuilder = OpWriteBlockProto.newBuilder().setHeader(header).setStage(OpWriteBlockProto.BlockConstructionStage.valueOf(stage.name())).setPipelineSize(1).setMinBytesRcvd(locatedBlock.getBlock().getNumBytes()).setMaxBytesRcvd(maxBytesRcvd).setLatestGenerationStamp(latestGS).setRequestedChecksum(checksumProto).setCachingStrategy(CachingStrategyProto.newBuilder().setDropBehind(true).build());
    List<Future<Channel>> futureList = new ArrayList<>(datanodeInfos.length);
    for (int i = 0; i < datanodeInfos.length; i++) {
        DatanodeInfo dnInfo = datanodeInfos[i];
        Enum<?> storageType = storageTypes[i];
        Promise<Channel> promise = eventLoop.newPromise();
        futureList.add(promise);
        String dnAddr = dnInfo.getXferAddr(connectToDnViaHostname);
        new Bootstrap().group(eventLoop).channel(NioSocketChannel.class).option(CONNECT_TIMEOUT_MILLIS, timeoutMs).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
            // we need to get the remote address of the channel so we can only move on after
            // channel connected. Leave an empty implementation here because netty does not allow
            // a null handler.
            }
        }).connect(NetUtils.createSocketAddr(dnAddr)).addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    initialize(conf, future.channel(), dnInfo, storageType, writeBlockProtoBuilder, timeoutMs, client, locatedBlock.getBlockToken(), promise);
                } else {
                    promise.tryFailure(future.cause());
                }
            }
        });
    }
    return futureList;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DatanodeInfo(org.apache.hadoop.hdfs.protocol.DatanodeInfo) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) ExtendedBlock(org.apache.hadoop.hdfs.protocol.ExtendedBlock) ArrayList(java.util.ArrayList) ChecksumProto(org.apache.hadoop.hdfs.protocol.proto.DataTransferProtos.ChecksumProto) OpWriteBlockProto(org.apache.hadoop.hdfs.protocol.proto.DataTransferProtos.OpWriteBlockProto) ChannelFutureListener(io.netty.channel.ChannelFutureListener) UnresolvedLinkException(org.apache.hadoop.fs.UnresolvedLinkException) LeaseExpiredException(org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException) InvalidBlockTokenException(org.apache.hadoop.hdfs.security.token.block.InvalidBlockTokenException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) RemoteException(org.apache.hadoop.ipc.RemoteException) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future) Bootstrap(io.netty.bootstrap.Bootstrap) ClientOperationHeaderProto(org.apache.hadoop.hdfs.protocol.proto.DataTransferProtos.ClientOperationHeaderProto)

Example 2 with Future

use of io.netty.util.concurrent.Future in project hbase by apache.

the class NettyRpcConnection method saslNegotiate.

private void saslNegotiate(final Channel ch) {
    UserGroupInformation ticket = getUGI();
    if (ticket == null) {
        failInit(ch, new FatalConnectionException("ticket/user is null"));
        return;
    }
    Promise<Boolean> saslPromise = ch.eventLoop().newPromise();
    final NettyHBaseSaslRpcClientHandler saslHandler;
    try {
        saslHandler = new NettyHBaseSaslRpcClientHandler(saslPromise, ticket, authMethod, token, serverPrincipal, rpcClient.fallbackAllowed, this.rpcClient.conf);
    } catch (IOException e) {
        failInit(ch, e);
        return;
    }
    ch.pipeline().addFirst(new SaslChallengeDecoder(), saslHandler);
    saslPromise.addListener(new FutureListener<Boolean>() {

        @Override
        public void operationComplete(Future<Boolean> future) throws Exception {
            if (future.isSuccess()) {
                ChannelPipeline p = ch.pipeline();
                p.remove(SaslChallengeDecoder.class);
                p.remove(NettyHBaseSaslRpcClientHandler.class);
                // check if negotiate with server for connection header is necessary
                if (saslHandler.isNeedProcessConnectionHeader()) {
                    Promise<Boolean> connectionHeaderPromise = ch.eventLoop().newPromise();
                    // create the handler to handle the connection header
                    ChannelHandler chHandler = new NettyHBaseRpcConnectionHeaderHandler(connectionHeaderPromise, conf, connectionHeaderWithLength);
                    // add ReadTimeoutHandler to deal with server doesn't response connection header
                    // because of the different configuration in client side and server side
                    p.addFirst(new ReadTimeoutHandler(RpcClient.DEFAULT_SOCKET_TIMEOUT_READ, TimeUnit.MILLISECONDS));
                    p.addLast(chHandler);
                    connectionHeaderPromise.addListener(new FutureListener<Boolean>() {

                        @Override
                        public void operationComplete(Future<Boolean> future) throws Exception {
                            if (future.isSuccess()) {
                                ChannelPipeline p = ch.pipeline();
                                p.remove(ReadTimeoutHandler.class);
                                p.remove(NettyHBaseRpcConnectionHeaderHandler.class);
                                // don't send connection header, NettyHbaseRpcConnectionHeaderHandler
                                // sent it already
                                established(ch);
                            } else {
                                final Throwable error = future.cause();
                                scheduleRelogin(error);
                                failInit(ch, toIOE(error));
                            }
                        }
                    });
                } else {
                    // send the connection header to server
                    ch.write(connectionHeaderWithLength.retainedDuplicate());
                    established(ch);
                }
            } else {
                final Throwable error = future.cause();
                scheduleRelogin(error);
                failInit(ch, toIOE(error));
            }
        }
    });
}
Also used : ChannelFutureListener(io.netty.channel.ChannelFutureListener) FutureListener(io.netty.util.concurrent.FutureListener) IOException(java.io.IOException) ChannelHandler(io.netty.channel.ChannelHandler) IOException(java.io.IOException) ChannelPipeline(io.netty.channel.ChannelPipeline) Promise(io.netty.util.concurrent.Promise) NettyHBaseRpcConnectionHeaderHandler(org.apache.hadoop.hbase.security.NettyHBaseRpcConnectionHeaderHandler) SaslChallengeDecoder(org.apache.hadoop.hbase.security.SaslChallengeDecoder) NettyHBaseSaslRpcClientHandler(org.apache.hadoop.hbase.security.NettyHBaseSaslRpcClientHandler) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Example 3 with Future

use of io.netty.util.concurrent.Future in project jersey by jersey.

the class NettyHttpContainerProvider method createServer.

/**
     * Create and start Netty server.
     *
     * @param baseUri       base uri.
     * @param configuration Jersey configuration.
     * @param sslContext    Netty SSL context (can be null).
     * @param block         when {@code true}, this method will block until the server is stopped. When {@code false}, the
     *                      execution will
     *                      end immediately after the server is started.
     * @return Netty channel instance.
     * @throws ProcessingException when there is an issue with creating new container.
     */
public static Channel createServer(final URI baseUri, final ResourceConfig configuration, SslContext sslContext, final boolean block) throws ProcessingException {
    // Configure the server.
    final EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    final EventLoopGroup workerGroup = new NioEventLoopGroup();
    final NettyHttpContainer container = new NettyHttpContainer(configuration);
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new JerseyServerInitializer(baseUri, sslContext, container));
        int port = getPort(baseUri);
        Channel ch = b.bind(port).sync().channel();
        ch.closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() {

            @Override
            public void operationComplete(Future<? super Void> future) throws Exception {
                container.getApplicationHandler().onShutdown(container);
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        });
        if (block) {
            ch.closeFuture().sync();
            return ch;
        } else {
            return ch;
        }
    } catch (InterruptedException e) {
        throw new ProcessingException(e);
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ProcessingException(javax.ws.rs.ProcessingException) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Future(io.netty.util.concurrent.Future) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ProcessingException(javax.ws.rs.ProcessingException)

Example 4 with Future

use of io.netty.util.concurrent.Future in project jersey by jersey.

the class JerseyHttp2ServerHandler method createContainerRequest.

/**
     * Create Jersey {@link ContainerRequest} based on Netty {@link HttpRequest}.
     *
     * @param ctx          Netty channel context.
     * @param http2Headers Netty Http/2 headers.
     * @return created Jersey Container Request.
     */
private ContainerRequest createContainerRequest(ChannelHandlerContext ctx, Http2HeadersFrame http2Headers) {
    String path = http2Headers.headers().path().toString();
    String s = path.startsWith("/") ? path.substring(1) : path;
    URI requestUri = URI.create(baseUri + ContainerUtils.encodeUnsafeCharacters(s));
    ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri, http2Headers.headers().method().toString(), getSecurityContext(), new PropertiesDelegate() {

        private final Map<String, Object> properties = new HashMap<>();

        @Override
        public Object getProperty(String name) {
            return properties.get(name);
        }

        @Override
        public Collection<String> getPropertyNames() {
            return properties.keySet();
        }

        @Override
        public void setProperty(String name, Object object) {
            properties.put(name, object);
        }

        @Override
        public void removeProperty(String name) {
            properties.remove(name);
        }
    });
    // request entity handling.
    if (!http2Headers.isEndStream()) {
        ctx.channel().closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() {

            @Override
            public void operationComplete(Future<? super Void> future) throws Exception {
                isList.add(NettyInputStream.END_OF_INPUT_ERROR);
            }
        });
        requestContext.setEntityStream(new NettyInputStream(isList));
    } else {
        requestContext.setEntityStream(new InputStream() {

            @Override
            public int read() throws IOException {
                return -1;
            }
        });
    }
    // copying headers from netty request to jersey container request context.
    for (CharSequence name : http2Headers.headers().names()) {
        requestContext.headers(name.toString(), mapToString(http2Headers.headers().getAll(name)));
    }
    return requestContext;
}
Also used : HashMap(java.util.HashMap) NettyInputStream(org.glassfish.jersey.netty.connector.internal.NettyInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) NettyInputStream(org.glassfish.jersey.netty.connector.internal.NettyInputStream) URI(java.net.URI) IOException(java.io.IOException) Collection(java.util.Collection) Future(io.netty.util.concurrent.Future) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) PropertiesDelegate(org.glassfish.jersey.internal.PropertiesDelegate)

Example 5 with Future

use of io.netty.util.concurrent.Future in project jersey by jersey.

the class JerseyServerHandler method createContainerRequest.

/**
     * Create Jersey {@link ContainerRequest} based on Netty {@link HttpRequest}.
     *
     * @param ctx Netty channel context.
     * @param req Netty Http request.
     * @return created Jersey Container Request.
     */
private ContainerRequest createContainerRequest(ChannelHandlerContext ctx, HttpRequest req) {
    String s = req.uri().startsWith("/") ? req.uri().substring(1) : req.uri();
    URI requestUri = URI.create(baseUri + ContainerUtils.encodeUnsafeCharacters(s));
    ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri, req.method().name(), getSecurityContext(), new PropertiesDelegate() {

        private final Map<String, Object> properties = new HashMap<>();

        @Override
        public Object getProperty(String name) {
            return properties.get(name);
        }

        @Override
        public Collection<String> getPropertyNames() {
            return properties.keySet();
        }

        @Override
        public void setProperty(String name, Object object) {
            properties.put(name, object);
        }

        @Override
        public void removeProperty(String name) {
            properties.remove(name);
        }
    });
    // request entity handling.
    if ((req.headers().contains(HttpHeaderNames.CONTENT_LENGTH) && HttpUtil.getContentLength(req) > 0) || HttpUtil.isTransferEncodingChunked(req)) {
        ctx.channel().closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() {

            @Override
            public void operationComplete(Future<? super Void> future) throws Exception {
                isList.add(NettyInputStream.END_OF_INPUT_ERROR);
            }
        });
        requestContext.setEntityStream(new NettyInputStream(isList));
    } else {
        requestContext.setEntityStream(new InputStream() {

            @Override
            public int read() throws IOException {
                return -1;
            }
        });
    }
    // copying headers from netty request to jersey container request context.
    for (String name : req.headers().names()) {
        requestContext.headers(name, req.headers().getAll(name));
    }
    return requestContext;
}
Also used : HashMap(java.util.HashMap) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) NettyInputStream(org.glassfish.jersey.netty.connector.internal.NettyInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) NettyInputStream(org.glassfish.jersey.netty.connector.internal.NettyInputStream) URI(java.net.URI) IOException(java.io.IOException) Collection(java.util.Collection) Future(io.netty.util.concurrent.Future) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) PropertiesDelegate(org.glassfish.jersey.internal.PropertiesDelegate)

Aggregations

Future (io.netty.util.concurrent.Future)46 FutureListener (io.netty.util.concurrent.FutureListener)29 RFuture (org.redisson.api.RFuture)22 ChannelFuture (io.netty.channel.ChannelFuture)15 ChannelFutureListener (io.netty.channel.ChannelFutureListener)11 Channel (io.netty.channel.Channel)9 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 Timeout (io.netty.util.Timeout)8 TimerTask (io.netty.util.TimerTask)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 RedisException (org.redisson.client.RedisException)7 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)6 Collection (java.util.Collection)6 RedisConnection (org.redisson.client.RedisConnection)6 RedisConnectionException (org.redisson.client.RedisConnectionException)6 MasterSlaveEntry (org.redisson.connection.MasterSlaveEntry)6 EventLoopGroup (io.netty.channel.EventLoopGroup)5