Repository: hbase
Updated Branches:
  refs/heads/branch-1 5cb3ab85a -> 73ed12703


HBASE-15321 - Ability to open a HRegion from hdfs snapshot.


Project: http://git-wip-us.apache.org/repos/asf/hbase/repo
Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/73ed1270
Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/73ed1270
Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/73ed1270

Branch: refs/heads/branch-1
Commit: 73ed127039c255bd97ceaa58a4f20252ba74d59b
Parents: 5cb3ab8
Author: Rahul Gidwani <chu...@apache.org>
Authored: Mon Jan 22 12:22:57 2018 -0800
Committer: Rahul Gidwani <chu...@apache.org>
Committed: Mon Jan 22 12:22:57 2018 -0800

----------------------------------------------------------------------
 .../hadoop/hbase/regionserver/HRegion.java      |  25 ++++
 .../hbase/regionserver/HRegionFileSystem.java   |   3 +-
 .../regionserver/TestHdfsSnapshotHRegion.java   | 115 +++++++++++++++++++
 3 files changed, 142 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hbase/blob/73ed1270/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java
----------------------------------------------------------------------
diff --git 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java
index c006a44..e95a392 100644
--- 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java
+++ 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java
@@ -6998,6 +6998,31 @@ public class HRegion implements HeapSize, 
PropagatingConfigurationObserver, Regi
     return this;
   }
 
+  /**
+   * Open a Region on a read-only file-system (like hdfs snapshots)
+   * @param conf The Configuration object to use.
+   * @param fs Filesystem to use
+   * @param info Info for region to be opened.
+   * @param htd the table descriptor
+   * @return new HRegion
+   * @throws IOException e
+   */
+  public static HRegion openReadOnlyFileSystemHRegion(final Configuration 
conf, final FileSystem fs,
+      final Path tableDir, HRegionInfo info, final HTableDescriptor htd) 
throws IOException {
+    if (info == null) {
+      throw new NullPointerException("Passed region info is null");
+    }
+    if (LOG.isDebugEnabled()) {
+      LOG.debug("Opening region (readOnly filesystem): " + info);
+    }
+    if (info.getReplicaId() <= 0) {
+      info = new HRegionInfo((HRegionInfo) info, 1);
+    }
+    HRegion r = HRegion.newHRegion(tableDir, null, fs, conf, info, htd, null);
+    r.writestate.setReadOnly(true);
+    return r.openHRegion(null);
+  }
+
   public static void warmupHRegion(final HRegionInfo info,
       final HTableDescriptor htd, final WAL wal, final Configuration conf,
       final RegionServerServices rsServices,

http://git-wip-us.apache.org/repos/asf/hbase/blob/73ed1270/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionFileSystem.java
----------------------------------------------------------------------
diff --git 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionFileSystem.java
 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionFileSystem.java
index 3a0b30a..884485c 100644
--- 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionFileSystem.java
+++ 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionFileSystem.java
@@ -28,6 +28,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
+import com.google.common.annotations.VisibleForTesting;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.hbase.classification.InterfaceAudience;
@@ -75,7 +76,7 @@ public class HRegionFileSystem {
   public static final String REGION_SPLITS_DIR = ".splits";
 
   /** Temporary subdirectory of the region directory used for compaction 
output. */
-  private static final String REGION_TEMP_DIR = ".tmp";
+  @VisibleForTesting static final String REGION_TEMP_DIR = ".tmp";
 
   private final HRegionInfo regionInfo;
   //regionInfo for interacting with FS (getting encodedName, etc)

http://git-wip-us.apache.org/repos/asf/hbase/blob/73ed1270/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestHdfsSnapshotHRegion.java
----------------------------------------------------------------------
diff --git 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestHdfsSnapshotHRegion.java
 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestHdfsSnapshotHRegion.java
new file mode 100644
index 0000000..64c3735
--- /dev/null
+++ 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestHdfsSnapshotHRegion.java
@@ -0,0 +1,115 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.regionserver;
+
+import java.io.IOException;
+import org.apache.commons.lang.StringUtils;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hbase.HBaseTestingUtility;
+import org.apache.hadoop.hbase.HRegionInfo;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.client.Table;
+import org.apache.hadoop.hbase.testclassification.MediumTests;
+import org.apache.hadoop.hbase.testclassification.RegionServerTests;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hadoop.hbase.util.FSUtils;
+import org.apache.hadoop.hdfs.DFSClient;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+@Category({RegionServerTests.class, MediumTests.class})
+public class TestHdfsSnapshotHRegion {
+
+  private static final HBaseTestingUtility TEST_UTIL = new 
HBaseTestingUtility();
+  private static final String SNAPSHOT_NAME = "foo_snapshot";
+  private Table table;
+  public static final TableName TABLE_NAME = TableName.valueOf("foo");
+  public static final byte[] FAMILY = Bytes.toBytes("f1");
+  private DFSClient client;
+  private String baseDir;
+
+
+  @Before
+  public void setUp() throws Exception {
+    Configuration c = TEST_UTIL.getConfiguration();
+    c.setBoolean("dfs.support.append", true);
+    TEST_UTIL.startMiniCluster(1);
+    table = TEST_UTIL.createMultiRegionTable(TABLE_NAME, FAMILY);
+    TEST_UTIL.loadTable(table, FAMILY);
+
+    // setup the hdfssnapshots
+    client = new DFSClient(TEST_UTIL.getDFSCluster().getURI(), 
TEST_UTIL.getConfiguration());
+    String fullUrIPath = TEST_UTIL.getDefaultRootDirPath().toString();
+    String uriString = TEST_UTIL.getTestFileSystem().getUri().toString();
+    baseDir = StringUtils.removeStart(fullUrIPath, uriString);
+    client.allowSnapshot(baseDir);
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    client.deleteSnapshot(baseDir, SNAPSHOT_NAME);
+    TEST_UTIL.shutdownMiniCluster();
+  }
+
+  @Test
+  public void testOpeningReadOnlyRegionBasic() throws Exception {
+    String snapshotDir = client.createSnapshot(baseDir, SNAPSHOT_NAME);
+    HRegionInfo firstRegion = 
TEST_UTIL.getHBaseAdmin().getTableRegions(table.getName()).get(0);
+    Path tableDir = FSUtils.getTableDir(new Path(snapshotDir), TABLE_NAME);
+    HRegion snapshottedRegion = openSnapshotRegion(firstRegion, tableDir);
+    Assert.assertNotNull(snapshottedRegion);
+    snapshottedRegion.close();
+  }
+
+  @Test
+  public void testSnapshottingWithTmpSplitsAndMergeDirectoriesPresent() throws 
Exception {
+    // lets get a region and create those directories and make sure we ignore 
them
+    HRegionInfo firstRegion = 
TEST_UTIL.getHBaseAdmin().getTableRegions(table.getName()).get(0);
+    String encodedName = firstRegion.getEncodedName();
+    Path tableDir = FSUtils.getTableDir(TEST_UTIL.getDefaultRootDirPath(), 
TABLE_NAME);
+    Path regionDirectoryPath = new Path(tableDir, encodedName);
+    TEST_UTIL.getTestFileSystem().create(
+        new Path(regionDirectoryPath, HRegionFileSystem.REGION_TEMP_DIR));
+    TEST_UTIL.getTestFileSystem().create(
+        new Path(regionDirectoryPath, HRegionFileSystem.REGION_SPLITS_DIR));
+    TEST_UTIL.getTestFileSystem().create(
+        new Path(regionDirectoryPath, HRegionFileSystem.REGION_MERGES_DIR));
+    // now snapshot
+    String snapshotDir = client.createSnapshot(baseDir, "foo_snapshot");
+    // everything should still open just fine
+    HRegion snapshottedRegion = openSnapshotRegion(firstRegion,
+        FSUtils.getTableDir(new Path(snapshotDir), TABLE_NAME));
+    Assert.assertNotNull(snapshottedRegion); // no errors and the region 
should open
+    snapshottedRegion.close();
+  }
+
+  private HRegion openSnapshotRegion(HRegionInfo firstRegion, Path tableDir) 
throws IOException {
+    return HRegion.openReadOnlyFileSystemHRegion(
+        TEST_UTIL.getConfiguration(),
+        TEST_UTIL.getTestFileSystem(),
+        tableDir,
+        firstRegion,
+        table.getTableDescriptor()
+    );
+  }
+}

Reply via email to