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.util.ReflectionUtils;
28  import org.cloudera.htrace.SpanReceiver;
29  import org.cloudera.htrace.Trace;
30  
31  /**
32   * This class provides functions for reading the names of SpanReceivers from
33   * hbase-site.xml, adding those SpanReceivers to the Tracer, and closing those
34   * SpanReceivers when appropriate.
35   */
36  public class SpanReceiverHost {
37    public static final String SPAN_RECEIVERS_CONF_KEY = "hbase.trace.spanreceiver.classes";
38    private static final Log LOG = LogFactory.getLog(SpanReceiverHost.class);
39    private Collection<SpanReceiver> receivers;
40    private Configuration conf;
41  
42    public SpanReceiverHost(Configuration conf) {
43      receivers = new HashSet<SpanReceiver>();
44      this.conf = conf;
45    }
46  
47    /**
48     * Reads the names of classes specified in the
49     * "hbase.trace.spanreceiver.classes" property and instantiates and registers
50     * them with the Tracer as SpanReceiver's.
51     * 
52     * The nullary constructor is called during construction, but if the classes
53     * specified implement the Configurable interface, setConfiguration() will be
54     * called on them. This allows SpanReceivers to use values from
55     * hbase-site.xml. See
56     * {@link org.apache.hadoop.hbase.trace.HBaseLocalFileSpanReceiver} for an
57     * example.
58     */
59    public void loadSpanReceivers() {
60      Class<?> implClass = null;
61      String[] receiverNames = conf.getStrings(SPAN_RECEIVERS_CONF_KEY);
62      if (receiverNames == null || receiverNames.length == 0) {
63        return;
64      }
65      for (String className : receiverNames) {
66        className = className.trim();
67  
68        try {
69          implClass = Class.forName(className);
70          receivers.add(loadInstance(implClass));
71          LOG.info("SpanReceiver " + className + " was loaded successfully.");
72        } catch (ClassNotFoundException e) {
73          LOG.warn("Class " + className + " cannot be found. " + e.getMessage());
74        } catch (IOException e) {
75          LOG.warn("Load SpanReceiver " + className + " failed. "
76              + e.getMessage());
77        }
78      }
79      for (SpanReceiver rcvr : receivers) {
80        Trace.addReceiver(rcvr);
81      }
82    }
83  
84    private SpanReceiver loadInstance(Class<?> implClass)
85        throws IOException {
86      SpanReceiver impl;
87      try {
88        Object o = ReflectionUtils.newInstance(implClass, conf);
89        impl = (SpanReceiver)o;
90      } catch (SecurityException e) {
91        throw new IOException(e);
92      } catch (IllegalArgumentException e) {
93        throw new IOException(e);
94      } catch (RuntimeException e) {
95        throw new IOException(e);
96      }
97  
98      return impl;
99    }
100 
101   /**
102    * Calls close() on all SpanReceivers created by this SpanReceiverHost.
103    */
104   public void closeReceivers() {
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 }