Search in sources :

Example 1 with VariableKeyPartitioner

use of io.s4.dispatcher.partitioner.VariableKeyPartitioner in project core by s4.

the class Dispatcher method dispatchEvent.

private void dispatchEvent(String streamName, Object event, boolean variableKey, List<List<String>> compoundKeyNames) {
    synchronized (this) {
        rawEventCount++;
    }
    if (eventEmitter.getNodeCount() <= 0) {
        return;
    } else {
        if (counts == null) {
            counts = new int[eventEmitter.getNodeCount()];
        }
    }
    try {
        synchronized (this) {
            eventCount++;
        }
        List<CompoundKeyInfo> partionInfoList = new ArrayList<CompoundKeyInfo>();
        for (Partitioner partitioner : partitioners) {
            List<CompoundKeyInfo> pInfoList = null;
            if (!variableKey) {
                pInfoList = partitioner.partition(streamName, event, eventEmitter.getNodeCount());
            } else {
                if (partitioner instanceof VariableKeyPartitioner) {
                    VariableKeyPartitioner vp = (VariableKeyPartitioner) partitioner;
                    pInfoList = vp.partition(streamName, compoundKeyNames, event, eventEmitter.getNodeCount());
                }
            }
            if (pInfoList != null) {
                partionInfoList.addAll(pInfoList);
            }
        }
        Map<Integer, List<CompoundKeyInfo>> pInfoMap = new HashMap<Integer, List<CompoundKeyInfo>>();
        for (CompoundKeyInfo partitionInfo : partionInfoList) {
            int partitionId = partitionInfo.getPartitionId();
            List<CompoundKeyInfo> listByPartitionNumber = pInfoMap.get(partitionId);
            if (listByPartitionNumber == null) {
                listByPartitionNumber = new ArrayList<CompoundKeyInfo>();
                pInfoMap.put(partitionId, listByPartitionNumber);
            }
            listByPartitionNumber.add(partitionInfo);
        }
        for (int partitionId : pInfoMap.keySet()) {
            EventWrapper eventWrapper = new EventWrapper(streamName, event, pInfoMap.get(partitionId));
            counts[partitionId]++;
            eventEmitter.emit(partitionId, eventWrapper);
        }
    } catch (Exception e) {
        Logger.getLogger(loggerName).error("Exception in processEvent on thread " + Thread.currentThread().getId() + " at time " + System.currentTimeMillis(), e);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CompoundKeyInfo(io.s4.dispatcher.partitioner.CompoundKeyInfo) List(java.util.List) ArrayList(java.util.ArrayList) VariableKeyPartitioner(io.s4.dispatcher.partitioner.VariableKeyPartitioner) Partitioner(io.s4.dispatcher.partitioner.Partitioner) VariableKeyPartitioner(io.s4.dispatcher.partitioner.VariableKeyPartitioner) EventWrapper(io.s4.collector.EventWrapper)

Aggregations

EventWrapper (io.s4.collector.EventWrapper)1 CompoundKeyInfo (io.s4.dispatcher.partitioner.CompoundKeyInfo)1 Partitioner (io.s4.dispatcher.partitioner.Partitioner)1 VariableKeyPartitioner (io.s4.dispatcher.partitioner.VariableKeyPartitioner)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1