1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | package org.apache.hadoop.metrics.spi; |
---|
19 | |
---|
20 | import java.io.IOException; |
---|
21 | import java.lang.reflect.InvocationHandler; |
---|
22 | import java.lang.reflect.Method; |
---|
23 | import java.lang.reflect.Proxy; |
---|
24 | import java.util.ArrayList; |
---|
25 | |
---|
26 | import org.apache.commons.logging.Log; |
---|
27 | import org.apache.commons.logging.LogFactory; |
---|
28 | |
---|
29 | import org.apache.hadoop.metrics.ContextFactory; |
---|
30 | import org.apache.hadoop.metrics.MetricsContext; |
---|
31 | import org.apache.hadoop.metrics.MetricsException; |
---|
32 | import org.apache.hadoop.metrics.MetricsRecord; |
---|
33 | import org.apache.hadoop.metrics.MetricsUtil; |
---|
34 | import org.apache.hadoop.metrics.Updater; |
---|
35 | |
---|
36 | public class CompositeContext extends AbstractMetricsContext { |
---|
37 | |
---|
38 | private static final Log LOG = LogFactory.getLog(CompositeContext.class); |
---|
39 | private static final String ARITY_LABEL = "arity"; |
---|
40 | private static final String SUB_FMT = "%s.sub%d"; |
---|
41 | private final ArrayList<MetricsContext> subctxt = |
---|
42 | new ArrayList<MetricsContext>(); |
---|
43 | |
---|
44 | public CompositeContext() { |
---|
45 | } |
---|
46 | |
---|
47 | public void init(String contextName, ContextFactory factory) { |
---|
48 | super.init(contextName, factory); |
---|
49 | int nKids; |
---|
50 | try { |
---|
51 | String sKids = getAttribute(ARITY_LABEL); |
---|
52 | nKids = Integer.valueOf(sKids); |
---|
53 | } catch (Exception e) { |
---|
54 | LOG.error("Unable to initialize composite metric " + contextName + |
---|
55 | ": could not init arity", e); |
---|
56 | return; |
---|
57 | } |
---|
58 | for (int i = 0; i < nKids; ++i) { |
---|
59 | MetricsContext ctxt = MetricsUtil.getContext( |
---|
60 | String.format(SUB_FMT, contextName, i), contextName); |
---|
61 | if (null != ctxt) { |
---|
62 | subctxt.add(ctxt); |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | @Override |
---|
68 | public MetricsRecord newRecord(String recordName) { |
---|
69 | return (MetricsRecord) Proxy.newProxyInstance( |
---|
70 | MetricsRecord.class.getClassLoader(), |
---|
71 | new Class[] { MetricsRecord.class }, |
---|
72 | new MetricsRecordDelegator(recordName, subctxt)); |
---|
73 | } |
---|
74 | |
---|
75 | @Override |
---|
76 | protected void emitRecord(String contextName, String recordName, |
---|
77 | OutputRecord outRec) throws IOException { |
---|
78 | for (MetricsContext ctxt : subctxt) { |
---|
79 | try { |
---|
80 | ((AbstractMetricsContext)ctxt).emitRecord( |
---|
81 | contextName, recordName, outRec); |
---|
82 | if (contextName == null || recordName == null || outRec == null) { |
---|
83 | throw new IOException(contextName + ":" + recordName + ":" + outRec); |
---|
84 | } |
---|
85 | } catch (IOException e) { |
---|
86 | LOG.warn("emitRecord failed: " + ctxt.getContextName(), e); |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | @Override |
---|
92 | protected void flush() throws IOException { |
---|
93 | for (MetricsContext ctxt : subctxt) { |
---|
94 | try { |
---|
95 | ((AbstractMetricsContext)ctxt).flush(); |
---|
96 | } catch (IOException e) { |
---|
97 | LOG.warn("flush failed: " + ctxt.getContextName(), e); |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | @Override |
---|
103 | public void startMonitoring() throws IOException { |
---|
104 | for (MetricsContext ctxt : subctxt) { |
---|
105 | try { |
---|
106 | ctxt.startMonitoring(); |
---|
107 | } catch (IOException e) { |
---|
108 | LOG.warn("startMonitoring failed: " + ctxt.getContextName(), e); |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | @Override |
---|
114 | public void stopMonitoring() { |
---|
115 | for (MetricsContext ctxt : subctxt) { |
---|
116 | ctxt.stopMonitoring(); |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * Return true if all subcontexts are monitoring. |
---|
122 | */ |
---|
123 | @Override |
---|
124 | public boolean isMonitoring() { |
---|
125 | boolean ret = true; |
---|
126 | for (MetricsContext ctxt : subctxt) { |
---|
127 | ret &= ctxt.isMonitoring(); |
---|
128 | } |
---|
129 | return ret; |
---|
130 | } |
---|
131 | |
---|
132 | @Override |
---|
133 | public void close() { |
---|
134 | for (MetricsContext ctxt : subctxt) { |
---|
135 | ctxt.close(); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | @Override |
---|
140 | public void registerUpdater(Updater updater) { |
---|
141 | for (MetricsContext ctxt : subctxt) { |
---|
142 | ctxt.registerUpdater(updater); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | @Override |
---|
147 | public void unregisterUpdater(Updater updater) { |
---|
148 | for (MetricsContext ctxt : subctxt) { |
---|
149 | ctxt.unregisterUpdater(updater); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | private static class MetricsRecordDelegator implements InvocationHandler { |
---|
154 | private static final Method m_getRecordName = initMethod(); |
---|
155 | private static Method initMethod() { |
---|
156 | try { |
---|
157 | return MetricsRecord.class.getMethod("getRecordName", new Class[0]); |
---|
158 | } catch (Exception e) { |
---|
159 | throw new RuntimeException("Internal error", e); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | private final String recordName; |
---|
164 | private final ArrayList<MetricsRecord> subrecs; |
---|
165 | |
---|
166 | MetricsRecordDelegator(String recordName, ArrayList<MetricsContext> ctxts) { |
---|
167 | this.recordName = recordName; |
---|
168 | this.subrecs = new ArrayList<MetricsRecord>(ctxts.size()); |
---|
169 | for (MetricsContext ctxt : ctxts) { |
---|
170 | subrecs.add(ctxt.createRecord(recordName)); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | public Object invoke(Object p, Method m, Object[] args) throws Throwable { |
---|
175 | if (m_getRecordName.equals(m)) { |
---|
176 | return recordName; |
---|
177 | } |
---|
178 | assert Void.TYPE.equals(m.getReturnType()); |
---|
179 | for (MetricsRecord rec : subrecs) { |
---|
180 | m.invoke(rec, args); |
---|
181 | } |
---|
182 | return null; |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | } |
---|