Search in sources :

Example 1 with BufferedDataOutputStreamPlus

use of org.apache.cassandra.io.util.BufferedDataOutputStreamPlus 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 2 with BufferedDataOutputStreamPlus

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

the class Record method record.

public static void record(String saveToDir, long seed, boolean withRng, boolean withRngCallSites, ClusterSimulation.Builder<?> builder) {
    File eventFile = new File(new File(saveToDir), Long.toHexString(seed) + ".gz");
    File rngFile = new File(new File(saveToDir), Long.toHexString(seed) + ".rng.gz");
    {
        Set<String> modifiers = new LinkedHashSet<>();
        if (withRngCallSites)
            modifiers.add("rngCallSites");
        else if (withRng)
            modifiers.add("rng");
        if (builder.capture().waitSites)
            modifiers.add("WaitSites");
        if (builder.capture().wakeSites)
            modifiers.add("WakeSites");
        logger.error("Seed 0x{} ({}) (With: {})", Long.toHexString(seed), eventFile, modifiers);
    }
    try (PrintWriter eventOut = new PrintWriter(new GZIPOutputStream(eventFile.newOutputStream(OVERWRITE), 1 << 16));
        DataOutputStreamPlus rngOut = new BufferedDataOutputStreamPlus(Channels.newChannel(withRng ? new GZIPOutputStream(rngFile.newOutputStream(OVERWRITE), 1 << 16) : new ByteArrayOutputStream(0)))) {
        eventOut.println("modifiers:" + (withRng ? "rng," : "") + (withRngCallSites ? "rngCallSites," : "") + (builder.capture().waitSites ? "waitSites," : "") + (builder.capture().wakeSites ? "wakeSites," : ""));
        RandomSourceRecorder random;
        if (withRng) {
            random = new RandomSourceRecorder(rngOut, new RandomSource.Default(), withRngCallSites);
            builder.random(random);
        } else {
            random = null;
        }
        // periodic forced flush to ensure state is on disk after some kind of stall
        Thread flusher = new Thread(() -> {
            try {
                while (true) {
                    Thread.sleep(1000);
                    eventOut.flush();
                    if (random != null) {
                        synchronized (random) {
                            rngOut.flush();
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException ignore) {
            } finally {
                eventOut.flush();
                try {
                    if (random != null) {
                        synchronized (random) {
                            rngOut.flush();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }, "Flush Recordings of " + seed);
        flusher.setDaemon(true);
        flusher.start();
        try (ClusterSimulation<?> cluster = builder.create(seed)) {
            try (CloseableIterator<?> iter = cluster.simulation.iterator()) {
                while (iter.hasNext()) eventOut.println(normaliseRecordingOut(iter.next().toString()));
                if (random != null)
                    random.close();
            } finally {
                eventOut.flush();
                rngOut.flush();
            }
        } finally {
            flusher.interrupt();
        }
    } catch (Throwable t) {
        t.printStackTrace();
        throw new RuntimeException("Failed on seed " + Long.toHexString(seed), t);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedDataOutputStreamPlus(org.apache.cassandra.io.util.BufferedDataOutputStreamPlus) BufferedDataOutputStreamPlus(org.apache.cassandra.io.util.BufferedDataOutputStreamPlus) DataOutputStreamPlus(org.apache.cassandra.io.util.DataOutputStreamPlus) GZIPOutputStream(java.util.zip.GZIPOutputStream) File(org.apache.cassandra.io.util.File) PrintWriter(java.io.PrintWriter)

Example 3 with BufferedDataOutputStreamPlus

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

the class AsyncStreamingInputPlusTest method consumeUntilTestCycle.

private void consumeUntilTestCycle(int nBuffs, int buffSize, int startOffset, int len) throws IOException {
    inputPlus = new AsyncStreamingInputPlus(channel);
    byte[] expectedBytes = new byte[len];
    int count = 0;
    for (int j = 0; j < nBuffs; j++) {
        ByteBuf buf = channel.alloc().buffer(buffSize);
        for (int i = 0; i < buf.capacity(); i++) {
            buf.writeByte(j);
            if (count >= startOffset && (count - startOffset) < len)
                expectedBytes[count - startOffset] = (byte) j;
            count++;
        }
        inputPlus.append(buf);
    }
    inputPlus.requestClosure();
    TestableWritableByteChannel wbc = new TestableWritableByteChannel(len);
    inputPlus.skipBytesFully(startOffset);
    BufferedDataOutputStreamPlus writer = new BufferedDataOutputStreamPlus(wbc);
    inputPlus.consume(buffer -> {
        writer.write(buffer);
        return buffer.remaining();
    }, len);
    writer.close();
    Assert.assertEquals(String.format("Test with %d buffers starting at %d consuming %d bytes", nBuffs, startOffset, len), len, wbc.writtenBytes.readableBytes());
    Assert.assertArrayEquals(expectedBytes, wbc.writtenBytes.array());
}
Also used : AsyncStreamingInputPlus(org.apache.cassandra.net.AsyncStreamingInputPlus) ByteBuf(io.netty.buffer.ByteBuf) BufferedDataOutputStreamPlus(org.apache.cassandra.io.util.BufferedDataOutputStreamPlus)

Example 4 with BufferedDataOutputStreamPlus

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

Example 5 with BufferedDataOutputStreamPlus

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

the class AbstractSerializationsTester method getOutput.

@SuppressWarnings("resource")
protected static DataOutputStreamPlus getOutput(String version, String name) throws IOException {
    File f = new File("test/data/serialization/" + version + '/' + name);
    f.getParentFile().mkdirs();
    return new BufferedDataOutputStreamPlus(new FileOutputStream(f).getChannel());
}
Also used : FileOutputStream(java.io.FileOutputStream) BufferedDataOutputStreamPlus(org.apache.cassandra.io.util.BufferedDataOutputStreamPlus) File(java.io.File)

Aggregations

BufferedDataOutputStreamPlus (org.apache.cassandra.io.util.BufferedDataOutputStreamPlus)7 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 WritableByteChannel (java.nio.channels.WritableByteChannel)3 WrappedDataOutputStreamPlus (org.apache.cassandra.io.util.WrappedDataOutputStreamPlus)2 ByteBuf (io.netty.buffer.ByteBuf)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 SocketException (java.net.SocketException)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 CRC32 (java.util.zip.CRC32)1 Checksum (java.util.zip.Checksum)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1