Search in sources :

Example 56 with Tuple

use of edu.iu.dsc.tws.api.comms.structs.Tuple in project twister2 by DSC-SPIDAL.

the class KeyedReceiver method offerMessage.

/**
 * saves the given message (or messages if the object is a list) into the messages data structure
 * if possible and rejects the message if the whole message cannot be added to the messages
 * data structure.
 *
 * @param target target for which the messages are to be added
 * @param object the message/messages to be added
 * @return true if the message was added or false otherwise
 */
@SuppressWarnings("rawtypes")
protected boolean offerMessage(int target, Object object) {
    Map<Object, Queue<Object>> messagesPerTarget = messages.get(target);
    if (!isFinalBatchReceiver && messagesPerTarget.size() > keyLimit) {
        LOG.fine(String.format("Executor %d Partial cannot add any further keys needs flush ", executor));
        moveMessagesToSendQueue(target, messagesPerTarget);
        return false;
    }
    if (object instanceof AggregatedObjects) {
        List dataList = (List) object;
        Map<Object, List<Object>> tempList = new HashMap<>();
        for (Object dataEntry : dataList) {
            Tuple tuple = (Tuple) dataEntry;
            // If any of the keys are full the method returns false because partial objects cannot be
            // added to the messages data structure
            Object key = tuple.getKey();
            if (!isFinalBatchReceiver && messagesPerTarget.containsKey(key) && messagesPerTarget.get(key).size() >= limitPerKey) {
                moveMessageToSendQueue(target, messagesPerTarget, tuple.getKey());
                LOG.fine(String.format("Executor %d Partial cannot add any further values for key " + "needs flush ", executor));
                return false;
            }
            if (tempList.containsKey(key)) {
                tempList.get(key).add(tuple.getValue());
            } else {
                tempList.put(key, new AggregatedObjects<>());
                tempList.get(key).add(tuple.getValue());
            }
        }
        boolean offerDone = true;
        for (Object key : tempList.keySet()) {
            if (messagesPerTarget.containsKey(key)) {
                List<Object> values = tempList.get(key);
                for (Object value : values) {
                    offerDone &= messagesPerTarget.get(key).offer(value);
                }
            } else {
                ArrayDeque<Object> messagesPerKey = new ArrayDeque<>();
                List<Object> values = tempList.get(key);
                for (Object value : values) {
                    offerDone &= messagesPerKey.offer(value);
                }
                messagesPerTarget.put(key, messagesPerKey);
            }
        }
        // cannot be recovered
        if (!offerDone) {
            throw new RuntimeException("Message lost during processing");
        }
    } else {
        Tuple tuple = (Tuple) object;
        if (messagesPerTarget.containsKey(tuple.getKey())) {
            if (messagesPerTarget.get(tuple.getKey()).size() < limitPerKey) {
                return messagesPerTarget.get(tuple.getKey()).offer(tuple.getValue());
            } else {
                LOG.fine(String.format("Executor %d Partial cannot add any further values for key " + "needs flush ", executor));
                moveMessageToSendQueue(target, messagesPerTarget, tuple.getKey());
                return false;
            }
        } else {
            ArrayDeque<Object> messagesPerKey = new ArrayDeque<>();
            messagesPerKey.add(tuple.getValue());
            messagesPerTarget.put(tuple.getKey(), messagesPerKey);
        }
    }
    return true;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayDeque(java.util.ArrayDeque) List(java.util.List) Queue(java.util.Queue) Tuple(edu.iu.dsc.tws.api.comms.structs.Tuple)

Example 57 with Tuple

use of edu.iu.dsc.tws.api.comms.structs.Tuple in project twister2 by DSC-SPIDAL.

the class ControlledMemoryReader method next.

@Override
public Tuple next() {
    if (!this.hasNext()) {
        throw new NoSuchElementException("No more keys to iterate");
    }
    Tuple tuple = inMemoryMessages.get(readIndex);
    readIndex++;
    return tuple;
}
Also used : NoSuchElementException(java.util.NoSuchElementException) Tuple(edu.iu.dsc.tws.api.comms.structs.Tuple)

Example 58 with Tuple

use of edu.iu.dsc.tws.api.comms.structs.Tuple in project twister2 by DSC-SPIDAL.

the class FSKeyedMerger method deserializeObjects.

private void deserializeObjects() {
    for (int i = 0; i < recordsInMemory.size(); i++) {
        Tuple kv = recordsInMemory.get(i);
        Object o = dataType.getDataPacker().unpackFromByteArray((byte[]) kv.getValue());
        objectsInMemory.add(new Tuple(kv.getKey(), o));
    }
}
Also used : Tuple(edu.iu.dsc.tws.api.comms.structs.Tuple)

Example 59 with Tuple

use of edu.iu.dsc.tws.api.comms.structs.Tuple in project twister2 by DSC-SPIDAL.

the class FSKeyedSortedMerger2 method deserializeObjects.

private void deserializeObjects() {
    // this thread is also counted
    int threads = CommonThreadPool.getThreadCount() + 1;
    List<Future<Boolean>> deserializeFutures = new ArrayList<>();
    int chunkSize = this.recordsInMemory.size() / threads;
    if (this.recordsInMemory.size() % threads != 0) {
        chunkSize++;
    }
    for (int i = 0; i < this.recordsInMemory.size(); i += chunkSize) {
        final int start = i;
        final int end = Math.min(this.recordsInMemory.size(), i + chunkSize);
        // last chunk will be processed in this thread
        if (end == this.recordsInMemory.size()) {
            Iterator<Tuple> itr = recordsInMemory.listIterator(start);
            int count = 0;
            int elements = end - start;
            while (itr.hasNext() && count < elements) {
                Tuple tuple = itr.next();
                Object o = dataType.getDataPacker().unpackFromByteArray((byte[]) tuple.getValue());
                tuple.setValue(o);
                count++;
            }
        } else {
            deserializeFutures.add(CommonThreadPool.getExecutor().submit(() -> {
                Iterator<Tuple> itr = recordsInMemory.listIterator(start);
                int count = 0;
                int elements = end - start;
                while (itr.hasNext() && count < elements) {
                    Tuple tuple = itr.next();
                    Object o = dataType.getDataPacker().unpackFromByteArray((byte[]) tuple.getValue());
                    tuple.setValue(o);
                    count++;
                }
                return true;
            }));
        }
    }
    for (int i = 0; i < deserializeFutures.size(); i++) {
        try {
            deserializeFutures.get(i).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException("Error in deserializing records in memory", e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Tuple(edu.iu.dsc.tws.api.comms.structs.Tuple)

Example 60 with Tuple

use of edu.iu.dsc.tws.api.comms.structs.Tuple in project twister2 by DSC-SPIDAL.

the class FSKeyedSortedMerger2 method add.

/**
 * Add the data to the file
 */
public synchronized void add(Object key, byte[] data, int length) {
    if (status == FSStatus.READING) {
        throw new RuntimeException("Cannot add after switching to reading");
    }
    if (status == FSStatus.WRITING_MEMORY) {
        this.recordsInMemory.add(new Tuple(key, data));
        this.numOfBytesInMemory += data.length;
        // we switch to disk
        if (numOfBytesInMemory >= maxBytesToKeepInMemory) {
            LOG.info(String.format("Switching to write to disk memory %d >= maxMemory %d", numOfBytesInMemory, maxBytesToKeepInMemory));
            status = FSStatus.WRITING_DISK;
            this.numOfBytesInMemory = 0;
        }
    } else {
        this.recordsToDisk.add(new Tuple(key, data));
        this.numOfBytesInMemory += data.length;
    }
}
Also used : Tuple(edu.iu.dsc.tws.api.comms.structs.Tuple)

Aggregations

Tuple (edu.iu.dsc.tws.api.comms.structs.Tuple)98 Iterator (java.util.Iterator)38 List (java.util.List)35 Logger (java.util.logging.Logger)34 ArrayList (java.util.ArrayList)29 Config (edu.iu.dsc.tws.api.config.Config)27 WorkerEnvironment (edu.iu.dsc.tws.api.resource.WorkerEnvironment)24 Test (org.junit.Test)24 BatchEnvironment (edu.iu.dsc.tws.tset.env.BatchEnvironment)18 InMessage (edu.iu.dsc.tws.comms.dfw.InMessage)17 HashMap (java.util.HashMap)16 TSetEnvironment (edu.iu.dsc.tws.tset.env.TSetEnvironment)15 JobConfig (edu.iu.dsc.tws.api.JobConfig)14 MessageTypes (edu.iu.dsc.tws.api.comms.messaging.types.MessageTypes)14 JoinedTuple (edu.iu.dsc.tws.api.comms.structs.JoinedTuple)14 ResourceAllocator (edu.iu.dsc.tws.rsched.core.ResourceAllocator)14 SourceTSet (edu.iu.dsc.tws.tset.sets.batch.SourceTSet)13 CommunicationContext (edu.iu.dsc.tws.api.comms.CommunicationContext)11 MessageType (edu.iu.dsc.tws.api.comms.messaging.types.MessageType)11 Comparator (java.util.Comparator)11