View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.io.IOException;
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.fs.Path;
25  import org.apache.hadoop.hbase.HBaseTestingUtility;
26  import org.apache.hadoop.hbase.HRegionInfo;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.client.Table;
29  import org.apache.hadoop.hbase.testclassification.MediumTests;
30  import org.apache.hadoop.hbase.testclassification.RegionServerTests;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.apache.hadoop.hbase.util.FSUtils;
33  import org.apache.hadoop.hdfs.DFSClient;
34  import org.junit.After;
35  import org.junit.Assert;
36  import org.junit.Before;
37  import org.junit.Test;
38  import org.junit.experimental.categories.Category;
39  
40  @Category({RegionServerTests.class, MediumTests.class})
41  public class TestHdfsSnapshotHRegion {
42  
43    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
44    private static final String SNAPSHOT_NAME = "foo_snapshot";
45    private Table table;
46    public static final TableName TABLE_NAME = TableName.valueOf("foo");
47    public static final byte[] FAMILY = Bytes.toBytes("f1");
48    private DFSClient client;
49    private String baseDir;
50  
51  
52    @Before
53    public void setUp() throws Exception {
54      Configuration c = TEST_UTIL.getConfiguration();
55      c.setBoolean("dfs.support.append", true);
56      TEST_UTIL.startMiniCluster(1);
57      table = TEST_UTIL.createMultiRegionTable(TABLE_NAME, FAMILY);
58      TEST_UTIL.loadTable(table, FAMILY);
59  
60      // setup the hdfssnapshots
61      client = new DFSClient(TEST_UTIL.getDFSCluster().getURI(), TEST_UTIL.getConfiguration());
62      String fullUrIPath = TEST_UTIL.getDefaultRootDirPath().toString();
63      String uriString = TEST_UTIL.getTestFileSystem().getUri().toString();
64      baseDir = StringUtils.removeStart(fullUrIPath, uriString);
65      client.allowSnapshot(baseDir);
66    }
67  
68    @After
69    public void tearDown() throws Exception {
70      client.deleteSnapshot(baseDir, SNAPSHOT_NAME);
71      TEST_UTIL.shutdownMiniCluster();
72    }
73  
74    @Test
75    public void testOpeningReadOnlyRegionBasic() throws Exception {
76      String snapshotDir = client.createSnapshot(baseDir, SNAPSHOT_NAME);
77      HRegionInfo firstRegion = TEST_UTIL.getHBaseAdmin().getTableRegions(table.getName()).get(0);
78      Path tableDir = FSUtils.getTableDir(new Path(snapshotDir), TABLE_NAME);
79      HRegion snapshottedRegion = openSnapshotRegion(firstRegion, tableDir);
80      Assert.assertNotNull(snapshottedRegion);
81      snapshottedRegion.close();
82    }
83  
84    @Test
85    public void testSnapshottingWithTmpSplitsAndMergeDirectoriesPresent() throws Exception {
86      // lets get a region and create those directories and make sure we ignore them
87      HRegionInfo firstRegion = TEST_UTIL.getHBaseAdmin().getTableRegions(table.getName()).get(0);
88      String encodedName = firstRegion.getEncodedName();
89      Path tableDir = FSUtils.getTableDir(TEST_UTIL.getDefaultRootDirPath(), TABLE_NAME);
90      Path regionDirectoryPath = new Path(tableDir, encodedName);
91      TEST_UTIL.getTestFileSystem().create(
92          new Path(regionDirectoryPath, HRegionFileSystem.REGION_TEMP_DIR));
93      TEST_UTIL.getTestFileSystem().create(
94          new Path(regionDirectoryPath, HRegionFileSystem.REGION_SPLITS_DIR));
95      TEST_UTIL.getTestFileSystem().create(
96          new Path(regionDirectoryPath, HRegionFileSystem.REGION_MERGES_DIR));
97      // now snapshot
98      String snapshotDir = client.createSnapshot(baseDir, "foo_snapshot");
99      // everything should still open just fine
100     HRegion snapshottedRegion = openSnapshotRegion(firstRegion,
101         FSUtils.getTableDir(new Path(snapshotDir), TABLE_NAME));
102     Assert.assertNotNull(snapshottedRegion); // no errors and the region should open
103     snapshottedRegion.close();
104   }
105 
106   private HRegion openSnapshotRegion(HRegionInfo firstRegion, Path tableDir) throws IOException {
107     return HRegion.openReadOnlyFileSystemHRegion(
108         TEST_UTIL.getConfiguration(),
109         TEST_UTIL.getTestFileSystem(),
110         tableDir,
111         firstRegion,
112         table.getTableDescriptor()
113     );
114   }
115 }