Search in sources :

Example 56 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project pravega by pravega.

the class DirectMemoryCache method get.

@Override
public BufferView get(int address) {
    Exceptions.checkNotClosed(this.closed.get(), this);
    List<ByteBuf> readBuffers = new ArrayList<>();
    while (address != CacheLayout.NO_ADDRESS) {
        // Locate the Buffer-Block for the current address.
        int bufferId = this.layout.getBufferId(address);
        int blockId = this.layout.getBlockId(address);
        DirectMemoryBuffer b = this.buffers[bufferId];
        // Fetch the read data into our buffer collection and then set the address to the next in the chain.
        address = b.read(blockId, readBuffers);
    }
    if (readBuffers.isEmpty()) {
        // Couldn't read anything, so this address must not point to anything.
        return null;
    } else {
        // Compose the result and return it.
        ByteBuf first = readBuffers.get(0);
        ByteBuf result = readBuffers.size() == 1 ? first : new CompositeByteBuf(first.alloc(), false, readBuffers.size(), Lists.reverse(readBuffers));
        this.metrics.get(result.readableBytes());
        return new NonReleaseableByteBufWrapper(result);
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ArrayList(java.util.ArrayList) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 57 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project mongo-java-driver by mongodb.

the class NettyStream method readAsync.

/**
 * @param numBytes Must be equal to {@link #pendingReader}{@code .numBytes} when called by a Netty channel handler.
 * @param handler Must be equal to {@link #pendingReader}{@code .handler} when called by a Netty channel handler.
 * @param readTimeoutMillis Must be equal to {@link #NO_SCHEDULE_TIME} when called by a Netty channel handler.
 *                          Timeouts may be scheduled only by the public read methods. Taking into account that concurrent pending
 *                          readers are not allowed, there must not be a situation when threads attempt to schedule a timeout
 *                          before the previous one is either cancelled or completed.
 */
private void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler, final long readTimeoutMillis) {
    ByteBuf buffer = null;
    Throwable exceptionResult = null;
    synchronized (this) {
        exceptionResult = pendingException;
        if (exceptionResult == null) {
            if (!hasBytesAvailable(numBytes)) {
                if (pendingReader == null) {
                    // called by a public read method
                    pendingReader = new PendingReader(numBytes, handler, scheduleReadTimeout(readTimeoutTask, readTimeoutMillis));
                }
            } else {
                CompositeByteBuf composite = allocator.compositeBuffer(pendingInboundBuffers.size());
                int bytesNeeded = numBytes;
                for (Iterator<io.netty.buffer.ByteBuf> iter = pendingInboundBuffers.iterator(); iter.hasNext(); ) {
                    io.netty.buffer.ByteBuf next = iter.next();
                    int bytesNeededFromCurrentBuffer = Math.min(next.readableBytes(), bytesNeeded);
                    if (bytesNeededFromCurrentBuffer == next.readableBytes()) {
                        composite.addComponent(next);
                        iter.remove();
                    } else {
                        next.retain();
                        composite.addComponent(next.readSlice(bytesNeededFromCurrentBuffer));
                    }
                    composite.writerIndex(composite.writerIndex() + bytesNeededFromCurrentBuffer);
                    bytesNeeded -= bytesNeededFromCurrentBuffer;
                    if (bytesNeeded == 0) {
                        break;
                    }
                }
                buffer = new NettyByteBuf(composite).flip();
            }
        }
        if (// the read operation has completed
        !(exceptionResult == null && buffer == null) && pendingReader != null) {
            // we need to clear the pending reader
            cancel(pendingReader.timeout);
            this.pendingReader = null;
        }
    }
    if (exceptionResult != null) {
        handler.failed(exceptionResult);
    }
    if (buffer != null) {
        handler.completed(buffer);
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(org.bson.ByteBuf)

Example 58 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project grpc-java by grpc.

the class NettyHandlerTestBase method captureWrite.

protected final ByteBuf captureWrite(ChannelHandlerContext ctx) {
    ArgumentCaptor<ByteBuf> captor = ArgumentCaptor.forClass(ByteBuf.class);
    verify(ctx, atLeastOnce()).write(captor.capture(), any(ChannelPromise.class));
    CompositeByteBuf composite = Unpooled.compositeBuffer();
    for (ByteBuf buf : captor.getAllValues()) {
        composite.addComponent(buf);
        composite.writerIndex(composite.writerIndex() + buf.readableBytes());
    }
    return composite;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ChannelPromise(io.netty.channel.ChannelPromise) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 59 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project grpc-java by grpc.

the class AltsProtocolNegotiatorTest method protectShouldRoundtrip.

@Test
// List cast
@SuppressWarnings("unchecked")
public void protectShouldRoundtrip() throws Exception {
    doHandshake();
    // Write the message 1 character at a time. The message should be buffered
    // and not interfere with the handshake.
    final AtomicInteger writeCount = new AtomicInteger();
    String message = "hello";
    for (int ix = 0; ix < message.length(); ++ix) {
        ByteBuf in = Unpooled.copiedBuffer(message, ix, 1, UTF_8);
        // go/futurereturn-lsc
        @SuppressWarnings("unused") Future<?> possiblyIgnoredError = channel.write(in).addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    writeCount.incrementAndGet();
                }
            }
        });
    }
    channel.flush();
    // Capture the protected data written to the wire.
    assertEquals(1, channel.outboundMessages().size());
    ByteBuf protectedData = channel.readOutbound();
    assertEquals(message.length(), writeCount.get());
    // Read the protected message at the server and verify it matches the original message.
    TsiFrameProtector serverProtector = serverHandshaker.createFrameProtector(channel.alloc());
    List<ByteBuf> unprotected = new ArrayList<>();
    serverProtector.unprotect(protectedData, (List<Object>) (List<?>) unprotected, channel.alloc());
    // We try our best to remove the HTTP2 handler as soon as possible, but just by constructing it
    // a settings frame is written (and an HTTP2 preface).  This is hard coded into Netty, so we
    // have to remove it here.  See {@code Http2ConnectionHandler.PrefaceDecode.sendPreface}.
    int settingsFrameLength = 9;
    CompositeByteBuf unprotectedAll = new CompositeByteBuf(channel.alloc(), false, unprotected.size() + 1, unprotected);
    ByteBuf unprotectedData = unprotectedAll.slice(settingsFrameLength, message.length());
    assertEquals(message, unprotectedData.toString(UTF_8));
    // Protect the same message at the server.
    final AtomicReference<ByteBuf> newlyProtectedData = new AtomicReference<>();
    serverProtector.protectFlush(Collections.singletonList(unprotectedData), new Consumer<ByteBuf>() {

        @Override
        public void accept(ByteBuf buf) {
            newlyProtectedData.set(buf);
        }
    }, channel.alloc());
    // Read the protected message at the client and verify that it matches the original message.
    channel.writeInbound(newlyProtectedData.get());
    assertEquals(1, channel.inboundMessages().size());
    assertEquals(message, channel.<ByteBuf>readInbound().toString(UTF_8));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) GeneralSecurityException(java.security.GeneralSecurityException) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 60 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project ambry by linkedin.

the class WriteCallback method writeToChannel.

/**
 * Writes {@code writeCount} number of random chunks to the given {@link ByteBufferAsyncWritableChannel}.
 * @param writeCount the number of chunks to write.
 */
public void writeToChannel(int writeCount) {
    for (int i = 0; i < writeCount; i++) {
        WriteCallback writeCallback = new WriteCallback(i);
        byte[] data = new byte[100];
        random.nextBytes(data);
        Future<Long> future = null;
        if (useNettyByteBuf) {
            ByteBuf chunk = null;
            if (!useCompositeByteBuf) {
                chunk = ByteBufAllocator.DEFAULT.heapBuffer(data.length);
                chunk.writeBytes(data);
            } else {
                CompositeByteBuf composite = ByteBufAllocator.DEFAULT.compositeHeapBuffer(100);
                ByteBuf c = ByteBufAllocator.DEFAULT.heapBuffer(50);
                c.writeBytes(data, 0, 50);
                composite.addComponent(true, c);
                c = ByteBufAllocator.DEFAULT.heapBuffer(50);
                c.writeBytes(data, 50, 50);
                composite.addComponent(true, c);
                chunk = composite;
            }
            final ByteBuf finalByteBuf = chunk;
            future = channel.write(finalByteBuf, (result, exception) -> {
                finalByteBuf.release();
                writeCallback.onCompletion(result, exception);
            });
        } else {
            ByteBuffer chunk = ByteBuffer.wrap(data);
            future = channel.write(chunk, writeCallback);
        }
        writes.add(new WriteData(data, future, writeCallback));
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) Arrays(java.util.Arrays) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) ClosedChannelException(java.nio.channels.ClosedChannelException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Utils(com.github.ambry.utils.Utils) Random(java.util.Random) Test(org.junit.Test) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) List(java.util.List) Future(java.util.concurrent.Future) ByteBuf(io.netty.buffer.ByteBuf) After(org.junit.After) NettyByteBufLeakHelper(com.github.ambry.utils.NettyByteBufLeakHelper) Assert(org.junit.Assert) Before(org.junit.Before) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CompositeByteBuf (io.netty.buffer.CompositeByteBuf)86 ByteBuf (io.netty.buffer.ByteBuf)65 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)8 ByteBuffer (java.nio.ByteBuffer)7 ChannelFuture (io.netty.channel.ChannelFuture)6 Channel (io.netty.channel.Channel)5 ChannelFutureListener (io.netty.channel.ChannelFutureListener)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 Test (org.junit.jupiter.api.Test)4 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)3 CodecException (io.netty.handler.codec.CodecException)3 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)3 InetSocketAddress (java.net.InetSocketAddress)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 List (java.util.List)3 ExecutionException (java.util.concurrent.ExecutionException)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3