Search in sources :

Example 1 with IVersionedAsymmetricSerializer

use of org.apache.cassandra.io.IVersionedAsymmetricSerializer in project cassandra by apache.

the class Instance method serializeMessage.

public static IMessage serializeMessage(InetAddressAndPort from, InetAddressAndPort to, Message<?> messageOut) {
    int fromVersion = MessagingService.instance().versions.get(from);
    int toVersion = MessagingService.instance().versions.get(to);
    // See CASSANDRA-16157 for details.
    if (fromVersion < MessagingService.current_version && ((messageOut.verb().serializer() == ((IVersionedAsymmetricSerializer) NoPayload.serializer) || messageOut.payload == null))) {
        return new MessageImpl(messageOut.verb().id, ByteArrayUtil.EMPTY_BYTE_ARRAY, messageOut.id(), toVersion, fromCassandraInetAddressAndPort(from));
    }
    try (DataOutputBuffer out = new DataOutputBuffer(1024)) {
        // we use a special procedure here that "re-serializes" a "remote" batch to build the message.
        if (fromVersion >= MessagingService.VERSION_40 && messageOut.verb().id == BATCH_STORE_REQ.id) {
            Object maybeBatch = messageOut.payload;
            if (maybeBatch instanceof Batch) {
                Batch batch = (Batch) maybeBatch;
                // If the batch is local, it can be serialized along the normal path.
                if (!batch.isLocal()) {
                    reserialize(batch, out, toVersion);
                    byte[] bytes = out.toByteArray();
                    return new MessageImpl(messageOut.verb().id, bytes, messageOut.id(), toVersion, fromCassandraInetAddressAndPort(from));
                }
            }
        }
        Message.serializer.serialize(messageOut, out, toVersion);
        byte[] bytes = out.toByteArray();
        if (messageOut.serializedSize(toVersion) != bytes.length)
            throw new AssertionError(String.format("Message serializedSize(%s) does not match what was written with serialize(out, %s) for verb %s and serializer %s; " + "expected %s, actual %s", toVersion, toVersion, messageOut.verb(), Message.serializer.getClass(), messageOut.serializedSize(toVersion), bytes.length));
        return new MessageImpl(messageOut.verb().id, bytes, messageOut.id(), toVersion, fromCassandraInetAddressAndPort(from));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Batch(org.apache.cassandra.batchlog.Batch) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) IVersionedAsymmetricSerializer(org.apache.cassandra.io.IVersionedAsymmetricSerializer) IOException(java.io.IOException)

Example 2 with IVersionedAsymmetricSerializer

use of org.apache.cassandra.io.IVersionedAsymmetricSerializer 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)

Aggregations

IVersionedAsymmetricSerializer (org.apache.cassandra.io.IVersionedAsymmetricSerializer)2 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 Consumer (java.util.function.Consumer)1 Supplier (java.util.function.Supplier)1 Batch (org.apache.cassandra.batchlog.Batch)1 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)1 ReadCommand (org.apache.cassandra.db.ReadCommand)1 ReadQuery (org.apache.cassandra.db.ReadQuery)1 DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)1 DataOutputPlus (org.apache.cassandra.io.util.DataOutputPlus)1 Message.serializer (org.apache.cassandra.net.Message.serializer)1 SchemaProvider (org.apache.cassandra.schema.SchemaProvider)1 TableMetadata (org.apache.cassandra.schema.TableMetadata)1 ByteBufferUtil (org.apache.cassandra.utils.ByteBufferUtil)1 CassandraGenerators (org.apache.cassandra.utils.CassandraGenerators)1 MESSAGE_GEN (org.apache.cassandra.utils.CassandraGenerators.MESSAGE_GEN)1 FBUtilities (org.apache.cassandra.utils.FBUtilities)1 FailingConsumer.orFail (org.apache.cassandra.utils.FailingConsumer.orFail)1