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        float globalMemstoreLimit = conf.getFloat("hbase.regionserver.global.memstore.upperLimit", 0.4f);
78        int gml = (int)(globalMemstoreLimit * CONVERT_TO_PERCENTAGE);
79        float blockCacheUpperLimit =
80          conf.getFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY,
81            HConstants.HFILE_BLOCK_CACHE_SIZE_DEFAULT);
82        int bcul = (int)(blockCacheUpperLimit * CONVERT_TO_PERCENTAGE);
83        if (CONVERT_TO_PERCENTAGE - (gml + bcul)
84                < (int)(CONVERT_TO_PERCENTAGE *
85                        HConstants.HBASE_CLUSTER_MINIMUM_MEMORY_THRESHOLD)) {
86            throw new RuntimeException(
87              "Current heap configuration for MemStore and BlockCache exceeds " +
88              "the threshold required for successful cluster operation. " +
89              "The combined value cannot exceed 0.8. Please check " +
90              "the settings for hbase.regionserver.global.memstore.upperLimit and " +
91              "hfile.block.cache.size in your configuration. " +
92              "hbase.regionserver.global.memstore.upperLimit is " +
93              globalMemstoreLimit +
94              " hfile.block.cache.size is " + blockCacheUpperLimit);
95        }
96    }
97  
98    public static Configuration addHbaseResources(Configuration conf) {
99      conf.addResource("hbase-default.xml");
100     conf.addResource("hbase-site.xml");
101 
102     checkDefaultsVersion(conf);
103     checkForClusterFreeMemoryLimit(conf);
104     return conf;
105   }
106 
107   /**
108    * Creates a Configuration with HBase resources
109    * @return a Configuration with HBase resources
110    */
111   public static Configuration create() {
112     Configuration conf = new Configuration();
113     return addHbaseResources(conf);
114   }
115 
116   /**
117    * @param that Configuration to clone.
118    * @return a Configuration created with the hbase-*.xml files plus
119    * the given configuration.
120    */
121   public static Configuration create(final Configuration that) {
122     Configuration conf = create();
123     merge(conf, that);
124     return conf;
125   }
126 
127   /**
128    * Merge two configurations.
129    * @param destConf the configuration that will be overwritten with items
130    *                 from the srcConf
131    * @param srcConf the source configuration
132    **/
133   public static void merge(Configuration destConf, Configuration srcConf) {
134     for (Entry<String, String> e : srcConf) {
135       destConf.set(e.getKey(), e.getValue());
136     }
137   }
138 
139   /**
140    * @return whether to show HBase Configuration in servlet
141    */
142   public static boolean isShowConfInServlet() {
143     boolean isShowConf = false;
144     try {
145       if (Class.forName("org.apache.hadoop.conf.ConfServlet") != null) {
146         isShowConf = true;
147       }
148     } catch (LinkageError e) {
149        // should we handle it more aggressively in addition to log the error?
150        LOG.warn("Error thrown: ", e);
151     } catch (ClassNotFoundException ce) {
152       LOG.debug("ClassNotFound: ConfServlet");
153       // ignore
154     }
155     return isShowConf;
156   }
157 
158   /**
159    * Get the value of the <code>name</code> property as an <code>int</code>, possibly
160    * referring to the deprecated name of the configuration property.
161    * If no such property exists, the provided default value is returned,
162    * or if the specified value is not a valid <code>int</code>,
163    * then an error is thrown.
164    *
165    * @param name property name.
166    * @param deprecatedName a deprecatedName for the property to use
167    * if non-deprecated name is not used
168    * @param defaultValue default value.
169    * @throws NumberFormatException when the value is invalid
170    * @return property value as an <code>int</code>,
171    *         or <code>defaultValue</code>.
172    */
173   // TODO: developer note: This duplicates the functionality of deprecated
174   // property support in Configuration in Hadoop 2. But since Hadoop-1 does not
175   // contain these changes, we will do our own as usual. Replace these when H2 is default.
176   public static int getInt(Configuration conf, String name,
177       String deprecatedName, int defaultValue) {
178     if (conf.get(deprecatedName) != null) {
179       LOG.warn(String.format("Config option \"%s\" is deprecated. Instead, use \"%s\""
180         , deprecatedName, name));
181       return conf.getInt(deprecatedName, defaultValue);
182     } else {
183       return conf.getInt(name, defaultValue);
184     }
185   }
186 
187   /** For debugging.  Dump configurations to system output as xml format.
188    * Master and RS configurations can also be dumped using
189    * http services. e.g. "curl http://master:60010/dump"
190    */
191   public static void main(String[] args) throws Exception {
192     HBaseConfiguration.create().writeXml(System.out);
193   }
194 }