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.htrace.SpanReceiver;
28  import org.htrace.Trace;
29  
30  /**
31   * This class provides functions for reading the names of SpanReceivers from
32   * hbase-site.xml, adding those SpanReceivers to the Tracer, and closing those
33   * SpanReceivers when appropriate.
34   */
35  public class SpanReceiverHost {
36    public static final String SPAN_RECEIVERS_CONF_KEY = "hbase.trace.spanreceiver.classes";
37    private static final Log LOG = LogFactory.getLog(SpanReceiverHost.class);
38    private Collection<SpanReceiver> receivers;
39    private Configuration conf;
40    private boolean closed = false;
41  
42    private static enum SingletonHolder {
43      INSTANCE;
44      Object lock = new Object();
45      SpanReceiverHost host = null;
46    }
47  
48    public static SpanReceiverHost getInstance(Configuration conf) {
49      synchronized (SingletonHolder.INSTANCE.lock) {
50        if (SingletonHolder.INSTANCE.host != null) {
51          return SingletonHolder.INSTANCE.host;
52        }
53  
54        SpanReceiverHost host = new SpanReceiverHost(conf);
55        host.loadSpanReceivers();
56        SingletonHolder.INSTANCE.host = host;
57        return SingletonHolder.INSTANCE.host;
58      }
59  
60    }
61  
62    SpanReceiverHost(Configuration conf) {
63      receivers = new HashSet<SpanReceiver>();
64      this.conf = conf;
65    }
66  
67    /**
68     * Reads the names of classes specified in the
69     * "hbase.trace.spanreceiver.classes" property and instantiates and registers
70     * them with the Tracer as SpanReceiver's.
71     *
72     */
73    public void loadSpanReceivers() {
74      Class<?> implClass = null;
75      String[] receiverNames = conf.getStrings(SPAN_RECEIVERS_CONF_KEY);
76      if (receiverNames == null || receiverNames.length == 0) {
77        return;
78      }
79      for (String className : receiverNames) {
80        className = className.trim();
81  
82        try {
83          implClass = Class.forName(className);
84          SpanReceiver receiver = loadInstance(implClass);
85          if (receiver != null) {
86            receivers.add(receiver);
87            LOG.info("SpanReceiver " + className + " was loaded successfully.");
88          }
89  
90        } catch (ClassNotFoundException e) {
91          LOG.warn("Class " + className + " cannot be found. " + e.getMessage());
92        } catch (IOException e) {
93          LOG.warn("Load SpanReceiver " + className + " failed. "
94              + e.getMessage());
95        }
96      }
97      for (SpanReceiver rcvr : receivers) {
98        Trace.addReceiver(rcvr);
99      }
100   }
101 
102   private SpanReceiver loadInstance(Class<?> implClass)
103       throws IOException {
104     SpanReceiver impl = null;
105     try {
106       Object o = implClass.newInstance();
107       impl = (SpanReceiver)o;
108       impl.configure(new HBaseHTraceConfiguration(this.conf));
109     } catch (SecurityException e) {
110       throw new IOException(e);
111     } catch (IllegalArgumentException e) {
112       throw new IOException(e);
113     } catch (RuntimeException e) {
114       throw new IOException(e);
115     } catch (InstantiationException e) {
116       e.printStackTrace();
117     } catch (IllegalAccessException e) {
118       e.printStackTrace();
119     }
120 
121     return impl;
122   }
123 
124   /**
125    * Calls close() on all SpanReceivers created by this SpanReceiverHost.
126    */
127   public synchronized void closeReceivers() {
128     if (closed) return;
129     closed = true;
130     for (SpanReceiver rcvr : receivers) {
131       try {
132         rcvr.close();
133       } catch (IOException e) {
134         LOG.warn("Unable to close SpanReceiver correctly: " + e.getMessage(), e);
135       }
136     }
137   }
138 }