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