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