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.snapshot;
019
020import static org.junit.Assert.assertNull;
021
022import java.io.IOException;
023import java.io.UncheckedIOException;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtility;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.ResultScanner;
028import org.apache.hadoop.hbase.client.Table;
029import org.apache.hadoop.hbase.master.locking.LockManager.MasterLock;
030import org.apache.hadoop.hbase.master.locking.LockProcedure;
031import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
032import org.apache.hadoop.hbase.procedure2.LockType;
033import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
034import org.apache.hadoop.hbase.testclassification.MasterTests;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.junit.AfterClass;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043@Category({ MasterTests.class, MediumTests.class })
044public class TestSnapshotWhileRSCrashes {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestSnapshotWhileRSCrashes.class);
049
050  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
051
052  private static TableName NAME = TableName.valueOf("Cleanup");
053
054  private static byte[] CF = Bytes.toBytes("cf");
055
056  @BeforeClass
057  public static void setUp() throws Exception {
058    UTIL.startMiniCluster(3);
059    UTIL.createMultiRegionTable(NAME, CF);
060    UTIL.waitTableAvailable(NAME);
061  }
062
063  @AfterClass
064  public static void tearDown() throws Exception {
065    UTIL.shutdownMiniCluster();
066  }
067
068  @Test
069  public void test() throws InterruptedException, IOException {
070    String snName = "sn";
071    MasterLock lock = UTIL.getMiniHBaseCluster().getMaster().getLockManager().createMasterLock(NAME,
072      LockType.EXCLUSIVE, "for testing");
073    lock.acquire();
074    Thread t = new Thread(() -> {
075      try {
076        UTIL.getAdmin().snapshot(snName, NAME);
077      } catch (IOException e) {
078        throw new UncheckedIOException(e);
079      }
080    });
081    t.setDaemon(true);
082    t.start();
083    ProcedureExecutor<MasterProcedureEnv> procExec =
084      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
085    UTIL.waitFor(10000,
086      () -> procExec.getProcedures().stream().filter(p -> !p.isFinished())
087        .filter(p -> p instanceof LockProcedure).map(p -> (LockProcedure) p)
088        .filter(p -> NAME.equals(p.getTableName())).anyMatch(p -> !p.isLocked()));
089    UTIL.getMiniHBaseCluster().stopRegionServer(0);
090    lock.release();
091    // the snapshot can not work properly when there are rs crashes, so here we just want to make
092    // sure that the regions could online
093    try (Table table = UTIL.getConnection().getTable(NAME);
094      ResultScanner scanner = table.getScanner(CF)) {
095      assertNull(scanner.next());
096    }
097  }
098}