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    private boolean closed = false;
42  
43    private static enum SingleTonholder {
44      INSTANCE;
45      Object lock = new Object();
46      SpanReceiverHost host = null;
47    }
48  
49    public static SpanReceiverHost getInstance(Configuration conf) {
50      if (SingleTonholder.INSTANCE.host != null) {
51        return SingleTonholder.INSTANCE.host;
52      }
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
73     * "hbase.trace.spanreceiver.classes" property and instantiates and registers
74     * them with the Tracer as SpanReceiver's.
75     *
76     */
77    public void loadSpanReceivers() {
78      Class<?> implClass = null;
79      String[] receiverNames = conf.getStrings(SPAN_RECEIVERS_CONF_KEY);
80      if (receiverNames == null || receiverNames.length == 0) {
81        return;
82      }
83      for (String className : receiverNames) {
84        className = className.trim();
85  
86        try {
87          implClass = Class.forName(className);
88          SpanReceiver receiver = loadInstance(implClass);
89          if (receiver != null) {
90            receivers.add(receiver);
91            LOG.info("SpanReceiver " + className + " was loaded successfully.");
92          }
93  
94        } catch (ClassNotFoundException e) {
95          LOG.warn("Class " + className + " cannot be found. " + e.getMessage());
96        } catch (IOException e) {
97          LOG.warn("Load SpanReceiver " + className + " failed. "
98              + e.getMessage());
99        }
100     }
101     for (SpanReceiver rcvr : receivers) {
102       Trace.addReceiver(rcvr);
103     }
104   }
105 
106   private SpanReceiver loadInstance(Class<?> implClass)
107       throws IOException {
108     SpanReceiver impl = null;
109     try {
110       Object o = implClass.newInstance();
111       impl = (SpanReceiver)o;
112       impl.configure(new HBaseHTraceConfiguration(this.conf));
113     } catch (SecurityException e) {
114       throw new IOException(e);
115     } catch (IllegalArgumentException e) {
116       throw new IOException(e);
117     } catch (RuntimeException e) {
118       throw new IOException(e);
119     } catch (InstantiationException e) {
120       e.printStackTrace();
121     } catch (IllegalAccessException e) {
122       e.printStackTrace();
123     }
124 
125     return impl;
126   }
127 
128   /**
129    * Calls close() on all SpanReceivers created by this SpanReceiverHost.
130    */
131   public synchronized void closeReceivers() {
132     if (closed) return;
133     closed = true;
134     for (SpanReceiver rcvr : receivers) {
135       try {
136         rcvr.close();
137       } catch (IOException e) {
138         LOG.warn("Unable to close SpanReceiver correctly: " + e.getMessage(), e);
139       }
140     }
141   }
142 }