Search in sources :

Example 6 with Pair

use of com.alibaba.jstorm.utils.Pair in project jstorm by alibaba.

the class SpoutBatchCollector method sendBatch.

public List<Integer> sendBatch(String outStreamId, String outTaskId, List<MsgInfo> batchTobeFlushed) {
    long startTime = emitTotalTimer.getTime();
    try {
        List<Integer> ret = null;
        Map<Object, List<MsgInfo>> outTasks;
        if (outTaskId != null) {
            outTasks = sendTargets.getBatch(Integer.valueOf(outTaskId), outStreamId, batchTobeFlushed);
        } else {
            outTasks = sendTargets.getBatch(outStreamId, batchTobeFlushed);
        }
        if (outTasks == null || outTasks.size() == 0) {
            // don't need send tuple to other task
            return new ArrayList<Integer>();
        }
        Map<Long, MsgInfo> ackBatch = new HashMap<Long, MsgInfo>();
        for (Map.Entry<Object, List<MsgInfo>> entry : outTasks.entrySet()) {
            Object target = entry.getKey();
            List<Integer> tasks = (target instanceof Integer) ? JStormUtils.mk_list((Integer) target) : ((List<Integer>) target);
            List<MsgInfo> batch = entry.getValue();
            for (int i = 0; i < tasks.size(); i++) {
                Integer t = tasks.get(i);
                List<Object> batchValues = new ArrayList<Object>();
                for (MsgInfo msg : batch) {
                    SpoutMsgInfo msgInfo = (SpoutMsgInfo) msg;
                    Pair<MessageId, List<Object>> pair = new Pair<MessageId, List<Object>>(getMessageId(msgInfo, ackBatch), msgInfo.values);
                    batchValues.add(pair);
                }
                TupleImplExt batchTuple = new TupleImplExt(topology_context, batchValues, task_id, outStreamId, null);
                batchTuple.setTargetTaskId(t);
                batchTuple.setBatchTuple(true);
                transfer_fn.transfer(batchTuple);
            }
            for (MsgInfo msg : batch) {
                if (msg.callback != null) {
                    msg.callback.execute(outStreamId, tasks, msg.values);
                }
            }
        }
        if (ackBatch.size() > 0) {
            sendBatch(Acker.ACKER_INIT_STREAM_ID, null, new ArrayList<MsgInfo>(ackBatch.values()));
        }
        return ret;
    } finally {
        emitTotalTimer.updateTime(startTime);
    }
}
Also used : MsgInfo(com.alibaba.jstorm.task.execute.MsgInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TimeOutMap(com.alibaba.jstorm.utils.TimeOutMap) Pair(com.alibaba.jstorm.utils.Pair)

Example 7 with Pair

use of com.alibaba.jstorm.utils.Pair in project jstorm by alibaba.

the class TransactionBolt method execute.

@Override
public void execute(Tuple input) {
    try {
        String stream = input.getSourceStreamId();
        if (stream.equals(Common.TOPOLOGY_MASTER_CONTROL_STREAM_ID)) {
            processTransactionEvent(input);
        } else {
            List<Object> firstTupleValue = ((Pair<MessageId, List<Object>>) input.getValues().get(0)).getSecond();
            BatchGroupId batchGroupId = (BatchGroupId) firstTupleValue.get(0);
            if (isDiscarded(batchGroupId)) {
                LOG.debug("Tuple was discarded. {}", input);
                return;
            } else if (batchCache.isPendingBatch(batchGroupId, lastSuccessfulBatch)) {
                if (batchCache.cachePendingBatch(batchGroupId, input, lastSuccessfulBatch)) {
                    return;
                }
            }
            currentBatchTracker = getProcessingBatch(batchGroupId, true);
            outputCollector.setCurrBatchTracker(currentBatchTracker);
            processBatchTuple(input);
        }
    } catch (Exception e) {
        LOG.info("Failed to process input={}", input, e);
        throw new RuntimeException(e);
    }
}
Also used : BatchGroupId(com.alibaba.jstorm.transactional.BatchGroupId) InvalidTopologyException(backtype.storm.generated.InvalidTopologyException) Pair(com.alibaba.jstorm.utils.Pair)

Example 8 with Pair

use of com.alibaba.jstorm.utils.Pair in project jstorm by alibaba.

the class TransactionBolt method processBatchTuple.

public void processBatchTuple(Tuple batchEvent) {
    String stream = batchEvent.getSourceStreamId();
    if (stream.equals(TransactionCommon.BARRIER_STREAM_ID)) {
        Pair<MessageId, List<Object>> val = (Pair<MessageId, List<Object>>) batchEvent.getValue(0);
        BatchSnapshot snapshot = (BatchSnapshot) val.getSecond().get(1);
        currentBatchTracker.receiveBarrier(batchEvent.getSourceTask());
        currentBatchTracker.expectedTupleCount += snapshot.getTupleCount();
        LOG.debug("Received batch, stream={}, batchGroupId={}, sourceTask={}, values={}", stream, currentBatchTracker.bactchGroupId, batchEvent.getSourceTask(), snapshot);
        LOG.debug("currentBatchTracker={}, processingBatches={}, pendingBatches={}", currentBatchTracker, processingBatches, batchCache);
    } else {
        for (Object value : batchEvent.getValues()) {
            /*List<Object> firstTupleValue = ((Pair<MessageId, List<Object>>) value).getSecond();
                BatchGroupId batchGroupId = (BatchGroupId) firstTupleValue.get(0);
                if (!batchGroupId.equals(currentBatchTracker.bactchGroupId)) {
                    LOG.warn("batchgroupid-{} is not equal to the once of current batch tracker-{}!", batchGroupId, currentBatchTracker.bactchGroupId);
                }*/
            Pair<MessageId, List<Object>> val = (Pair<MessageId, List<Object>>) value;
            val.getSecond().remove(0);
            TupleImplExt tuple = new TupleImplExt(topologyContext, val.getSecond(), val.getFirst(), ((TupleImplExt) batchEvent));
            boltExecutor.execute(tuple);
        }
        currentBatchTracker.incrementReceivedCount(batchEvent.getValues().size());
    }
    if (currentBatchTracker.checkFinish()) {
        finishCurrentBatch();
    }
}
Also used : TupleImplExt(backtype.storm.tuple.TupleImplExt) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.alibaba.jstorm.utils.Pair) MessageId(backtype.storm.tuple.MessageId) BatchSnapshot(com.alibaba.jstorm.transactional.BatchSnapshot)

Example 9 with Pair

use of com.alibaba.jstorm.utils.Pair in project jstorm by alibaba.

the class SpoutExecutors method onEvent.

/**
     * Handle acker message
     *
     * @see com.lmax.disruptor.EventHandler#onEvent(java.lang.Object, long, boolean)
     */
@Override
public void onEvent(Object event, long sequence, boolean endOfBatch) throws Exception {
    try {
        if (event == null) {
            return;
        }
        Runnable runnable = null;
        if (event instanceof Tuple) {
            if (((TupleExt) event).isBatchTuple()) {
                List<Object> values = ((Tuple) event).getValues();
                for (Object value : values) {
                    Pair<MessageId, List<Object>> val = (Pair<MessageId, List<Object>>) value;
                    TupleImplExt tuple = new TupleImplExt(sysTopologyCtx, val.getSecond(), val.getFirst(), ((TupleImplExt) event));
                    processControlEvent();
                    runnable = processTupleEvent(tuple);
                    if (runnable != null) {
                        runnable.run();
                        runnable = null;
                    }
                }
            } else {
                runnable = processTupleEvent((Tuple) event);
            }
        } else if (event instanceof TimerTrigger.TimerEvent) {
            processTimerEvent((TimerTrigger.TimerEvent) event);
            return;
        } else if (event instanceof IAckMsg) {
            runnable = (Runnable) event;
        } else if (event instanceof Runnable) {
            runnable = (Runnable) event;
        } else {
            LOG.warn("Receive one unknow event-" + event.toString() + " " + idStr);
            return;
        }
        if (runnable != null)
            runnable.run();
    } catch (Throwable e) {
        if (!taskStatus.isShutdown()) {
            LOG.info("Unknow excpetion ", e);
            report_error.report(e);
        }
    }
}
Also used : TupleExt(backtype.storm.tuple.TupleExt) TupleImplExt(backtype.storm.tuple.TupleImplExt) List(java.util.List) TimerTrigger(com.alibaba.jstorm.daemon.worker.timer.TimerTrigger) Tuple(backtype.storm.tuple.Tuple) Pair(com.alibaba.jstorm.utils.Pair) MessageId(backtype.storm.tuple.MessageId)

Example 10 with Pair

use of com.alibaba.jstorm.utils.Pair in project jstorm by alibaba.

the class KryoTupleSerializer method serializeTuple.

/**
     * @@@ in the furture, it will skill serialize 'targetTask' through check some flag
     * @see backtype.storm.serialization.ITupleSerializer#serialize(int, backtype.storm.tuple.Tuple)
     */
private void serializeTuple(Output output, Tuple tuple) {
    try {
        boolean isBatchTuple = false;
        if (tuple instanceof TupleExt) {
            output.writeInt(((TupleExt) tuple).getTargetTaskId());
            output.writeLong(((TupleExt) tuple).getCreationTimeStamp());
            output.writeBoolean(((TupleExt) tuple).isBatchTuple());
            isBatchTuple = ((TupleExt) tuple).isBatchTuple();
        }
        output.writeInt(tuple.getSourceTask(), true);
        output.writeInt(_ids.getStreamId(tuple.getSourceComponent(), tuple.getSourceStreamId()), true);
        if (isBatchTuple) {
            List<Object> values = tuple.getValues();
            int len = values.size();
            output.writeInt(len, true);
            if (_ackerNum > 0) {
                for (Object value : values) {
                    Pair<MessageId, List<Object>> pairValue = (Pair<MessageId, List<Object>>) value;
                    if (pairValue.getFirst() != null) {
                        pairValue.getFirst().serialize(output);
                    } else {
                        output.writeInt(0, true);
                    }
                    _kryo.serializeInto(pairValue.getSecond(), output);
                }
            } else {
                for (Object value : values) {
                    Pair<MessageId, List<Object>> pairValue = (Pair<MessageId, List<Object>>) value;
                    _kryo.serializeInto(pairValue.getSecond(), output);
                }
            }
        } else {
            MessageId msgId = tuple.getMessageId();
            if (msgId != null) {
                msgId.serialize(output);
            } else {
                output.writeInt(0, true);
            }
            _kryo.serializeInto(tuple.getValues(), output);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : TupleExt(backtype.storm.tuple.TupleExt) List(java.util.List) IOException(java.io.IOException) Pair(com.alibaba.jstorm.utils.Pair) MessageId(backtype.storm.tuple.MessageId)

Aggregations

Pair (com.alibaba.jstorm.utils.Pair)10 List (java.util.List)7 MessageId (backtype.storm.tuple.MessageId)6 TupleImplExt (backtype.storm.tuple.TupleImplExt)5 ArrayList (java.util.ArrayList)4 Tuple (backtype.storm.tuple.Tuple)3 TupleExt (backtype.storm.tuple.TupleExt)3 TimerTrigger (com.alibaba.jstorm.daemon.worker.timer.TimerTrigger)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 InvalidTopologyException (backtype.storm.generated.InvalidTopologyException)1 IRichBatchBolt (backtype.storm.topology.IRichBatchBolt)1 LocalState (backtype.storm.utils.LocalState)1 MetricMeta (com.alibaba.jstorm.common.metric.MetricMeta)1 MetaType (com.alibaba.jstorm.metric.MetaType)1 MsgInfo (com.alibaba.jstorm.task.execute.MsgInfo)1 BatchGroupId (com.alibaba.jstorm.transactional.BatchGroupId)1 BatchSnapshot (com.alibaba.jstorm.transactional.BatchSnapshot)1 RotatingMap (com.alibaba.jstorm.utils.RotatingMap)1