HBASE-23892 SecureTestCluster should allow its subclasses to pass their Class referen...
[hbase.git] / hbase-common / src / main / java / org / apache / hadoop / hbase / trace / SpanReceiverHost.java
blobb967db7f27dcf698a8c291dfef1f63493a73b082
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.trace;
20 import java.io.IOException;
21 import java.util.Collection;
22 import java.util.HashSet;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.htrace.core.SpanReceiver;
26 import org.apache.yetus.audience.InterfaceAudience;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 /**
31 * This class provides functions for reading the names of SpanReceivers from
32 * hbase-site.xml, adding those SpanReceivers to the Tracer, and closing those
33 * SpanReceivers when appropriate.
35 @InterfaceAudience.Private
36 public class SpanReceiverHost {
37 public static final String SPAN_RECEIVERS_CONF_KEY = "hbase.trace.spanreceiver.classes";
38 private static final Logger LOG = LoggerFactory.getLogger(SpanReceiverHost.class);
39 private Collection<SpanReceiver> receivers;
40 private Configuration conf;
41 private boolean closed = false;
43 private enum SingletonHolder {
44 INSTANCE;
45 final transient Object lock = new Object();
46 transient SpanReceiverHost host = null;
49 public static SpanReceiverHost getInstance(Configuration conf) {
50 synchronized (SingletonHolder.INSTANCE.lock) {
51 if (SingletonHolder.INSTANCE.host != null) {
52 return SingletonHolder.INSTANCE.host;
55 SpanReceiverHost host = new SpanReceiverHost(conf);
56 host.loadSpanReceivers();
57 SingletonHolder.INSTANCE.host = host;
58 return SingletonHolder.INSTANCE.host;
63 public static Configuration getConfiguration(){
64 synchronized (SingletonHolder.INSTANCE.lock) {
65 if (SingletonHolder.INSTANCE.host == null || SingletonHolder.INSTANCE.host.conf == null) {
66 return null;
69 return SingletonHolder.INSTANCE.host.conf;
73 SpanReceiverHost(Configuration conf) {
74 receivers = new HashSet<>();
75 this.conf = conf;
78 /**
79 * Reads the names of classes specified in the {@code hbase.trace.spanreceiver.classes} property
80 * and instantiates and registers them with the Tracer.
82 public void loadSpanReceivers() {
83 String[] receiverNames = conf.getStrings(SPAN_RECEIVERS_CONF_KEY);
84 if (receiverNames == null || receiverNames.length == 0) {
85 return;
88 SpanReceiver.Builder builder = new SpanReceiver.Builder(new HBaseHTraceConfiguration(conf));
89 for (String className : receiverNames) {
90 className = className.trim();
92 SpanReceiver receiver = builder.className(className).build();
93 if (receiver != null) {
94 receivers.add(receiver);
95 LOG.info("SpanReceiver {} was loaded successfully.", className);
98 for (SpanReceiver rcvr : receivers) {
99 TraceUtil.addReceiver(rcvr);
104 * Calls close() on all SpanReceivers created by this SpanReceiverHost.
106 public synchronized void closeReceivers() {
107 if (closed) {
108 return;
111 closed = true;
112 for (SpanReceiver rcvr : receivers) {
113 try {
114 rcvr.close();
115 } catch (IOException e) {
116 LOG.warn("Unable to close SpanReceiver correctly: " + e.getMessage(), e);