Search in sources :

Example 81 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project heron by twitter.

the class ConnectTest method testStart.

/**
   * Test connection
   */
@Test
public void testStart() throws Exception {
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(HOST, serverPort));
    SocketChannel socketChannel = null;
    try {
        runStreamManagerClient();
        socketChannel = serverSocketChannel.accept();
        configure(socketChannel);
        socketChannel.configureBlocking(false);
        close(serverSocketChannel);
        // Receive request
        IncomingPacket incomingPacket = new IncomingPacket();
        while (incomingPacket.readFromChannel(socketChannel) != 0) {
            // 1ms sleep to mitigate busy looping
            SysUtils.sleep(1);
        }
        // Send back response
        // Though we do not use typeName, we need to unpack it first,
        // since the order is required
        String typeName = incomingPacket.unpackString();
        REQID rid = incomingPacket.unpackREQID();
        OutgoingPacket outgoingPacket = new OutgoingPacket(rid, UnitTestHelper.getRegisterInstanceResponse());
        outgoingPacket.writeToChannel(socketChannel);
        for (int i = 0; i < Constants.RETRY_TIMES; i++) {
            InstanceControlMsg instanceControlMsg = inControlQueue.poll();
            if (instanceControlMsg != null) {
                nioLooper.exitLoop();
                threadsPool.shutdownNow();
                PhysicalPlanHelper physicalPlanHelper = instanceControlMsg.getNewPhysicalPlanHelper();
                Assert.assertEquals("test-bolt", physicalPlanHelper.getMyComponent());
                Assert.assertEquals(InetAddress.getLocalHost().getHostName(), physicalPlanHelper.getMyHostname());
                Assert.assertEquals(0, physicalPlanHelper.getMyInstanceIndex());
                Assert.assertEquals(1, physicalPlanHelper.getMyTaskId());
                break;
            } else {
                SysUtils.sleep(Constants.RETRY_INTERVAL_MS);
            }
        }
    } catch (ClosedChannelException ignored) {
    } finally {
        close(socketChannel);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) IncomingPacket(com.twitter.heron.common.network.IncomingPacket) InstanceControlMsg(com.twitter.heron.instance.InstanceControlMsg) PhysicalPlanHelper(com.twitter.heron.common.utils.misc.PhysicalPlanHelper) InetSocketAddress(java.net.InetSocketAddress) REQID(com.twitter.heron.common.network.REQID) ServerSocketChannel(java.nio.channels.ServerSocketChannel) OutgoingPacket(com.twitter.heron.common.network.OutgoingPacket) Test(org.junit.Test)

Example 82 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project undertow by undertow-io.

the class HeadStreamSinkConduit method write.

public long write(final ByteBuffer[] srcs, final int offset, final int length) throws IOException {
    if (anyAreSet(state, FLAG_CLOSE_COMPLETE)) {
        throw new ClosedChannelException();
    }
    long total = 0;
    for (int i = offset; i < offset + length; ++i) {
        ByteBuffer src = srcs[i];
        int remaining = src.remaining();
        total += remaining;
        src.position(src.position() + remaining);
    }
    return total;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) ByteBuffer(java.nio.ByteBuffer)

Example 83 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project undertow by undertow-io.

the class InflatingStreamSourceConduit method read.

@Override
public int read(ByteBuffer dst) throws IOException {
    if (isReadShutdown()) {
        throw new ClosedChannelException();
    }
    if (uncompressed != null) {
        int ret = Buffers.copy(dst, uncompressed.getBuffer());
        if (!uncompressed.getBuffer().hasRemaining()) {
            uncompressed.close();
            uncompressed = null;
        }
        return ret;
    }
    for (; ; ) {
        if (compressed == null && !nextDone) {
            compressed = exchange.getConnection().getByteBufferPool().getArrayBackedPool().allocate();
            ByteBuffer buf = compressed.getBuffer();
            int res = next.read(buf);
            if (res == -1) {
                nextDone = true;
                compressed.close();
                compressed = null;
            } else if (res == 0) {
                compressed.close();
                compressed = null;
                return 0;
            } else {
                buf.flip();
                if (!headerDone) {
                    headerDone = readHeader(buf);
                }
                inflater.setInput(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining());
            }
        }
        if (nextDone && inflater.needsInput() && !inflater.finished()) {
            throw UndertowLogger.ROOT_LOGGER.unexpectedEndOfCompressedInput();
        } else if (nextDone && inflater.finished()) {
            done();
            return -1;
        } else if (inflater.finished()) {
            int rem = inflater.getRemaining();
            ByteBuffer buf = compressed.getBuffer();
            buf.position(buf.limit() - rem);
            readFooter(buf);
            int res;
            do {
                buf.clear();
                res = next.read(buf);
                buf.flip();
                if (res == -1) {
                    done();
                    nextDone = true;
                    return -1;
                } else if (res > 0) {
                    readFooter(buf);
                }
            } while (res != 0);
            compressed.close();
            compressed = null;
            return 0;
        } else if (compressed == null) {
            throw new RuntimeException();
        }
        uncompressed = exchange.getConnection().getByteBufferPool().getArrayBackedPool().allocate();
        try {
            int read = inflater.inflate(uncompressed.getBuffer().array(), uncompressed.getBuffer().arrayOffset(), uncompressed.getBuffer().limit());
            uncompressed.getBuffer().limit(read);
            dataDeflated(uncompressed.getBuffer().array(), uncompressed.getBuffer().arrayOffset(), read);
            if (inflater.needsInput()) {
                compressed.close();
                compressed = null;
            }
            int ret = Buffers.copy(dst, uncompressed.getBuffer());
            if (!uncompressed.getBuffer().hasRemaining()) {
                uncompressed.close();
                uncompressed = null;
            }
            if (ret > 0) {
                return ret;
            }
        } catch (DataFormatException e) {
            done();
            throw new IOException(e);
        }
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer)

Example 84 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project undertow by undertow-io.

the class Http2Channel method sendRstStream.

public void sendRstStream(int streamId, int statusCode) {
    handleRstStream(streamId);
    if (UndertowLogger.REQUEST_IO_LOGGER.isDebugEnabled()) {
        UndertowLogger.REQUEST_IO_LOGGER.debugf(new ClosedChannelException(), "Sending rststream on channel %s stream %s", this, streamId);
    }
    Http2RstStreamSinkChannel channel = new Http2RstStreamSinkChannel(this, streamId, statusCode);
    flushChannelIgnoreFailure(channel);
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException)

Example 85 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project undertow by undertow-io.

the class HttpResponseConduit method processWrite.

/**
     * Handles writing out the header data. It can also take a byte buffer of user
     * data, to enable both user data and headers to be written out in a single operation,
     * which has a noticeable performance impact.
     * <p/>
     * It is up to the caller to note the current position of this buffer before and after they
     * call this method, and use this to figure out how many bytes (if any) have been written.
     *
     * @param state
     * @param userData
     * @return
     * @throws IOException
     */
private int processWrite(int state, final Object userData, int pos, int length) throws IOException {
    if (done || exchange == null) {
        throw new ClosedChannelException();
    }
    try {
        assert state != STATE_BODY;
        if (state == STATE_BUF_FLUSH) {
            final ByteBuffer byteBuffer = pooledBuffer.getBuffer();
            do {
                long res = 0;
                ByteBuffer[] data;
                if (userData == null || length == 0) {
                    res = next.write(byteBuffer);
                } else if (userData instanceof ByteBuffer) {
                    data = writevBuffer;
                    if (data == null) {
                        data = writevBuffer = new ByteBuffer[2];
                    }
                    data[0] = byteBuffer;
                    data[1] = (ByteBuffer) userData;
                    res = next.write(data, 0, 2);
                } else {
                    data = writevBuffer;
                    if (data == null || data.length < length + 1) {
                        data = writevBuffer = new ByteBuffer[length + 1];
                    }
                    data[0] = byteBuffer;
                    System.arraycopy(userData, pos, data, 1, length);
                    res = next.write(data, 0, data.length);
                }
                if (res == 0) {
                    return STATE_BUF_FLUSH;
                }
            } while (byteBuffer.hasRemaining());
            bufferDone();
            return STATE_BODY;
        } else if (state != STATE_START) {
            return processStatefulWrite(state, userData, pos, length);
        }
        //merge the cookies into the header map
        Connectors.flattenCookies(exchange);
        if (pooledBuffer == null) {
            pooledBuffer = pool.allocate();
        }
        ByteBuffer buffer = pooledBuffer.getBuffer();
        assert buffer.remaining() >= 50;
        exchange.getProtocol().appendTo(buffer);
        buffer.put((byte) ' ');
        int code = exchange.getStatusCode();
        assert 999 >= code && code >= 100;
        buffer.put((byte) (code / 100 + '0'));
        buffer.put((byte) (code / 10 % 10 + '0'));
        buffer.put((byte) (code % 10 + '0'));
        buffer.put((byte) ' ');
        String string = exchange.getReasonPhrase();
        if (string == null) {
            string = StatusCodes.getReason(code);
        }
        if (string.length() > buffer.remaining()) {
            pooledBuffer.close();
            pooledBuffer = null;
            truncateWrites();
            throw UndertowMessages.MESSAGES.reasonPhraseToLargeForBuffer(string);
        }
        writeString(buffer, string);
        buffer.put((byte) '\r').put((byte) '\n');
        int remaining = buffer.remaining();
        HeaderMap headers = exchange.getResponseHeaders();
        long fiCookie = headers.fastIterateNonEmpty();
        while (fiCookie != -1) {
            HeaderValues headerValues = headers.fiCurrent(fiCookie);
            HttpString header = headerValues.getHeaderName();
            int headerSize = header.length();
            int valueIdx = 0;
            while (valueIdx < headerValues.size()) {
                remaining -= (headerSize + 2);
                if (remaining < 0) {
                    this.fiCookie = fiCookie;
                    this.string = string;
                    this.headerValues = headerValues;
                    this.valueIdx = valueIdx;
                    this.charIndex = 0;
                    this.state = STATE_HDR_NAME;
                    buffer.flip();
                    return processStatefulWrite(STATE_HDR_NAME, userData, pos, length);
                }
                header.appendTo(buffer);
                buffer.put((byte) ':').put((byte) ' ');
                string = headerValues.get(valueIdx++);
                remaining -= (string.length() + 2);
                if (remaining < 2) {
                    //we use 2 here, to make sure we always have room for the final \r\n
                    this.fiCookie = fiCookie;
                    this.string = string;
                    this.headerValues = headerValues;
                    this.valueIdx = valueIdx;
                    this.charIndex = 0;
                    this.state = STATE_HDR_VAL;
                    buffer.flip();
                    return processStatefulWrite(STATE_HDR_VAL, userData, pos, length);
                }
                writeString(buffer, string);
                buffer.put((byte) '\r').put((byte) '\n');
            }
            fiCookie = headers.fiNextNonEmpty(fiCookie);
        }
        buffer.put((byte) '\r').put((byte) '\n');
        buffer.flip();
        do {
            long res = 0;
            ByteBuffer[] data;
            if (userData == null) {
                res = next.write(buffer);
            } else if (userData instanceof ByteBuffer) {
                data = writevBuffer;
                if (data == null) {
                    data = writevBuffer = new ByteBuffer[2];
                }
                data[0] = buffer;
                data[1] = (ByteBuffer) userData;
                res = next.write(data, 0, 2);
            } else {
                data = writevBuffer;
                if (data == null || data.length < length + 1) {
                    data = writevBuffer = new ByteBuffer[length + 1];
                }
                data[0] = buffer;
                System.arraycopy(userData, pos, data, 1, length);
                res = next.write(data, 0, length + 1);
            }
            if (res == 0) {
                return STATE_BUF_FLUSH;
            }
        } while (buffer.hasRemaining());
        bufferDone();
        return STATE_BODY;
    } catch (IOException | RuntimeException e) {
        //WFLY-4696, just to be safe
        if (pooledBuffer != null) {
            pooledBuffer.close();
            pooledBuffer = null;
        }
        throw e;
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) HeaderMap(io.undertow.util.HeaderMap) HeaderValues(io.undertow.util.HeaderValues) HttpString(io.undertow.util.HttpString) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) HttpString(io.undertow.util.HttpString)

Aggregations

ClosedChannelException (java.nio.channels.ClosedChannelException)211 ByteBuffer (java.nio.ByteBuffer)67 IOException (java.io.IOException)60 Test (org.junit.Test)23 InetSocketAddress (java.net.InetSocketAddress)19 SelectionKey (java.nio.channels.SelectionKey)18 SocketChannel (java.nio.channels.SocketChannel)15 ArrayList (java.util.ArrayList)13 NotYetConnectedException (java.nio.channels.NotYetConnectedException)11 InterruptedIOException (java.io.InterruptedIOException)10 CancelledKeyException (java.nio.channels.CancelledKeyException)10 ShutdownCommand (com.cloud.agent.api.ShutdownCommand)9 File (java.io.File)9 ServerSocketChannel (java.nio.channels.ServerSocketChannel)9 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)8 FileChannel (java.nio.channels.FileChannel)8 ConnectException (java.net.ConnectException)7 FsVolumeReference (org.apache.hadoop.hdfs.server.datanode.fsdataset.FsVolumeReference)6 AgentControlCommand (com.cloud.agent.api.AgentControlCommand)5 Command (com.cloud.agent.api.Command)5