001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.trace;
019
020import java.io.IOException;
021import java.util.Collection;
022import java.util.HashSet;
023
024import org.apache.hadoop.conf.Configuration;
025import org.apache.htrace.core.SpanReceiver;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * This class provides functions for reading the names of SpanReceivers from
032 * hbase-site.xml, adding those SpanReceivers to the Tracer, and closing those
033 * SpanReceivers when appropriate.
034 */
035@InterfaceAudience.Private
036public class SpanReceiverHost {
037  public static final String SPAN_RECEIVERS_CONF_KEY = "hbase.trace.spanreceiver.classes";
038  private static final Logger LOG = LoggerFactory.getLogger(SpanReceiverHost.class);
039  private Collection<SpanReceiver> receivers;
040  private Configuration conf;
041  private boolean closed = false;
042
043  private enum SingletonHolder {
044    INSTANCE;
045    final transient Object lock = new Object();
046    transient SpanReceiverHost host = null;
047  }
048
049  public static SpanReceiverHost getInstance(Configuration conf) {
050    synchronized (SingletonHolder.INSTANCE.lock) {
051      if (SingletonHolder.INSTANCE.host != null) {
052        return SingletonHolder.INSTANCE.host;
053      }
054
055      SpanReceiverHost host = new SpanReceiverHost(conf);
056      host.loadSpanReceivers();
057      SingletonHolder.INSTANCE.host = host;
058      return SingletonHolder.INSTANCE.host;
059    }
060
061  }
062
063  public static Configuration getConfiguration(){
064    synchronized (SingletonHolder.INSTANCE.lock) {
065      if (SingletonHolder.INSTANCE.host == null || SingletonHolder.INSTANCE.host.conf == null) {
066        return null;
067      }
068
069      return SingletonHolder.INSTANCE.host.conf;
070    }
071  }
072
073  SpanReceiverHost(Configuration conf) {
074    receivers = new HashSet<>();
075    this.conf = conf;
076  }
077
078  /**
079   * Reads the names of classes specified in the {@code hbase.trace.spanreceiver.classes} property
080   * and instantiates and registers them with the Tracer.
081   */
082  public void loadSpanReceivers() {
083    String[] receiverNames = conf.getStrings(SPAN_RECEIVERS_CONF_KEY);
084    if (receiverNames == null || receiverNames.length == 0) {
085      return;
086    }
087
088    SpanReceiver.Builder builder = new SpanReceiver.Builder(new HBaseHTraceConfiguration(conf));
089    for (String className : receiverNames) {
090      className = className.trim();
091
092      SpanReceiver receiver = builder.className(className).build();
093      if (receiver != null) {
094        receivers.add(receiver);
095        LOG.info("SpanReceiver {} was loaded successfully.", className);
096      }
097    }
098    for (SpanReceiver rcvr : receivers) {
099      TraceUtil.addReceiver(rcvr);
100    }
101  }
102
103  /**
104   * Calls close() on all SpanReceivers created by this SpanReceiverHost.
105   */
106  public synchronized void closeReceivers() {
107    if (closed) {
108      return;
109    }
110
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);
117      }
118    }
119  }
120}