Search in sources :

Example 1 with MetricInfo

use of backtype.storm.generated.MetricInfo in project jstorm by alibaba.

the class JStormUtils method getMetrics.

/**
     * Get Topology Metrics
     *
     * @param topologyName topology name
     * @param metricType,  please refer to MetaType, default to MetaType.TASK.getT()
     * @param window,      please refer to AsmWindow, default to AsmWindow.M1_WINDOW
     * @return
     */
public static Map<String, Double> getMetrics(Map conf, String topologyName, MetaType metricType, Integer window) {
    NimbusClientWrapper nimbusClient = null;
    Iface client = null;
    Map<String, Double> summary = new HashMap<>();
    try {
        nimbusClient = new NimbusClientWrapper();
        nimbusClient.init(conf);
        client = nimbusClient.getClient();
        String topologyId = client.getTopologyId(topologyName);
        if (metricType == null) {
            metricType = MetaType.TASK;
        }
        List<MetricInfo> allTaskMetrics = client.getMetrics(topologyId, metricType.getT());
        if (allTaskMetrics == null) {
            throw new RuntimeException("Failed to get metrics");
        }
        if (window == null || !AsmWindow.TIME_WINDOWS.contains(window)) {
            window = AsmWindow.M1_WINDOW;
        }
        for (MetricInfo taskMetrics : allTaskMetrics) {
            Map<String, Map<Integer, MetricSnapshot>> metrics = taskMetrics.get_metrics();
            if (metrics == null) {
                System.out.println("Failed to get task metrics");
                continue;
            }
            for (Entry<String, Map<Integer, MetricSnapshot>> entry : metrics.entrySet()) {
                String key = entry.getKey();
                MetricSnapshot metric = entry.getValue().get(window);
                if (metric == null) {
                    throw new RuntimeException("Failed to get one minute metrics of " + key);
                }
                if (metric.get_metricType() == MetricType.COUNTER.getT()) {
                    summary.put(key, (double) metric.get_longValue());
                } else if (metric.get_metricType() == MetricType.GAUGE.getT()) {
                    summary.put(key, metric.get_doubleValue());
                } else {
                    summary.put(key, metric.get_mean());
                }
            }
        }
        return summary;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (client != null) {
            nimbusClient.cleanup();
        }
    }
}
Also used : NimbusClientWrapper(backtype.storm.utils.NimbusClientWrapper) HashMap(java.util.HashMap) ExecuteException(org.apache.commons.exec.ExecuteException) TException(org.apache.thrift.TException) IOException(java.io.IOException) Iface(backtype.storm.generated.Nimbus.Iface) MetricInfo(backtype.storm.generated.MetricInfo) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) MetricSnapshot(backtype.storm.generated.MetricSnapshot)

Example 2 with MetricInfo

use of backtype.storm.generated.MetricInfo in project jstorm by alibaba.

the class JStormMetricCache method unregisterWorker.

public void unregisterWorker(String topologyId, String host, int port) {
    String prefix = MetricUtils.workerMetricPrefix(topologyId, host, port);
    synchronized (lock) {
        //remove dead worker meta info in METRIC_META_PREFIX
        Map<String, Long> nodes = (Map<String, Long>) cache.get(METRIC_META_PREFIX + topologyId);
        if (nodes != null) {
            Iterator<String> keyIterator = nodes.keySet().iterator();
            while (keyIterator.hasNext()) {
                String metricName = keyIterator.next();
                // remove metric type
                metricName = metricName.charAt(0) + metricName.substring(2, metricName.length());
                if (metricName.startsWith(prefix)) {
                    keyIterator.remove();
                }
            }
            cache.put(METRIC_META_PREFIX + topologyId, nodes);
        }
        //remove dead worker in METRIC_DATA_30M_WORKER
        Object data = cache.get(METRIC_DATA_30M_WORKER + topologyId);
        if (data != null) {
            Object[] parts = (Object[]) data;
            MetricInfo old = (MetricInfo) parts[1];
            Iterator<String> oldKeys = old.get_metrics().keySet().iterator();
            while (oldKeys.hasNext()) {
                String metricName = oldKeys.next();
                metricName = metricName.charAt(0) + metricName.substring(2, metricName.length());
                if (metricName.startsWith(prefix)) {
                    oldKeys.remove();
                    LOG.info("remove dead worker metric : {}", metricName);
                }
            }
            cache.put(METRIC_DATA_30M_WORKER + topologyId, data);
        }
    }
}
Also used : MetricInfo(backtype.storm.generated.MetricInfo)

Example 3 with MetricInfo

use of backtype.storm.generated.MetricInfo in project jstorm by alibaba.

the class JStormMetricCache method tryCombineMetricInfo.

private void tryCombineMetricInfo(String key, MetricInfo incoming, MetaType metaType, long ts) {
    Object data = cache.get(key);
    if (data != null) {
        try {
            Object[] parts = (Object[]) data;
            MetricInfo old = (MetricInfo) parts[1];
            LOG.info("combine {} metrics, old:{}, new:{}", metaType, old.get_metrics_size(), incoming.get_metrics_size());
            old.get_metrics().putAll(incoming.get_metrics());
            // remove dead worker
            cache.put(key, new Object[] { ts, old });
        } catch (Exception ignored) {
            cache.remove(key);
            cache.put(key, new Object[] { ts, incoming });
        }
    } else {
        cache.put(key, new Object[] { ts, incoming });
    }
}
Also used : MetricInfo(backtype.storm.generated.MetricInfo)

Example 4 with MetricInfo

use of backtype.storm.generated.MetricInfo in project jstorm by alibaba.

the class MetricUtils method mkMetricInfo.

public static MetricInfo mkMetricInfo() {
    MetricInfo ret = new MetricInfo();
    ret.set_metrics(new HashMap<String, Map<Integer, MetricSnapshot>>());
    return ret;
}
Also used : MetricInfo(backtype.storm.generated.MetricInfo)

Example 5 with MetricInfo

use of backtype.storm.generated.MetricInfo in project jstorm by alibaba.

the class TopologyMetricContext method mergeCounters.

public void mergeCounters(TopologyMetric tpMetric, MetaType metaType, String meta, Map<Integer, MetricSnapshot> data) {
    MetricInfo metricInfo = getMetricInfoByType(tpMetric, metaType);
    Map<Integer, MetricSnapshot> existing = metricInfo.get_metrics().get(meta);
    if (existing == null) {
        metricInfo.put_to_metrics(meta, data);
    } else {
        for (Map.Entry<Integer, MetricSnapshot> dataEntry : data.entrySet()) {
            Integer win = dataEntry.getKey();
            MetricSnapshot snapshot = dataEntry.getValue();
            MetricSnapshot old = existing.get(win);
            if (old == null) {
                existing.put(win, snapshot);
            } else {
                old.set_ts(snapshot.get_ts());
                old.set_longValue(old.get_longValue() + snapshot.get_longValue());
            }
        }
    }
}
Also used : MetricInfo(backtype.storm.generated.MetricInfo) MetricSnapshot(backtype.storm.generated.MetricSnapshot) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Aggregations

MetricInfo (backtype.storm.generated.MetricInfo)29 MetricSnapshot (backtype.storm.generated.MetricSnapshot)11 HashMap (java.util.HashMap)10 Map (java.util.Map)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 TreeMap (java.util.TreeMap)7 TopologyMetric (backtype.storm.generated.TopologyMetric)6 TimeCacheMap (com.alibaba.jstorm.utils.TimeCacheMap)5 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 WorkerSummary (backtype.storm.generated.WorkerSummary)3 IOException (java.io.IOException)3 TException (org.apache.thrift.TException)3 AlreadyAliveException (backtype.storm.generated.AlreadyAliveException)2 InvalidTopologyException (backtype.storm.generated.InvalidTopologyException)2 KeyAlreadyExistsException (backtype.storm.generated.KeyAlreadyExistsException)2 KeyNotFoundException (backtype.storm.generated.KeyNotFoundException)2 NotAliveException (backtype.storm.generated.NotAliveException)2 SupervisorWorkers (backtype.storm.generated.SupervisorWorkers)2 TopologyAssignException (backtype.storm.generated.TopologyAssignException)2 NimbusClient (backtype.storm.utils.NimbusClient)2