Search in sources :

Example 1 with InMessage

use of edu.iu.dsc.tws.comms.dfw.InMessage in project twister2 by DSC-SPIDAL.

the class FixedSchemaDataDeserializer 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;
    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");
    }
    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();
        if (currentMessage.getUnPkBuffers() == 0) {
            currentLocation = DFWIOUtils.HEADER_SIZE;
            remaining = remaining - DFWIOUtils.HEADER_SIZE;
        } else {
            // source(int) + last_buffer_indicator(byte)
            currentLocation = DFWIOUtils.SHORT_HEADER_SIZE;
            remaining = remaining - DFWIOUtils.SHORT_HEADER_SIZE;
        }
        if (currentObjectLength == -1 || currentMessage.getUnPkBuffers() == 0) {
            currentObjectLength = this.messageSchema.getMessageSize();
            // starting to build a new object
            currentMessage.getDataBuilder().init(dataPacker, currentObjectLength);
        }
        while (remaining > 0) {
            // read the values from the buffer
            ObjectBuilderImpl dataBuilder = currentMessage.getDataBuilder();
            int bytesRead = dataPacker.readDataFromBuffer(dataBuilder, currentLocation, buffer);
            dataBuilder.incrementCompletedSizeBy(bytesRead);
            currentLocation += bytesRead;
            remaining = remaining - bytesRead;
            // okay we are done with this object
            if (dataBuilder.isBuilt()) {
                currentMessage.addCurrentObject();
                currentMessage.setUnPkCurrentObjectLength(-1);
            } else {
                // lets break the inner while loop
                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);
    }
}
Also used : ArrayList(java.util.ArrayList) InMessage(edu.iu.dsc.tws.comms.dfw.InMessage) MessageHeader(edu.iu.dsc.tws.api.comms.messaging.MessageHeader) DataPacker(edu.iu.dsc.tws.api.comms.packing.DataPacker) DataBuffer(edu.iu.dsc.tws.api.comms.packing.DataBuffer) ChannelMessage(edu.iu.dsc.tws.api.comms.messaging.ChannelMessage)

Example 2 with InMessage

use of edu.iu.dsc.tws.comms.dfw.InMessage in project twister2 by DSC-SPIDAL.

the class BaseSerializeTest method keyedListValueCase.

public InMessage keyedListValueCase(int numBuffers, int size, List<Object> data, MessageType type, MessageType keyType) {
    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;
}
Also used : OutMessage(edu.iu.dsc.tws.comms.dfw.OutMessage) ArrayList(java.util.ArrayList) InMessage(edu.iu.dsc.tws.comms.dfw.InMessage) MessageHeader(edu.iu.dsc.tws.api.comms.messaging.MessageHeader) DataBuffer(edu.iu.dsc.tws.api.comms.packing.DataBuffer) ChannelMessage(edu.iu.dsc.tws.api.comms.messaging.ChannelMessage)

Example 3 with InMessage

use of edu.iu.dsc.tws.comms.dfw.InMessage in project twister2 by DSC-SPIDAL.

the class KeyedSerializerLargeTest method testBuildLargeListByteMessage.

@SuppressWarnings("Unchecked")
@Test
public void testBuildLargeListByteMessage() {
    int numBuffers = 32;
    int size = 1024000;
    for (int numObjects = 1; numObjects < 5000; numObjects++) {
        System.out.println("Starting test : " + numObjects);
        List<Object> data = new AggregatedObjects<>();
        for (int i = 0; i < numObjects; i++) {
            Object o = createKeyedData(100, MessageTypes.INTEGER_ARRAY, 1, MessageTypes.INTEGER);
            data.add(o);
        }
        InMessage inMessage = keyedListValueCase(numBuffers, size, data, MessageTypes.INTEGER_ARRAY, MessageTypes.INTEGER);
        List<Object> result = (List<Object>) inMessage.getDeserializedData();
        Assert.assertEquals(numObjects, result.size());
        for (int i = 0; i < result.size(); i++) {
            Tuple deserializedData = (Tuple) result.get(i);
            Tuple d = (Tuple) data.get(i);
            Assert.assertEquals(deserializedData.getKey(), d.getKey());
            Assert.assertArrayEquals((int[]) deserializedData.getValue(), (int[]) ((Tuple) d).getValue());
        }
    }
}
Also used : InMessage(edu.iu.dsc.tws.comms.dfw.InMessage) ArrayList(java.util.ArrayList) List(java.util.List) Tuple(edu.iu.dsc.tws.api.comms.structs.Tuple) Test(org.junit.Test)

Example 4 with InMessage

use of edu.iu.dsc.tws.comms.dfw.InMessage in project twister2 by DSC-SPIDAL.

the class KeyedSerializerLargeTest method keyedListValueCase.

public InMessage keyedListValueCase(int numBuffers, int size, List<Object> data, MessageType type, MessageType keyType) {
    OutMessage outMessage = new OutMessage(0, 1, -1, 10, 0, null, null, type, keyType, null, data);
    List<ChannelMessage> messages = new ArrayList<>();
    int count = 0;
    while (outMessage.getSendState() != OutMessage.SendState.SERIALIZED) {
        ChannelMessage ch = serializer.build(data, outMessage);
        messages.add(ch);
        System.out.println("Adding count " + count++);
    }
    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);
    for (ChannelMessage d : inMessage.getBuiltMessages()) {
        for (DataBuffer buffer : d.getNormalBuffers()) {
            buffer.getByteBuffer().clear();
            bufferQueue.offer(buffer);
        }
    }
    return inMessage;
}
Also used : OutMessage(edu.iu.dsc.tws.comms.dfw.OutMessage) ArrayList(java.util.ArrayList) InMessage(edu.iu.dsc.tws.comms.dfw.InMessage) MessageHeader(edu.iu.dsc.tws.api.comms.messaging.MessageHeader) ChannelMessage(edu.iu.dsc.tws.api.comms.messaging.ChannelMessage) DataBuffer(edu.iu.dsc.tws.api.comms.packing.DataBuffer)

Example 5 with InMessage

use of edu.iu.dsc.tws.comms.dfw.InMessage in project twister2 by DSC-SPIDAL.

the class TWSUCXChannelTest method ucxTest.

@Test
public void ucxTest() throws InterruptedException {
    final int dataSize = 25;
    final int[] data = new int[dataSize];
    for (int i = 0; i < dataSize; i++) {
        data[i] = i;
    }
    new Thread(() -> {
        TWSUCXChannel sender = new TWSUCXChannel(Config.newBuilder().build(), workerControllers.get(0));
        OutMessage outMessage = new OutMessage(0, 1, -1, 10, 0, null, null, MessageTypes.INTEGER_ARRAY, null, null, data);
        Queue<DataBuffer> sendingBuffers = new ConcurrentLinkedQueue<>();
        sendingBuffers.add(new DataBuffer(sender.createBuffer(50)));
        sendingBuffers.add(new DataBuffer(sender.createBuffer(50)));
        DataSerializer dataSerializer = new DataSerializer();
        dataSerializer.init(Config.newBuilder().build(), sendingBuffers);
        AtomicInteger sendingCount = new AtomicInteger();
        AtomicBoolean progress = new AtomicBoolean(true);
        while (progress.get()) {
            ChannelMessage channelMessage = dataSerializer.build(data, outMessage);
            if (channelMessage != null) {
                sender.sendMessage(1, channelMessage, new ChannelListener() {

                    @Override
                    public void onReceiveComplete(int id, int stream, DataBuffer message) {
                    }

                    @Override
                    public void onSendComplete(int id, int stream, ChannelMessage message) {
                        message.getBuffers().forEach(dataBuffer -> {
                            dataBuffer.setSize(0);
                            dataBuffer.getByteBuffer().clear();
                        });
                        sendingBuffers.addAll(message.getBuffers());
                        progress.set(!outMessage.getSendState().equals(OutMessage.SendState.SERIALIZED));
                    // LOG.info("Sending Count : " + sendingCount.incrementAndGet());
                    }
                });
            }
            sender.progress();
        }
        LOG.info("Exiting send loop...");
    }).start();
    TWSUCXChannel receiver = new TWSUCXChannel(Config.newBuilder().build(), workerControllers.get(1));
    Queue<DataBuffer> recvBuffers = new ConcurrentLinkedQueue<>();
    recvBuffers.add(new DataBuffer(receiver.createBuffer(50)));
    recvBuffers.add(new DataBuffer(receiver.createBuffer(50)));
    DataDeserializer dataDeserializer = new DataDeserializer();
    dataDeserializer.init(Config.newBuilder().build());
    AtomicInteger recvCount = new AtomicInteger(0);
    AtomicBoolean received = new AtomicBoolean(false);
    final int[][] receivedData = new int[1][dataSize];
    receiver.receiveMessage(0, 0, 1, new ChannelListener() {

        private InMessage inMessage;

        @Override
        public void onReceiveComplete(int id, int stream, DataBuffer message) {
            if (recvCount.incrementAndGet() == 1) {
                MessageHeader messageHeader = dataDeserializer.buildHeader(message, 1);
                this.inMessage = new InMessage(id, MessageTypes.INTEGER_ARRAY, channelMessage -> {
                    LOG.info("Release called");
                }, messageHeader);
            }
            LOG.info("Received : " + recvCount.get());
            if (this.inMessage.addBufferAndCalculate(message)) {
                LOG.info("Built");
                dataDeserializer.build(this.inMessage, 1);
                receivedData[0] = (int[]) this.inMessage.getDataBuilder().getFinalObject();
                received.set(true);
            }
            dataDeserializer.build(this.inMessage, 1);
            message.getByteBuffer().clear();
            recvBuffers.add(message);
        }

        @Override
        public void onSendComplete(int id, int stream, ChannelMessage message) {
        }
    }, recvBuffers);
    while (!received.get()) {
        receiver.progress();
    }
    Assert.assertArrayEquals(data, receivedData[0]);
}
Also used : ChannelListener(edu.iu.dsc.tws.api.comms.channel.ChannelListener) OutMessage(edu.iu.dsc.tws.comms.dfw.OutMessage) InMessage(edu.iu.dsc.tws.comms.dfw.InMessage) ChannelMessage(edu.iu.dsc.tws.api.comms.messaging.ChannelMessage) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataDeserializer(edu.iu.dsc.tws.comms.dfw.io.DataDeserializer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageHeader(edu.iu.dsc.tws.api.comms.messaging.MessageHeader) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) DataBuffer(edu.iu.dsc.tws.api.comms.packing.DataBuffer) DataSerializer(edu.iu.dsc.tws.comms.dfw.io.DataSerializer) Test(org.junit.Test)

Aggregations

InMessage (edu.iu.dsc.tws.comms.dfw.InMessage)38 Test (org.junit.Test)29 Tuple (edu.iu.dsc.tws.api.comms.structs.Tuple)17 ArrayList (java.util.ArrayList)17 MessageType (edu.iu.dsc.tws.api.comms.messaging.types.MessageType)15 List (java.util.List)14 ChannelMessage (edu.iu.dsc.tws.api.comms.messaging.ChannelMessage)10 MessageHeader (edu.iu.dsc.tws.api.comms.messaging.MessageHeader)10 DataBuffer (edu.iu.dsc.tws.api.comms.packing.DataBuffer)10 OutMessage (edu.iu.dsc.tws.comms.dfw.OutMessage)6 DataPacker (edu.iu.dsc.tws.api.comms.packing.DataPacker)4 ChannelListener (edu.iu.dsc.tws.api.comms.channel.ChannelListener)1 DataDeserializer (edu.iu.dsc.tws.comms.dfw.io.DataDeserializer)1 DataSerializer (edu.iu.dsc.tws.comms.dfw.io.DataSerializer)1 Queue (java.util.Queue)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1