View Javadoc

1   /**
2    * Copyright 2007 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase;
21  
22  import java.util.Map.Entry;
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.util.VersionInfo;
28  
29  /**
30   * Adds HBase configuration files to a Configuration
31   */
32  public class HBaseConfiguration extends Configuration {
33  
34    private static final Log LOG = LogFactory.getLog(HBaseConfiguration.class);
35  
36    /**
37     * Instantinating HBaseConfiguration() is deprecated. Please use
38     * HBaseConfiguration#create() to construct a plain Configuration
39     */
40    @Deprecated
41    public HBaseConfiguration() {
42      //TODO:replace with private constructor, HBaseConfiguration should not extend Configuration
43      super();
44      addHbaseResources(this);
45      LOG.warn("instantiating HBaseConfiguration() is deprecated. Please use" +
46      		" HBaseConfiguration#create() to construct a plain Configuration");
47    }
48  
49    /**
50     * Instantiating HBaseConfiguration() is deprecated. Please use
51     * HBaseConfiguration#create(conf) to construct a plain Configuration
52     */
53    @Deprecated
54    public HBaseConfiguration(final Configuration c) {
55      //TODO:replace with private constructor
56      this();
57      for (Entry<String, String>e: c) {
58        set(e.getKey(), e.getValue());
59      }
60    }
61  
62    private static void checkDefaultsVersion(Configuration conf) {
63      String defaultsVersion = conf.get("hbase.defaults.for.version");
64      String thisVersion = VersionInfo.getVersion();
65      if (!thisVersion.equals(defaultsVersion)) {
66        throw new RuntimeException(
67          "hbase-default.xml file seems to be for and old version of HBase (" +
68          defaultsVersion + "), this version is " + thisVersion);
69      }
70    }
71  
72    private static void checkForClusterFreeMemoryLimit(Configuration conf) {
73        float globalMemstoreLimit = conf.getFloat("hbase.regionserver.global.memstore.upperLimit", 0.4f);
74        float blockCacheUpperLimit = conf.getFloat("hfile.block.cache.size", 0.2f);
75        if (1.0f - (globalMemstoreLimit + blockCacheUpperLimit)
76                < HConstants.HBASE_CLUSTER_MINIMUM_MEMORY_THRESHOLD) {
77            throw new RuntimeException(
78          "Current heap configuration for MemStore and BlockCache exceeds the threshold required for " +
79                  "successful cluster operation. The combined value cannot exceed 0.8. Please check " +
80                  "the settings for hbase.regionserver.global.memstore.upperLimit and" +
81                  " hfile.block.cache.size in your configuration.");
82        }
83    }
84  
85    public static Configuration addHbaseResources(Configuration conf) {
86      conf.addResource("hbase-default.xml");
87      conf.addResource("hbase-site.xml");
88  
89      checkDefaultsVersion(conf);
90      checkForClusterFreeMemoryLimit(conf);
91      return conf;
92    }
93  
94    /**
95     * Creates a Configuration with HBase resources
96     * @return a Configuration with HBase resources
97     */
98    public static Configuration create() {
99      Configuration conf = new Configuration();
100     return addHbaseResources(conf);
101   }
102 
103   /**
104    * Creates a clone of passed configuration.
105    * @param that Configuration to clone.
106    * @return a Configuration created with the hbase-*.xml files plus
107    * the given configuration.
108    */
109   public static Configuration create(final Configuration that) {
110     Configuration conf = create();
111     for (Entry<String, String>e: that) {
112       conf.set(e.getKey(), e.getValue());
113     }
114     return conf;
115   }
116 }