Search in sources :

Example 11 with DataInputPlus

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

the class HintMessageTest method testSerializer.

@Test
public void testSerializer() throws IOException {
    SchemaLoader.prepareServer();
    SchemaLoader.createKeyspace(KEYSPACE, KeyspaceParams.simple(1), SchemaLoader.standardCFMD(KEYSPACE, TABLE));
    UUID hostId = UUID.randomUUID();
    long now = FBUtilities.timestampMicros();
    TableMetadata table = Schema.instance.getTableMetadata(KEYSPACE, TABLE);
    Mutation mutation = new RowUpdateBuilder(table, now, bytes("key")).clustering("column").add("val", "val" + 1234).build();
    Hint hint = Hint.create(mutation, now / 1000);
    HintMessage message = new HintMessage(hostId, hint);
    // serialize
    int serializedSize = (int) HintMessage.serializer.serializedSize(message, MessagingService.current_version);
    DataOutputBuffer dob = new DataOutputBuffer();
    HintMessage.serializer.serialize(message, dob, MessagingService.current_version);
    assertEquals(serializedSize, dob.getLength());
    // deserialize
    DataInputPlus di = new DataInputBuffer(dob.buffer(), true);
    HintMessage deserializedMessage = HintMessage.serializer.deserialize(di, MessagingService.current_version);
    // compare before/after
    assertEquals(hostId, deserializedMessage.hostId);
    assertHintsEqual(message.hint, deserializedMessage.hint);
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) Mutation(org.apache.cassandra.db.Mutation) UUID(java.util.UUID) Test(org.junit.Test)

Example 12 with DataInputPlus

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

the class BytesReadTrackerTest method internalTestReadLine.

public void internalTestReadLine(boolean inputStream) throws Exception {
    DataInputStream in = new DataInputStream(new ByteArrayInputStream("1".getBytes()));
    BytesReadTracker tracker = inputStream ? new TrackedInputStream(in) : new TrackedDataInputPlus(in);
    DataInputPlus reader = inputStream ? new DataInputPlus.DataInputStreamPlus((TrackedInputStream) tracker) : (DataInputPlus) tracker;
    try {
        String line = reader.readLine();
        if (inputStream)
            assertEquals(line, "1");
        else
            fail("Should have thrown UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
        if (inputStream)
            fail("Should have not thrown UnsupportedOperationException");
    } finally {
        in.close();
    }
}
Also used : 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) DataInputStream(java.io.DataInputStream) TrackedDataInputPlus(org.apache.cassandra.io.util.TrackedDataInputPlus)

Example 13 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 14 with DataInputPlus

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

the class MerkleTreeTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    Range<Token> full = new Range<>(tok(-1), tok(-1));
    // populate and validate the tree
    mt.maxsize(256);
    mt.init();
    for (TreeRange range : mt.invalids()) range.addAll(new HIterator(range.right));
    byte[] initialhash = mt.hash(full);
    DataOutputBuffer out = new DataOutputBuffer();
    MerkleTree.serializer.serialize(mt, out, MessagingService.current_version);
    byte[] serialized = out.toByteArray();
    DataInputPlus in = new DataInputBuffer(serialized);
    MerkleTree restored = MerkleTree.serializer.deserialize(in, MessagingService.current_version);
    assertHashEquals(initialhash, restored.hash(full));
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) Range(org.apache.cassandra.dht.Range) TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) Test(org.junit.Test)

Example 15 with DataInputPlus

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

the class IncomingStreamingConnection method run.

@Override
// Not closing constructed DataInputPlus's as the stream needs to remain open.
@SuppressWarnings("resource")
public void run() {
    try {
        // we can't do anything with a wrong-version stream connection, so drop it.
        if (version != StreamMessage.CURRENT_VERSION)
            throw new IOException(String.format("Received stream using protocol version %d (my version %d). Terminating connection", version, StreamMessage.CURRENT_VERSION));
        DataInputPlus input = new DataInputStreamPlus(socket.getInputStream());
        StreamInitMessage init = StreamInitMessage.serializer.deserialize(input, version);
        //Set SO_TIMEOUT on follower side
        if (!init.isForOutgoing)
            socket.setSoTimeout(DatabaseDescriptor.getStreamingSocketTimeout());
        // The initiator makes two connections, one for incoming and one for outgoing.
        // The receiving side distinguish two connections by looking at StreamInitMessage#isForOutgoing.
        // Note: we cannot use the same socket for incoming and outgoing streams because we want to
        // parallelize said streams and the socket is blocking, so we might deadlock.
        StreamResultFuture.initReceivingSide(init.sessionIndex, init.planId, init.description, init.from, this, init.isForOutgoing, version, init.keepSSTableLevel, init.isIncremental, init.pendingRepair);
    } catch (Throwable t) {
        logger.error("Error while reading from socket from {}.", socket.getRemoteSocketAddress(), t);
        close();
    }
}
Also used : DataInputStreamPlus(org.apache.cassandra.io.util.DataInputPlus.DataInputStreamPlus) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) StreamInitMessage(org.apache.cassandra.streaming.messages.StreamInitMessage) IOException(java.io.IOException)

Aggregations

DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)16 DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)10 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)8 Test (org.junit.Test)7 ByteArrayInputStream (java.io.ByteArrayInputStream)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 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 UUID (java.util.UUID)2 Mutation (org.apache.cassandra.db.Mutation)2 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)2 DataInputStreamPlus (org.apache.cassandra.io.util.DataInputPlus.DataInputStreamPlus)2 DataOutputPlus (org.apache.cassandra.io.util.DataOutputPlus)2 TableMetadata (org.apache.cassandra.schema.TableMetadata)2 DataInputStream (java.io.DataInputStream)1