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 static 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   */
083  public void loadSpanReceivers() {
084    String[] receiverNames = conf.getStrings(SPAN_RECEIVERS_CONF_KEY);
085    if (receiverNames == null || receiverNames.length == 0) {
086      return;
087    }
088
089    SpanReceiver.Builder builder = new SpanReceiver.Builder(new HBaseHTraceConfiguration(conf));
090    for (String className : receiverNames) {
091      className = className.trim();
092
093      SpanReceiver receiver = builder.className(className).build();
094      if (receiver != null) {
095        receivers.add(receiver);
096        LOG.info("SpanReceiver " + className + " was loaded successfully.");
097      }
098    }
099    for (SpanReceiver rcvr : receivers) {
100      TraceUtil.addReceiver(rcvr);
101    }
102  }
103
104  /**
105   * Calls close() on all SpanReceivers created by this SpanReceiverHost.
106   */
107  public synchronized void closeReceivers() {
108    if (closed) return;
109    closed = true;
110    for (SpanReceiver rcvr : receivers) {
111      try {
112        rcvr.close();
113      } catch (IOException e) {
114        LOG.warn("Unable to close SpanReceiver correctly: " + e.getMessage(), e);
115      }
116    }
117  }
118}