acromusashi.stream.topology.state.StormSummaryExtractor.java Source code

Java tutorial

Introduction

Here is the source code for acromusashi.stream.topology.state.StormSummaryExtractor.java

Source

/**
* Copyright (c) Acroquest Technology Co, Ltd. All Rights Reserved.
* Please read the associated COPYRIGHTS file for more details.
*
* THE SOFTWARE IS PROVIDED BY Acroquest Technolog Co., Ltd.,
* WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDER BE LIABLE FOR ANY
* CLAIM, DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
package acromusashi.stream.topology.state;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.storm.generated.ClusterSummary;
import org.apache.storm.generated.ExecutorSpecificStats;
import org.apache.storm.generated.ExecutorStats;
import org.apache.storm.generated.ExecutorSummary;
import org.apache.storm.generated.SpoutStats;
import org.apache.storm.generated.TopologyInfo;
import org.apache.storm.generated.TopologySummary;

/**
 * StormNimbus????Storm???
 *
 * @author kimura
 */
public class StormSummaryExtractor {
    /** ???? */
    private static final String ALLTIME_KEY = ":all-time";

    /** DefaultStream???? */
    private static final String DEFAULT_KEY = "default";

    /**
     * ???
     */
    private StormSummaryExtractor() {
        // Do nothing.
    }

    /**
     * ?????????ID?
     *
     * @param clusterSummary 
     * @param topologyName ???
     * @return ??????ID?????null?
     */
    public static String extractStormTopologyId(ClusterSummary clusterSummary, String topologyName) {

        List<TopologySummary> topologyList = clusterSummary.get_topologies();

        for (TopologySummary targetTopology : topologyList) {
            // ?????????ID?
            if (StringUtils.equals(topologyName, targetTopology.get_name()) == true) {
                return targetTopology.get_id();
            }
        }

        return null;
    }

    /**
     * ??????
     *
     * @param topologyInfo ?
     * @return ?
     */
    public static TopologyExecutionCount convertToTotalCount(TopologyInfo topologyInfo) {
        List<ExecutorSummary> executors = topologyInfo.get_executors();
        TopologyExecutionCount totalCount = new TopologyExecutionCount();

        for (ExecutorSummary summary : executors) {
            ExecutorStats stats = summary.get_stats();

            Map<String, Map<String, Long>> emitStats = stats.get_emitted();
            long emitted = extractAllDefaultValue(emitStats);
            Map<String, Map<String, Long>> transferStats = stats.get_transferred();
            long transferred = extractAllDefaultValue(transferStats);

            long acked = 0;
            long failed = 0;

            ExecutorSpecificStats specificStats = stats.get_specific();

            if (specificStats.is_set_spout() == true) {
                SpoutStats spoutStats = specificStats.get_spout();
                Map<String, Map<String, Long>> ackStats = spoutStats.get_acked();
                acked = extractAllDefaultValue(ackStats);
                Map<String, Map<String, Long>> failStats = spoutStats.get_failed();
                failed = extractAllDefaultValue(failStats);
            }

            // ?????
            totalCount.setEmitted(totalCount.getEmitted() + emitted);
            totalCount.setTransferred(totalCount.getTransferred() + transferred);
            totalCount.setAcked(totalCount.getAcked() + acked);
            totalCount.setFailed(totalCount.getFailed() + failed);
        }

        return totalCount;
    }

    /**
     * ????????
     *
     * @param targetStats ??
     * @return ?????????0
     */
    public static long extractAllDefaultValue(Map<String, Map<String, Long>> targetStats) {

        Map<String, Long> allTimeMap = targetStats.get(ALLTIME_KEY);
        if (allTimeMap == null) {
            return 0L;
        }

        Long defaultValue = allTimeMap.get(DEFAULT_KEY);
        if (defaultValue == null) {
            return 0L;
        }

        return defaultValue;
    }
}