Search in sources :

Example 1 with ConcurrentHashSet

use of org.apache.storm.shade.org.eclipse.jetty.util.ConcurrentHashSet in project open-kilda by telstra.

the class TransactionBolt method execute.

@Override
public void execute(Tuple tuple) {
    if (CtrlAction.boltHandlerEntrance(this, tuple))
        return;
    logger.trace("States before: {}", transactions);
    ComponentType componentId = ComponentType.valueOf(tuple.getSourceComponent());
    StreamType streamId = StreamType.valueOf(tuple.getSourceStreamId());
    Long transactionId = (Long) tuple.getValueByField(Utils.TRANSACTION_ID);
    String switchId = (String) tuple.getValueByField(FlowTopology.SWITCH_ID_FIELD);
    String flowId = (String) tuple.getValueByField(Utils.FLOW_ID);
    Object message = tuple.getValueByField(FlowTopology.MESSAGE_FIELD);
    Map<String, Set<Long>> flowTransactions;
    Set<Long> flowTransactionIds;
    Values values = null;
    try {
        logger.debug("Request tuple={}", tuple);
        switch(componentId) {
            case TOPOLOGY_ENGINE_BOLT:
                logger.info("Transaction from TopologyEngine: switch-id={}, {}={}, {}={}", switchId, Utils.FLOW_ID, flowId, Utils.TRANSACTION_ID, transactionId);
                flowTransactions = transactions.get(switchId);
                if (flowTransactions == null) {
                    flowTransactions = new ConcurrentHashMap<>();
                    transactions.put(switchId, flowTransactions);
                }
                flowTransactionIds = flowTransactions.get(flowId);
                if (flowTransactionIds == null) {
                    flowTransactionIds = new ConcurrentHashSet<>();
                    flowTransactions.put(flowId, flowTransactionIds);
                }
                if (!flowTransactionIds.add(transactionId)) {
                    throw new RuntimeException(String.format("Transaction adding failure: id %d already exists", transactionId));
                }
                logger.info("Set status {}: switch-id={}, {}={}, {}={}", FlowState.IN_PROGRESS, switchId, Utils.FLOW_ID, flowId, Utils.TRANSACTION_ID, transactionId);
                values = new Values(flowId, FlowState.IN_PROGRESS);
                outputCollector.emit(StreamType.STATUS.toString(), tuple, values);
                values = new Values(message);
                outputCollector.emit(streamId.toString(), tuple, values);
                break;
            case SPEAKER_BOLT:
                logger.info("Transaction from Speaker: switch-id={}, {}={}, {}={}", switchId, Utils.FLOW_ID, flowId, Utils.TRANSACTION_ID, transactionId);
                flowTransactions = transactions.get(switchId);
                if (flowTransactions != null) {
                    flowTransactionIds = flowTransactions.get(flowId);
                    if (flowTransactionIds != null) {
                        if (flowTransactionIds.remove(transactionId)) {
                            if (flowTransactionIds.isEmpty()) {
                                // 
                                // All transactions have been removed .. the Flow
                                // can now be considered "UP"
                                // 
                                logger.info("Set status {}: switch-id={}, {}={}, {}={}", FlowState.UP, switchId, Utils.FLOW_ID, flowId, Utils.TRANSACTION_ID, transactionId);
                                values = new Values(flowId, FlowState.UP);
                                outputCollector.emit(StreamType.STATUS.toString(), tuple, values);
                                flowTransactions.remove(flowId);
                            } else {
                                logger.debug("Transaction {} not empty yet, count = {}", transactionId, flowTransactionIds.size());
                            }
                        } else {
                            logger.warn("Transaction removing: transaction id not found");
                        }
                    } else {
                        logger.warn("Transaction removing failure: flow id not found");
                    }
                    if (flowTransactions.isEmpty()) {
                        transactions.delete(switchId);
                    }
                } else {
                    logger.warn("Transaction removing failure: switch id not found");
                }
                break;
            default:
                logger.debug("Skip undefined message: message={}", tuple);
                break;
        }
    } catch (RuntimeException exception) {
        logger.error("Set status {}: switch-id={}, {}={}, {}={}", FlowState.DOWN, switchId, Utils.FLOW_ID, flowId, Utils.TRANSACTION_ID, transactionId, exception);
        values = new Values(flowId, FlowState.DOWN);
        outputCollector.emit(StreamType.STATUS.toString(), tuple, values);
    } finally {
        logger.debug("Transaction message ack: component={}, stream={}, tuple={}, values={}", tuple.getSourceComponent(), tuple.getSourceStreamId(), tuple, values);
        outputCollector.ack(tuple);
    }
    logger.trace("States after: {}", transactions);
}
Also used : StreamType(org.openkilda.wfm.topology.flow.StreamType) ComponentType(org.openkilda.wfm.topology.flow.ComponentType) Set(java.util.Set) ConcurrentHashSet(org.apache.storm.shade.org.eclipse.jetty.util.ConcurrentHashSet) Values(org.apache.storm.tuple.Values)

Aggregations

Set (java.util.Set)1 ConcurrentHashSet (org.apache.storm.shade.org.eclipse.jetty.util.ConcurrentHashSet)1 Values (org.apache.storm.tuple.Values)1 ComponentType (org.openkilda.wfm.topology.flow.ComponentType)1 StreamType (org.openkilda.wfm.topology.flow.StreamType)1