View Javadoc

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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.hadoop.hbase.client;
19  
20  import java.io.IOException;
21  import java.nio.file.Files;
22  import java.nio.file.Path;
23  import java.nio.file.Paths;
24  import java.nio.file.StandardCopyOption;
25  import org.apache.hadoop.hbase.HBaseTestingUtility;
26  import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
27  
28  /**
29   * Base class to test Configuration Update logic. It wraps up things needed to
30   * test configuration change and provides utility methods for test cluster setup,
31   * updating/restoring configuration file.
32   */
33  public abstract class AbstractTestUpdateConfiguration {
34    private static final String SERVER_CONFIG = "hbase-site.xml";
35    private static final String OVERRIDE_SERVER_CONFIG  = "override-hbase-site.xml";
36    private static final String BACKUP_SERVER_CONFIG  = "backup-hbase-site.xml";
37  
38    private static Path configFileUnderTestDataDir;
39    private static Path overrideConfigFileUnderTestDataDir;
40    private static Path backupConfigFileUnderTestDataDir;
41  
42    protected static void setUpConfigurationFiles(final HBaseTestingUtility testUtil)
43      throws Exception {
44      // Before this change, the test will update hbase-site.xml under target/test-classes and
45      // trigger a config reload. Since target/test-classes/hbase-site.xml is being used by
46      // other testing cases at the same time, this update will break other testing cases so it will
47      // be flakey in nature.
48      // To avoid this, the change is to make target/test-classes/hbase-site.xml immutable. A new
49      // hbase-site.xml will be created under its test data directory, i.e,
50      // hbase-server/target/test-data/UUID, this new file will be added as a resource for the
51      // config, new update will be applied to this new file and only visible to this specific test
52      // case. The target/test-classes/hbase-site.xml will not be changed during the test.
53  
54      String absoluteDataPath = testUtil.getDataTestDir().toString();
55  
56      // Create test-data directories.
57      Files.createDirectories(Paths.get(absoluteDataPath));
58  
59      // Copy hbase-site.xml from target/test-class to target/test-data/UUID directory.
60      Path configFile = Paths.get("target", "test-classes", SERVER_CONFIG);
61      configFileUnderTestDataDir = Paths.get(absoluteDataPath, SERVER_CONFIG);
62      Files.copy(configFile, configFileUnderTestDataDir);
63  
64      // Copy override config file overrider-hbase-site.xml from target/test-class to
65      // target/test-data/UUID directory.
66      Path overrideConfigFile = Paths.get("target", "test-classes",
67        OVERRIDE_SERVER_CONFIG);
68      overrideConfigFileUnderTestDataDir = Paths.get(absoluteDataPath, OVERRIDE_SERVER_CONFIG);
69      Files.copy(overrideConfigFile, overrideConfigFileUnderTestDataDir);
70  
71      backupConfigFileUnderTestDataDir = Paths.get(absoluteDataPath, BACKUP_SERVER_CONFIG);
72  
73      // Add the new custom config file to Configuration
74      testUtil.getConfiguration().addResource(testUtil.getDataTestDir(SERVER_CONFIG));
75    }
76  
77    protected static void addResourceToRegionServerConfiguration(final HBaseTestingUtility testUtil) {
78      // When RegionServer is created in MiniHBaseCluster, it uses HBaseConfiguration.create(conf) of
79      // the master Configuration. The create() just copies config params over, it does not do
80      // a clone for a historic reason. Properties such as resources are lost during this process.
81      // Exposing a new method in HBaseConfiguration causes confusion. Instead, the new hbase-site.xml
82      // under test-data directory is added to RegionServer's configuration as a workaround.
83      for (RegionServerThread rsThread : testUtil.getMiniHBaseCluster().getRegionServerThreads()) {
84        rsThread.getRegionServer().getConfiguration().addResource(
85          testUtil.getDataTestDir(SERVER_CONFIG));
86      }
87    }
88  
89    /**
90     * Replace the hbase-site.xml file under this test's data directory with the content of the
91     * override-hbase-site.xml file. Stashes the current existing file so that it can be restored
92     * using {@link #restoreHBaseSiteXML()}.
93     *
94     * @throws IOException if an I/O error occurs
95     */
96    protected void replaceHBaseSiteXML() throws IOException {
97      // make a backup of hbase-site.xml
98      Files.copy(configFileUnderTestDataDir,
99        backupConfigFileUnderTestDataDir, StandardCopyOption.REPLACE_EXISTING);
100     // update hbase-site.xml by overwriting it
101     Files.copy(overrideConfigFileUnderTestDataDir,
102       configFileUnderTestDataDir, StandardCopyOption.REPLACE_EXISTING);
103   }
104 
105   /**
106    * Restores the hbase-site.xml file that was stashed by a previous call to
107    * {@link #replaceHBaseSiteXML()}.
108    *
109    * @throws IOException if an I/O error occurs
110    */
111   protected void restoreHBaseSiteXML() throws IOException {
112     // restore hbase-site.xml
113     Files.copy(backupConfigFileUnderTestDataDir,
114       configFileUnderTestDataDir, StandardCopyOption.REPLACE_EXISTING);
115   }
116 }