Search in sources :

Example 51 with DataInputBuffer

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

the class MessageTest method testCycle.

// serialize (using both variants, all in one or header then rest), verify serialized size, deserialize, compare to the original
private void testCycle(Message msg, int version) throws IOException {
    try (DataOutputBuffer out = new DataOutputBuffer()) {
        serializer.serialize(msg, out, version);
        assertEquals(msg.serializedSize(version), out.getLength());
        // deserialize the message in one go, compare outcomes
        try (DataInputBuffer in = new DataInputBuffer(out.buffer(), true)) {
            Message msgOut = serializer.deserialize(in, msg.from(), version);
            assertEquals(0, in.available());
            assertMessagesEqual(msg, msgOut);
        }
        // extract header first, then deserialize the rest of the message and compare outcomes
        ByteBuffer buffer = out.buffer();
        try (DataInputBuffer in = new DataInputBuffer(out.buffer(), false)) {
            Message.Header headerOut = serializer.extractHeader(buffer, msg.from(), approxTime.now(), version);
            Message msgOut = serializer.deserialize(in, headerOut, version);
            assertEquals(0, in.available());
            assertMessagesEqual(msg, msgOut);
        }
    }
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 52 with DataInputBuffer

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

the class ForwardingInfoTest method testVersion.

private void testVersion(int version) throws Exception {
    InetAddressAndPort.initializeDefaultPort(65532);
    List<InetAddressAndPort> addresses = ImmutableList.of(InetAddressAndPort.getByNameOverrideDefaults("127.0.0.1", 42), InetAddressAndPort.getByName("127.0.0.1"), InetAddressAndPort.getByName("127.0.0.1:7000"), InetAddressAndPort.getByNameOverrideDefaults("2001:0db8:0000:0000:0000:ff00:0042:8329", 42), InetAddressAndPort.getByName("2001:0db8:0000:0000:0000:ff00:0042:8329"), InetAddressAndPort.getByName("[2001:0db8:0000:0000:0000:ff00:0042:8329]:7000"));
    ForwardingInfo ftc = new ForwardingInfo(addresses, new long[] { 44, 45, 46, 47, 48, 49 });
    ByteBuffer buffer;
    try (DataOutputBuffer dob = new DataOutputBuffer()) {
        ForwardingInfo.serializer.serialize(ftc, dob, version);
        buffer = dob.buffer();
    }
    assertEquals(buffer.remaining(), ForwardingInfo.serializer.serializedSize(ftc, version));
    ForwardingInfo deserialized;
    try (DataInputBuffer dib = new DataInputBuffer(buffer, false)) {
        deserialized = ForwardingInfo.serializer.deserialize(dib, version);
    }
    assertTrue(Arrays.equals(ftc.messageIds, deserialized.messageIds));
    Iterator<InetAddressAndPort> iterator = deserialized.targets.iterator();
    if (version >= MessagingService.VERSION_40) {
        for (int ii = 0; ii < addresses.size(); ii++) {
            InetAddressAndPort original = addresses.get(ii);
            InetAddressAndPort roundtripped = iterator.next();
            assertEquals(original, roundtripped);
        }
    } else {
        for (int ii = 0; ii < addresses.size(); ii++) {
            InetAddressAndPort original = addresses.get(ii);
            InetAddressAndPort roundtripped = iterator.next();
            assertEquals(original.getAddress(), roundtripped.getAddress());
            // 3.0 can't send port numbers so you get the defaults
            assertEquals(65532, roundtripped.getPort());
        }
    }
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 53 with DataInputBuffer

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

the class MessageSerializationPropertyTest method testMessageSerialization.

/**
 * Message and payload don't define equals, so have to rely on another way to define equality; serialized bytes!
 * The assumption is that serialize(deserialize(serialize(message))) == serialize(message)
 */
@Test
public void testMessageSerialization() throws Exception {
    SchemaProvider schema = Mockito.mock(SchemaProvider.class, Mockito.CALLS_REAL_METHODS);
    ReadCommand.Serializer readCommandSerializer = new ReadCommand.Serializer(schema);
    Supplier<? extends IVersionedAsymmetricSerializer<?, ?>> original = Verb.READ_REQ.unsafeSetSerializer(() -> readCommandSerializer);
    try (DataOutputBuffer first = new DataOutputBuffer(1024);
        DataOutputBuffer second = new DataOutputBuffer(1024)) {
        qt().forAll(MESSAGE_GEN).checkAssert(orFail(message -> {
            withTable(schema, message, orFail(ignore -> {
                for (MessagingService.Version version : MessagingService.Version.values()) {
                    first.clear();
                    second.clear();
                    // sync the clock with the generated createdAtNanos
                    FixedMonotonicClock.setNowInNanos(message.createdAtNanos());
                    serializer.serialize(message, first, version.value);
                    Message<Object> read = serializer.deserialize(new DataInputBuffer(first.buffer(), true), FBUtilities.getBroadcastAddressAndPort(), version.value);
                    serializer.serialize(read, second, version.value);
                    // using hex as byte buffer equality kept failing, and was harder to debug difference
                    // using hex means the specific section of the string that is different will be shown
                    Assertions.assertThat(ByteBufferUtil.bytesToHex(second.buffer())).as("Property serialize(deserialize(serialize(message))) == serialize(message) " + "was violated for version %s and verb %s" + "\n first=%s" + "\nsecond=%s\n", version, message.header.verb, // toString methods are not relyable for messages, so use reflection to generate one
                    new Object() {

                        public String toString() {
                            return CassandraGenerators.toStringRecursive(message);
                        }
                    }, new Object() {

                        public String toString() {
                            return CassandraGenerators.toStringRecursive(read);
                        }
                    }).isEqualTo(ByteBufferUtil.bytesToHex(first.buffer()));
                }
            }));
        }));
    } finally {
        Verb.READ_REQ.unsafeSetSerializer(original);
    }
}
Also used : CassandraGenerators(org.apache.cassandra.utils.CassandraGenerators) ReadQuery(org.apache.cassandra.db.ReadQuery) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) FBUtilities(org.apache.cassandra.utils.FBUtilities) BeforeClass(org.junit.BeforeClass) ByteBufferUtil(org.apache.cassandra.utils.ByteBufferUtil) FixedMonotonicClock(org.apache.cassandra.utils.FixedMonotonicClock) Test(org.junit.Test) ReadCommand(org.apache.cassandra.db.ReadCommand) QuickTheory.qt(org.quicktheories.QuickTheory.qt) Supplier(java.util.function.Supplier) Serializable(java.io.Serializable) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) Message.serializer(org.apache.cassandra.net.Message.serializer) TableMetadata(org.apache.cassandra.schema.TableMetadata) IVersionedAsymmetricSerializer(org.apache.cassandra.io.IVersionedAsymmetricSerializer) Assertions(org.assertj.core.api.Assertions) DataOutputPlus(org.apache.cassandra.io.util.DataOutputPlus) SchemaProvider(org.apache.cassandra.schema.SchemaProvider) MESSAGE_GEN(org.apache.cassandra.utils.CassandraGenerators.MESSAGE_GEN) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) FailingConsumer.orFail(org.apache.cassandra.utils.FailingConsumer.orFail) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) SchemaProvider(org.apache.cassandra.schema.SchemaProvider) ReadCommand(org.apache.cassandra.db.ReadCommand) IVersionedAsymmetricSerializer(org.apache.cassandra.io.IVersionedAsymmetricSerializer) Test(org.junit.Test)

Example 54 with DataInputBuffer

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

the class RepairMessageSerializerTest method serdes.

private static <T extends RepairMessage> T serdes(IVersionedSerializer<T> serializer, T message) {
    int expectedSize = (int) serializer.serializedSize(message, MS_VERSION);
    try (DataOutputBuffer out = new DataOutputBuffer(expectedSize)) {
        serializer.serialize(message, out, MS_VERSION);
        Assert.assertEquals(expectedSize, out.buffer().limit());
        try (DataInputBuffer in = new DataInputBuffer(out.buffer(), false)) {
            return serializer.deserialize(in, MS_VERSION);
        }
    } catch (IOException e) {
        throw new AssertionError(e);
    }
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) IOException(java.io.IOException)

Aggregations

DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)54 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)33 Test (org.junit.Test)23 IOException (java.io.IOException)14 ByteBuffer (java.nio.ByteBuffer)13 DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)13 TableMetadata (org.apache.cassandra.schema.TableMetadata)6 Mutation (org.apache.cassandra.db.Mutation)5 UUID (java.util.UUID)4 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)4 DataOutputPlus (org.apache.cassandra.io.util.DataOutputPlus)4 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)4 InputStreamReader (java.io.InputStreamReader)3 Reader (java.io.Reader)3 ArrayList (java.util.ArrayList)3 DataOutputBufferFixed (org.apache.cassandra.io.util.DataOutputBufferFixed)3 ByteBuf (io.netty.buffer.ByteBuf)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ClusteringIndexSliceFilter (org.apache.cassandra.db.filter.ClusteringIndexSliceFilter)2