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();
}
}
}
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);
}
}
}
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 });
}
}
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;
}
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());
}
}
}
}
Aggregations