Search in sources :

Example 16 with Tuple

use of backtype.storm.tuple.Tuple in project jstorm by alibaba.

the class StatefulBoltExecutor method handleCheckpoint.

@Override
protected void handleCheckpoint(Tuple checkpointTuple, Action action, long txid) {
    LOG.debug("handleCheckPoint with tuple {}, action {}, txid {}", checkpointTuple, action, txid);
    if (action == Action.PREPARE) {
        if (boltInitialized) {
            bolt.prePrepare(txid);
            state.prepareCommit(txid);
            preparedTuples.addAll(collector.ackedTuples());
        } else {
            /*
                 * May be the task restarted in the middle and the state needs be initialized.
                 * Fail fast and trigger recovery.
                  */
            LOG.debug("Failing checkpointTuple, PREPARE received when bolt state is not initialized.");
            collector.fail(checkpointTuple);
            return;
        }
    } else if (action == Action.COMMIT) {
        bolt.preCommit(txid);
        state.commit(txid);
        ack(preparedTuples);
    } else if (action == Action.ROLLBACK) {
        bolt.preRollback();
        state.rollback();
        fail(preparedTuples);
        fail(collector.ackedTuples());
    } else if (action == Action.INITSTATE) {
        if (!boltInitialized) {
            bolt.initState((T) state);
            boltInitialized = true;
            LOG.debug("{} pending tuples to process", pendingTuples.size());
            for (Tuple tuple : pendingTuples) {
                doExecute(tuple);
            }
            pendingTuples.clear();
        } else {
            LOG.debug("Bolt state is already initialized, ignoring tuple {}, action {}, txid {}", checkpointTuple, action, txid);
        }
    }
    collector.emit(CheckpointSpout.CHECKPOINT_STREAM_ID, checkpointTuple, new Values(txid, action));
    collector.delegate.ack(checkpointTuple);
}
Also used : Values(backtype.storm.tuple.Values) Tuple(backtype.storm.tuple.Tuple)

Example 17 with Tuple

use of backtype.storm.tuple.Tuple in project jstorm by alibaba.

the class TransactionBolt method finishCurrentBatch.

private void finishCurrentBatch() {
    BatchGroupId batchGroupId = currentBatchTracker.bactchGroupId;
    if (batchGroupId.batchId == TransactionCommon.INIT_BATCH_ID) {
        LOG.info("Received all init events");
        cleanupBuffer(batchGroupId.groupId);
        boltStatus = State.ACTIVE;
    } else {
        commit();
        lastSuccessfulBatch.put(batchGroupId.groupId, batchGroupId.batchId);
    }
    if (downstreamTasks.size() == 0) {
        ackBatch(batchGroupId);
    } else {
        outputCollector.flushBarrier();
    }
    removeProcessingBatch(batchGroupId);
    currentBatchTracker = null;
    LOG.debug("finishCurrentBatch, {}", currentBatchStatusInfo());
    List<Tuple> goingtoProcessBatch = batchCache.getNextPendingTuples(lastSuccessfulBatch);
    if (goingtoProcessBatch != null) {
        BatchGroupId nextGroupBatchId = new BatchGroupId(batchGroupId.groupId, batchGroupId.batchId + 1);
        //LOG.info("Get pending batch-{} which is going to be processed. size={}", nextGroupBatchId, goingtoProcessBatch.size());
        processBatch(nextGroupBatchId, goingtoProcessBatch);
    }
}
Also used : BatchGroupId(com.alibaba.jstorm.transactional.BatchGroupId) Tuple(backtype.storm.tuple.Tuple)

Example 18 with Tuple

use of backtype.storm.tuple.Tuple in project jstorm by alibaba.

the class BoltBatchCollector method getMessageId.

@Override
protected MessageId getMessageId(Collection<Tuple> anchors) {
    MessageId ret = null;
    if (anchors != null) {
        Map<Long, Long> anchors_to_ids = new HashMap<Long, Long>();
        long now = System.currentTimeMillis();
        if (now - lastRotate > rotateTime) {
            pending_acks.rotate();
            synchronized (pendingTuples) {
                pendingTuples.rotate();
            }
            lastRotate = now;
        }
        for (Tuple a : anchors) {
            // Long edge_id = MessageId.generateId();
            Long edge_id = MessageId.generateId(random);
            synchronized (pending_acks) {
                put_xor(pending_acks, a, edge_id);
            }
            MessageId messageId = a.getMessageId();
            if (messageId != null) {
                for (Long root_id : messageId.getAnchorsToIds().keySet()) {
                    put_xor(anchors_to_ids, root_id, edge_id);
                }
            }
        }
        ret = MessageId.makeId(anchors_to_ids);
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) Tuple(backtype.storm.tuple.Tuple) MessageId(backtype.storm.tuple.MessageId)

Example 19 with Tuple

use of backtype.storm.tuple.Tuple in project jstorm by alibaba.

the class BoltBatchCollector method sendBatch.

public List<Integer> sendBatch(String outStreamId, String outTaskId, List<MsgInfo> batchTobeFlushed, boolean isFlush) {
    final long start = emitTimer.getTime();
    try {
        Map<Object, List<MsgInfo>> outTasks = null;
        if (outTaskId != null) {
            outTasks = sendTargets.getBatch(Integer.valueOf(outTaskId), outStreamId, batchTobeFlushed);
        } else {
            outTasks = sendTargets.getBatch(outStreamId, batchTobeFlushed);
        }
        if (outTasks == null || outTasks.size() == 0) {
        } else {
            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 (Integer t : tasks) {
                    List<Object> batchValues = new ArrayList<Object>();
                    for (MsgInfo msg : batch) {
                        BoltMsgInfo msgInfo = (BoltMsgInfo) msg;
                        Pair<MessageId, List<Object>> pair = new Pair<MessageId, List<Object>>(getMessageId(msgInfo.anchors), msgInfo.values);
                        batchValues.add(pair);
                    }
                    TupleImplExt batchTuple = new TupleImplExt(topologyContext, batchValues, task_id, outStreamId, null);
                    batchTuple.setTargetTaskId(t);
                    batchTuple.setBatchTuple(true);
                    taskTransfer.transfer(batchTuple);
                }
                for (MsgInfo msg : batch) {
                    if (msg.callback != null) {
                        msg.callback.execute(outStreamId, tasks, msg.values);
                    }
                }
            }
        }
        for (MsgInfo msg : batchTobeFlushed) {
            Collection<Tuple> anchors = ((BoltMsgInfo) msg).anchors;
            if (anchors != null && anchors.size() > 0) {
                for (Tuple a : anchors) {
                    synchronized (pendingTuples) {
                        Integer pendingCount = pendingTuples.get(a);
                        if (pendingCount != null) {
                            if (--pendingCount <= 0) {
                                pendingTuples.remove(a);
                            } else {
                                pendingTuples.put(a, pendingCount);
                            }
                        }
                    }
                }
            }
        }
        return null;
    } catch (Exception e) {
        LOG.error("bolt emit", e);
    } finally {
        emitTimer.updateTime(start);
    }
    return new ArrayList<Integer>();
}
Also used : ArrayList(java.util.ArrayList) TupleImplExt(backtype.storm.tuple.TupleImplExt) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) RotatingMap(com.alibaba.jstorm.utils.RotatingMap) Tuple(backtype.storm.tuple.Tuple) Pair(com.alibaba.jstorm.utils.Pair) MessageId(backtype.storm.tuple.MessageId)

Example 20 with Tuple

use of backtype.storm.tuple.Tuple in project jstorm by alibaba.

the class BoltCollector method getMessageId.

protected MessageId getMessageId(Collection<Tuple> anchors) {
    MessageId ret = null;
    if (anchors != null && ackerNum > 0) {
        Map<Long, Long> anchors_to_ids = new HashMap<Long, Long>();
        for (Tuple a : anchors) {
            if (a.getMessageId() != null) {
                Long edge_id = MessageId.generateId(random);
                put_xor(pending_acks, a, edge_id);
                MessageId messageId = a.getMessageId();
                if (messageId != null) {
                    for (Long root_id : messageId.getAnchorsToIds().keySet()) {
                        put_xor(anchors_to_ids, root_id, edge_id);
                    }
                }
            }
        }
        ret = MessageId.makeId(anchors_to_ids);
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) Tuple(backtype.storm.tuple.Tuple) MessageId(backtype.storm.tuple.MessageId)

Aggregations

Tuple (backtype.storm.tuple.Tuple)49 Values (backtype.storm.tuple.Values)11 ArrayList (java.util.ArrayList)10 MessageId (backtype.storm.tuple.MessageId)5 List (java.util.List)5 Test (org.testng.annotations.Test)5 Message (com.yahoo.pulsar.client.api.Message)4 TupleImpl (backtype.storm.tuple.TupleImpl)3 TupleImplExt (backtype.storm.tuple.TupleImplExt)3 Pair (com.alibaba.jstorm.utils.Pair)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 GlobalStreamId (backtype.storm.generated.GlobalStreamId)2 FailedException (backtype.storm.topology.FailedException)2 TupleExt (backtype.storm.tuple.TupleExt)2 TimerTrigger (com.alibaba.jstorm.daemon.worker.timer.TimerTrigger)2 TopoMasterCtrlEvent (com.alibaba.jstorm.task.master.ctrlevent.TopoMasterCtrlEvent)2 RotatingMap (com.alibaba.jstorm.utils.RotatingMap)2 Pair (com.alipay.dw.jstorm.example.sequence.bean.Pair)2 TradeCustomer (com.alipay.dw.jstorm.example.sequence.bean.TradeCustomer)2