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