HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-common / src / main / java / org / apache / hadoop / hbase / HBaseConfiguration.java
blob596c52b00930d223fc8bec97e1598c81606e2990
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase;
20 import java.io.IOException;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.util.Map;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.util.VersionInfo;
27 import org.apache.hadoop.hbase.zookeeper.ZKConfig;
28 import org.apache.yetus.audience.InterfaceAudience;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 /**
33 * Adds HBase configuration files to a Configuration
35 @InterfaceAudience.Public
36 public class HBaseConfiguration extends Configuration {
37 private static final Logger LOG = LoggerFactory.getLogger(HBaseConfiguration.class);
39 static {
40 addDeprecatedKeys();
43 private static void checkDefaultsVersion(Configuration conf) {
44 if (conf.getBoolean("hbase.defaults.for.version.skip", Boolean.FALSE)) return;
45 String defaultsVersion = conf.get("hbase.defaults.for.version");
46 String thisVersion = VersionInfo.getVersion();
47 if (!thisVersion.equals(defaultsVersion)) {
48 throw new RuntimeException(
49 "hbase-default.xml file seems to be for an older version of HBase (" +
50 defaultsVersion + "), this version is " + thisVersion);
54 /**
55 * The hbase.ipc.server.reservoir.initial.max and hbase.ipc.server.reservoir.initial.buffer.size
56 * were introduced in HBase2.0.0, while in HBase3.0.0 the two config keys will be replaced by
57 * hbase.server.allocator.max.buffer.count and hbase.server.allocator.buffer.size.
58 * Also the hbase.ipc.server.reservoir.enabled will be replaced by
59 * hbase.server.allocator.pool.enabled. Keep the three old config keys here for HBase2.x
60 * compatibility.
61 * <br>
62 * HBASE-24667: This config hbase.regionserver.hostname.disable.master.reversedns will be
63 * replaced by hbase.unsafe.regionserver.hostname.disable.master.reversedns. Keep the old config
64 * keys here for backward compatibility.
65 * <br>
66 * Note: Before Hadoop-3.3, we must call the addDeprecations method before creating the
67 * Configuration object to work correctly. After this bug is fixed in hadoop-3.3, there will be
68 * no order problem.
69 * @see <a href="https://issues.apache.org/jira/browse/HADOOP-15708">HADOOP-15708</a>
71 private static void addDeprecatedKeys() {
72 Configuration.addDeprecations(new DeprecationDelta[]{
73 new DeprecationDelta("hbase.regionserver.hostname", "hbase.unsafe.regionserver.hostname"),
74 new DeprecationDelta("hbase.regionserver.hostname.disable.master.reversedns",
75 "hbase.unsafe.regionserver.hostname.disable.master.reversedns"),
76 new DeprecationDelta("hbase.offheapcache.minblocksize",
77 "hbase.blockcache.minblocksize"),
78 new DeprecationDelta("hbase.ipc.server.reservoir.enabled",
79 "hbase.server.allocator.pool.enabled"),
80 new DeprecationDelta("hbase.ipc.server.reservoir.initial.max",
81 "hbase.server.allocator.max.buffer.count"),
82 new DeprecationDelta("hbase.ipc.server.reservoir.initial.buffer.size",
83 "hbase.server.allocator.buffer.size"),
84 new DeprecationDelta("hlog.bulk.output", "wal.bulk.output"),
85 new DeprecationDelta("hlog.input.tables", "wal.input.tables"),
86 new DeprecationDelta("hlog.input.tablesmap", "wal.input.tablesmap"),
87 new DeprecationDelta("hbase.master.mob.ttl.cleaner.period",
88 "hbase.master.mob.cleaner.period"),
89 new DeprecationDelta("hbase.normalizer.min.region.count",
90 "hbase.normalizer.merge.min.region.count")
91 });
94 public static Configuration addHbaseResources(Configuration conf) {
95 conf.addResource("hbase-default.xml");
96 conf.addResource("hbase-site.xml");
98 checkDefaultsVersion(conf);
99 return conf;
103 * Creates a Configuration with HBase resources
104 * @return a Configuration with HBase resources
106 public static Configuration create() {
107 Configuration conf = new Configuration();
108 // In case HBaseConfiguration is loaded from a different classloader than
109 // Configuration, conf needs to be set with appropriate class loader to resolve
110 // HBase resources.
111 conf.setClassLoader(HBaseConfiguration.class.getClassLoader());
112 return addHbaseResources(conf);
116 * @param that Configuration to clone.
117 * @return a Configuration created with the hbase-*.xml files plus
118 * the given configuration.
120 public static Configuration create(final Configuration that) {
121 Configuration conf = create();
122 merge(conf, that);
123 return conf;
127 * Merge two configurations.
128 * @param destConf the configuration that will be overwritten with items
129 * from the srcConf
130 * @param srcConf the source configuration
132 public static void merge(Configuration destConf, Configuration srcConf) {
133 for (Map.Entry<String, String> e : srcConf) {
134 destConf.set(e.getKey(), e.getValue());
139 * Returns a subset of the configuration properties, matching the given key prefix.
140 * The prefix is stripped from the return keys, ie. when calling with a prefix of "myprefix",
141 * the entry "myprefix.key1 = value1" would be returned as "key1 = value1". If an entry's
142 * key matches the prefix exactly ("myprefix = value2"), it will <strong>not</strong> be
143 * included in the results, since it would show up as an entry with an empty key.
145 public static Configuration subset(Configuration srcConf, String prefix) {
146 Configuration newConf = new Configuration(false);
147 for (Map.Entry<String, String> entry : srcConf) {
148 if (entry.getKey().startsWith(prefix)) {
149 String newKey = entry.getKey().substring(prefix.length());
150 // avoid entries that would produce an empty key
151 if (!newKey.isEmpty()) {
152 newConf.set(newKey, entry.getValue());
156 return newConf;
160 * Sets all the entries in the provided {@code Map<String, String>} as properties in the
161 * given {@code Configuration}. Each property will have the specified prefix prepended,
162 * so that the configuration entries are keyed by {@code prefix + entry.getKey()}.
164 public static void setWithPrefix(Configuration conf, String prefix,
165 Iterable<Map.Entry<String, String>> properties) {
166 for (Map.Entry<String, String> entry : properties) {
167 conf.set(prefix + entry.getKey(), entry.getValue());
172 * @return whether to show HBase Configuration in servlet
174 public static boolean isShowConfInServlet() {
175 boolean isShowConf = false;
176 try {
177 if (Class.forName("org.apache.hadoop.conf.ConfServlet") != null) {
178 isShowConf = true;
180 } catch (LinkageError e) {
181 // should we handle it more aggressively in addition to log the error?
182 LOG.warn("Error thrown: ", e);
183 } catch (ClassNotFoundException ce) {
184 LOG.debug("ClassNotFound: ConfServlet");
185 // ignore
187 return isShowConf;
191 * Get the password from the Configuration instance using the
192 * getPassword method if it exists. If not, then fall back to the
193 * general get method for configuration elements.
195 * @param conf configuration instance for accessing the passwords
196 * @param alias the name of the password element
197 * @param defPass the default password
198 * @return String password or default password
199 * @throws IOException
201 public static String getPassword(Configuration conf, String alias,
202 String defPass) throws IOException {
203 String passwd;
204 char[] p = conf.getPassword(alias);
205 if (p != null) {
206 LOG.debug("Config option {} was found through the Configuration getPassword method.", alias);
207 passwd = new String(p);
208 } else {
209 LOG.debug("Config option {} was not found. Using provided default value", alias);
210 passwd = defPass;
212 return passwd;
216 * Generates a {@link Configuration} instance by applying the ZooKeeper cluster key
217 * to the base Configuration. Note that additional configuration properties may be needed
218 * for a remote cluster, so it is preferable to use
219 * {@link #createClusterConf(Configuration, String, String)}.
221 * @param baseConf the base configuration to use, containing prefixed override properties
222 * @param clusterKey the ZooKeeper quorum cluster key to apply, or {@code null} if none
224 * @return the merged configuration with override properties and cluster key applied
226 * @see #createClusterConf(Configuration, String, String)
228 public static Configuration createClusterConf(Configuration baseConf, String clusterKey)
229 throws IOException {
230 return createClusterConf(baseConf, clusterKey, null);
234 * Generates a {@link Configuration} instance by applying property overrides prefixed by
235 * a cluster profile key to the base Configuration. Override properties are extracted by
236 * the {@link #subset(Configuration, String)} method, then the merged on top of the base
237 * Configuration and returned.
239 * @param baseConf the base configuration to use, containing prefixed override properties
240 * @param clusterKey the ZooKeeper quorum cluster key to apply, or {@code null} if none
241 * @param overridePrefix the property key prefix to match for override properties,
242 * or {@code null} if none
243 * @return the merged configuration with override properties and cluster key applied
245 public static Configuration createClusterConf(Configuration baseConf, String clusterKey,
246 String overridePrefix) throws IOException {
247 Configuration clusterConf = HBaseConfiguration.create(baseConf);
248 if (clusterKey != null && !clusterKey.isEmpty()) {
249 applyClusterKeyToConf(clusterConf, clusterKey);
252 if (overridePrefix != null && !overridePrefix.isEmpty()) {
253 Configuration clusterSubset = HBaseConfiguration.subset(clusterConf, overridePrefix);
254 HBaseConfiguration.merge(clusterConf, clusterSubset);
256 return clusterConf;
260 * Apply the settings in the given key to the given configuration, this is
261 * used to communicate with distant clusters
262 * @param conf configuration object to configure
263 * @param key string that contains the 3 required configuratins
265 private static void applyClusterKeyToConf(Configuration conf, String key)
266 throws IOException {
267 ZKConfig.ZKClusterKey zkClusterKey = ZKConfig.transformClusterKey(key);
268 conf.set(HConstants.ZOOKEEPER_QUORUM, zkClusterKey.getQuorumString());
269 conf.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, zkClusterKey.getClientPort());
270 conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, zkClusterKey.getZnodeParent());
271 // Without the right registry, the above configs are useless. Also, we don't use setClass()
272 // here because the ConnectionRegistry* classes are not resolvable from this module.
273 // This will be broken if ZkConnectionRegistry class gets renamed or moved. Is there a better
274 // way?
275 LOG.info("Overriding client registry implementation to {}",
276 HConstants.ZK_CONNECTION_REGISTRY_CLASS);
277 conf.set(HConstants.CLIENT_CONNECTION_REGISTRY_IMPL_CONF_KEY,
278 HConstants.ZK_CONNECTION_REGISTRY_CLASS);
282 * For debugging. Dump configurations to system output as xml format.
283 * Master and RS configurations can also be dumped using
284 * http services. e.g. "curl http://master:16010/dump"
286 public static void main(String[] args) throws Exception {
287 HBaseConfiguration.create().writeXml(System.out);