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.FileSystem;
23  import org.apache.hadoop.fs.Path;
24  import org.apache.hadoop.hbase.Cell;
25  import org.apache.hadoop.hbase.CellScanner;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.client.HBaseAdmin;
29  import org.apache.hadoop.hbase.client.HTable;
30  import org.apache.hadoop.hbase.client.Result;
31  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
32  import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
33  import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.apache.hadoop.hbase.util.FSUtils;
36  import org.junit.Assert;
37  import org.junit.Test;
38  
39  import java.io.IOException;
40  import java.util.Arrays;
41  
42  public abstract class TableSnapshotInputFormatTestBase {
43  
44    protected final HBaseTestingUtility UTIL = new HBaseTestingUtility();
45    protected static final int NUM_REGION_SERVERS = 2;
46    protected static final byte[][] FAMILIES = {Bytes.toBytes("f1"), Bytes.toBytes("f2")};
47  
48    protected FileSystem fs;
49    protected Path rootDir;
50  
51    public void setupCluster() throws Exception {
52      setupConf(UTIL.getConfiguration());
53      UTIL.startMiniCluster(NUM_REGION_SERVERS);
54      rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
55      fs = rootDir.getFileSystem(UTIL.getConfiguration());
56    }
57  
58    public void tearDownCluster() throws Exception {
59      UTIL.shutdownMiniCluster();
60    }
61  
62    private static void setupConf(Configuration conf) {
63      // Enable snapshot
64      conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
65    }
66  
67    protected abstract void testWithMockedMapReduce(HBaseTestingUtility util, String snapshotName,
68      int numRegions, int expectedNumSplits) throws Exception;
69  
70    protected abstract void testWithMapReduceImpl(HBaseTestingUtility util, TableName tableName,
71      String snapshotName, Path tableDir, int numRegions, int expectedNumSplits,
72      boolean shutdownCluster) throws Exception;
73  
74    protected abstract byte[] getStartRow();
75  
76    protected abstract byte[] getEndRow();
77  
78    @Test
79    public void testWithMockedMapReduceSingleRegion() throws Exception {
80      testWithMockedMapReduce(UTIL, "testWithMockedMapReduceSingleRegion", 1, 1);
81    }
82  
83    @Test
84    public void testWithMockedMapReduceMultiRegion() throws Exception {
85      testWithMockedMapReduce(UTIL, "testWithMockedMapReduceMultiRegion", 10, 8);
86    }
87  
88    @Test
89    public void testWithMapReduceSingleRegion() throws Exception {
90      testWithMapReduce(UTIL, "testWithMapReduceSingleRegion", 1, 1, false);
91    }
92  
93    @Test
94    public void testWithMapReduceMultiRegion() throws Exception {
95      testWithMapReduce(UTIL, "testWithMapReduceMultiRegion", 10, 8, false);
96    }
97  
98    @Test
99    // run the MR job while HBase is offline
100   public void testWithMapReduceAndOfflineHBaseMultiRegion() throws Exception {
101     testWithMapReduce(UTIL, "testWithMapReduceAndOfflineHBaseMultiRegion", 10, 8, true);
102   }
103 
104   protected void testWithMapReduce(HBaseTestingUtility util, String snapshotName,
105       int numRegions, int expectedNumSplits, boolean shutdownCluster) throws Exception {
106     setupCluster();
107     util.startMiniMapReduceCluster();
108     try {
109       Path tableDir = util.getDataTestDirOnTestFS(snapshotName);
110       TableName tableName = TableName.valueOf("testWithMapReduce");
111       testWithMapReduceImpl(util, tableName, snapshotName, tableDir, numRegions,
112         expectedNumSplits, shutdownCluster);
113     } finally {
114       util.shutdownMiniMapReduceCluster();
115       tearDownCluster();
116     }
117   }
118 
119   protected static void verifyRowFromMap(ImmutableBytesWritable key, Result result)
120     throws IOException {
121     byte[] row = key.get();
122     CellScanner scanner = result.cellScanner();
123     while (scanner.advance()) {
124       Cell cell = scanner.current();
125 
126       //assert that all Cells in the Result have the same key
127       Assert.assertEquals(0, Bytes.compareTo(row, 0, row.length,
128         cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
129     }
130 
131     for (int j = 0; j < FAMILIES.length; j++) {
132       byte[] actual = result.getValue(FAMILIES[j], null);
133       Assert.assertArrayEquals("Row in snapshot does not match, expected:" + Bytes.toString(row)
134         + " ,actual:" + Bytes.toString(actual), row, actual);
135     }
136   }
137 
138   protected static void createTableAndSnapshot(HBaseTestingUtility util, TableName tableName,
139     String snapshotName, byte[] startRow, byte[] endRow, int numRegions)
140     throws Exception {
141     try {
142       util.deleteTable(tableName);
143     } catch(Exception ex) {
144       // ignore
145     }
146 
147     if (numRegions > 1) {
148       util.createTable(tableName, FAMILIES, 1, startRow, endRow, numRegions);
149     } else {
150       util.createTable(tableName, FAMILIES);
151     }
152     HBaseAdmin admin = util.getHBaseAdmin();
153 
154     // put some stuff in the table
155     HTable table = new HTable(util.getConfiguration(), tableName);
156     util.loadTable(table, FAMILIES);
157 
158     Path rootDir = FSUtils.getRootDir(util.getConfiguration());
159     FileSystem fs = rootDir.getFileSystem(util.getConfiguration());
160 
161     SnapshotTestingUtils.createSnapshotAndValidate(admin, tableName,
162       Arrays.asList(FAMILIES), null, snapshotName, rootDir, fs, true);
163 
164     // load different values
165     byte[] value = Bytes.toBytes("after_snapshot_value");
166     util.loadTable(table, FAMILIES, value);
167 
168     // cause flush to create new files in the region
169     admin.flush(tableName.toString());
170     table.close();
171   }
172 
173 }