use of edu.iu.dsc.tws.comms.dfw.InMessage in project twister2 by DSC-SPIDAL.
the class KeyedDataDeSerializer method build.
/**
* Builds the message from the data buffers in the partialObject. Since this method
* supports multi-messages it iterates through the buffers and builds all the messages separately
*
* @param partialObject message object that needs to be built
* @param edge the edge value associated with this message
*/
@Override
public void build(Object partialObject, int edge) {
InMessage currentMessage = (InMessage) partialObject;
MessageType keyType = currentMessage.getKeyType();
DataPacker keyPacker = keyType.getDataPacker();
DataPacker dataPacker = currentMessage.getDataType().getDataPacker();
Queue<DataBuffer> buffers = currentMessage.getBuffers();
MessageHeader header = currentMessage.getHeader();
if (header == null) {
throw new RuntimeException("Header must be built before the message");
}
if ((header.getFlags() & MessageFlags.SYNC_BARRIER) == MessageFlags.SYNC_BARRIER) {
keyType = MessageTypes.BYTE_ARRAY;
keyPacker = MessageTypes.BYTE_ARRAY.getDataPacker();
dataPacker = keyPacker;
}
List<DataBuffer> builtBuffers = new ArrayList<>();
// get the number of objects deserialized
DataBuffer buffer = buffers.peek();
while (buffer != null) {
int currentLocation = 0;
int remaining = buffer.getSize();
if (header.getNumberTuples() == 0) {
builtBuffers.add(buffer);
break;
}
// if we are at the beginning
int currentObjectLength = currentMessage.getUnPkCurrentObjectLength();
int currentKeyLength = currentMessage.getUnPkCurrentKeyLength();
if (currentMessage.getUnPkBuffers() == 0) {
currentLocation = DFWIOUtils.HEADER_SIZE;
remaining = remaining - DFWIOUtils.HEADER_SIZE;
} else {
currentLocation = DFWIOUtils.SHORT_HEADER_SIZE;
remaining = remaining - DFWIOUtils.SHORT_HEADER_SIZE;
}
if (currentObjectLength == -1 || currentMessage.getUnPkBuffers() == 0) {
currentObjectLength = buffer.getByteBuffer().getInt(currentLocation);
remaining = remaining - Integer.BYTES;
currentLocation += Integer.BYTES;
}
if (currentKeyLength == -1) {
// we assume we can read the key length from here
int right = DataPackerProxy.getKeyLengthRight(keyType, buffer, currentLocation);
int left = DataPackerProxy.getKeyLengthLeft(keyType, buffer, currentLocation);
remaining = remaining - right;
currentLocation += right;
// we have to set the current object length
currentObjectLength = currentObjectLength - left - right;
currentKeyLength = left;
currentMessage.getKeyBuilder().init(keyPacker, currentKeyLength);
try {
currentMessage.getDataBuilder().init(dataPacker, currentObjectLength);
currentMessage.setUnPkCurrentKeyLength(currentKeyLength);
currentMessage.setUnPkCurrentObjectLength(currentObjectLength);
// we are going to read the key first
currentMessage.setReadingKey(true);
} catch (NegativeArraySizeException e) {
throw new RuntimeException(e);
}
}
while (remaining > 0) {
if (currentMessage.isReadingKey()) {
ObjectBuilderImpl keyBuilder = currentMessage.getKeyBuilder();
int bytesRead = keyPacker.readDataFromBuffer(keyBuilder, currentLocation, buffer);
keyBuilder.incrementCompletedSizeBy(bytesRead);
currentLocation += bytesRead;
remaining = remaining - bytesRead;
currentMessage.setReadingKey(!keyBuilder.isBuilt());
if (keyBuilder.isBuilt()) {
// done reading key
currentMessage.setReadingKey(false);
} else {
break;
}
}
if (!currentMessage.isReadingKey()) {
ObjectBuilderImpl dataBuilder = currentMessage.getDataBuilder();
// read the values from the buffer
int byteRead = dataPacker.readDataFromBuffer(dataBuilder, currentLocation, buffer);
dataBuilder.incrementCompletedSizeBy(byteRead);
currentLocation += byteRead;
remaining = remaining - byteRead;
// okay we are done with this object
if (dataBuilder.isBuilt()) {
// lets add the object
currentMessage.addCurrentKeyedObject();
} else {
// lets break the inner while loop
break;
}
int bytesToReadKey = 0;
if (keyPacker.isHeaderRequired()) {
bytesToReadKey += Integer.BYTES;
}
if (remaining >= Integer.BYTES + bytesToReadKey) {
currentObjectLength = buffer.getByteBuffer().getInt(currentLocation);
remaining = remaining - Integer.BYTES;
currentLocation += Integer.BYTES;
// we assume we can read the key length from here
int right = DataPackerProxy.getKeyLengthRight(keyType, buffer, currentLocation);
int left = DataPackerProxy.getKeyLengthLeft(keyType, buffer, currentLocation);
remaining = remaining - right;
currentLocation += right;
// we have to set the current object length
currentObjectLength = currentObjectLength - left - right;
currentKeyLength = left;
currentMessage.getKeyBuilder().init(keyPacker, currentKeyLength);
currentMessage.getDataBuilder().init(dataPacker, currentObjectLength);
currentMessage.setUnPkCurrentKeyLength(currentKeyLength);
currentMessage.setUnPkCurrentObjectLength(currentObjectLength);
// we are going to read the key first
currentMessage.setReadingKey(true);
} else if (remaining >= Integer.BYTES) {
currentObjectLength = buffer.getByteBuffer().getInt(currentLocation);
remaining = remaining - Integer.BYTES;
currentLocation += Integer.BYTES;
currentMessage.setUnPkCurrentObjectLength(currentObjectLength);
currentMessage.setUnPkCurrentKeyLength(-1);
currentMessage.setReadingKey(true);
} else {
// we have to break here as we cannot read further
break;
}
}
}
// lets remove this buffer
buffers.poll();
builtBuffers.add(buffer);
// increment the unpacked buffers
currentMessage.incrementUnPkBuffers();
// lets check weather we have read everythong
int readObjectNumber = currentMessage.getUnPkNumberObjects();
// we need to get number of tuples and get abs because we are using -1 for single messages
if (readObjectNumber == Math.abs(currentMessage.getHeader().getNumberTuples())) {
break;
}
// lets move to next
buffer = buffers.peek();
}
if (builtBuffers.size() > 0) {
ChannelMessage channelMessage = new ChannelMessage(currentMessage.getOriginatingId(), currentMessage.getDataType(), MessageDirection.IN, currentMessage.getReleaseListener());
channelMessage.addBuffers(builtBuffers);
channelMessage.setHeader(currentMessage.getHeader());
channelMessage.incrementRefCount();
currentMessage.addBuiltMessage(channelMessage);
}
}
use of edu.iu.dsc.tws.comms.dfw.InMessage in project twister2 by DSC-SPIDAL.
the class BaseSerializeTest method keyedSingleValueCase.
public InMessage keyedSingleValueCase(int numBuffers, int size, MessageType type, MessageType keyType, Object data) {
BlockingQueue<DataBuffer> bufferQueue = createDataQueue(numBuffers, size);
OutMessage outMessage = new OutMessage(0, 1, -1, 10, 0, null, null, type, keyType, null, data);
KeyedDataSerializer serializer = new KeyedDataSerializer();
serializer.init(Config.newBuilder().build(), bufferQueue);
List<ChannelMessage> messages = new ArrayList<>();
while (outMessage.getSendState() != OutMessage.SendState.SERIALIZED) {
ChannelMessage ch = serializer.build(data, outMessage);
messages.add(ch);
}
KeyedDataDeSerializer deserializer = new KeyedDataDeSerializer();
deserializer.init(Config.newBuilder().build());
MessageHeader header = deserializer.buildHeader(messages.get(0).getBuffers().get(0), 1);
InMessage inMessage = new InMessage(0, type, null, header);
inMessage.setKeyType(keyType);
for (ChannelMessage channelMessage : messages) {
for (DataBuffer dataBuffer : channelMessage.getBuffers()) {
inMessage.addBufferAndCalculate(dataBuffer);
}
}
deserializer.build(inMessage, 1);
return inMessage;
}
use of edu.iu.dsc.tws.comms.dfw.InMessage in project twister2 by DSC-SPIDAL.
the class DataSerializerTest method testBuildLargeListShortMessage.
@SuppressWarnings("Unchecked")
@Test
public void testBuildLargeListShortMessage() {
int numBuffers = 32;
int size = 1000;
List<Object> data = new AggregatedObjects<>();
for (int i = 0; i < 4; i++) {
Object o = createData(800, MessageTypes.SHORT_ARRAY);
data.add(o);
}
InMessage inMessage = listValueCase(numBuffers, size, data, MessageTypes.SHORT_ARRAY);
List<Object> result = (List<Object>) inMessage.getDeserializedData();
for (int i = 0; i < result.size(); i++) {
Object exp = result.get(i);
Object d = data.get(i);
Assert.assertArrayEquals((short[]) exp, (short[]) d);
}
}
use of edu.iu.dsc.tws.comms.dfw.InMessage in project twister2 by DSC-SPIDAL.
the class DataSerializerTest method testBuildLargeListByteMessage.
@SuppressWarnings("Unchecked")
@Test
public void testBuildLargeListByteMessage() {
int numBuffers = 32;
int size = 1000;
List<Object> data = new AggregatedObjects<>();
for (int i = 0; i < 4; i++) {
Object o = createData(800, MessageTypes.BYTE_ARRAY);
data.add(o);
}
InMessage inMessage = listValueCase(numBuffers, size, data, MessageTypes.BYTE_ARRAY);
List<Object> result = (List<Object>) inMessage.getDeserializedData();
for (int i = 0; i < result.size(); i++) {
Object exp = result.get(i);
Object d = data.get(i);
Assert.assertArrayEquals((byte[]) exp, (byte[]) d);
}
}
use of edu.iu.dsc.tws.comms.dfw.InMessage in project twister2 by DSC-SPIDAL.
the class KeyedSerializerTest method testBuildLargeByteMessage.
@Test
public void testBuildLargeByteMessage() {
int numBuffers = 10;
int size = 1000;
MessageType type = MessageTypes.BYTE_ARRAY;
Object data = createKeyedData(800, type, MessageTypes.BYTE_ARRAY);
InMessage inMessage = keyedSingleValueCase(numBuffers, size, type, MessageTypes.BYTE_ARRAY, data);
Tuple deserializedData = (Tuple) inMessage.getDeserializedData();
Assert.assertArrayEquals((byte[]) deserializedData.getKey(), (byte[]) ((Tuple) data).getKey());
Assert.assertArrayEquals((byte[]) deserializedData.getValue(), (byte[]) ((Tuple) data).getValue());
}
Aggregations