Search in sources :

Example 11 with BatchGroupId

use of com.alibaba.jstorm.transactional.BatchGroupId 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 12 with BatchGroupId

use of com.alibaba.jstorm.transactional.BatchGroupId in project jstorm by alibaba.

the class TransactionOutputCollector method emit.

@Override
public List<Integer> emit(String streamId, Collection<Tuple> anchors, List<Object> tuple, ICollectorCallback callback) {
    List<Object> tupleWithId = new ArrayList<Object>();
    tupleWithId.add(new BatchGroupId(currBatchTracker.getBatchGroupId()));
    tupleWithId.addAll(tuple);
    return delegate.emit(streamId, anchors, tupleWithId, callback);
}
Also used : BatchGroupId(com.alibaba.jstorm.transactional.BatchGroupId) ArrayList(java.util.ArrayList)

Example 13 with BatchGroupId

use of com.alibaba.jstorm.transactional.BatchGroupId in project jstorm by alibaba.

the class TransactionSpoutOutputCollector method emitDirect.

@Override
public void emitDirect(int taskId, String streamId, List<Object> tuple, Object messageId, ICollectorCallback callback) {
    try {
        //waitActive();
        lock.readLock().lock();
        List<Object> tupleWithId = new ArrayList<Object>();
        tupleWithId.add(new BatchGroupId(groupId, currBatchId));
        tupleWithId.addAll(tuple);
        delegate.emitDirect(taskId, streamId, tupleWithId, null, (callback != null) ? callback : new CollectorCallback());
    //currBatchInfo.endPos = messageId;
    } finally {
        lock.readLock().unlock();
    }
}
Also used : ICollectorCallback(backtype.storm.task.ICollectorCallback) BatchGroupId(com.alibaba.jstorm.transactional.BatchGroupId) ArrayList(java.util.ArrayList)

Example 14 with BatchGroupId

use of com.alibaba.jstorm.transactional.BatchGroupId in project jstorm by alibaba.

the class TransactionSpoutOutputCollector method flushBarrier.

public BatchInfo flushBarrier() {
    BatchInfo ret = null;
    try {
        lock.writeLock().lock();
        ret = new BatchInfo(currBatchInfo);
        // flush pending message in outputCollector
        delegate.flush();
        // Emit and flush barrier message to all downstream tasks
        BatchGroupId batchGroupId = new BatchGroupId(groupId, currBatchId);
        for (Entry<Integer, Integer> entry : msgCount.entrySet()) {
            int taskId = entry.getKey();
            int count = entry.setValue(0);
            BatchSnapshot barrierSnapshot = new BatchSnapshot(batchGroupId, count);
            emitDirectByDelegate(taskId, TransactionCommon.BARRIER_STREAM_ID, new Values(batchGroupId, barrierSnapshot), null, null);
        }
        delegate.flush();
        moveToNextBatch();
    } finally {
        lock.writeLock().unlock();
    }
    return ret;
}
Also used : BatchGroupId(com.alibaba.jstorm.transactional.BatchGroupId) Values(backtype.storm.tuple.Values) BatchSnapshot(com.alibaba.jstorm.transactional.BatchSnapshot)

Example 15 with BatchGroupId

use of com.alibaba.jstorm.transactional.BatchGroupId in project jstorm by alibaba.

the class SnapshotStateMaster method finishSnapshotStateCommit.

private void finishSnapshotStateCommit(BatchGroupId batchGroupId, SnapshotState snapshotState) {
    Set<Integer> statefulTasks = new HashSet<Integer>();
    statefulTasks.addAll(snapshotState.getSpoutTasks());
    TopoMasterCtrlEvent resp = null;
    boolean isCommitSuccess = false;
    if (batchGroupId.batchId != TransactionCommon.INIT_BATCH_ID) {
        if (snapshotState.isActive()) {
            resp = new TopoMasterCtrlEvent(EventType.transactionCommit);
            resp.addEventValue(batchGroupId);
            statefulTasks.addAll(snapshotState.getStatefulTasks());
            snapshotState.successBatch(batchGroupId.batchId);
            // Try to persist the topology snapshot state. But if any failure happened, just continue.
            try {
                isCommitSuccess = stateOperator.commitState(topologyName, batchGroupId.groupId, topologySnapshotState);
                if (!isCommitSuccess) {
                    LOG.warn("Failed to commit topology state for batch-{}", batchGroupId);
                }
            } catch (Exception e) {
                LOG.warn("Got exception, when committing topology state for batch-{}, {}", batchGroupId, e);
            }
        }
    } else {
        snapshotState.setActive();
        resp = new TopoMasterCtrlEvent(EventType.transactionStart);
        snapshotState.successBatch(batchGroupId.batchId);
        isCommitSuccess = true;
    }
    if (isCommitSuccess) {
        // Ack the init or commit request from spout and stateful bolt
        LOG.debug("Send ack to spouts-{}, event={}", statefulTasks, resp);
        for (Integer spoutTask : statefulTasks) {
            ((BoltCollector) (outputCollector.getDelegate())).emitDirectCtrl(spoutTask, Common.TOPOLOGY_MASTER_CONTROL_STREAM_ID, null, new Values(resp));
        }
    }
    long nextPendingSuccessBatch = snapshotState.getPendingSuccessBatch();
    if (nextPendingSuccessBatch != -1) {
        LOG.info("Try to commit a pending successful batch-{}", nextPendingSuccessBatch);
        finishSnapshotStateCommit(new BatchGroupId(batchGroupId.groupId, nextPendingSuccessBatch), snapshotState);
    }
}
Also used : TopoMasterCtrlEvent(com.alibaba.jstorm.task.master.ctrlevent.TopoMasterCtrlEvent) BatchGroupId(com.alibaba.jstorm.transactional.BatchGroupId) Values(backtype.storm.tuple.Values) InvalidTopologyException(backtype.storm.generated.InvalidTopologyException) BoltCollector(com.alibaba.jstorm.task.execute.BoltCollector) HashSet(java.util.HashSet)

Aggregations

BatchGroupId (com.alibaba.jstorm.transactional.BatchGroupId)18 Values (backtype.storm.tuple.Values)6 ArrayList (java.util.ArrayList)5 TopoMasterCtrlEvent (com.alibaba.jstorm.task.master.ctrlevent.TopoMasterCtrlEvent)4 BatchSnapshot (com.alibaba.jstorm.transactional.BatchSnapshot)3 TransactionState (com.alibaba.jstorm.transactional.state.TransactionState)3 InvalidTopologyException (backtype.storm.generated.InvalidTopologyException)2 ICollectorCallback (backtype.storm.task.ICollectorCallback)2 BoltCollector (com.alibaba.jstorm.task.execute.BoltCollector)2 Tuple (backtype.storm.tuple.Tuple)1 CountValue (com.alibaba.jstorm.transactional.bolt.TransactionBolt.CountValue)1 BatchInfo (com.alibaba.jstorm.transactional.spout.TransactionSpoutOutputCollector.BatchInfo)1 Pair (com.alibaba.jstorm.utils.Pair)1 HashSet (java.util.HashSet)1