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.hbase.classification.InterfaceAudience;
28 import org.apache.htrace.SpanReceiver;
29 import org.apache.htrace.SpanReceiverBuilder;
30 import org.apache.htrace.Trace;
31
32
33
34
35
36
37 @InterfaceAudience.Private
38 public class SpanReceiverHost {
39 public static final String SPAN_RECEIVERS_CONF_KEY = "hbase.trace.spanreceiver.classes";
40 private static final Log LOG = LogFactory.getLog(SpanReceiverHost.class);
41 private Collection<SpanReceiver> receivers;
42 private Configuration conf;
43 private boolean closed = false;
44
45 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="SE_BAD_FIELD")
46 private enum SingletonHolder {
47 INSTANCE;
48 final Object lock = new Object();
49 SpanReceiverHost host = null;
50 }
51
52 public static SpanReceiverHost getInstance(Configuration conf) {
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<>();
68 this.conf = conf;
69 }
70
71
72
73
74
75 public void loadSpanReceivers() {
76 String[] receiverNames = conf.getStrings(SPAN_RECEIVERS_CONF_KEY);
77 if (receiverNames == null || receiverNames.length == 0) {
78 return;
79 }
80
81 SpanReceiverBuilder builder = new SpanReceiverBuilder(new HBaseHTraceConfiguration(conf));
82 for (String className : receiverNames) {
83 className = className.trim();
84
85 SpanReceiver receiver = builder.spanReceiverClass(className).build();
86 if (receiver != null) {
87 receivers.add(receiver);
88 LOG.info("SpanReceiver " + className + " was loaded successfully.");
89 }
90 }
91 for (SpanReceiver rcvr : receivers) {
92 Trace.addReceiver(rcvr);
93 }
94 }
95
96
97
98
99 public synchronized void closeReceivers() {
100 if (closed) {
101 return;
102 }
103
104 closed = true;
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 }