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;
19  
20  import java.util.Map.Entry;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.classification.InterfaceAudience;
25  import org.apache.hadoop.classification.InterfaceStability;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.io.util.HeapMemorySizeUtil;
28  import org.apache.hadoop.hbase.util.VersionInfo;
29  
30  /**
31   * Adds HBase configuration files to a Configuration
32   */
33  @InterfaceAudience.Public
34  @InterfaceStability.Stable
35  public class HBaseConfiguration extends Configuration {
36  
37    private static final Log LOG = LogFactory.getLog(HBaseConfiguration.class);
38  
39    /**
40     * Instantinating HBaseConfiguration() is deprecated. Please use
41     * HBaseConfiguration#create() to construct a plain Configuration
42     */
43    @Deprecated
44    public HBaseConfiguration() {
45      //TODO:replace with private constructor, HBaseConfiguration should not extend Configuration
46      super();
47      addHbaseResources(this);
48      LOG.warn("instantiating HBaseConfiguration() is deprecated. Please use"
49          + " HBaseConfiguration#create() to construct a plain Configuration");
50    }
51  
52    /**
53     * Instantiating HBaseConfiguration() is deprecated. Please use
54     * HBaseConfiguration#create(conf) to construct a plain Configuration
55     */
56    @Deprecated
57    public HBaseConfiguration(final Configuration c) {
58      //TODO:replace with private constructor
59      this();
60      merge(this, c);
61    }
62  
63    private static void checkDefaultsVersion(Configuration conf) {
64      if (conf.getBoolean("hbase.defaults.for.version.skip", Boolean.FALSE)) return;
65      String defaultsVersion = conf.get("hbase.defaults.for.version");
66      String thisVersion = VersionInfo.getVersion();
67      if (!thisVersion.equals(defaultsVersion)) {
68        throw new RuntimeException(
69          "hbase-default.xml file seems to be for and old version of HBase (" +
70          defaultsVersion + "), this version is " + thisVersion);
71      }
72    }
73  
74    public static Configuration addHbaseResources(Configuration conf) {
75      conf.addResource("hbase-default.xml");
76      conf.addResource("hbase-site.xml");
77  
78      checkDefaultsVersion(conf);
79      HeapMemorySizeUtil.checkForClusterFreeMemoryLimit(conf);
80      return conf;
81    }
82  
83    /**
84     * Creates a Configuration with HBase resources
85     * @return a Configuration with HBase resources
86     */
87    public static Configuration create() {
88      Configuration conf = new Configuration();
89      return addHbaseResources(conf);
90    }
91  
92    /**
93     * @param that Configuration to clone.
94     * @return a Configuration created with the hbase-*.xml files plus
95     * the given configuration.
96     */
97    public static Configuration create(final Configuration that) {
98      Configuration conf = create();
99      merge(conf, that);
100     return conf;
101   }
102 
103   /**
104    * Merge two configurations.
105    * @param destConf the configuration that will be overwritten with items
106    *                 from the srcConf
107    * @param srcConf the source configuration
108    **/
109   public static void merge(Configuration destConf, Configuration srcConf) {
110     for (Entry<String, String> e : srcConf) {
111       destConf.set(e.getKey(), e.getValue());
112     }
113   }
114 
115   /**
116    * @return whether to show HBase Configuration in servlet
117    */
118   public static boolean isShowConfInServlet() {
119     boolean isShowConf = false;
120     try {
121       if (Class.forName("org.apache.hadoop.conf.ConfServlet") != null) {
122         isShowConf = true;
123       }
124     } catch (LinkageError e) {
125        // should we handle it more aggressively in addition to log the error?
126        LOG.warn("Error thrown: ", e);
127     } catch (ClassNotFoundException ce) {
128       LOG.debug("ClassNotFound: ConfServlet");
129       // ignore
130     }
131     return isShowConf;
132   }
133 
134   /**
135    * Get the value of the <code>name</code> property as an <code>int</code>, possibly
136    * referring to the deprecated name of the configuration property.
137    * If no such property exists, the provided default value is returned,
138    * or if the specified value is not a valid <code>int</code>,
139    * then an error is thrown.
140    *
141    * @param name property name.
142    * @param deprecatedName a deprecatedName for the property to use
143    * if non-deprecated name is not used
144    * @param defaultValue default value.
145    * @throws NumberFormatException when the value is invalid
146    * @return property value as an <code>int</code>,
147    *         or <code>defaultValue</code>.
148    */
149   // TODO: developer note: This duplicates the functionality of deprecated
150   // property support in Configuration in Hadoop 2. But since Hadoop-1 does not
151   // contain these changes, we will do our own as usual. Replace these when H2 is default.
152   public static int getInt(Configuration conf, String name,
153       String deprecatedName, int defaultValue) {
154     if (conf.get(deprecatedName) != null) {
155       LOG.warn(String.format("Config option \"%s\" is deprecated. Instead, use \"%s\""
156         , deprecatedName, name));
157       return conf.getInt(deprecatedName, defaultValue);
158     } else {
159       return conf.getInt(name, defaultValue);
160     }
161   }
162 
163   /** For debugging.  Dump configurations to system output as xml format.
164    * Master and RS configurations can also be dumped using
165    * http services. e.g. "curl http://master:16010/dump"
166    */
167   public static void main(String[] args) throws Exception {
168     HBaseConfiguration.create().writeXml(System.out);
169   }
170 }