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.mapreduce; 019 020import static org.junit.Assert.assertFalse; 021 022import java.io.IOException; 023import java.util.Arrays; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.fs.FileStatus; 026import org.apache.hadoop.fs.FileSystem; 027import org.apache.hadoop.fs.Path; 028import org.apache.hadoop.hbase.Cell; 029import org.apache.hadoop.hbase.CellScanner; 030import org.apache.hadoop.hbase.HBaseTestingUtility; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.client.Admin; 033import org.apache.hadoop.hbase.client.Result; 034import org.apache.hadoop.hbase.client.Table; 035import org.apache.hadoop.hbase.io.HFileLink; 036import org.apache.hadoop.hbase.io.ImmutableBytesWritable; 037import org.apache.hadoop.hbase.master.snapshot.SnapshotManager; 038import org.apache.hadoop.hbase.regionserver.StoreFileInfo; 039import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils; 040import org.apache.hadoop.hbase.util.Bytes; 041import org.apache.hadoop.hbase.util.FSUtils; 042import org.apache.hadoop.hbase.util.HFileArchiveUtil; 043import org.junit.Assert; 044import org.junit.Test; 045import org.slf4j.Logger; 046import org.slf4j.LoggerFactory; 047 048public abstract class TableSnapshotInputFormatTestBase { 049 private static final Logger LOG = LoggerFactory.getLogger(TableSnapshotInputFormatTestBase.class); 050 protected final HBaseTestingUtility UTIL = new HBaseTestingUtility(); 051 protected static final int NUM_REGION_SERVERS = 2; 052 protected static final byte[][] FAMILIES = {Bytes.toBytes("f1"), Bytes.toBytes("f2")}; 053 054 protected FileSystem fs; 055 protected Path rootDir; 056 057 public void setupCluster() throws Exception { 058 setupConf(UTIL.getConfiguration()); 059 UTIL.startMiniCluster(NUM_REGION_SERVERS, true); 060 rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir(); 061 fs = rootDir.getFileSystem(UTIL.getConfiguration()); 062 } 063 064 public void tearDownCluster() throws Exception { 065 UTIL.shutdownMiniCluster(); 066 } 067 068 private static void setupConf(Configuration conf) { 069 // Enable snapshot 070 conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true); 071 } 072 073 protected abstract void testWithMockedMapReduce(HBaseTestingUtility util, String snapshotName, 074 int numRegions, int numSplitsPerRegion, int expectedNumSplits, boolean setLocalityEnabledTo) 075 throws Exception; 076 077 protected abstract void testWithMapReduceImpl(HBaseTestingUtility util, TableName tableName, 078 String snapshotName, Path tableDir, int numRegions, int numSplitsPerRegion, 079 int expectedNumSplits, boolean shutdownCluster) throws Exception; 080 081 protected abstract byte[] getStartRow(); 082 083 protected abstract byte[] getEndRow(); 084 085 @Test 086 public void testWithMockedMapReduceSingleRegion() throws Exception { 087 testWithMockedMapReduce(UTIL, "testWithMockedMapReduceSingleRegion", 1, 1, 1, true); 088 } 089 090 @Test 091 public void testWithMockedMapReduceMultiRegion() throws Exception { 092 testWithMockedMapReduce(UTIL, "testWithMockedMapReduceMultiRegion", 10, 1, 8, false); 093 } 094 095 @Test 096 public void testWithMapReduceSingleRegion() throws Exception { 097 testWithMapReduce(UTIL, "testWithMapReduceSingleRegion", 1, 1, 1, false); 098 } 099 100 @Test 101 public void testWithMapReduceMultiRegion() throws Exception { 102 testWithMapReduce(UTIL, "testWithMapReduceMultiRegion", 10, 1, 8, false); 103 } 104 105 @Test 106 // run the MR job while HBase is offline 107 public void testWithMapReduceAndOfflineHBaseMultiRegion() throws Exception { 108 testWithMapReduce(UTIL, "testWithMapReduceAndOfflineHBaseMultiRegion", 10, 1, 8, true); 109 } 110 111 // Test that snapshot restore does not create back references in the HBase root dir. 112 @Test 113 public void testRestoreSnapshotDoesNotCreateBackRefLinks() throws Exception { 114 setupCluster(); 115 TableName tableName = TableName.valueOf("testRestoreSnapshotDoesNotCreateBackRefLinks"); 116 String snapshotName = "foo"; 117 118 try { 119 createTableAndSnapshot(UTIL, tableName, snapshotName, getStartRow(), getEndRow(), 1); 120 121 Path tmpTableDir = UTIL.getDataTestDirOnTestFS(snapshotName); 122 123 testRestoreSnapshotDoesNotCreateBackRefLinksInit(tableName, snapshotName,tmpTableDir); 124 125 Path rootDir = FSUtils.getRootDir(UTIL.getConfiguration()); 126 for (Path regionDir : FSUtils.getRegionDirs(fs, FSUtils.getTableDir(rootDir, tableName))) { 127 for (Path storeDir : FSUtils.getFamilyDirs(fs, regionDir)) { 128 for (FileStatus status : fs.listStatus(storeDir)) { 129 System.out.println(status.getPath()); 130 if (StoreFileInfo.isValid(status)) { 131 Path archiveStoreDir = HFileArchiveUtil.getStoreArchivePath(UTIL.getConfiguration(), 132 tableName, regionDir.getName(), storeDir.getName()); 133 134 Path path = HFileLink.getBackReferencesDir(storeDir, status.getPath().getName()); 135 // assert back references directory is empty 136 assertFalse("There is a back reference in " + path, fs.exists(path)); 137 138 path = HFileLink.getBackReferencesDir(archiveStoreDir, status.getPath().getName()); 139 // assert back references directory is empty 140 assertFalse("There is a back reference in " + path, fs.exists(path)); 141 } 142 } 143 } 144 } 145 } finally { 146 UTIL.getAdmin().deleteSnapshot(snapshotName); 147 UTIL.deleteTable(tableName); 148 tearDownCluster(); 149 } 150 } 151 152 public abstract void testRestoreSnapshotDoesNotCreateBackRefLinksInit(TableName tableName, 153 String snapshotName, Path tmpTableDir) throws Exception; 154 155 protected void testWithMapReduce(HBaseTestingUtility util, String snapshotName, 156 int numRegions, int numSplitsPerRegion, int expectedNumSplits, boolean shutdownCluster) 157 throws Exception { 158 setupCluster(); 159 try { 160 Path tableDir = util.getDataTestDirOnTestFS(snapshotName); 161 TableName tableName = TableName.valueOf("testWithMapReduce"); 162 testWithMapReduceImpl(util, tableName, snapshotName, tableDir, numRegions, 163 numSplitsPerRegion, expectedNumSplits, shutdownCluster); 164 } finally { 165 tearDownCluster(); 166 } 167 } 168 169 protected static void verifyRowFromMap(ImmutableBytesWritable key, Result result) 170 throws IOException { 171 byte[] row = key.get(); 172 CellScanner scanner = result.cellScanner(); 173 while (scanner.advance()) { 174 Cell cell = scanner.current(); 175 176 //assert that all Cells in the Result have the same key 177 Assert.assertEquals(0, Bytes.compareTo(row, 0, row.length, 178 cell.getRowArray(), cell.getRowOffset(), cell.getRowLength())); 179 } 180 181 for (byte[] family : FAMILIES) { 182 byte[] actual = result.getValue(family, family); 183 Assert.assertArrayEquals( 184 "Row in snapshot does not match, expected:" + Bytes.toString(row) + " ,actual:" + Bytes 185 .toString(actual), row, actual); 186 } 187 } 188 189 protected static void createTableAndSnapshot(HBaseTestingUtility util, TableName tableName, 190 String snapshotName, byte[] startRow, byte[] endRow, int numRegions) 191 throws Exception { 192 try { 193 LOG.debug("Ensuring table doesn't exist."); 194 util.deleteTable(tableName); 195 } catch(Exception ex) { 196 // ignore 197 } 198 199 LOG.info("creating table '" + tableName + "'"); 200 if (numRegions > 1) { 201 util.createTable(tableName, FAMILIES, 1, startRow, endRow, numRegions); 202 } else { 203 util.createTable(tableName, FAMILIES); 204 } 205 Admin admin = util.getAdmin(); 206 207 LOG.info("put some stuff in the table"); 208 Table table = util.getConnection().getTable(tableName); 209 util.loadTable(table, FAMILIES); 210 211 Path rootDir = FSUtils.getRootDir(util.getConfiguration()); 212 FileSystem fs = rootDir.getFileSystem(util.getConfiguration()); 213 214 LOG.info("snapshot"); 215 SnapshotTestingUtils.createSnapshotAndValidate(admin, tableName, 216 Arrays.asList(FAMILIES), null, snapshotName, rootDir, fs, true); 217 218 LOG.info("load different values"); 219 byte[] value = Bytes.toBytes("after_snapshot_value"); 220 util.loadTable(table, FAMILIES, value); 221 222 LOG.info("cause flush to create new files in the region"); 223 admin.flush(tableName); 224 table.close(); 225 } 226}