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.assertNotNull;
021
022import java.io.IOException;
023import org.apache.commons.lang3.StringUtils;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.RegionInfo;
029import org.apache.hadoop.hbase.client.Table;
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.CommonFSUtils;
034import org.apache.hadoop.hdfs.DFSClient;
035import org.junit.jupiter.api.AfterEach;
036import org.junit.jupiter.api.BeforeEach;
037import org.junit.jupiter.api.Tag;
038import org.junit.jupiter.api.Test;
039
040@Tag(RegionServerTests.TAG)
041@Tag(MediumTests.TAG)
042public class TestHdfsSnapshotHRegion {
043
044  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
045  private static final String SNAPSHOT_NAME = "foo_snapshot";
046  private Table table;
047  public static final TableName TABLE_NAME = TableName.valueOf("foo");
048  public static final byte[] FAMILY = Bytes.toBytes("f1");
049  private DFSClient client;
050  private String baseDir;
051
052  @BeforeEach
053  public void setUp() throws Exception {
054    Configuration c = TEST_UTIL.getConfiguration();
055    c.setBoolean("dfs.support.append", true);
056    TEST_UTIL.startMiniCluster(1);
057    table = TEST_UTIL.createMultiRegionTable(TABLE_NAME, FAMILY);
058    TEST_UTIL.loadTable(table, FAMILY);
059
060    // setup the hdfssnapshots
061    client = new DFSClient(TEST_UTIL.getDFSCluster().getURI(), TEST_UTIL.getConfiguration());
062    String fullUrIPath = TEST_UTIL.getDefaultRootDirPath().toString();
063    String uriString = TEST_UTIL.getTestFileSystem().getUri().toString();
064    baseDir = StringUtils.removeStart(fullUrIPath, uriString);
065    client.allowSnapshot(baseDir);
066  }
067
068  @AfterEach
069  public void tearDown() throws Exception {
070    client.deleteSnapshot(baseDir, SNAPSHOT_NAME);
071    TEST_UTIL.shutdownMiniCluster();
072  }
073
074  @Test
075  public void testOpeningReadOnlyRegionBasic() throws Exception {
076    String snapshotDir = client.createSnapshot(baseDir, SNAPSHOT_NAME);
077    RegionInfo firstRegion = TEST_UTIL.getConnection().getRegionLocator(table.getName())
078      .getAllRegionLocations().stream().findFirst().get().getRegion();
079    Path tableDir = CommonFSUtils.getTableDir(new Path(snapshotDir), TABLE_NAME);
080    HRegion snapshottedRegion = openSnapshotRegion(firstRegion, tableDir);
081    assertNotNull(snapshottedRegion);
082    snapshottedRegion.close();
083  }
084
085  @Test
086  public void testSnapshottingWithTmpSplitsAndMergeDirectoriesPresent() throws Exception {
087    // lets get a region and create those directories and make sure we ignore them
088    RegionInfo firstRegion = TEST_UTIL.getConnection().getRegionLocator(table.getName())
089      .getAllRegionLocations().stream().findFirst().get().getRegion();
090    String encodedName = firstRegion.getEncodedName();
091    Path tableDir = CommonFSUtils.getTableDir(TEST_UTIL.getDefaultRootDirPath(), TABLE_NAME);
092    Path regionDirectoryPath = new Path(tableDir, encodedName);
093    TEST_UTIL.getTestFileSystem()
094      .create(new Path(regionDirectoryPath, HRegionFileSystem.REGION_TEMP_DIR));
095    TEST_UTIL.getTestFileSystem()
096      .create(new Path(regionDirectoryPath, HRegionFileSystem.REGION_SPLITS_DIR));
097    TEST_UTIL.getTestFileSystem()
098      .create(new Path(regionDirectoryPath, HRegionFileSystem.REGION_MERGES_DIR));
099    // now snapshot
100    String snapshotDir = client.createSnapshot(baseDir, "foo_snapshot");
101    // everything should still open just fine
102    HRegion snapshottedRegion =
103      openSnapshotRegion(firstRegion, CommonFSUtils.getTableDir(new Path(snapshotDir), TABLE_NAME));
104    assertNotNull(snapshottedRegion); // no errors and the region should open
105    snapshottedRegion.close();
106  }
107
108  private HRegion openSnapshotRegion(RegionInfo firstRegion, Path tableDir) throws IOException {
109    return HRegion.openReadOnlyFileSystemHRegion(TEST_UTIL.getConfiguration(),
110      TEST_UTIL.getTestFileSystem(), tableDir, firstRegion, table.getDescriptor());
111  }
112}