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