1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase;
19
20 import java.io.IOException;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.util.Map.Entry;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.hbase.classification.InterfaceAudience;
28 import org.apache.hadoop.hbase.classification.InterfaceStability;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.util.VersionInfo;
31
32
33
34
35 @InterfaceAudience.Public
36 @InterfaceStability.Stable
37 public class HBaseConfiguration extends Configuration {
38
39 private static final Log LOG = LogFactory.getLog(HBaseConfiguration.class);
40
41
42 private static final int CONVERT_TO_PERCENTAGE = 100;
43
44
45
46
47
48 @Deprecated
49 public HBaseConfiguration() {
50
51 super();
52 addHbaseResources(this);
53 LOG.warn("instantiating HBaseConfiguration() is deprecated. Please use"
54 + " HBaseConfiguration#create() to construct a plain Configuration");
55 }
56
57
58
59
60
61 @Deprecated
62 public HBaseConfiguration(final Configuration c) {
63
64 this();
65 merge(this, c);
66 }
67
68 private static void checkDefaultsVersion(Configuration conf) {
69 if (conf.getBoolean("hbase.defaults.for.version.skip", Boolean.FALSE)) return;
70 String defaultsVersion = conf.get("hbase.defaults.for.version");
71 String thisVersion = VersionInfo.getVersion();
72 if (!thisVersion.equals(defaultsVersion)) {
73 throw new RuntimeException(
74 "hbase-default.xml file seems to be for and old version of HBase (" +
75 defaultsVersion + "), this version is " + thisVersion);
76 }
77 }
78
79 private static void checkForClusterFreeMemoryLimit(Configuration conf) {
80 float globalMemstoreLimit = conf.getFloat("hbase.regionserver.global.memstore.upperLimit", 0.4f);
81 int gml = (int)(globalMemstoreLimit * CONVERT_TO_PERCENTAGE);
82 float blockCacheUpperLimit =
83 conf.getFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY,
84 HConstants.HFILE_BLOCK_CACHE_SIZE_DEFAULT);
85 int bcul = (int)(blockCacheUpperLimit * CONVERT_TO_PERCENTAGE);
86 if (CONVERT_TO_PERCENTAGE - (gml + bcul)
87 < (int)(CONVERT_TO_PERCENTAGE *
88 HConstants.HBASE_CLUSTER_MINIMUM_MEMORY_THRESHOLD)) {
89 throw new RuntimeException(
90 "Current heap configuration for MemStore and BlockCache exceeds " +
91 "the threshold required for successful cluster operation. " +
92 "The combined value cannot exceed 0.8. Please check " +
93 "the settings for hbase.regionserver.global.memstore.upperLimit and " +
94 "hfile.block.cache.size in your configuration. " +
95 "hbase.regionserver.global.memstore.upperLimit is " +
96 globalMemstoreLimit +
97 " hfile.block.cache.size is " + blockCacheUpperLimit);
98 }
99 }
100
101 public static Configuration addHbaseResources(Configuration conf) {
102 conf.addResource("hbase-default.xml");
103 conf.addResource("hbase-site.xml");
104
105 checkDefaultsVersion(conf);
106 checkForClusterFreeMemoryLimit(conf);
107 return conf;
108 }
109
110
111
112
113
114 public static Configuration create() {
115 Configuration conf = new Configuration();
116 return addHbaseResources(conf);
117 }
118
119
120
121
122
123
124 public static Configuration create(final Configuration that) {
125 Configuration conf = create();
126 merge(conf, that);
127 return conf;
128 }
129
130
131
132
133
134
135
136 public static void merge(Configuration destConf, Configuration srcConf) {
137 for (Entry<String, String> e : srcConf) {
138 destConf.set(e.getKey(), e.getValue());
139 }
140 }
141
142
143
144
145 public static boolean isShowConfInServlet() {
146 boolean isShowConf = false;
147 try {
148 if (Class.forName("org.apache.hadoop.conf.ConfServlet") != null) {
149 isShowConf = true;
150 }
151 } catch (LinkageError e) {
152
153 LOG.warn("Error thrown: ", e);
154 } catch (ClassNotFoundException ce) {
155 LOG.debug("ClassNotFound: ConfServlet");
156
157 }
158 return isShowConf;
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179 public static int getInt(Configuration conf, String name,
180 String deprecatedName, int defaultValue) {
181 if (conf.get(deprecatedName) != null) {
182 LOG.warn(String.format("Config option \"%s\" is deprecated. Instead, use \"%s\""
183 , deprecatedName, name));
184 return conf.getInt(deprecatedName, defaultValue);
185 } else {
186 return conf.getInt(name, defaultValue);
187 }
188 }
189
190
191
192
193
194
195
196
197
198
199
200 public static String getPassword(Configuration conf, String alias,
201 String defPass) throws IOException {
202 String passwd = null;
203 try {
204 Method m = Configuration.class.getMethod("getPassword", String.class);
205 char[] p = (char[]) m.invoke(conf, alias);
206 if (p != null) {
207 LOG.debug(String.format("Config option \"%s\" was found through" +
208 " the Configuration getPassword method.", alias));
209 passwd = new String(p);
210 }
211 else {
212 LOG.debug(String.format(
213 "Config option \"%s\" was not found. Using provided default value",
214 alias));
215 passwd = defPass;
216 }
217 } catch (NoSuchMethodException e) {
218
219
220 LOG.debug(String.format(
221 "Credential.getPassword method is not available." +
222 " Falling back to configuration."));
223 passwd = conf.get(alias, defPass);
224 } catch (SecurityException e) {
225 throw new IOException(e.getMessage(), e);
226 } catch (IllegalAccessException e) {
227 throw new IOException(e.getMessage(), e);
228 } catch (IllegalArgumentException e) {
229 throw new IOException(e.getMessage(), e);
230 } catch (InvocationTargetException e) {
231 throw new IOException(e.getMessage(), e);
232 }
233 return passwd;
234 }
235
236
237
238
239
240 public static void main(String[] args) throws Exception {
241 HBaseConfiguration.create().writeXml(System.out);
242 }
243 }