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.regionserver;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import java.io.IOException;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.Get;
026import org.apache.hadoop.hbase.client.Put;
027import org.apache.hadoop.hbase.client.Table;
028import org.apache.hadoop.hbase.master.HMaster;
029import org.apache.hadoop.hbase.master.ServerManager;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.apache.hadoop.hbase.testclassification.RegionServerTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
034import org.apache.hadoop.hbase.wal.WALFactory;
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-20742
042 */
043@Tag(RegionServerTests.TAG)
044@Tag(MediumTests.TAG)
045public class TestRegionServerCrashDisableWAL {
046
047  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
048
049  private static TableName TABLE_NAME = TableName.valueOf("test");
050
051  private static byte[] CF = Bytes.toBytes("cf");
052
053  private static byte[] CQ = Bytes.toBytes("cq");
054
055  @BeforeAll
056  public static void setUp() throws Exception {
057    UTIL.getConfiguration().setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1);
058    UTIL.getConfiguration().setBoolean(WALFactory.WAL_ENABLED, false);
059    UTIL.startMiniCluster(2);
060    UTIL.createTable(TABLE_NAME, CF);
061    UTIL.waitTableAvailable(TABLE_NAME);
062    HRegionServer rs = UTIL.getRSForFirstRegionInTable(TABLE_NAME);
063    if (!rs.getRegions(TableName.META_TABLE_NAME).isEmpty()) {
064      HRegionServer rs1 = UTIL.getOtherRegionServer(rs);
065      UTIL.moveRegionAndWait(
066        UTIL.getMiniHBaseCluster().getRegions(TABLE_NAME).get(0).getRegionInfo(),
067        rs1.getServerName());
068    }
069    UTIL.getAdmin().balancerSwitch(false, true);
070  }
071
072  @AfterAll
073  public static void tearDown() throws Exception {
074    UTIL.shutdownMiniCluster();
075  }
076
077  @Test
078  public void test() throws InterruptedException, IOException {
079    HMaster master = UTIL.getMiniHBaseCluster().stopMaster(0).getMaster();
080    // Shutdown master before shutting down rs
081    UTIL.waitFor(30000, () -> !master.isAlive());
082    RegionServerThread thread = null;
083    for (RegionServerThread t : UTIL.getMiniHBaseCluster().getRegionServerThreads()) {
084      if (!t.getRegionServer().getRegions(TABLE_NAME).isEmpty()) {
085        thread = t;
086        break;
087      }
088    }
089    // shutdown rs
090    thread.getRegionServer().abort("For testing");
091    thread.join();
092    // restart master
093    UTIL.getMiniHBaseCluster().startMaster();
094    // make sure that we can schedule a SCP for the crashed server which WAL is disabled and bring
095    // the region online.
096    try (Table table =
097      UTIL.getConnection().getTableBuilder(TABLE_NAME, null).setOperationTimeout(30000).build()) {
098      table.put(new Put(Bytes.toBytes(1)).addColumn(CF, CQ, Bytes.toBytes(1)));
099      assertEquals(1, Bytes.toInt(table.get(new Get(Bytes.toBytes(1))).getValue(CF, CQ)));
100    }
101  }
102}