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 private boolean closed = false;
42
43 private static enum SingleTonholder {
44 INSTANCE;
45 Object lock = new Object();
46 SpanReceiverHost host = null;
47 }
48
49 public static SpanReceiverHost getInstance(Configuration conf) {
50 if (SingleTonholder.INSTANCE.host != null) {
51 return SingleTonholder.INSTANCE.host;
52 }
53 synchronized (SingleTonholder.INSTANCE.lock) {
54 if (SingleTonholder.INSTANCE.host != null) {
55 return SingleTonholder.INSTANCE.host;
56 }
57
58 SpanReceiverHost host = new SpanReceiverHost(conf);
59 host.loadSpanReceivers();
60 SingleTonholder.INSTANCE.host = host;
61 return SingleTonholder.INSTANCE.host;
62 }
63
64 }
65
66 SpanReceiverHost(Configuration conf) {
67 receivers = new HashSet<SpanReceiver>();
68 this.conf = conf;
69 }
70
71
72
73
74
75
76
77 public void loadSpanReceivers() {
78 Class<?> implClass = null;
79 String[] receiverNames = conf.getStrings(SPAN_RECEIVERS_CONF_KEY);
80 if (receiverNames == null || receiverNames.length == 0) {
81 return;
82 }
83 for (String className : receiverNames) {
84 className = className.trim();
85
86 try {
87 implClass = Class.forName(className);
88 SpanReceiver receiver = loadInstance(implClass);
89 if (receiver != null) {
90 receivers.add(receiver);
91 LOG.info("SpanReceiver " + className + " was loaded successfully.");
92 }
93
94 } catch (ClassNotFoundException e) {
95 LOG.warn("Class " + className + " cannot be found. " + e.getMessage());
96 } catch (IOException e) {
97 LOG.warn("Load SpanReceiver " + className + " failed. "
98 + e.getMessage());
99 }
100 }
101 for (SpanReceiver rcvr : receivers) {
102 Trace.addReceiver(rcvr);
103 }
104 }
105
106 private SpanReceiver loadInstance(Class<?> implClass)
107 throws IOException {
108 SpanReceiver impl = null;
109 try {
110 Object o = implClass.newInstance();
111 impl = (SpanReceiver)o;
112 impl.configure(new HBaseHTraceConfiguration(this.conf));
113 } catch (SecurityException e) {
114 throw new IOException(e);
115 } catch (IllegalArgumentException e) {
116 throw new IOException(e);
117 } catch (RuntimeException e) {
118 throw new IOException(e);
119 } catch (InstantiationException e) {
120 e.printStackTrace();
121 } catch (IllegalAccessException e) {
122 e.printStackTrace();
123 }
124
125 return impl;
126 }
127
128
129
130
131 public synchronized void closeReceivers() {
132 if (closed) return;
133 closed = true;
134 for (SpanReceiver rcvr : receivers) {
135 try {
136 rcvr.close();
137 } catch (IOException e) {
138 LOG.warn("Unable to close SpanReceiver correctly: " + e.getMessage(), e);
139 }
140 }
141 }
142 }