View Javadoc

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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
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   * This class provides functions for reading the names of SpanReceivers from
34   * hbase-site.xml, adding those SpanReceivers to the Tracer, and closing those
35   * SpanReceivers when appropriate.
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 static enum SingletonHolder {
47      INSTANCE;
48      Object lock = new Object();
49      SpanReceiverHost host = null; // FindBugs: SE_BAD_FIELD
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<SpanReceiver>();
68      this.conf = conf;
69    }
70  
71    /**
72     * Reads the names of classes specified in the {@code hbase.trace.spanreceiver.classes} property
73     * and instantiates and registers them with the Tracer.
74     *
75     */
76    public void loadSpanReceivers() {
77      String[] receiverNames = conf.getStrings(SPAN_RECEIVERS_CONF_KEY);
78      if (receiverNames == null || receiverNames.length == 0) {
79        return;
80      }
81  
82      SpanReceiverBuilder builder = new SpanReceiverBuilder(new HBaseHTraceConfiguration(conf));
83      for (String className : receiverNames) {
84        className = className.trim();
85  
86        SpanReceiver receiver = builder.spanReceiverClass(className).build();
87        if (receiver != null) {
88          receivers.add(receiver);
89          LOG.info("SpanReceiver " + className + " was loaded successfully.");
90        }
91      }
92      for (SpanReceiver rcvr : receivers) {
93        Trace.addReceiver(rcvr);
94      }
95    }
96  
97    /**
98     * Calls close() on all SpanReceivers created by this SpanReceiverHost.
99     */
100   public synchronized void closeReceivers() {
101     if (closed) return;
102     closed = true;
103     for (SpanReceiver rcvr : receivers) {
104       try {
105         rcvr.close();
106       } catch (IOException e) {
107         LOG.warn("Unable to close SpanReceiver correctly: " + e.getMessage(), e);
108       }
109     }
110   }
111 }