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.assertArrayEquals;
021
022import java.io.IOException;
023import org.apache.hadoop.fs.Path;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.Get;
029import org.apache.hadoop.hbase.client.Put;
030import org.apache.hadoop.hbase.client.Table;
031import org.apache.hadoop.hbase.testclassification.MasterTests;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.apache.hadoop.hbase.util.CommonFSUtils;
035import org.junit.AfterClass;
036import org.junit.BeforeClass;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040
041/**
042 * Simulate the scenario described in HBASE-26245, where we clean the WAL directory and try to start
043 * the cluster.
044 */
045@Category({ MasterTests.class, MediumTests.class })
046public class TestRestartWithEmptyWALDirectory {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050    HBaseClassTestRule.forClass(TestRestartWithEmptyWALDirectory.class);
051
052  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
053
054  private static TableName NAME = TableName.valueOf("test");
055
056  private static byte[] FAMILY = Bytes.toBytes("family");
057
058  private static byte[] QUALIFIER = Bytes.toBytes("qualifier");
059
060  @BeforeClass
061  public static void setUp() throws Exception {
062    // in the test we shutdown the only master and after restarting its port will be changed, so the
063    // default rpc region server can not work
064    UTIL.getConfiguration().set(HConstants.CLIENT_CONNECTION_REGISTRY_IMPL_CONF_KEY,
065      HConstants.ZK_CONNECTION_REGISTRY_CLASS);
066    UTIL.startMiniCluster(1);
067    UTIL.createTable(NAME, FAMILY).close();
068    UTIL.waitTableAvailable(NAME);
069  }
070
071  @AfterClass
072  public static void tearDown() throws IOException {
073    UTIL.shutdownMiniCluster();
074  }
075
076  @Test
077  public void testRestart() throws IOException, InterruptedException {
078    byte[] row = Bytes.toBytes(0);
079    try (Table table = UTIL.getConnection().getTable(NAME)) {
080      table.put(new Put(row).addColumn(FAMILY, QUALIFIER, row));
081    }
082    // flush all in memory data
083    UTIL.flush(TableName.META_TABLE_NAME);
084    UTIL.flush(NAME);
085
086    // stop master first, so when stopping region server, we will not schedule a SCP.
087    UTIL.getMiniHBaseCluster().stopMaster(0).join();
088    UTIL.getMiniHBaseCluster().stopRegionServer(0).join();
089
090    // let's cleanup the WAL directory
091    UTIL.getTestFileSystem().delete(new Path(CommonFSUtils.getWALRootDir(UTIL.getConfiguration()),
092      HConstants.HREGION_LOGDIR_NAME), true);
093
094    // restart the cluster
095    UTIL.getMiniHBaseCluster().startMaster();
096    UTIL.getMiniHBaseCluster().startRegionServer();
097    UTIL.waitTableAvailable(NAME);
098
099    // the start up should succeed and the data should be persist
100    try (Table table = UTIL.getConnection().getTable(NAME)) {
101      assertArrayEquals(row, table.get(new Get(row)).getValue(FAMILY, QUALIFIER));
102    }
103  }
104}