View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.mapreduce;
20  
21  import org.apache.hadoop.conf.Configuration;
22  import org.apache.hadoop.fs.FileStatus;
23  import org.apache.hadoop.fs.FileSystem;
24  import org.apache.hadoop.fs.Path;
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.CellScanner;
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.client.HBaseAdmin;
30  import org.apache.hadoop.hbase.client.HTable;
31  import org.apache.hadoop.hbase.client.Result;
32  import org.apache.hadoop.hbase.io.HFileLink;
33  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
34  import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
35  import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
36  import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.apache.hadoop.hbase.util.FSUtils;
39  import org.apache.hadoop.hbase.util.HFileArchiveUtil;
40  import org.junit.Assert;
41  import org.junit.Test;
42  
43  import static org.junit.Assert.assertFalse;
44  
45  import java.io.IOException;
46  import java.util.Arrays;
47  
48  public abstract class TableSnapshotInputFormatTestBase {
49  
50    protected final HBaseTestingUtility UTIL = new HBaseTestingUtility();
51    protected static final int NUM_REGION_SERVERS = 2;
52    protected static final byte[][] FAMILIES = {Bytes.toBytes("f1"), Bytes.toBytes("f2")};
53  
54    protected FileSystem fs;
55    protected Path rootDir;
56  
57    public void setupCluster() throws Exception {
58      setupConf(UTIL.getConfiguration());
59      UTIL.startMiniCluster(NUM_REGION_SERVERS);
60      rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
61      fs = rootDir.getFileSystem(UTIL.getConfiguration());
62    }
63  
64    public void tearDownCluster() throws Exception {
65      UTIL.shutdownMiniCluster();
66    }
67  
68    private static void setupConf(Configuration conf) {
69      // Enable snapshot
70      conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
71    }
72  
73    protected abstract void testWithMockedMapReduce(HBaseTestingUtility util, String snapshotName,
74      int numRegions, int expectedNumSplits) throws Exception;
75  
76    protected abstract void testWithMapReduceImpl(HBaseTestingUtility util, TableName tableName,
77      String snapshotName, Path tableDir, int numRegions, int expectedNumSplits,
78      boolean shutdownCluster) throws Exception;
79  
80    protected abstract byte[] getStartRow();
81  
82    protected abstract byte[] getEndRow();
83  
84    @Test
85    public void testWithMockedMapReduceSingleRegion() throws Exception {
86      testWithMockedMapReduce(UTIL, "testWithMockedMapReduceSingleRegion", 1, 1);
87    }
88  
89    @Test
90    public void testWithMockedMapReduceMultiRegion() throws Exception {
91      testWithMockedMapReduce(UTIL, "testWithMockedMapReduceMultiRegion", 10, 8);
92    }
93  
94    @Test
95    public void testWithMapReduceSingleRegion() throws Exception {
96      testWithMapReduce(UTIL, "testWithMapReduceSingleRegion", 1, 1, false);
97    }
98  
99    @Test
100   public void testWithMapReduceMultiRegion() throws Exception {
101     testWithMapReduce(UTIL, "testWithMapReduceMultiRegion", 10, 8, false);
102   }
103 
104   @Test
105   // run the MR job while HBase is offline
106   public void testWithMapReduceAndOfflineHBaseMultiRegion() throws Exception {
107     testWithMapReduce(UTIL, "testWithMapReduceAndOfflineHBaseMultiRegion", 10, 8, true);
108   }
109 
110   // Test that snapshot restore does not create back references in the HBase root dir.
111   @Test
112   public void testRestoreSnapshotDoesNotCreateBackRefLinks() throws Exception {
113     setupCluster();
114     TableName tableName = TableName.valueOf("testRestoreSnapshotDoesNotCreateBackRefLinks");
115     String snapshotName = "foo";
116 
117     try {
118       createTableAndSnapshot(UTIL, tableName, snapshotName, getStartRow(), getEndRow(), 1);
119 
120       Path tmpTableDir = UTIL.getDataTestDirOnTestFS(snapshotName);
121 
122       testRestoreSnapshotDoesNotCreateBackRefLinksInit(tableName, snapshotName,tmpTableDir);
123 
124       Path rootDir = FSUtils.getRootDir(UTIL.getConfiguration());
125       for (Path regionDir : FSUtils.getRegionDirs(fs, FSUtils.getTableDir(rootDir, tableName))) {
126         for (Path storeDir : FSUtils.getFamilyDirs(fs, regionDir)) {
127           for (FileStatus status : fs.listStatus(storeDir)) {
128             System.out.println(status.getPath());
129             if (StoreFileInfo.isValid(status)) {
130               Path archiveStoreDir = HFileArchiveUtil.getStoreArchivePath(UTIL.getConfiguration(),
131                 tableName, regionDir.getName(), storeDir.getName());
132 
133               Path path = HFileLink.getBackReferencesDir(storeDir, status.getPath().getName());
134               // assert back references directory is empty
135               assertFalse("There is a back reference in " + path, fs.exists(path));
136 
137               path = HFileLink.getBackReferencesDir(archiveStoreDir, status.getPath().getName());
138               // assert back references directory is empty
139               assertFalse("There is a back reference in " + path, fs.exists(path));
140             }
141           }
142         }
143       }
144     } finally {
145       UTIL.getHBaseAdmin().deleteSnapshot(snapshotName);
146       UTIL.deleteTable(tableName);
147       tearDownCluster();
148     }
149   }
150 
151   public abstract void testRestoreSnapshotDoesNotCreateBackRefLinksInit(TableName tableName,
152       String snapshotName, Path tmpTableDir) throws Exception;
153 
154   protected void testWithMapReduce(HBaseTestingUtility util, String snapshotName,
155       int numRegions, int expectedNumSplits, boolean shutdownCluster) throws Exception {
156     setupCluster();
157     util.startMiniMapReduceCluster();
158     try {
159       Path tableDir = util.getDataTestDirOnTestFS(snapshotName);
160       TableName tableName = TableName.valueOf("testWithMapReduce");
161       testWithMapReduceImpl(util, tableName, snapshotName, tableDir, numRegions,
162         expectedNumSplits, shutdownCluster);
163     } finally {
164       util.shutdownMiniMapReduceCluster();
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 (int j = 0; j < FAMILIES.length; j++) {
182       byte[] actual = result.getValue(FAMILIES[j], null);
183       Assert.assertArrayEquals("Row in snapshot does not match, expected:" + Bytes.toString(row)
184         + " ,actual:" + Bytes.toString(actual), row, actual);
185     }
186   }
187 
188   protected static void createTableAndSnapshot(HBaseTestingUtility util, TableName tableName,
189     String snapshotName, byte[] startRow, byte[] endRow, int numRegions)
190     throws Exception {
191     try {
192       util.deleteTable(tableName);
193     } catch(Exception ex) {
194       // ignore
195     }
196 
197     if (numRegions > 1) {
198       util.createTable(tableName, FAMILIES, 1, startRow, endRow, numRegions);
199     } else {
200       util.createTable(tableName, FAMILIES);
201     }
202     HBaseAdmin admin = util.getHBaseAdmin();
203 
204     // put some stuff in the table
205     HTable table = new HTable(util.getConfiguration(), tableName);
206     util.loadTable(table, FAMILIES);
207 
208     Path rootDir = FSUtils.getRootDir(util.getConfiguration());
209     FileSystem fs = rootDir.getFileSystem(util.getConfiguration());
210 
211     SnapshotTestingUtils.createSnapshotAndValidate(admin, tableName,
212       Arrays.asList(FAMILIES), null, snapshotName, rootDir, fs, true);
213 
214     // load different values
215     byte[] value = Bytes.toBytes("after_snapshot_value");
216     util.loadTable(table, FAMILIES, value);
217 
218     // cause flush to create new files in the region
219     admin.flush(tableName.toString());
220     table.close();
221   }
222 
223 }