1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.metrics;
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.management.AttributeNotFoundException;
28 import javax.management.MBeanAttributeInfo;
29 import javax.management.MBeanException;
30 import javax.management.MBeanInfo;
31 import javax.management.ReflectionException;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.hadoop.metrics.util.MetricsBase;
36 import org.apache.hadoop.metrics.util.MetricsDynamicMBeanBase;
37 import org.apache.hadoop.metrics.util.MetricsRegistry;
38
39
40
41
42
43
44
45
46 public class MetricsMBeanBase extends MetricsDynamicMBeanBase {
47
48 private static final Log LOG = LogFactory.getLog("org.apache.hadoop.hbase.metrics");
49
50 protected final MetricsRegistry registry;
51 protected final String description;
52 protected int registryLength;
53
54
55
56 protected Map<String,MetricsBase> extendedAttributes =
57 new HashMap<String,MetricsBase>();
58 protected MBeanInfo extendedInfo;
59
60 protected MetricsMBeanBase( MetricsRegistry mr, String description ) {
61 super(copyMinusHBaseMetrics(mr), description);
62 this.registry = mr;
63 this.description = description;
64 this.init();
65 }
66
67
68
69
70
71 private static MetricsRegistry copyMinusHBaseMetrics(final MetricsRegistry mr) {
72 MetricsRegistry copy = new MetricsRegistry();
73 for (MetricsBase metric : mr.getMetricsList()) {
74 if (metric instanceof org.apache.hadoop.hbase.metrics.MetricsRate) {
75 continue;
76 }
77 copy.add(metric.getName(), metric);
78 }
79 return copy;
80 }
81
82 protected void init() {
83 List<MBeanAttributeInfo> attributes = new ArrayList<MBeanAttributeInfo>();
84 MBeanInfo parentInfo = super.getMBeanInfo();
85 List<String> parentAttributes = new ArrayList<String>();
86 for (MBeanAttributeInfo attr : parentInfo.getAttributes()) {
87 attributes.add(attr);
88 parentAttributes.add(attr.getName());
89 }
90
91 this.registryLength = this.registry.getMetricsList().size();
92
93 for (MetricsBase metric : this.registry.getMetricsList()) {
94 if (metric.getName() == null || parentAttributes.contains(metric.getName()))
95 continue;
96
97
98 if (metric instanceof org.apache.hadoop.hbase.metrics.MetricsRate) {
99 attributes.add( new MBeanAttributeInfo(metric.getName(),
100 "java.lang.Float", metric.getDescription(), true, false, false) );
101 extendedAttributes.put(metric.getName(), metric);
102 }
103
104 }
105
106 this.extendedInfo = new MBeanInfo( this.getClass().getName(),
107 this.description, attributes.toArray( new MBeanAttributeInfo[0] ),
108 parentInfo.getConstructors(), parentInfo.getOperations(),
109 parentInfo.getNotifications() );
110 }
111
112 private void checkAndUpdateAttributes() {
113 if (this.registryLength != this.registry.getMetricsList().size())
114 this.init();
115 }
116
117 @Override
118 public Object getAttribute( String name )
119 throws AttributeNotFoundException, MBeanException,
120 ReflectionException {
121
122 if (name == null) {
123 throw new IllegalArgumentException("Attribute name is NULL");
124 }
125
126
127
128
129
130
131
132
133 try {
134 return super.getAttribute(name);
135 } catch (AttributeNotFoundException ex) {
136
137 checkAndUpdateAttributes();
138
139 MetricsBase metric = this.extendedAttributes.get(name);
140 if (metric != null) {
141 if (metric instanceof MetricsRate) {
142 return ((MetricsRate) metric).getPreviousIntervalValue();
143 } else {
144 LOG.warn( String.format("unknown metrics type %s for attribute %s",
145 metric.getClass().getName(), name) );
146 }
147 }
148 }
149
150 throw new AttributeNotFoundException();
151 }
152
153 @Override
154 public MBeanInfo getMBeanInfo() {
155 return this.extendedInfo;
156 }
157
158 }