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.awaitility.Awaitility.await;
021
022import java.io.IOException;
023import java.time.Duration;
024import java.util.Collections;
025import org.apache.hadoop.fs.FileStatus;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.RegionInfo;
030import org.apache.hadoop.hbase.regionserver.HRegionServer;
031import org.apache.hadoop.hbase.regionserver.Region;
032import org.apache.hadoop.hbase.testclassification.MasterTests;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.util.RecoverLeaseFSUtils;
035import org.junit.jupiter.api.AfterAll;
036import org.junit.jupiter.api.BeforeAll;
037import org.junit.jupiter.api.Tag;
038import org.junit.jupiter.api.Test;
039
040/**
041 * Testcase for HBASE-29797, where the lazy initialized WALProvider may recreate the WAL directory
042 * and cause our fencing way loses efficacy.
043 */
044@Tag(MasterTests.TAG)
045@Tag(MediumTests.TAG)
046public class TestWALFencing {
047
048  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
049
050  @BeforeAll
051  public static void setUp() throws Exception {
052    UTIL.startMiniCluster(3);
053    UTIL.getAdmin().balancerSwitch(false, true);
054  }
055
056  @AfterAll
057  public static void tearDown() throws IOException {
058    UTIL.shutdownMiniCluster();
059  }
060
061  @Test
062  public void testMoveMeta() throws Exception {
063    HRegionServer metaRs = UTIL.getRSForFirstRegionInTable(TableName.META_TABLE_NAME);
064    HRegionServer otherRs = UTIL.getOtherRegionServer(metaRs);
065    // do fencing here, i.e, kill otherRs
066    Path splittingDir = UTIL.getMiniHBaseCluster().getMaster().getMasterWalManager()
067      .getLogDirs(Collections.singleton(otherRs.getServerName())).get(0);
068    for (FileStatus walFile : otherRs.getWALFileSystem().listStatus(splittingDir)) {
069      RecoverLeaseFSUtils.recoverFileLease(otherRs.getWALFileSystem(), walFile.getPath(),
070        otherRs.getConfiguration());
071    }
072    // move meta region to otherRs, which should fail and crash otherRs, and then master will try to
073    // assign meta region to another rs
074    RegionInfo metaRegionInfo = metaRs.getRegions().stream().map(Region::getRegionInfo)
075      .filter(RegionInfo::isMetaRegion).findAny().get();
076    UTIL.getAdmin().move(metaRegionInfo.getRegionName(), otherRs.getServerName());
077    // make sure that meta region is not on otherRs
078    await().during(Duration.ofSeconds(5)).atMost(Duration.ofSeconds(6))
079      .until(() -> otherRs.getRegions(TableName.META_TABLE_NAME).isEmpty());
080  }
081}