use of org.apache.cassandra.io.util.DataInputPlus 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);
}
}
use of org.apache.cassandra.io.util.DataInputPlus in project cassandra by apache.
the class EmptyTypeTest method read.
@Test
public void read() {
DataInputPlus input = Mockito.mock(DataInputPlus.class);
ByteBuffer buffer = EmptyType.instance.readBuffer(input);
assertThat(buffer).isNotNull().matches(b -> !b.hasRemaining());
buffer = EmptyType.instance.readBuffer(input, 42);
assertThat(buffer).isNotNull().matches(b -> !b.hasRemaining());
Mockito.verifyNoInteractions(input);
}
use of org.apache.cassandra.io.util.DataInputPlus in project cassandra by apache.
the class SinglePartitionSliceCommandTest method staticColumnsAreReturned.
@Test
public void staticColumnsAreReturned() throws IOException {
DecoratedKey key = metadata.partitioner.decorateKey(ByteBufferUtil.bytes("k1"));
QueryProcessor.executeInternal("INSERT INTO ks.tbl (k, s) VALUES ('k1', 's')");
Assert.assertFalse(QueryProcessor.executeInternal("SELECT s FROM ks.tbl WHERE k='k1'").isEmpty());
ColumnFilter columnFilter = ColumnFilter.selection(RegularAndStaticColumns.of(s));
ClusteringIndexSliceFilter sliceFilter = new ClusteringIndexSliceFilter(Slices.NONE, false);
ReadCommand cmd = SinglePartitionReadCommand.create(metadata, FBUtilities.nowInSeconds(), columnFilter, RowFilter.NONE, DataLimits.NONE, key, sliceFilter);
// check raw iterator for static cell
try (ReadExecutionController executionController = cmd.executionController();
UnfilteredPartitionIterator pi = cmd.executeLocally(executionController)) {
checkForS(pi);
}
ReadResponse response;
DataOutputBuffer out;
DataInputPlus in;
ReadResponse dst;
// check (de)serialized iterator for memtable static cell
try (ReadExecutionController executionController = cmd.executionController();
UnfilteredPartitionIterator pi = cmd.executeLocally(executionController)) {
response = ReadResponse.createDataResponse(pi, cmd, executionController.getRepairedDataInfo());
}
out = new DataOutputBuffer((int) ReadResponse.serializer.serializedSize(response, MessagingService.VERSION_30));
ReadResponse.serializer.serialize(response, out, MessagingService.VERSION_30);
in = new DataInputBuffer(out.buffer(), true);
dst = ReadResponse.serializer.deserialize(in, MessagingService.VERSION_30);
try (UnfilteredPartitionIterator pi = dst.makeIterator(cmd)) {
checkForS(pi);
}
// check (de)serialized iterator for sstable static cell
Schema.instance.getColumnFamilyStoreInstance(metadata.id).forceBlockingFlush();
try (ReadExecutionController executionController = cmd.executionController();
UnfilteredPartitionIterator pi = cmd.executeLocally(executionController)) {
response = ReadResponse.createDataResponse(pi, cmd, executionController.getRepairedDataInfo());
}
out = new DataOutputBuffer((int) ReadResponse.serializer.serializedSize(response, MessagingService.VERSION_30));
ReadResponse.serializer.serialize(response, out, MessagingService.VERSION_30);
in = new DataInputBuffer(out.buffer(), true);
dst = ReadResponse.serializer.deserialize(in, MessagingService.VERSION_30);
try (UnfilteredPartitionIterator pi = dst.makeIterator(cmd)) {
checkForS(pi);
}
}
use of org.apache.cassandra.io.util.DataInputPlus in project cassandra by apache.
the class CassandraEntireSSTableStreamReader method read.
/**
* @param in where this reads data from
* @return SSTable transferred
* @throws IOException if reading the remote sstable fails. Will throw an RTE if local write fails.
*/
// input needs to remain open, streams on top of it can't be closed
@SuppressWarnings("resource")
@Override
public SSTableMultiWriter read(DataInputPlus in) throws Throwable {
ColumnFamilyStore cfs = ColumnFamilyStore.getIfExists(tableId);
if (cfs == null) {
// schema was dropped during streaming
throw new IOException("Table " + tableId + " was dropped during streaming");
}
ComponentManifest manifest = header.componentManifest;
long totalSize = manifest.totalSize();
logger.debug("[Stream #{}] Started receiving sstable #{} from {}, size = {}, table = {}", session.planId(), fileSequenceNumber, session.peer, prettyPrintMemory(totalSize), cfs.metadata());
BigTableZeroCopyWriter writer = null;
try {
writer = createWriter(cfs, totalSize, manifest.components());
long bytesRead = 0;
for (Component component : manifest.components()) {
long length = manifest.sizeOf(component);
logger.debug("[Stream #{}] Started receiving {} component from {}, componentSize = {}, readBytes = {}, totalSize = {}", session.planId(), component, session.peer, prettyPrintMemory(length), prettyPrintMemory(bytesRead), prettyPrintMemory(totalSize));
writer.writeComponent(component.type, in, length);
session.progress(writer.descriptor.filenameFor(component), ProgressInfo.Direction.IN, length, length);
bytesRead += length;
logger.debug("[Stream #{}] Finished receiving {} component from {}, componentSize = {}, readBytes = {}, totalSize = {}", session.planId(), component, session.peer, prettyPrintMemory(length), prettyPrintMemory(bytesRead), prettyPrintMemory(totalSize));
}
UnaryOperator<StatsMetadata> transform = stats -> stats.mutateLevel(header.sstableLevel).mutateRepairedMetadata(messageHeader.repairedAt, messageHeader.pendingRepair, false);
String description = String.format("level %s and repairedAt time %s and pendingRepair %s", header.sstableLevel, messageHeader.repairedAt, messageHeader.pendingRepair);
writer.descriptor.getMetadataSerializer().mutate(writer.descriptor, description, transform);
return writer;
} catch (Throwable e) {
logger.error("[Stream {}] Error while reading sstable from stream for table = {}", session.planId(), cfs.metadata(), e);
if (writer != null)
e = writer.abort(e);
throw e;
}
}
use of org.apache.cassandra.io.util.DataInputPlus in project cassandra by apache.
the class BytesReadTrackerTest method internalTestSkipBytesAndReadFully.
public void internalTestSkipBytesAndReadFully(boolean inputStream) throws Exception {
String testStr = "1234567890";
byte[] testData = testStr.getBytes();
DataInputPlus.DataInputStreamPlus in = new DataInputPlus.DataInputStreamPlus(new ByteArrayInputStream(testData));
BytesReadTracker tracker = inputStream ? new TrackedInputStream(in) : new TrackedDataInputPlus(in);
DataInputPlus reader = inputStream ? new DataInputPlus.DataInputStreamPlus((TrackedInputStream) tracker) : (DataInputPlus) tracker;
try {
// read first 5 bytes
byte[] out = new byte[5];
reader.readFully(out, 0, 5);
assertEquals("12345", new String(out));
assertEquals(5, tracker.getBytesRead());
// then skip 2 bytes
reader.skipBytes(2);
assertEquals(7, tracker.getBytesRead());
// and read the rest
out = new byte[3];
reader.readFully(out);
assertEquals("890", new String(out));
assertEquals(10, tracker.getBytesRead());
assertEquals(testData.length, tracker.getBytesRead());
} finally {
in.close();
}
}
Aggregations