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.Admin;
30  import org.apache.hadoop.hbase.client.HTable;
31  import org.apache.hadoop.hbase.client.Result;
32  import org.apache.hadoop.hbase.client.Table;
33  import org.apache.hadoop.hbase.io.HFileLink;
34  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
35  import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
36  import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
37  import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.apache.hadoop.hbase.util.FSUtils;
40  import org.apache.hadoop.hbase.util.HFileArchiveUtil;
41  import org.junit.Assert;
42  import org.junit.Test;
43  
44  import static org.junit.Assert.assertFalse;
45  
46  import java.io.IOException;
47  import java.util.Arrays;
48  
49  public abstract class TableSnapshotInputFormatTestBase {
50  
51    protected final HBaseTestingUtility UTIL = new HBaseTestingUtility();
52    protected static final int NUM_REGION_SERVERS = 2;
53    protected static final byte[][] FAMILIES = {Bytes.toBytes("f1"), Bytes.toBytes("f2")};
54  
55    protected FileSystem fs;
56    protected Path rootDir;
57  
58    public void setupCluster() throws Exception {
59      setupConf(UTIL.getConfiguration());
60      UTIL.startMiniCluster(NUM_REGION_SERVERS, true);
61      rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
62      fs = rootDir.getFileSystem(UTIL.getConfiguration());
63    }
64  
65    public void tearDownCluster() throws Exception {
66      UTIL.shutdownMiniCluster();
67    }
68  
69    private static void setupConf(Configuration conf) {
70      // Enable snapshot
71      conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
72    }
73  
74    protected abstract void testWithMockedMapReduce(HBaseTestingUtility util, String snapshotName,
75      int numRegions, int expectedNumSplits) throws Exception;
76  
77    protected abstract void testWithMapReduceImpl(HBaseTestingUtility util, TableName tableName,
78      String snapshotName, Path tableDir, int numRegions, int expectedNumSplits,
79      boolean shutdownCluster) throws Exception;
80  
81    protected abstract byte[] getStartRow();
82  
83    protected abstract byte[] getEndRow();
84  
85    @Test
86    public void testWithMockedMapReduceSingleRegion() throws Exception {
87      testWithMockedMapReduce(UTIL, "testWithMockedMapReduceSingleRegion", 1, 1);
88    }
89  
90    @Test
91    public void testWithMockedMapReduceMultiRegion() throws Exception {
92      testWithMockedMapReduce(UTIL, "testWithMockedMapReduceMultiRegion", 10, 8);
93    }
94  
95    @Test
96    public void testWithMapReduceSingleRegion() throws Exception {
97      testWithMapReduce(UTIL, "testWithMapReduceSingleRegion", 1, 1, false);
98    }
99  
100   @Test
101   public void testWithMapReduceMultiRegion() throws Exception {
102     testWithMapReduce(UTIL, "testWithMapReduceMultiRegion", 10, 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, 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.getHBaseAdmin().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 expectedNumSplits, boolean shutdownCluster) throws Exception {
157     setupCluster();
158     util.startMiniMapReduceCluster();
159     try {
160       Path tableDir = util.getDataTestDirOnTestFS(snapshotName);
161       TableName tableName = TableName.valueOf("testWithMapReduce");
162       testWithMapReduceImpl(util, tableName, snapshotName, tableDir, numRegions,
163         expectedNumSplits, shutdownCluster);
164     } finally {
165       util.shutdownMiniMapReduceCluster();
166       tearDownCluster();
167     }
168   }
169 
170   protected static void verifyRowFromMap(ImmutableBytesWritable key, Result result)
171     throws IOException {
172     byte[] row = key.get();
173     CellScanner scanner = result.cellScanner();
174     while (scanner.advance()) {
175       Cell cell = scanner.current();
176 
177       //assert that all Cells in the Result have the same key
178       Assert.assertEquals(0, Bytes.compareTo(row, 0, row.length,
179         cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
180     }
181 
182     for (int j = 0; j < FAMILIES.length; j++) {
183       byte[] actual = result.getValue(FAMILIES[j], FAMILIES[j]);
184       Assert.assertArrayEquals("Row in snapshot does not match, expected:" + Bytes.toString(row)
185         + " ,actual:" + Bytes.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       util.deleteTable(tableName);
194     } catch(Exception ex) {
195       // ignore
196     }
197 
198     if (numRegions > 1) {
199       util.createTable(tableName, FAMILIES, 1, startRow, endRow, numRegions);
200     } else {
201       util.createTable(tableName, FAMILIES);
202     }
203     Admin admin = util.getHBaseAdmin();
204 
205     // put some stuff in the table
206     HTable table = new HTable(util.getConfiguration(), tableName);
207     util.loadTable(table, FAMILIES);
208 
209     Path rootDir = FSUtils.getRootDir(util.getConfiguration());
210     FileSystem fs = rootDir.getFileSystem(util.getConfiguration());
211 
212     SnapshotTestingUtils.createSnapshotAndValidate(admin, tableName,
213       Arrays.asList(FAMILIES), null, snapshotName, rootDir, fs, true);
214 
215     // load different values
216     byte[] value = Bytes.toBytes("after_snapshot_value");
217     util.loadTable(table, FAMILIES, value);
218 
219     // cause flush to create new files in the region
220     admin.flush(tableName);
221     table.close();
222   }
223 
224 }