Search in sources :

Example 1 with NIODataInputStream

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

the class IncomingTcpConnection method receiveMessages.

// Not closing constructed DataInputPlus's as the stream needs to remain open.
@SuppressWarnings("resource")
private void receiveMessages() throws IOException {
    // handshake (true) endpoint versions
    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    // if this version is < the MS version the other node is trying
    // to connect with, the other node will disconnect
    out.writeInt(MessagingService.current_version);
    out.flush();
    DataInputPlus in = new DataInputStreamPlus(socket.getInputStream());
    int maxVersion = in.readInt();
    // outbound side will reconnect if necessary to upgrade version
    assert version <= MessagingService.current_version;
    from = CompactEndpointSerializationHelper.deserialize(in);
    // record the (true) version of the endpoint
    MessagingService.instance().setVersion(from, maxVersion);
    logger.trace("Set version for {} to {} (will use {})", from, maxVersion, MessagingService.instance().getVersion(from));
    if (compressed) {
        logger.trace("Upgrading incoming connection to be compressed");
        LZ4FastDecompressor decompressor = LZ4Factory.fastestInstance().fastDecompressor();
        Checksum checksum = XXHashFactory.fastestInstance().newStreamingHash32(OutboundTcpConnection.LZ4_HASH_SEED).asChecksum();
        in = new DataInputStreamPlus(new LZ4BlockInputStream(socket.getInputStream(), decompressor, checksum));
    } else {
        ReadableByteChannel channel = socket.getChannel();
        in = new NIODataInputStream(channel != null ? channel : Channels.newChannel(socket.getInputStream()), BUFFER_SIZE);
    }
    while (true) {
        MessagingService.validateMagic(in.readInt());
        receiveMessage(in, version);
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) LZ4FastDecompressor(net.jpountz.lz4.LZ4FastDecompressor) Checksum(java.util.zip.Checksum) DataInputStreamPlus(org.apache.cassandra.io.util.DataInputPlus.DataInputStreamPlus) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) LZ4BlockInputStream(net.jpountz.lz4.LZ4BlockInputStream) NIODataInputStream(org.apache.cassandra.io.util.NIODataInputStream)

Aggregations

ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 Checksum (java.util.zip.Checksum)1 LZ4BlockInputStream (net.jpountz.lz4.LZ4BlockInputStream)1 LZ4FastDecompressor (net.jpountz.lz4.LZ4FastDecompressor)1 DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)1 DataInputStreamPlus (org.apache.cassandra.io.util.DataInputPlus.DataInputStreamPlus)1 NIODataInputStream (org.apache.cassandra.io.util.NIODataInputStream)1