001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.master; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertTrue; 023 024import java.io.IOException; 025import java.time.Duration; 026import java.util.List; 027import org.apache.hadoop.fs.Path; 028import org.apache.hadoop.hbase.Cell; 029import org.apache.hadoop.hbase.HBaseClassTestRule; 030import org.apache.hadoop.hbase.HBaseTestingUtil; 031import org.apache.hadoop.hbase.ServerName; 032import org.apache.hadoop.hbase.SingleProcessHBaseCluster; 033import org.apache.hadoop.hbase.StartTestingClusterOption; 034import org.apache.hadoop.hbase.TableName; 035import org.apache.hadoop.hbase.Waiter; 036import org.apache.hadoop.hbase.client.Get; 037import org.apache.hadoop.hbase.client.Put; 038import org.apache.hadoop.hbase.client.RegionInfo; 039import org.apache.hadoop.hbase.client.Result; 040import org.apache.hadoop.hbase.client.Table; 041import org.apache.hadoop.hbase.regionserver.HRegionServer; 042import org.apache.hadoop.hbase.testclassification.LargeTests; 043import org.apache.hadoop.hbase.util.Bytes; 044import org.apache.hadoop.hbase.util.CommonFSUtils; 045import org.apache.hadoop.hbase.zookeeper.ZKUtil; 046import org.junit.After; 047import org.junit.Before; 048import org.junit.ClassRule; 049import org.junit.Rule; 050import org.junit.Test; 051import org.junit.experimental.categories.Category; 052import org.junit.rules.TestName; 053 054/** 055 * Test reuse storefiles within data directory when cluster failover with a set of new region 056 * servers with different hostnames with or without WALs and Zookeeper ZNodes, the master and 057 * cluster should fail respectively if there is any situation considered as not supported. 058 */ 059@Category({ LargeTests.class }) 060public class TestRecreateCluster { 061 @ClassRule 062 public static final HBaseClassTestRule CLASS_RULE = 063 HBaseClassTestRule.forClass(TestRecreateCluster.class); 064 065 @Rule 066 public TestName name = new TestName(); 067 068 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 069 private static final int NUM_RS = 3; 070 private static final long TIMEOUT_MS = Duration.ofMinutes(1).toMillis(); 071 private static final long MASTER_INIT_TIMEOUT_MS = Duration.ofSeconds(45).toMillis(); 072 073 @Before 074 public void setup() throws Exception { 075 TEST_UTIL.getConfiguration().setLong("hbase.master.init.timeout.localHBaseCluster", 076 MASTER_INIT_TIMEOUT_MS); 077 TEST_UTIL.startMiniCluster(StartTestingClusterOption.builder().numRegionServers(NUM_RS) 078 .numDataNodes(NUM_RS).createWALDir(true).build()); 079 } 080 081 @After 082 public void tearDown() throws Exception { 083 TEST_UTIL.shutdownMiniCluster(); 084 } 085 086 @Test 087 public void testRecreateCluster_UserTableDisabled_ReuseWALsAndZNodes() throws Exception { 088 validateRecreateClusterWithUserDisabled(false, false); 089 } 090 091 @Test 092 public void testRecreateCluster_UserTableEnabled_ReuseWALsAndZNodes() throws Exception { 093 validateRecreateClusterWithUserTableEnabled(false, false); 094 } 095 096 @Test 097 public void testRecreateCluster_UserTableEnabled_CleanupZNodes() throws Exception { 098 // this is no longer failing because master region stores the information the region servers 099 // as long as it's gracefully flushed before shutdown 100 validateRecreateClusterWithUserTableEnabled(false, true); 101 } 102 103 @Test 104 public void testRecreateCluster_UserTableEnabled_CleanupWALAndZNodes() throws Exception { 105 validateRecreateClusterWithUserTableEnabled(true, true); 106 } 107 108 private void validateRecreateClusterWithUserDisabled(boolean cleanupWALs, boolean cleanUpZNodes) 109 throws Exception { 110 TableName tableName = TableName.valueOf("t1"); 111 prepareDataBeforeRecreate(TEST_UTIL, tableName); 112 TEST_UTIL.getAdmin().disableTable(tableName); 113 TEST_UTIL.waitTableDisabled(tableName.getName()); 114 restartHBaseCluster(cleanupWALs, cleanUpZNodes); 115 TEST_UTIL.getAdmin().enableTable(tableName); 116 validateDataAfterRecreate(TEST_UTIL, tableName); 117 } 118 119 private void validateRecreateClusterWithUserTableEnabled(boolean cleanupWALs, 120 boolean cleanUpZNodes) throws Exception { 121 TableName tableName = TableName.valueOf("t1"); 122 prepareDataBeforeRecreate(TEST_UTIL, tableName); 123 restartHBaseCluster(cleanupWALs, cleanUpZNodes); 124 validateDataAfterRecreate(TEST_UTIL, tableName); 125 } 126 127 private void restartHBaseCluster(boolean cleanUpWALs, boolean cleanUpZnodes) throws Exception { 128 // flush cache so that everything is on disk 129 TEST_UTIL.getMiniHBaseCluster().flushcache(TableName.META_TABLE_NAME); 130 TEST_UTIL.getMiniHBaseCluster().flushcache(); 131 132 List<ServerName> oldServers = 133 TEST_UTIL.getHBaseCluster().getMaster().getServerManager().getOnlineServersList(); 134 135 // make sure there is no procedures pending 136 TEST_UTIL.waitFor(TIMEOUT_MS, () -> TEST_UTIL.getHBaseCluster().getMaster().getProcedures() 137 .stream().filter(p -> p.isFinished()).findAny().isPresent()); 138 139 // shutdown and delete data if needed 140 Path walRootDirPath = TEST_UTIL.getMiniHBaseCluster().getMaster().getWALRootDir(); 141 Path rootDirPath = CommonFSUtils.getRootDir(TEST_UTIL.getConfiguration()); 142 TEST_UTIL.shutdownMiniHBaseCluster(); 143 144 if (cleanUpWALs) { 145 TEST_UTIL.getDFSCluster().getFileSystem().delete(walRootDirPath, true); 146 } 147 148 if (cleanUpZnodes) { 149 // delete all zk data 150 // we cannot keep ZK data because it will hold the meta region states as open and 151 // didn't submit a InitMetaProcedure 152 ZKUtil.deleteChildrenRecursively(TEST_UTIL.getZooKeeperWatcher(), 153 TEST_UTIL.getZooKeeperWatcher().getZNodePaths().baseZNode); 154 TEST_UTIL.shutdownMiniZKCluster(); 155 TEST_UTIL.startMiniZKCluster(); 156 } 157 158 TEST_UTIL.restartHBaseCluster(NUM_RS); 159 TEST_UTIL.waitFor(TIMEOUT_MS, new Waiter.Predicate<Exception>() { 160 @Override 161 public boolean evaluate() throws Exception { 162 return TEST_UTIL.getMiniHBaseCluster().getNumLiveRegionServers() == NUM_RS; 163 } 164 }); 165 166 // make sure we have a new set of region servers with different hostnames and ports 167 List<ServerName> newServers = 168 TEST_UTIL.getHBaseCluster().getMaster().getServerManager().getOnlineServersList(); 169 assertFalse(newServers.stream().filter((newServer) -> oldServers.contains(newServer)).findAny() 170 .isPresent()); 171 } 172 173 private void prepareDataBeforeRecreate(HBaseTestingUtil testUtil, TableName tableName) 174 throws Exception { 175 Table table = testUtil.createTable(tableName, "f"); 176 Put put = new Put(Bytes.toBytes("r1")); 177 put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("c"), Bytes.toBytes("v")); 178 table.put(put); 179 180 ensureTableNotColocatedWithSystemTable(tableName, TableName.META_TABLE_NAME); 181 } 182 183 private void ensureTableNotColocatedWithSystemTable(TableName userTable, TableName systemTable) 184 throws IOException, InterruptedException { 185 SingleProcessHBaseCluster hbaseCluster = TEST_UTIL.getHBaseCluster(); 186 assertTrue("Please start more than 1 regionserver", 187 hbaseCluster.getRegionServerThreads().size() > 1); 188 int userTableServerNum = getServerNumForTableWithOnlyOneRegion(userTable); 189 int systemTableServerNum = getServerNumForTableWithOnlyOneRegion(systemTable); 190 191 if (userTableServerNum != systemTableServerNum) { 192 // no-ops if user table and system are already on a different host 193 return; 194 } 195 196 int destServerNum = (systemTableServerNum + 1) % NUM_RS; 197 assertTrue(systemTableServerNum != destServerNum); 198 199 HRegionServer systemTableServer = hbaseCluster.getRegionServer(systemTableServerNum); 200 HRegionServer destServer = hbaseCluster.getRegionServer(destServerNum); 201 assertTrue(!systemTableServer.equals(destServer)); 202 // make sure the dest server is live before moving region 203 hbaseCluster.waitForRegionServerToStart(destServer.getServerName().getHostname(), 204 destServer.getServerName().getPort(), TIMEOUT_MS); 205 // move region of userTable to a different regionserver not co-located with system table 206 TEST_UTIL.moveRegionAndWait(TEST_UTIL.getAdmin().getRegions(userTable).get(0), 207 destServer.getServerName()); 208 } 209 210 private int getServerNumForTableWithOnlyOneRegion(TableName tableName) throws IOException { 211 List<RegionInfo> tableRegionInfos = TEST_UTIL.getAdmin().getRegions(tableName); 212 assertEquals(1, tableRegionInfos.size()); 213 return TEST_UTIL.getHBaseCluster().getServerWith(tableRegionInfos.get(0).getRegionName()); 214 } 215 216 private void validateDataAfterRecreate(HBaseTestingUtil testUtil, TableName tableName) 217 throws Exception { 218 Table t1 = testUtil.getConnection().getTable(tableName); 219 Get get = new Get(Bytes.toBytes("r1")); 220 get.addColumn(Bytes.toBytes("f"), Bytes.toBytes("c")); 221 Result result = t1.get(get); 222 assertTrue(result.advance()); 223 Cell cell = result.current(); 224 assertEquals("v", 225 Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); 226 assertFalse(result.advance()); 227 } 228}