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