Search in sources :

Example 1 with ConsList

use of storm.trident.tuple.ConsList in project jstorm by alibaba.

the class WindowTridentProcessor method finishBatch.

@Override
public void finishBatch(ProcessorContext processorContext) {
    Object batchId = processorContext.batchId;
    Object batchTxnId = getBatchTxnId(batchId);
    LOG.debug("Received finishBatch of : [{}] ", batchId);
    // get all the tuples in a batch and add it to trident-window-manager
    List<TridentTuple> tuples = (List<TridentTuple>) processorContext.state[tridentContext.getStateIndex()];
    tridentWindowManager.addTuplesBatch(batchId, tuples);
    List<Integer> pendingTriggerIds = null;
    List<String> triggerKeys = new ArrayList<>();
    Iterable<Object> triggerValues = null;
    if (retriedAttempt(batchId)) {
        pendingTriggerIds = (List<Integer>) windowStore.get(inprocessTriggerKey(batchTxnId));
        for (Integer pendingTriggerId : pendingTriggerIds) {
            triggerKeys.add(triggerKey(pendingTriggerId));
        }
        triggerValues = windowStore.get(triggerKeys);
    }
    // if there are no trigger values in earlier attempts or this is a new batch, emit pending triggers.
    if (triggerValues == null) {
        pendingTriggerIds = new ArrayList<>();
        Queue<StoreBasedTridentWindowManager.TriggerResult> pendingTriggers = tridentWindowManager.getPendingTriggers();
        LOG.debug("pending triggers at batch: [{}] and triggers.size: [{}] ", batchId, pendingTriggers.size());
        try {
            Iterator<StoreBasedTridentWindowManager.TriggerResult> pendingTriggersIter = pendingTriggers.iterator();
            List<Object> values = new ArrayList<>();
            StoreBasedTridentWindowManager.TriggerResult triggerResult = null;
            while (pendingTriggersIter.hasNext()) {
                triggerResult = pendingTriggersIter.next();
                for (List<Object> aggregatedResult : triggerResult.result) {
                    String triggerKey = triggerKey(triggerResult.id);
                    triggerKeys.add(triggerKey);
                    values.add(aggregatedResult);
                    pendingTriggerIds.add(triggerResult.id);
                }
                pendingTriggersIter.remove();
            }
            triggerValues = values;
        } finally {
            // store inprocess triggers of a batch in store for batch retries for any failures
            if (!pendingTriggerIds.isEmpty()) {
                windowStore.put(inprocessTriggerKey(batchTxnId), pendingTriggerIds);
            }
        }
    }
    collector.setContext(processorContext);
    int i = 0;
    for (Object resultValue : triggerValues) {
        collector.emit(new ConsList(new TriggerInfo(windowTaskId, pendingTriggerIds.get(i++)), (List<Object>) resultValue));
    }
    collector.setContext(null);
}
Also used : ArrayList(java.util.ArrayList) ConsList(storm.trident.tuple.ConsList) ArrayList(java.util.ArrayList) List(java.util.List) TridentTuple(storm.trident.tuple.TridentTuple) ConsList(storm.trident.tuple.ConsList)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 ConsList (storm.trident.tuple.ConsList)1 TridentTuple (storm.trident.tuple.TridentTuple)1