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