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