Package com.facebook.presto.jdbc.internal.netty.channel

Examples of com.facebook.presto.jdbc.internal.netty.channel.Channel


                long wait = getTimeToWait(readLimit,
                        trafficCounter.getCurrentReadBytes(),
                        trafficCounter.getLastTime(), curtime);
                if (wait >= MINIMAL_WAIT) { // At least 10ms seems a minimal
                                            // time in order to
                    Channel channel = ctx.getChannel();
                    // try to limit the traffic
                    if (channel != null && channel.isConnected()) {
                        // Channel version
                        if (timer == null) {
                            // Sleep since no executor
                            // logger.warn("Read sleep since no timer for "+wait+" ms for "+this);
                            if (release.get()) {
                                return;
                            }
                            Thread.sleep(wait);
                            return;
                        }
                        if (ctx.getAttachment() == null) {
                            // readSuspended = true;
                            ctx.setAttachment(Boolean.TRUE);
                            channel.setReadable(false);
                            // logger.warn("Read will wakeup after "+wait+" ms "+this);
                            TimerTask timerTask = new ReopenReadTimerTask(ctx);
                            timeout = timer.newTimeout(timerTask, wait,
                                    TimeUnit.MILLISECONDS);
                        } else {
View Full Code Here


        } catch (Exception e) {
            throw new ChannelPipelineException("Failed to initialize a pipeline.", e);
        }

        // Set the options.
        Channel ch = getFactory().newChannel(pipeline);
        boolean success = false;
        try {
            ch.getConfig().setOptions(getOptions());
            success = true;
        } finally {
            if (!success) {
                ch.close();
            }
        }

        // Bind.
        if (localAddress != null) {
            ch.bind(localAddress);
        }

        // Connect.
        return ch.connect(remoteAddress);
    }
View Full Code Here

        } catch (Exception e) {
            throw new ChannelPipelineException("Failed to initialize a pipeline.", e);
        }

        // Set the options.
        Channel ch = getFactory().newChannel(pipeline);
        boolean success = false;
        try {
            ch.getConfig().setOptions(getOptions());
            success = true;
        } finally {
            if (!success) {
                ch.close();
            }
        }

        // Bind.
        return ch.bind(localAddress);
    }
View Full Code Here

            }
        }

        // Remove the entry when the channel closes.
        if (e instanceof ChannelStateEvent) {
            Channel channel = e.getChannel();
            ChannelStateEvent se = (ChannelStateEvent) e;
            if (se.getState() == ChannelState.OPEN &&
                !channel.isOpen()) {
                removeChildExecutor(key);
            }
        }
        return executor;
    }
View Full Code Here

        final ChannelHandlerContext ctx = this.ctx;
        if (ctx == null) {
            // No write request was made.
            return;
        }
        Channel channel = ctx.getChannel();
        boolean acquired;

        // use CAS to see if the have flush already running, if so we don't need to take further actions
        if (acquired = flush.compareAndSet(false, true)) {
            final Queue<MessageEvent> queue = getQueue();
            if (consolidateOnFlush) {
                if (queue.isEmpty()) {
                    flush.set(false);
                    return;
                }

                List<MessageEvent> pendingWrites = new ArrayList<MessageEvent>();
                for (;;) {
                    MessageEvent e = queue.poll();
                    if (e == null) {
                        break;
                    }
                    if (!(e.getMessage() instanceof ChannelBuffer)) {
                        if ((pendingWrites = consolidatedWrite(pendingWrites)) == null) {
                            pendingWrites = new ArrayList<MessageEvent>();
                        }
                        ctx.sendDownstream(e);
                    } else {
                        pendingWrites.add(e);
                    }
                }
                consolidatedWrite(pendingWrites);
            } else {
                for (;;) {
                    MessageEvent e = queue.poll();
                    if (e == null) {
                        break;
                    }
                    ctx.sendDownstream(e);
                }
            }
           flush.set(false);
       }

        if (acquired && (!channel.isConnected() || channel.isWritable() && !queue.isEmpty())) {
            flush(consolidateOnFlush);
        }
    }
View Full Code Here

    @Override
    public void messageReceived(ChannelHandlerContext context, MessageEvent event)
            throws Exception
    {
        Channel channel = context.getChannel();
        NettyResponseFuture<?, ?> nettyResponseFuture = (NettyResponseFuture<?, ?>) context.getAttachment();

        HttpResponse response;
        try {
            response = (HttpResponse) event.getMessage();
View Full Code Here

        }

        boolean offered = queue.offer((MessageEvent) e);
        assert offered;

        final Channel channel = ctx.getChannel();
        // call flush if the channel is writable or not connected. flush(..) will take care of the rest

        if (channel.isWritable() || !channel.isConnected()) {
            this.ctx = ctx;
            flush(ctx, false);
        }
    }
View Full Code Here

        }
    }

    private void flush(ChannelHandlerContext ctx, boolean fireNow) throws Exception {
        boolean acquired;
        final Channel channel = ctx.getChannel();
        boolean suspend = false;
        flushNeeded = true;
        // use CAS to see if the have flush already running, if so we don't need to take futher actions
        if (acquired = flush.compareAndSet(false, true)) {
            flushNeeded = false;
            try {
                if (!channel.isConnected()) {
                    discard(ctx, fireNow);
                    return;
                }

                while (channel.isWritable()) {
                    if (currentEvent == null) {
                        currentEvent = queue.poll();
                    }

                    if (currentEvent == null) {
                        break;
                    }

                    if (currentEvent.getFuture().isDone()) {
                        // Skip the current request because the previous partial write
                        // attempt for the current request has been failed.
                        currentEvent = null;
                    } else {
                        final MessageEvent currentEvent = this.currentEvent;
                        Object m = currentEvent.getMessage();
                        if (m instanceof ChunkedInput) {
                            final ChunkedInput chunks = (ChunkedInput) m;
                            Object chunk;
                            boolean endOfInput;
                            try {
                                chunk = chunks.nextChunk();
                                endOfInput = chunks.isEndOfInput();
                                if (chunk == null) {
                                    chunk = ChannelBuffers.EMPTY_BUFFER;
                                    // No need to suspend when reached at the end.
                                    suspend = !endOfInput;
                                } else {
                                    suspend = false;
                                }
                            } catch (Throwable t) {
                                this.currentEvent = null;

                                currentEvent.getFuture().setFailure(t);
                                if (fireNow) {
                                    fireExceptionCaught(ctx, t);
                                } else {
                                    fireExceptionCaughtLater(ctx, t);
                                }

                                closeInput(chunks);
                                break;
                            }

                            if (suspend) {
                                // ChunkedInput.nextChunk() returned null and it has
                                // not reached at the end of input.  Let's wait until
                                // more chunks arrive.  Nothing to write or notify.
                                break;
                            } else {
                                ChannelFuture writeFuture;
                                if (endOfInput) {
                                    this.currentEvent = null;
                                    writeFuture = currentEvent.getFuture();

                                    // Register a listener which will close the input once the write
                                    // is complete. This is needed because the Chunk may have some
                                    // resource bound that can not be closed before its not written
                                    //
                                    // See https://github.com/netty/netty/issues/303
                                    writeFuture.addListener(new ChannelFutureListener() {

                                        public void operationComplete(ChannelFuture future) throws Exception {
                                            closeInput(chunks);
                                        }
                                    });
                                } else {
                                    writeFuture = future(channel);
                                    writeFuture.addListener(new ChannelFutureListener() {
                                        public void operationComplete(ChannelFuture future) throws Exception {
                                            if (!future.isSuccess()) {
                                                currentEvent.getFuture().setFailure(future.getCause());
                                                closeInput((ChunkedInput) currentEvent.getMessage());
                                            }
                                        }
                                    });
                                }

                                write(
                                        ctx, writeFuture, chunk,
                                        currentEvent.getRemoteAddress());
                            }
                        } else {
                            this.currentEvent = null;
                            ctx.sendDownstream(currentEvent);
                        }
                    }

                    if (!channel.isConnected()) {
                        discard(ctx, fireNow);
                        return;
                    }
                }
            } finally {
                // mark the flush as done
                flush.set(false);
            }
        }

        if (acquired && (!channel.isConnected() || channel.isWritable() && !queue.isEmpty() && !suspend
                || flushNeeded)) {
            flush(ctx, fireNow);
        }
    }
View Full Code Here

    }

    private void connectionPermitAcquired(boolean isSsl, InetSocketAddress remoteAddress, ConnectionCallback connectionCallback)
    {
        Preconditions.checkState(enablePooling, "Pooling is not enabled");
        Channel channel = null;
        synchronized (this) {
            PoolKey key = new PoolKey(isSsl, remoteAddress);

            // find an existing connected channel
            List<Channel> channels = channelCache.get(key);
            while (channel == null && !channels.isEmpty()) {
                // remove last
                channel = channels.remove(channels.size() - 1);

                if (!channel.isConnected()) {
                    channel.close();
                    channel = null;
                }
            }

            if (channel == null) {
                // we did not find an existing connection, so we will create a new connection
                // if there are already too many pooled connection, destroy some

                int pooledConnectionCount = channelCache.size();
                int checkedOutConnectionCount = this.checkedOutConnections.get();

                int connectionsToDestroy = (checkedOutConnectionCount + pooledConnectionCount + 1) - maxConnections;
                for (int i = 0; !channels.isEmpty() && i < connectionsToDestroy; i++) {
                    Channel victim = channels.remove(channels.size() - 1);
                    victim.close();
                }
            }
        }

        checkedOutConnections.incrementAndGet();
View Full Code Here

        @Override
        public void operationComplete(ChannelFuture future)
                throws Exception
        {
            if (future.isSuccess()) {
                Channel channel = future.getChannel();
                try {
                    openChannels.add(channel);

                    // todo add close callback handler to remove this from the cache

                    connectionCallback.run(channel);
                }
                catch (Throwable e) {
                    try {
                        channel.close();
                    }
                    finally {
                        connectionCallback.onError(e);
                    }
                }
View Full Code Here

TOP

Related Classes of com.facebook.presto.jdbc.internal.netty.channel.Channel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.