Search in sources :

Example 91 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project cassandra by apache.

the class EntireSSTableStreamConcurrentComponentMutationTest method createMockNettyChannel.

private EmbeddedChannel createMockNettyChannel(ByteBuf serializedFile) {
    WritableByteChannel wbc = new WritableByteChannel() {

        private boolean isOpen = true;

        public int write(ByteBuffer src) {
            int size = src.limit();
            serializedFile.writeBytes(src);
            return size;
        }

        public boolean isOpen() {
            return isOpen;
        }

        public void close() {
            isOpen = false;
        }
    };
    return new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            if (msg instanceof BufferPoolAllocator.Wrapped) {
                ByteBuffer buf = ((BufferPoolAllocator.Wrapped) msg).adopt();
                wbc.write(buf);
            } else {
                ((SharedDefaultFileRegion) msg).transferTo(wbc, 0);
            }
            super.write(ctx, msg, promise);
        }
    });
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) BufferPoolAllocator(org.apache.cassandra.net.BufferPoolAllocator) ByteBuffer(java.nio.ByteBuffer) RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) IOException(java.io.IOException)

Example 92 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project cassandra by apache.

the class CassandraEntireSSTableStreamWriterTest method testBlockWriterOverWire.

@Test
public void testBlockWriterOverWire() throws IOException {
    StreamSession session = setupStreamingSessionForTest();
    EmbeddedChannel channel = new EmbeddedChannel();
    try (AsyncStreamingOutputPlus out = new AsyncStreamingOutputPlus(channel);
        ComponentContext context = ComponentContext.create(descriptor)) {
        CassandraEntireSSTableStreamWriter writer = new CassandraEntireSSTableStreamWriter(sstable, session, context);
        writer.write(out);
        Queue msgs = channel.outboundMessages();
        assertTrue(msgs.peek() instanceof DefaultFileRegion);
    }
}
Also used : StreamSession(org.apache.cassandra.streaming.StreamSession) AsyncStreamingOutputPlus(org.apache.cassandra.net.AsyncStreamingOutputPlus) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) SharedDefaultFileRegion(org.apache.cassandra.net.SharedDefaultFileRegion) DefaultFileRegion(io.netty.channel.DefaultFileRegion) Queue(java.util.Queue) Test(org.junit.Test)

Example 93 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project cassandra by apache.

the class CassandraEntireSSTableStreamWriterTest method testBlockReadingAndWritingOverWire.

@Test
public void testBlockReadingAndWritingOverWire() throws Throwable {
    StreamSession session = setupStreamingSessionForTest();
    InetAddressAndPort peer = FBUtilities.getBroadcastAddressAndPort();
    // This is needed as Netty releases the ByteBuffers as soon as the channel is flushed
    ByteBuf serializedFile = Unpooled.buffer(8192);
    EmbeddedChannel channel = createMockNettyChannel(serializedFile);
    try (AsyncStreamingOutputPlus out = new AsyncStreamingOutputPlus(channel);
        ComponentContext context = ComponentContext.create(descriptor)) {
        CassandraEntireSSTableStreamWriter writer = new CassandraEntireSSTableStreamWriter(sstable, session, context);
        writer.write(out);
        session.prepareReceiving(new StreamSummary(sstable.metadata().id, 1, 5104));
        CassandraStreamHeader header = CassandraStreamHeader.builder().withSSTableFormat(sstable.descriptor.formatType).withSSTableVersion(sstable.descriptor.version).withSSTableLevel(0).withEstimatedKeys(sstable.estimatedKeys()).withSections(Collections.emptyList()).withSerializationHeader(sstable.header.toComponent()).withComponentManifest(context.manifest()).isEntireSSTable(true).withFirstKey(sstable.first).withTableId(sstable.metadata().id).build();
        CassandraEntireSSTableStreamReader reader = new CassandraEntireSSTableStreamReader(new StreamMessageHeader(sstable.metadata().id, peer, session.planId(), false, 0, 0, 0, null), header, session);
        SSTableMultiWriter sstableWriter = reader.read(new DataInputBuffer(serializedFile.nioBuffer(), false));
        Collection<SSTableReader> newSstables = sstableWriter.finished();
        assertEquals(1, newSstables.size());
    }
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) AsyncStreamingOutputPlus(org.apache.cassandra.net.AsyncStreamingOutputPlus) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) StreamSession(org.apache.cassandra.streaming.StreamSession) StreamSummary(org.apache.cassandra.streaming.StreamSummary) StreamMessageHeader(org.apache.cassandra.streaming.messages.StreamMessageHeader) SSTableMultiWriter(org.apache.cassandra.io.sstable.SSTableMultiWriter) Test(org.junit.Test)

Example 94 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project cassandra by apache.

the class BigTableZeroCopyWriterTest method writeDataFile_RebufferingByteBufDataInputPlus.

@Test
public void writeDataFile_RebufferingByteBufDataInputPlus() {
    try (AsyncStreamingInputPlus input = new AsyncStreamingInputPlus(new EmbeddedChannel())) {
        writeDataTestCycle(buffer -> {
            input.append(Unpooled.wrappedBuffer(buffer));
            return input;
        });
        input.requestClosure();
    }
}
Also used : AsyncStreamingInputPlus(org.apache.cassandra.net.AsyncStreamingInputPlus) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 95 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project cassandra by apache.

the class AsyncStreamingOutputPlusTest method testWriteFileToChannel.

private void testWriteFileToChannel(boolean zeroCopy) throws IOException {
    File file = populateTempData("zero_copy_" + zeroCopy);
    int length = (int) file.length();
    EmbeddedChannel channel = new TestChannel(4);
    StreamManager.StreamRateLimiter limiter = zeroCopy ? StreamManager.getEntireSSTableRateLimiter(FBUtilities.getBroadcastAddressAndPort()) : StreamManager.getRateLimiter(FBUtilities.getBroadcastAddressAndPort());
    try (FileChannel fileChannel = file.newReadChannel();
        AsyncStreamingOutputPlus out = new AsyncStreamingOutputPlus(channel)) {
        assertTrue(fileChannel.isOpen());
        if (zeroCopy)
            out.writeFileToChannelZeroCopy(fileChannel, limiter, length, length, length * 2);
        else
            out.writeFileToChannel(fileChannel, limiter, length);
        assertEquals(length, out.flushed());
        assertEquals(length, out.flushedToNetwork());
        assertEquals(length, out.position());
        assertFalse(fileChannel.isOpen());
    }
}
Also used : StreamManager(org.apache.cassandra.streaming.StreamManager) FileChannel(java.nio.channels.FileChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) File(org.apache.cassandra.io.util.File)

Aggregations

EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1027 Test (org.junit.jupiter.api.Test)515 ByteBuf (io.netty.buffer.ByteBuf)356 Test (org.junit.Test)342 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)85 HttpResponse (io.netty.handler.codec.http.HttpResponse)73 HttpRequest (io.netty.handler.codec.http.HttpRequest)69 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)64 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)60 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)55 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)50 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)49 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)46 EmbeddedChannel (org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel)42 IOException (java.io.IOException)38 InetSocketAddress (java.net.InetSocketAddress)38 Executable (org.junit.jupiter.api.function.Executable)36 ArrayList (java.util.ArrayList)35 Before (org.junit.Before)32 ChannelHandler (io.netty.channel.ChannelHandler)27