|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.network.yarn; |
| 19 | + |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import com.codahale.metrics.*; |
| 23 | +import org.apache.hadoop.metrics2.MetricsCollector; |
| 24 | +import org.apache.hadoop.metrics2.MetricsInfo; |
| 25 | +import org.apache.hadoop.metrics2.MetricsRecordBuilder; |
| 26 | +import org.apache.hadoop.metrics2.MetricsSource; |
| 27 | + |
| 28 | +/** |
| 29 | + * Forward {@link org.apache.spark.network.shuffle.ExternalShuffleBlockHandler.ShuffleMetrics} |
| 30 | + * to hadoop metrics system. |
| 31 | + * NodeManager by default exposes JMX endpoint where can be collected. |
| 32 | + */ |
| 33 | +class YarnShuffleServiceMetrics implements MetricsSource { |
| 34 | + |
| 35 | + private final MetricSet metricSet; |
| 36 | + |
| 37 | + YarnShuffleServiceMetrics(MetricSet metricSet) { |
| 38 | + this.metricSet = metricSet; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Get metrics from the source |
| 43 | + * |
| 44 | + * @param collector to contain the resulting metrics snapshot |
| 45 | + * @param all if true, return all metrics even if unchanged. |
| 46 | + */ |
| 47 | + @Override |
| 48 | + public void getMetrics(MetricsCollector collector, boolean all) { |
| 49 | + MetricsRecordBuilder metricsRecordBuilder = collector.addRecord("sparkShuffleService"); |
| 50 | + |
| 51 | + for (Map.Entry<String, Metric> entry : metricSet.getMetrics().entrySet()) { |
| 52 | + collectMetric(metricsRecordBuilder, entry.getKey(), entry.getValue()); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * The metric types used in |
| 58 | + * {@link org.apache.spark.network.shuffle.ExternalShuffleBlockHandler.ShuffleMetrics}. |
| 59 | + * Visible for testing. |
| 60 | + */ |
| 61 | + public static void collectMetric( |
| 62 | + MetricsRecordBuilder metricsRecordBuilder, String name, Metric metric) { |
| 63 | + |
| 64 | + if (metric instanceof Timer) { |
| 65 | + Timer t = (Timer) metric; |
| 66 | + metricsRecordBuilder |
| 67 | + .addCounter(new ShuffleServiceMetricsInfo(name + "_count", "Count of timer " + name), |
| 68 | + t.getCount()) |
| 69 | + .addGauge( |
| 70 | + new ShuffleServiceMetricsInfo(name + "_rate15", "15 minute rate of timer " + name), |
| 71 | + t.getFifteenMinuteRate()) |
| 72 | + .addGauge( |
| 73 | + new ShuffleServiceMetricsInfo(name + "_rate5", "5 minute rate of timer " + name), |
| 74 | + t.getFiveMinuteRate()) |
| 75 | + .addGauge( |
| 76 | + new ShuffleServiceMetricsInfo(name + "_rate1", "1 minute rate of timer " + name), |
| 77 | + t.getOneMinuteRate()) |
| 78 | + .addGauge(new ShuffleServiceMetricsInfo(name + "_rateMean", "Mean rate of timer " + name), |
| 79 | + t.getMeanRate()); |
| 80 | + } else if (metric instanceof Meter) { |
| 81 | + Meter m = (Meter) metric; |
| 82 | + metricsRecordBuilder |
| 83 | + .addCounter(new ShuffleServiceMetricsInfo(name + "_count", "Count of meter " + name), |
| 84 | + m.getCount()) |
| 85 | + .addGauge( |
| 86 | + new ShuffleServiceMetricsInfo(name + "_rate15", "15 minute rate of meter " + name), |
| 87 | + m.getFifteenMinuteRate()) |
| 88 | + .addGauge( |
| 89 | + new ShuffleServiceMetricsInfo(name + "_rate5", "5 minute rate of meter " + name), |
| 90 | + m.getFiveMinuteRate()) |
| 91 | + .addGauge( |
| 92 | + new ShuffleServiceMetricsInfo(name + "_rate1", "1 minute rate of meter " + name), |
| 93 | + m.getOneMinuteRate()) |
| 94 | + .addGauge(new ShuffleServiceMetricsInfo(name + "_rateMean", "Mean rate of meter " + name), |
| 95 | + m.getMeanRate()); |
| 96 | + } else if (metric instanceof Gauge) { |
| 97 | + final Object gaugeValue = ((Gauge) metric).getValue(); |
| 98 | + if (gaugeValue instanceof Integer) { |
| 99 | + metricsRecordBuilder.addGauge(getShuffleServiceMetricsInfo(name), (Integer) gaugeValue); |
| 100 | + } else if (gaugeValue instanceof Long) { |
| 101 | + metricsRecordBuilder.addGauge(getShuffleServiceMetricsInfo(name), (Long) gaugeValue); |
| 102 | + } else if (gaugeValue instanceof Float) { |
| 103 | + metricsRecordBuilder.addGauge(getShuffleServiceMetricsInfo(name), (Float) gaugeValue); |
| 104 | + } else if (gaugeValue instanceof Double) { |
| 105 | + metricsRecordBuilder.addGauge(getShuffleServiceMetricsInfo(name), (Double) gaugeValue); |
| 106 | + } else { |
| 107 | + throw new IllegalStateException( |
| 108 | + "Not supported class type of metric[" + name + "] for value " + gaugeValue); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static MetricsInfo getShuffleServiceMetricsInfo(String name) { |
| 114 | + return new ShuffleServiceMetricsInfo(name, "Value of gauge " + name); |
| 115 | + } |
| 116 | + |
| 117 | + private static class ShuffleServiceMetricsInfo implements MetricsInfo { |
| 118 | + |
| 119 | + private final String name; |
| 120 | + private final String description; |
| 121 | + |
| 122 | + ShuffleServiceMetricsInfo(String name, String description) { |
| 123 | + this.name = name; |
| 124 | + this.description = description; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public String name() { |
| 129 | + return name; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public String description() { |
| 134 | + return description; |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments