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 MetricsRate || metric instanceof MetricsString) {
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 MetricsRate) {
99 attributes.add( new MBeanAttributeInfo(metric.getName(),
100 "java.lang.Float", metric.getDescription(), true, false, false) );
101 extendedAttributes.put(metric.getName(), metric);
102 } else if (metric instanceof MetricsString) {
103 attributes.add( new MBeanAttributeInfo(metric.getName(),
104 "java.lang.String", metric.getDescription(), true, false, false) );
105 extendedAttributes.put(metric.getName(), metric);
106 LOG.info("MetricsString added: " + metric.getName());
107 }
108
109 }
110
111 LOG.info("new MBeanInfo");
112 this.extendedInfo = new MBeanInfo( this.getClass().getName(),
113 this.description, attributes.toArray( new MBeanAttributeInfo[0] ),
114 parentInfo.getConstructors(), parentInfo.getOperations(),
115 parentInfo.getNotifications() );
116 }
117
118 private void checkAndUpdateAttributes() {
119 if (this.registryLength != this.registry.getMetricsList().size())
120 this.init();
121 }
122
123 @Override
124 public Object getAttribute( String name )
125 throws AttributeNotFoundException, MBeanException,
126 ReflectionException {
127
128 if (name == null) {
129 throw new IllegalArgumentException("Attribute name is NULL");
130 }
131
132
133
134
135
136
137
138
139 try {
140 return super.getAttribute(name);
141 } catch (AttributeNotFoundException ex) {
142
143 checkAndUpdateAttributes();
144
145 MetricsBase metric = this.extendedAttributes.get(name);
146 if (metric != null) {
147 if (metric instanceof MetricsRate) {
148 return ((MetricsRate) metric).getPreviousIntervalValue();
149 } else if (metric instanceof MetricsString) {
150 return ((MetricsString)metric).getValue();
151 } else {
152 LOG.warn( String.format("unknown metrics type %s for attribute %s",
153 metric.getClass().getName(), name) );
154 }
155 }
156 }
157
158 throw new AttributeNotFoundException();
159 }
160
161 @Override
162 public MBeanInfo getMBeanInfo() {
163 return this.extendedInfo;
164 }
165
166 }