Search in sources :

Example 1 with WrappedDataOutputStreamPlus

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

the class SerializingCache method serialize.

private RefCountedMemory serialize(V value) {
    long serializedSize = serializer.serializedSize(value);
    if (serializedSize > Integer.MAX_VALUE)
        throw new IllegalArgumentException(String.format("Unable to allocate %s", FBUtilities.prettyPrintMemory(serializedSize)));
    RefCountedMemory freeableMemory;
    try {
        freeableMemory = new RefCountedMemory(serializedSize);
    } catch (OutOfMemoryError e) {
        return null;
    }
    try {
        serializer.serialize(value, new WrappedDataOutputStreamPlus(new MemoryOutputStream(freeableMemory)));
    } catch (IOException e) {
        freeableMemory.unreference();
        throw new RuntimeException(e);
    }
    return freeableMemory;
}
Also used : WrappedDataOutputStreamPlus(org.apache.cassandra.io.util.WrappedDataOutputStreamPlus) IOException(java.io.IOException) MemoryOutputStream(org.apache.cassandra.io.util.MemoryOutputStream)

Example 2 with WrappedDataOutputStreamPlus

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

the class OutputStreamBench method setUp.

@Setup
public void setUp(final Blackhole bh) {
    StringBuilder sb = new StringBuilder();
    for (int ii = 0; ii < 11; ii++) {
        sb.append(BufferedDataOutputStreamTest.fourByte);
        sb.append(BufferedDataOutputStreamTest.threeByte);
        sb.append(BufferedDataOutputStreamTest.twoByte);
    }
    smallM = sb.toString();
    sb = new StringBuilder();
    while (sb.length() < 1024 * 12) {
        sb.append(small);
    }
    large = sb.toString();
    sb = new StringBuilder();
    while (sb.length() < 1024 * 12) {
        sb.append(smallM);
    }
    largeM = sb.toString();
    hole = new BufferedOutputStream(new OutputStream() {

        @Override
        public void write(int b) throws IOException {
            bh.consume(b);
        }

        @Override
        public void write(byte[] b) throws IOException {
            bh.consume(b);
        }

        @Override
        public void write(byte[] b, int a, int c) throws IOException {
            bh.consume(b);
            bh.consume(a);
            bh.consume(c);
        }
    });
    streamA = new WrappedDataOutputStreamPlus(hole);
    streamB = new BufferedDataOutputStreamPlus(new WritableByteChannel() {

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

        @Override
        public void close() throws IOException {
        }

        @Override
        public int write(ByteBuffer src) throws IOException {
            bh.consume(src);
            int remaining = src.remaining();
            src.position(src.limit());
            return remaining;
        }
    }, 8192);
}
Also used : OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) WrappedDataOutputStreamPlus(org.apache.cassandra.io.util.WrappedDataOutputStreamPlus) BufferedDataOutputStreamPlus(org.apache.cassandra.io.util.BufferedDataOutputStreamPlus) BufferedOutputStream(java.io.BufferedOutputStream) ByteBuffer(java.nio.ByteBuffer)

Example 3 with WrappedDataOutputStreamPlus

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

the class MessagingServiceTest method addDCLatency.

private static void addDCLatency(long sentAt, long nowTime) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DataOutputStreamPlus out = new WrappedDataOutputStreamPlus(baos)) {
        out.writeInt((int) sentAt);
    }
    DataInputStreamPlus in = new DataInputStreamPlus(new ByteArrayInputStream(baos.toByteArray()));
    MessageIn.readConstructionTime(InetAddress.getLocalHost(), in, nowTime);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStreamPlus(org.apache.cassandra.io.util.DataInputPlus.DataInputStreamPlus) WrappedDataOutputStreamPlus(org.apache.cassandra.io.util.WrappedDataOutputStreamPlus) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WrappedDataOutputStreamPlus(org.apache.cassandra.io.util.WrappedDataOutputStreamPlus) DataOutputStreamPlus(org.apache.cassandra.io.util.DataOutputStreamPlus)

Example 4 with WrappedDataOutputStreamPlus

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

the class OutboundTcpConnection method connect.

@SuppressWarnings("resource")
private boolean connect() {
    logger.debug("Attempting to connect to {}", poolReference.endPoint());
    long start = System.nanoTime();
    long timeout = TimeUnit.MILLISECONDS.toNanos(DatabaseDescriptor.getRpcTimeout());
    while (System.nanoTime() - start < timeout) {
        targetVersion = MessagingService.instance().getVersion(poolReference.endPoint());
        try {
            socket = poolReference.newSocket();
            socket.setKeepAlive(true);
            if (isLocalDC(poolReference.endPoint())) {
                socket.setTcpNoDelay(INTRADC_TCP_NODELAY);
            } else {
                socket.setTcpNoDelay(DatabaseDescriptor.getInterDCTcpNoDelay());
            }
            if (DatabaseDescriptor.getInternodeSendBufferSize() > 0) {
                try {
                    socket.setSendBufferSize(DatabaseDescriptor.getInternodeSendBufferSize());
                } catch (SocketException se) {
                    logger.warn("Failed to set send buffer size on internode socket.", se);
                }
            }
            // SocketChannel may be null when using SSL
            WritableByteChannel ch = socket.getChannel();
            out = new BufferedDataOutputStreamPlus(ch != null ? ch : Channels.newChannel(socket.getOutputStream()), BUFFER_SIZE);
            out.writeInt(MessagingService.PROTOCOL_MAGIC);
            writeHeader(out, targetVersion, shouldCompressConnection());
            out.flush();
            DataInputStream in = new DataInputStream(socket.getInputStream());
            int maxTargetVersion = handshakeVersion(in);
            if (maxTargetVersion == NO_VERSION) {
                // no version is returned, so disconnect an try again
                logger.trace("Target max version is {}; no version information yet, will retry", maxTargetVersion);
                disconnect();
                continue;
            } else {
                MessagingService.instance().setVersion(poolReference.endPoint(), maxTargetVersion);
            }
            if (targetVersion > maxTargetVersion) {
                logger.trace("Target max version is {}; will reconnect with that version", maxTargetVersion);
                try {
                    if (DatabaseDescriptor.getSeeds().contains(poolReference.endPoint()))
                        logger.warn("Seed gossip version is {}; will not connect with that version", maxTargetVersion);
                } catch (Throwable e) {
                    // If invalid yaml has been added to the config since startup, getSeeds() will throw an AssertionError
                    // Additionally, third party seed providers may throw exceptions if network is flakey
                    // Regardless of what's thrown, we must catch it, disconnect, and try again
                    JVMStabilityInspector.inspectThrowable(e);
                    logger.warn("Configuration error prevented outbound connection: {}", e.getLocalizedMessage());
                } finally {
                    disconnect();
                    return false;
                }
            }
            if (targetVersion < maxTargetVersion && targetVersion < MessagingService.current_version) {
                logger.trace("Detected higher max version {} (using {}); will reconnect when queued messages are done", maxTargetVersion, targetVersion);
                softCloseSocket();
            }
            out.writeInt(MessagingService.current_version);
            CompactEndpointSerializationHelper.serialize(FBUtilities.getBroadcastAddress(), out);
            if (shouldCompressConnection()) {
                out.flush();
                logger.trace("Upgrading OutputStream to {} to be compressed", poolReference.endPoint());
                // TODO: custom LZ4 OS that supports BB write methods
                LZ4Compressor compressor = LZ4Factory.fastestInstance().fastCompressor();
                Checksum checksum = XXHashFactory.fastestInstance().newStreamingHash32(LZ4_HASH_SEED).asChecksum();
                out = new WrappedDataOutputStreamPlus(new LZ4BlockOutputStream(socket.getOutputStream(), // 16k block size
                1 << 14, compressor, checksum, // no async flushing
                true));
            }
            logger.debug("Done connecting to {}", poolReference.endPoint());
            return true;
        } catch (SSLHandshakeException e) {
            logger.error("SSL handshake error for outbound connection to " + socket, e);
            socket = null;
            // SSL errors won't be recoverable within timeout period so we'll just abort
            return false;
        } catch (IOException e) {
            socket = null;
            logger.debug("Unable to connect to {}", poolReference.endPoint(), e);
            Uninterruptibles.sleepUninterruptibly(OPEN_RETRY_DELAY, TimeUnit.MILLISECONDS);
        }
    }
    return false;
}
Also used : SocketException(java.net.SocketException) WritableByteChannel(java.nio.channels.WritableByteChannel) IOException(java.io.IOException) BufferedDataOutputStreamPlus(org.apache.cassandra.io.util.BufferedDataOutputStreamPlus) DataInputStream(java.io.DataInputStream) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) LZ4Compressor(net.jpountz.lz4.LZ4Compressor) LZ4BlockOutputStream(net.jpountz.lz4.LZ4BlockOutputStream) Checksum(java.util.zip.Checksum) WrappedDataOutputStreamPlus(org.apache.cassandra.io.util.WrappedDataOutputStreamPlus)

Aggregations

WrappedDataOutputStreamPlus (org.apache.cassandra.io.util.WrappedDataOutputStreamPlus)4 IOException (java.io.IOException)2 WritableByteChannel (java.nio.channels.WritableByteChannel)2 BufferedDataOutputStreamPlus (org.apache.cassandra.io.util.BufferedDataOutputStreamPlus)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 OutputStream (java.io.OutputStream)1 SocketException (java.net.SocketException)1 ByteBuffer (java.nio.ByteBuffer)1 Checksum (java.util.zip.Checksum)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 LZ4BlockOutputStream (net.jpountz.lz4.LZ4BlockOutputStream)1 LZ4Compressor (net.jpountz.lz4.LZ4Compressor)1 DataInputStreamPlus (org.apache.cassandra.io.util.DataInputPlus.DataInputStreamPlus)1 DataOutputStreamPlus (org.apache.cassandra.io.util.DataOutputStreamPlus)1 MemoryOutputStream (org.apache.cassandra.io.util.MemoryOutputStream)1