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.procedure;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.util.List;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtil;
029import org.apache.hadoop.hbase.ServerName;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.RegionInfo;
032import org.apache.hadoop.hbase.client.SnapshotDescription;
033import org.apache.hadoop.hbase.client.SnapshotType;
034import org.apache.hadoop.hbase.client.Table;
035import org.apache.hadoop.hbase.master.HMaster;
036import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
037import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
038import org.apache.hadoop.hbase.procedure2.RemoteProcedureDispatcher;
039import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
040import org.apache.hadoop.hbase.snapshot.SnapshotManifestV2;
041import org.apache.hadoop.hbase.testclassification.MasterTests;
042import org.apache.hadoop.hbase.testclassification.MediumTests;
043import org.apache.hadoop.hbase.util.Bytes;
044import org.apache.hadoop.hbase.util.CommonFSUtils;
045import org.apache.hadoop.hbase.util.Pair;
046import org.apache.hadoop.hbase.util.RegionSplitter;
047import org.junit.After;
048import org.junit.Before;
049import org.junit.ClassRule;
050import org.junit.Test;
051import org.junit.experimental.categories.Category;
052import org.slf4j.Logger;
053import org.slf4j.LoggerFactory;
054
055import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
056import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos;
057
058@Category({ MasterTests.class, MediumTests.class })
059public class TestSnapshotRegionProcedure {
060  private static final Logger LOG = LoggerFactory.getLogger(TestSnapshotRegionProcedure.class);
061
062  @ClassRule
063  public static final HBaseClassTestRule CLASS_RULE =
064    HBaseClassTestRule.forClass(TestSnapshotRegionProcedure.class);
065
066  private static HBaseTestingUtil TEST_UTIL;
067  private HMaster master;
068  private TableName tableName;
069  private SnapshotProtos.SnapshotDescription snapshotProto;
070  private Path workingDir;
071  private FileSystem workingDirFs;
072
073  @Before
074  public void setup() throws Exception {
075    TEST_UTIL = new HBaseTestingUtil();
076    Configuration conf = TEST_UTIL.getConfiguration();
077    // disable info server. Info server is useful when we run unit tests locally, but it will
078    // fails integration testing of jenkins.
079    // conf.setInt(HConstants.MASTER_INFO_PORT, 8080);
080
081    // delay dispatch so that we can do something, for example kill a target server
082    conf.setInt(RemoteProcedureDispatcher.DISPATCH_DELAY_CONF_KEY, 10000);
083    conf.setInt(RemoteProcedureDispatcher.DISPATCH_MAX_QUEUE_SIZE_CONF_KEY, 128);
084    TEST_UTIL.startMiniCluster(3);
085    master = TEST_UTIL.getHBaseCluster().getMaster();
086    tableName = TableName.valueOf(Bytes.toBytes("SRPTestTable"));
087    byte[] cf = Bytes.toBytes("cf");
088    String SNAPSHOT_NAME = "SnapshotRegionProcedureTest";
089    SnapshotDescription snapshot =
090      new SnapshotDescription(SNAPSHOT_NAME, tableName, SnapshotType.FLUSH);
091    snapshotProto = ProtobufUtil.createHBaseProtosSnapshotDesc(snapshot);
092    snapshotProto = SnapshotDescriptionUtils.validate(snapshotProto, master.getConfiguration());
093    final byte[][] splitKeys = new RegionSplitter.HexStringSplit().split(10);
094    Table table = TEST_UTIL.createTable(tableName, cf, splitKeys);
095    TEST_UTIL.loadTable(table, cf, false);
096    Path rootDir = CommonFSUtils.getRootDir(conf);
097    this.workingDir = SnapshotDescriptionUtils.getWorkingSnapshotDir(snapshotProto, rootDir, conf);
098    this.workingDirFs = workingDir.getFileSystem(conf);
099    if (!workingDirFs.exists(workingDir)) {
100      workingDirFs.mkdirs(workingDir);
101    }
102  }
103
104  private boolean assertRegionManifestGenerated(RegionInfo region) throws Exception {
105    // path: /<root dir>/<snapshot dir>/<working dir>/<snapshot name>/region-manifest.<encode name>
106    String regionManifest = SnapshotManifestV2.SNAPSHOT_MANIFEST_PREFIX + region.getEncodedName();
107    Path targetPath = new Path(workingDir, regionManifest);
108    return workingDirFs.exists(targetPath);
109  }
110
111  @Test
112  public void testSimpleSnapshotRegion() throws Exception {
113    ProcedureExecutor<MasterProcedureEnv> procExec = master.getMasterProcedureExecutor();
114    List<Pair<RegionInfo, ServerName>> regions =
115      master.getAssignmentManager().getTableRegionsAndLocations(tableName, true);
116    assertEquals(10, regions.size());
117    Pair<RegionInfo, ServerName> region = regions.get(0);
118    SnapshotRegionProcedure srp = new SnapshotRegionProcedure(snapshotProto, region.getFirst());
119    long procId = procExec.submitProcedure(srp);
120    ProcedureTestingUtility.waitProcedure(procExec, procId);
121    assertTrue(assertRegionManifestGenerated(region.getFirst()));
122  }
123
124  @Test
125  public void testRegionServerCrashWhileTakingSnapshotRegion() throws Exception {
126    ProcedureExecutor<MasterProcedureEnv> procExec = master.getMasterProcedureExecutor();
127    List<Pair<RegionInfo, ServerName>> regions =
128      master.getAssignmentManager().getTableRegionsAndLocations(tableName, true);
129    assertEquals(10, regions.size());
130    Pair<RegionInfo, ServerName> pair = regions.get(0);
131    SnapshotRegionProcedure srp = new SnapshotRegionProcedure(snapshotProto, pair.getFirst());
132    long procId = procExec.submitProcedure(srp);
133    TEST_UTIL.getHBaseCluster().killRegionServer(pair.getSecond());
134    TEST_UTIL.waitFor(60000, () -> !pair.getSecond().equals(master.getAssignmentManager()
135      .getRegionStates().getRegionStateNode(pair.getFirst()).getRegionLocation()));
136    TEST_UTIL.waitFor(60000, () -> srp.inRetrying());
137    ProcedureTestingUtility.waitProcedure(procExec, procId);
138    assertTrue(assertRegionManifestGenerated(pair.getFirst()));
139  }
140
141  @After
142  public void teardown() throws Exception {
143    if (this.master != null) {
144      ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(master.getMasterProcedureExecutor(),
145        false);
146    }
147    TEST_UTIL.shutdownMiniCluster();
148  }
149}