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
117
118
119 conf.setClassLoader(HBaseConfiguration.class.getClassLoader());
120 return addHbaseResources(conf);
121 }
122
123
124
125
126
127
128 public static Configuration create(final Configuration that) {
129 Configuration conf = create();
130 merge(conf, that);
131 return conf;
132 }
133
134
135
136
137
138
139
140 public static void merge(Configuration destConf, Configuration srcConf) {
141 for (Entry<String, String> e : srcConf) {
142 destConf.set(e.getKey(), e.getValue());
143 }
144 }
145
146
147
148
149 public static boolean isShowConfInServlet() {
150 boolean isShowConf = false;
151 try {
152 if (Class.forName("org.apache.hadoop.conf.ConfServlet") != null) {
153 isShowConf = true;
154 }
155 } catch (LinkageError e) {
156
157 LOG.warn("Error thrown: ", e);
158 } catch (ClassNotFoundException ce) {
159 LOG.debug("ClassNotFound: ConfServlet");
160
161 }
162 return isShowConf;
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 public static int getInt(Configuration conf, String name,
184 String deprecatedName, int defaultValue) {
185 if (conf.get(deprecatedName) != null) {
186 LOG.warn(String.format("Config option \"%s\" is deprecated. Instead, use \"%s\""
187 , deprecatedName, name));
188 return conf.getInt(deprecatedName, defaultValue);
189 } else {
190 return conf.getInt(name, defaultValue);
191 }
192 }
193
194
195
196
197
198
199
200
201
202
203
204 public static String getPassword(Configuration conf, String alias,
205 String defPass) throws IOException {
206 String passwd = null;
207 try {
208 Method m = Configuration.class.getMethod("getPassword", String.class);
209 char[] p = (char[]) m.invoke(conf, alias);
210 if (p != null) {
211 LOG.debug(String.format("Config option \"%s\" was found through" +
212 " the Configuration getPassword method.", alias));
213 passwd = new String(p);
214 }
215 else {
216 LOG.debug(String.format(
217 "Config option \"%s\" was not found. Using provided default value",
218 alias));
219 passwd = defPass;
220 }
221 } catch (NoSuchMethodException e) {
222
223
224 LOG.debug(String.format(
225 "Credential.getPassword method is not available." +
226 " Falling back to configuration."));
227 passwd = conf.get(alias, defPass);
228 } catch (SecurityException e) {
229 throw new IOException(e.getMessage(), e);
230 } catch (IllegalAccessException e) {
231 throw new IOException(e.getMessage(), e);
232 } catch (IllegalArgumentException e) {
233 throw new IOException(e.getMessage(), e);
234 } catch (InvocationTargetException e) {
235 throw new IOException(e.getMessage(), e);
236 }
237 return passwd;
238 }
239
240
241
242
243
244 public static void main(String[] args) throws Exception {
245 HBaseConfiguration.create().writeXml(System.out);
246 }
247 }