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