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;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.hbase.classification.InterfaceStability;
30 import org.apache.hadoop.hbase.util.VersionInfo;
31 import org.apache.hadoop.hbase.zookeeper.ZKConfig;
32
33
34
35
36 @InterfaceAudience.Public
37 @InterfaceStability.Stable
38 public class HBaseConfiguration extends Configuration {
39
40 private static final Log LOG = LogFactory.getLog(HBaseConfiguration.class);
41
42
43 private static final int CONVERT_TO_PERCENTAGE = 100;
44
45
46
47
48
49 @Deprecated
50 public HBaseConfiguration() {
51
52 super();
53 addHbaseResources(this);
54 LOG.warn("instantiating HBaseConfiguration() is deprecated. Please use"
55 + " HBaseConfiguration#create() to construct a plain Configuration");
56 }
57
58
59
60
61
62 @Deprecated
63 public HBaseConfiguration(final Configuration c) {
64
65 this();
66 merge(this, c);
67 }
68
69 private static void checkDefaultsVersion(Configuration conf) {
70 if (conf.getBoolean("hbase.defaults.for.version.skip", Boolean.FALSE)) return;
71 String defaultsVersion = conf.get("hbase.defaults.for.version");
72 String thisVersion = VersionInfo.getVersion();
73 if (!thisVersion.equals(defaultsVersion)) {
74 throw new RuntimeException(
75 "hbase-default.xml file seems to be for and old version of HBase (" +
76 defaultsVersion + "), this version is " + thisVersion);
77 }
78 }
79
80 private static void checkForClusterFreeMemoryLimit(Configuration conf) {
81 float globalMemstoreLimit = conf.getFloat("hbase.regionserver.global.memstore.upperLimit", 0.4f);
82 int gml = (int)(globalMemstoreLimit * CONVERT_TO_PERCENTAGE);
83 float blockCacheUpperLimit =
84 conf.getFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY,
85 HConstants.HFILE_BLOCK_CACHE_SIZE_DEFAULT);
86 int bcul = (int)(blockCacheUpperLimit * CONVERT_TO_PERCENTAGE);
87 if (CONVERT_TO_PERCENTAGE - (gml + bcul)
88 < (int)(CONVERT_TO_PERCENTAGE *
89 HConstants.HBASE_CLUSTER_MINIMUM_MEMORY_THRESHOLD)) {
90 throw new RuntimeException(
91 "Current heap configuration for MemStore and BlockCache exceeds " +
92 "the threshold required for successful cluster operation. " +
93 "The combined value cannot exceed 0.8. Please check " +
94 "the settings for hbase.regionserver.global.memstore.upperLimit and " +
95 "hfile.block.cache.size in your configuration. " +
96 "hbase.regionserver.global.memstore.upperLimit is " +
97 globalMemstoreLimit +
98 " hfile.block.cache.size is " + blockCacheUpperLimit);
99 }
100 }
101
102 public static Configuration addHbaseResources(Configuration conf) {
103 conf.addResource("hbase-default.xml");
104 conf.addResource("hbase-site.xml");
105
106 checkDefaultsVersion(conf);
107 checkForClusterFreeMemoryLimit(conf);
108 return conf;
109 }
110
111
112
113
114
115 public static Configuration create() {
116 Configuration conf = new Configuration();
117
118
119
120 conf.setClassLoader(HBaseConfiguration.class.getClassLoader());
121 return addHbaseResources(conf);
122 }
123
124
125
126
127
128
129 public static Configuration create(final Configuration that) {
130 Configuration conf = create();
131 merge(conf, that);
132 return conf;
133 }
134
135
136
137
138
139
140
141 public static void merge(Configuration destConf, Configuration srcConf) {
142 for (Map.Entry<String, String> e : srcConf) {
143 destConf.set(e.getKey(), e.getValue());
144 }
145 }
146
147
148
149
150
151
152
153
154 public static Configuration subset(Configuration srcConf, String prefix) {
155 Configuration newConf = new Configuration(false);
156 for (Map.Entry<String, String> entry : srcConf) {
157 if (entry.getKey().startsWith(prefix)) {
158 String newKey = entry.getKey().substring(prefix.length());
159
160 if (!newKey.isEmpty()) {
161 newConf.set(newKey, entry.getValue());
162 }
163 }
164 }
165 return newConf;
166 }
167
168
169
170
171
172
173 public static void setWithPrefix(Configuration conf, String prefix,
174 Iterable<Map.Entry<String, String>> properties) {
175 for (Map.Entry<String, String> entry : properties) {
176 conf.set(prefix + entry.getKey(), entry.getValue());
177 }
178 }
179
180
181
182
183 public static boolean isShowConfInServlet() {
184 boolean isShowConf = false;
185 try {
186 if (Class.forName("org.apache.hadoop.conf.ConfServlet") != null) {
187 isShowConf = true;
188 }
189 } catch (LinkageError e) {
190
191 LOG.warn("Error thrown: ", e);
192 } catch (ClassNotFoundException ce) {
193 LOG.debug("ClassNotFound: ConfServlet");
194
195 }
196 return isShowConf;
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217 public static int getInt(Configuration conf, String name,
218 String deprecatedName, int defaultValue) {
219 if (conf.get(deprecatedName) != null) {
220 LOG.warn(String.format("Config option \"%s\" is deprecated. Instead, use \"%s\""
221 , deprecatedName, name));
222 return conf.getInt(deprecatedName, defaultValue);
223 } else {
224 return conf.getInt(name, defaultValue);
225 }
226 }
227
228
229
230
231
232
233
234
235
236
237
238 public static String getPassword(Configuration conf, String alias,
239 String defPass) throws IOException {
240 String passwd = null;
241 try {
242 Method m = Configuration.class.getMethod("getPassword", String.class);
243 char[] p = (char[]) m.invoke(conf, alias);
244 if (p != null) {
245 LOG.debug(String.format("Config option \"%s\" was found through" +
246 " the Configuration getPassword method.", alias));
247 passwd = new String(p);
248 }
249 else {
250 LOG.debug(String.format(
251 "Config option \"%s\" was not found. Using provided default value",
252 alias));
253 passwd = defPass;
254 }
255 } catch (NoSuchMethodException e) {
256
257
258 LOG.debug(String.format(
259 "Credential.getPassword method is not available." +
260 " Falling back to configuration."));
261 passwd = conf.get(alias, defPass);
262 } catch (SecurityException e) {
263 throw new IOException(e.getMessage(), e);
264 } catch (IllegalAccessException e) {
265 throw new IOException(e.getMessage(), e);
266 } catch (IllegalArgumentException e) {
267 throw new IOException(e.getMessage(), e);
268 } catch (InvocationTargetException e) {
269 throw new IOException(e.getMessage(), e);
270 }
271 return passwd;
272 }
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287 public static Configuration createClusterConf(Configuration baseConf, String clusterKey)
288 throws IOException {
289 return createClusterConf(baseConf, clusterKey, null);
290 }
291
292
293
294
295
296
297
298
299
300
301
302
303
304 public static Configuration createClusterConf(Configuration baseConf, String clusterKey,
305 String overridePrefix) throws IOException {
306 Configuration clusterConf = HBaseConfiguration.create(baseConf);
307 if (clusterKey != null && !clusterKey.isEmpty()) {
308 applyClusterKeyToConf(clusterConf, clusterKey);
309 }
310
311 if (overridePrefix != null && !overridePrefix.isEmpty()) {
312 Configuration clusterSubset = HBaseConfiguration.subset(clusterConf, overridePrefix);
313 HBaseConfiguration.merge(clusterConf, clusterSubset);
314 }
315 return clusterConf;
316 }
317
318
319
320
321
322
323
324
325 private static void applyClusterKeyToConf(Configuration conf, String key)
326 throws IOException{
327 ZKConfig.ZKClusterKey zkClusterKey = ZKConfig.transformClusterKey(key);
328 conf.set(HConstants.ZOOKEEPER_QUORUM, zkClusterKey.getQuorumString());
329 conf.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, zkClusterKey.getClientPort());
330 conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, zkClusterKey.getZnodeParent());
331 }
332
333
334
335
336
337
338 public static void main(String[] args) throws Exception {
339 HBaseConfiguration.create().writeXml(System.out);
340 }
341 }