Search in sources :

Example 26 with DataInputPlus

use of org.apache.cassandra.io.util.DataInputPlus in project cassandra by apache.

the class BytesReadTrackerTest method internalTestBytesRead.

public void internalTestBytesRead(boolean inputStream) throws Exception {
    byte[] testData;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(baos);
    try {
        // boolean
        out.writeBoolean(true);
        // byte
        out.writeByte(0x1);
        // char
        out.writeChar('a');
        // short
        out.writeShort(1);
        // int
        out.writeInt(1);
        // long
        out.writeLong(1L);
        // float
        out.writeFloat(1.0f);
        // double
        out.writeDouble(1.0d);
        // String
        out.writeUTF("abc");
        testData = baos.toByteArray();
    } finally {
        out.close();
    }
    DataInputPlus.DataInputStreamPlus in = new DataInputPlus.DataInputStreamPlus(new ByteArrayInputStream(testData));
    BytesReadTracker tracker = inputStream ? new TrackedInputStream(in) : new TrackedDataInputPlus(in);
    DataInputPlus reader = inputStream ? new DataInputPlus.DataInputStreamPlus((TrackedInputStream) tracker) : (DataInputPlus) tracker;
    try {
        // boolean = 1byte
        boolean bool = reader.readBoolean();
        assertTrue(bool);
        assertEquals(1, tracker.getBytesRead());
        // byte = 1byte
        byte b = reader.readByte();
        assertEquals(b, 0x1);
        assertEquals(2, tracker.getBytesRead());
        // char = 2byte
        char c = reader.readChar();
        assertEquals('a', c);
        assertEquals(4, tracker.getBytesRead());
        // short = 2bytes
        short s = reader.readShort();
        assertEquals(1, s);
        assertEquals((short) 6, tracker.getBytesRead());
        // int = 4bytes
        int i = reader.readInt();
        assertEquals(1, i);
        assertEquals(10, tracker.getBytesRead());
        // long = 8bytes
        long l = reader.readLong();
        assertEquals(1L, l);
        assertEquals(18, tracker.getBytesRead());
        // float = 4bytes
        float f = reader.readFloat();
        assertEquals(1.0f, f, 0);
        assertEquals(22, tracker.getBytesRead());
        // double = 8bytes
        double d = reader.readDouble();
        assertEquals(1.0d, d, 0);
        assertEquals(30, tracker.getBytesRead());
        // String("abc") = 2(string size) + 3 = 5 bytes
        String str = reader.readUTF();
        assertEquals("abc", str);
        assertEquals(35, tracker.getBytesRead());
        assertEquals(testData.length, tracker.getBytesRead());
    } finally {
        in.close();
    }
    tracker.reset(0);
    assertEquals(0, tracker.getBytesRead());
}
Also used : DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TrackedDataInputPlus(org.apache.cassandra.io.util.TrackedDataInputPlus) BytesReadTracker(org.apache.cassandra.io.util.BytesReadTracker) ByteArrayInputStream(java.io.ByteArrayInputStream) TrackedInputStream(org.apache.cassandra.io.util.TrackedInputStream) TrackedDataInputPlus(org.apache.cassandra.io.util.TrackedDataInputPlus) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus)

Example 27 with DataInputPlus

use of org.apache.cassandra.io.util.DataInputPlus in project cassandra by apache.

the class ConnectionTest method testInsufficientSpace.

@Test
public void testInsufficientSpace() throws Throwable {
    test(new Settings(null).outbound(settings -> settings.withApplicationReserveSendQueueCapacityInBytes(1 << 15, new ResourceLimits.Concurrent(1 << 16)).withApplicationSendQueueCapacityInBytes(1 << 16)), (inbound, outbound, endpoint) -> {
        CountDownLatch done = new CountDownLatch(1);
        Message<?> message = Message.out(Verb._TEST_1, new Object());
        MessagingService.instance().callbacks.addWithExpiration(new RequestCallback() {

            @Override
            public void onFailure(InetAddressAndPort from, RequestFailureReason failureReason) {
                done.countDown();
            }

            @Override
            public boolean invokeOnFailure() {
                return true;
            }

            @Override
            public void onResponse(Message msg) {
                throw new IllegalStateException();
            }
        }, message, endpoint);
        AtomicInteger delivered = new AtomicInteger();
        unsafeSetSerializer(Verb._TEST_1, () -> new IVersionedSerializer<Object>() {

            public void serialize(Object o, DataOutputPlus out, int version) throws IOException {
                for (int i = 0; i <= 4 << 16; i += 8L) out.writeLong(1L);
            }

            public Object deserialize(DataInputPlus in, int version) throws IOException {
                in.skipBytesFully(4 << 16);
                return null;
            }

            public long serializedSize(Object o, int version) {
                return 4 << 16;
            }
        });
        unsafeSetHandler(Verb._TEST_1, () -> msg -> delivered.incrementAndGet());
        outbound.enqueue(message);
        Assert.assertTrue(done.await(10, SECONDS));
        Assert.assertEquals(0, delivered.get());
        check(outbound).submitted(1).sent(0, 0).pending(0, 0).overload(1, message.serializedSize(current_version)).expired(0, 0).error(0, 0).check();
        check(inbound).received(0, 0).processed(0, 0).pending(0, 0).expired(0, 0).error(0, 0).check();
    });
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) CommitLog(org.apache.cassandra.db.commitlog.CommitLog) Random(java.util.Random) EncryptionOptions(org.apache.cassandra.config.EncryptionOptions) Global.nanoTime(org.apache.cassandra.utils.Clock.Global.nanoTime) ChannelPromise(io.netty.channel.ChannelPromise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LARGE_MESSAGE_THRESHOLD(org.apache.cassandra.net.OutboundConnections.LARGE_MESSAGE_THRESHOLD) Map(java.util.Map) After(org.junit.After) IVersionedAsymmetricSerializer(org.apache.cassandra.io.IVersionedAsymmetricSerializer) DataOutputPlus(org.apache.cassandra.io.util.DataOutputPlus) UnknownColumnException(org.apache.cassandra.exceptions.UnknownColumnException) ToLongFunction(java.util.function.ToLongFunction) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) VERSION_40(org.apache.cassandra.net.MessagingService.VERSION_40) LZ4(org.apache.cassandra.net.OutboundConnectionSettings.Framing.LZ4) AfterClass(org.junit.AfterClass) Global.approxTime(org.apache.cassandra.utils.MonotonicClock.Global.approxTime) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) FBUtilities(org.apache.cassandra.utils.FBUtilities) Set(java.util.Set) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) SMALL_MESSAGES(org.apache.cassandra.net.ConnectionType.SMALL_MESSAGES) Executors(java.util.concurrent.Executors) Sets(com.google.common.collect.Sets) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ConnectionUtils(org.apache.cassandra.net.ConnectionUtils) VERSION_30(org.apache.cassandra.net.MessagingService.VERSION_30) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) NoPayload.noPayload(org.apache.cassandra.net.NoPayload.noPayload) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) BeforeClass(org.junit.BeforeClass) Config(org.apache.cassandra.config.Config) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MINUTES(java.util.concurrent.TimeUnit.MINUTES) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ImmutableList(com.google.common.collect.ImmutableList) ByteBuf(io.netty.buffer.ByteBuf) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IVersionedSerializer(org.apache.cassandra.io.IVersionedSerializer) ExecutorService(java.util.concurrent.ExecutorService) VERSION_3014(org.apache.cassandra.net.MessagingService.VERSION_3014) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Logger(org.slf4j.Logger) LARGE_MESSAGES(org.apache.cassandra.net.ConnectionType.LARGE_MESSAGES) IOException(java.io.IOException) Test(org.junit.Test) MessagingService.current_version(org.apache.cassandra.net.MessagingService.current_version) RequestFailureReason(org.apache.cassandra.exceptions.RequestFailureReason) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Assert(org.junit.Assert) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) RequestFailureReason(org.apache.cassandra.exceptions.RequestFailureReason) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) DataOutputPlus(org.apache.cassandra.io.util.DataOutputPlus) Test(org.junit.Test)

Example 28 with DataInputPlus

use of org.apache.cassandra.io.util.DataInputPlus in project cassandra by apache.

the class ConnectionTest method testSendLarge.

@Test
public void testSendLarge() throws Throwable {
    test((inbound, outbound, endpoint) -> {
        int version = outbound.settings().acceptVersions.max;
        int count = 10;
        CountDownLatch deliveryDone = new CountDownLatch(1);
        CountDownLatch receiveDone = new CountDownLatch(count);
        unsafeSetSerializer(Verb._TEST_1, () -> new IVersionedSerializer<Object>() {

            public void serialize(Object noPayload, DataOutputPlus out, int version) throws IOException {
                for (int i = 0; i < LARGE_MESSAGE_THRESHOLD + 1; ++i) out.writeByte(i);
            }

            public Object deserialize(DataInputPlus in, int version) throws IOException {
                in.skipBytesFully(LARGE_MESSAGE_THRESHOLD + 1);
                return noPayload;
            }

            public long serializedSize(Object noPayload, int version) {
                return LARGE_MESSAGE_THRESHOLD + 1;
            }
        });
        unsafeSetHandler(Verb._TEST_1, () -> msg -> receiveDone.countDown());
        Message<?> message = Message.builder(Verb._TEST_1, new Object()).withExpiresAt(nanoTime() + SECONDS.toNanos(30L)).build();
        for (int i = 0; i < count; ++i) outbound.enqueue(message);
        Assert.assertTrue(receiveDone.await(10, SECONDS));
        outbound.unsafeRunOnDelivery(deliveryDone::countDown);
        Assert.assertTrue(deliveryDone.await(10, SECONDS));
        check(outbound).submitted(10).sent(10, 10 * message.serializedSize(version)).pending(0, 0).overload(0, 0).expired(0, 0).error(0, 0).check();
        check(inbound).received(10, 10 * message.serializedSize(version)).processed(10, 10 * message.serializedSize(version)).pending(0, 0).expired(0, 0).error(0, 0).check();
    });
}
Also used : DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) DataOutputPlus(org.apache.cassandra.io.util.DataOutputPlus) Test(org.junit.Test)

Aggregations

DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)28 Test (org.junit.Test)15 DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)13 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)11 IOException (java.io.IOException)9 DataOutputPlus (org.apache.cassandra.io.util.DataOutputPlus)8 ByteBuffer (java.nio.ByteBuffer)6 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ArrayList (java.util.ArrayList)4 UUID (java.util.UUID)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Mutation (org.apache.cassandra.db.Mutation)4 BytesReadTracker (org.apache.cassandra.io.util.BytesReadTracker)4 TrackedDataInputPlus (org.apache.cassandra.io.util.TrackedDataInputPlus)4 TrackedInputStream (org.apache.cassandra.io.util.TrackedInputStream)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 TableMetadata (org.apache.cassandra.schema.TableMetadata)3 ByteBuf (io.netty.buffer.ByteBuf)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2