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);
}
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);
}
}
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());
}
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;
}
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());
}
Aggregations