1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.trace;
19
20 import java.io.IOException;
21 import java.util.Collection;
22 import java.util.HashSet;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.util.ReflectionUtils;
28 import org.cloudera.htrace.SpanReceiver;
29 import org.cloudera.htrace.Trace;
30
31
32
33
34
35
36 public class SpanReceiverHost {
37 public static final String SPAN_RECEIVERS_CONF_KEY = "hbase.trace.spanreceiver.classes";
38 private static final Log LOG = LogFactory.getLog(SpanReceiverHost.class);
39 private Collection<SpanReceiver> receivers;
40 private Configuration conf;
41
42 public SpanReceiverHost(Configuration conf) {
43 receivers = new HashSet<SpanReceiver>();
44 this.conf = conf;
45 }
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public void loadSpanReceivers() {
60 Class<?> implClass = null;
61 String[] receiverNames = conf.getStrings(SPAN_RECEIVERS_CONF_KEY);
62 if (receiverNames == null || receiverNames.length == 0) {
63 return;
64 }
65 for (String className : receiverNames) {
66 className = className.trim();
67
68 try {
69 implClass = Class.forName(className);
70 receivers.add(loadInstance(implClass));
71 LOG.info("SpanReceiver " + className + " was loaded successfully.");
72 } catch (ClassNotFoundException e) {
73 LOG.warn("Class " + className + " cannot be found. " + e.getMessage());
74 } catch (IOException e) {
75 LOG.warn("Load SpanReceiver " + className + " failed. "
76 + e.getMessage());
77 }
78 }
79 for (SpanReceiver rcvr : receivers) {
80 Trace.addReceiver(rcvr);
81 }
82 }
83
84 private SpanReceiver loadInstance(Class<?> implClass)
85 throws IOException {
86 SpanReceiver impl;
87 try {
88 Object o = ReflectionUtils.newInstance(implClass, conf);
89 impl = (SpanReceiver)o;
90 } catch (SecurityException e) {
91 throw new IOException(e);
92 } catch (IllegalArgumentException e) {
93 throw new IOException(e);
94 } catch (RuntimeException e) {
95 throw new IOException(e);
96 }
97
98 return impl;
99 }
100
101
102
103
104 public void closeReceivers() {
105 for (SpanReceiver rcvr : receivers) {
106 try {
107 rcvr.close();
108 } catch (IOException e) {
109 LOG.warn("Unable to close SpanReceiver correctly: " + e.getMessage(), e);
110 }
111 }
112 }
113 }